I am trying to make a program on QtSpim that constantly takes new integers as input (through the console) and then prints them on the console only when they are even numbers. I set the input 5 as the exit case. The program compiles as it should and when I press the run button there doesn't seem to have a problem. The problem is when I try to type the input number, as the console doesn't respond to that (the number I just typed doesn't even appear on the screen).
Here is my code, I imagine the mistake must be in the first lines where the input number is read, but I can't find it:
.text
.globl __start
__start:
li $v0,5
syscall
move $t0,$v0
add $t1,$t0,$zero
addi $t2,$zero,5
LOOP: div $t0,$t0,2
bne $t0,$zero,LOOP
mfhi $t3
bne $t3,$zero,REPEAT
li $v0,1
move $a0,$t3
syscall
REPEAT:bne $t1,$t2,__start
li $v0,10
syscall
.data
The thing can be done in a much simpler way, using the bitwise and.
Every odd number will have the last bit set, which will make number & 1 equal to 1.
.text
.globl __start
__start:
li $t0, 5
loop:
move $v0, $t0 # set $v0 to 5: read integer
syscall # read in the number
andi $t1, $v0, 1 # check if it's divisible by 2
bnez $t1, check # if no, jump to a check for 5
move $a0, $v0 # if yes, print it
li $v0, 1 # set $v0 to 1: print integer
syscall # do the printing
j loop # continue
check:
bne $t0, $v0, loop # if the integer read is not equal to 5, run again
li $v0, 10
syscall # exit
Your method of checking for even numbers is incorrect. Whenever you enter a number >=1 you'll end up dividing 1 by 2 on the last iteration of your loop. And of course 1 MOD 2 is 1, so your code always thinks the number is odd.
A single division by 2 is sufficient to determine if the value is odd or even. But an AND operation would be even more efficient:
andi $t0,$t0,1 # if the least significant bit is set, the number is odd
bne $t0,$zero,REPEAT
After making that change you'll probably also have to change the printing code, since the value to print no longer is in $t3.
Related
so I figured out this program that basically codes a number pattern and the numbers have to be tabbed in between each other, so for example:
1 1 1
but the last "1" also has a tab and i need to remove that. this is what my code looks like for tabbing: i use this before the end of my for loop, so it can incrementing how many ever times. i don't really know where to start for creating a conditional that doesn't print the last number with a tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
You didn't provide enough code to give a full answer, but there are several ways to omit printing the last tab:
If you know you are processing the last item you can jump over the print tab code, e.g. suppose you are in a while-loop where you loop while $t0 is different than $t1, then you can write:
while_loop:
# .... do something
beq $t0, $t1, skip
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
skip:
# ... something else
bne $t0, $t1 while_loop % this is the condition to keep in the loop
if the printing of the tab is the last thing you were doing in the loop, then you can simplify a bit:
while_loop:
# .... do something
beq $t0, $t1, while_loop
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
b while_loop
Another approach is to print the tab at the beginning of the loop, save for the first iteration. Useful if you are iterating over some values on a register and know some initial value wont be repeated. In this example I will just use a supposedly spare register:
li $t7, 0 # $t7 will only have 0 on the first iteration of the loop
while_loop:
beq $t7, $zero, skip
# your code to print tab
li $v0, 11 #this is for tabbing the numbers
li $a0, 9
syscall
skip:
li $t7, 1
% your remaining code here, which at some point goes to the while_loop
My code comes up with Unsupported R-Type, how do I make it subtract correctly. Tried changing other details within.... Already tried using subu...............................................................................................
.data
str: .asciiz "\nHello World!\n"
# You can change what is between the quotes if you like
.text
.globl main
main:
# Do the addition
# For this, we first need to put the values
# to add into registers ($t0 and $t1)
li $t0, 30 # You can change the 10
li $t1, 20 # You can change the 20
# Now we can add the values in $t0
# and $t1, putting the result in special register $a0
sub $a0, $t0, $t1
# Set up for printing the value in $a0.
# A 1 in $v0 means we want to print an int
li $v0, 1
# The system call looks at what is in $v0
# and $a0, and knows to print what is in $a0
syscall
# Now we want to print Hello World
# So we load the (address of the) string into $a0
la $a0, str
# And put a 4 in $v0 to mean print a string
li $v0, 4
# And just like before syscall looks at
# $v0 and $a0 and knows to print the string
syscall
# Nicely end the program
li $v0, 0
jr $ra
Your program runs fine in the mars and spim simulators except for program termination. These simulators don't set up $ra, so it's zero. So, at the end, you're returning to something that may have semi-random instructions, including an illegal one. Thus, it's not your sub at all that is the problem. It's what happens later.
Change:
# Nicely end the program
li $v0, 0
jr $ra
Into:
# Nicely end the program
li $v0, 10
syscall
Hey guys Im new to ComArch. Im writing a homework assignment. My question is i have this loop in my program that divides a users int input by 10 and my loop will go through the and divide until the quotient is at 0 . Currently it prints out 0 for my quotient but doest print out each value of the remainder. So how would i modify my code to print out every single remainder that is left over after dividing.
loop:
li $s0, 10 #divisor
div $t0,$s0 #divide input by 10
mflo $t0 #quotiant
mfhi $t3 #remainder
sw $t3, ($t1) #stores emainder into address of int_a
addi $t1,$t1,4 #increases the pointer
bne $t0,0,loop
You will find in the MIPS Architecture and Assembly Language Overview the section "System Calls and I/O (SPIM Simulator)" which details how to print a value.
In your case, within the loop, you would have to print $t3.
li $v0, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
move $a0, $t3 # move integer to be printed into $a0: $a0 = $t3
syscall # call operating system to perform operation
I am trying to compute the length of a string given by a user. Every time I try to run the code, I get the message "Exception occurred at PC=(a certain address) followed by the message :"Bad address in Data/stack read: (another address). I know that it has something to do with the stack but I can't figure out the problem. The code in MIPS is bello and I am using QtSpim. Your help will be very appreciated.
sentence: .space 6
Prompt: .asciiz "Enter the sentence. Max 6 characters, plus a terminator .\n"
.text # Start of code section
main: # The prompt is displayed.
li $v0, 4 # system call code for printing string = 4
la $a0, Prompt # load address of string to be printed into $a0
syscall # call operating system to perform operation;
# $v0 specifies the system function called;
# syscall takes $v0 (and opt arguments)
##read the string, plus a terminator, into the sentence
la $t0, sentence
li $t0, 6
li $v0, 8
add $v0, $zero, $zero #initialize length to zero
loop:
lbu $s0, 0($t0) #load one character of string
addi $t0,$t0,1 #point to next character
addi $v0,$v0,1 #increment length by 1
bne $s0,$zero, loop #repeat if not null yet
end_loop:
addi $v0, $v0, -1 #don't count the null terminator
li $v0, 4 #display the actual length
syscall
exit: #exit the program
li $v0, 10
syscall
##read the string, plus a terminator, into the sentence
la $t0, sentence
li $t0, 6
Here you're loading the address of sentence into $t0, and then immediately overwrite $t0 with the value 6. This is probably the root cause of the exception, since the following lbu will attempt to read from address 0x00000006. I suggest that you remove the li.
li $v0, 8
add $v0, $zero, $zero #initialize length to zero
This li is pointless since you're setting $v0 to zero on the very next line, so this li can also be removed.
sentence: .space 6
Prompt: .asciiz "Enter the sentence. Max 6 characters, plus a terminator .\n"
You say that the user is allowed to enter up to 6 characters. But you only allocate space for 6 bytes, meaning that the NULL terminator wouldn't fit if the user actually enters 6 characters.
I'm working on a mips program that will run on pcspim and i need a little help. The description of the program is: Write a program that reads a string (from a keyboard), stores it in the memory, and computes and prints the frequency of each character; and then it reverses the string and prints the reversed string.
so far i have is...
.data # Data declaration section
userString: .space 256
Prompt: .asciiz "\nEnter a word: "
newLine: .asciiz "\n"
.text
main: # Start of code section
li $v0, 4
la $a0, Prompt
syscall
li $v0, 8
la $a0, userString
li $a1, 256
syscall
jr $ra
la $a0, userString
move $t0, $a0
lb $t1, 0($t0)
li $v0, 4
move $a0, $t1
syscall # prints first letter of word
Right now i just wanted to see if i've actually stored the input into the userString array. So at the end i tried to print out the first letter. but it doesnt seem to be printing anything.
Any suggestion?
thank.
I've broken your code down into three parts: prompting, input, display. I assume the first two parts were given to you and the third is what you are focusing on right now. I'll explain what the first to parts are doing then explain what the third is doing right now and what you probably want it to do at this point.
.data # Data declaration section
userString: .space 256
Prompt: .asciiz "\nEnter a word: "
newLine: .asciiz "\n"
.text
# Part I
main: # Start of code section
li $v0, 4
la $a0, Prompt
syscall
# Part II
li $v0, 8
la $a0, userString
li $a1, 256
syscall
jr $ra
# Part III
la $a0, userString
move $t0, $a0
lb $t1, 0($t0)
li $v0, 4
move $a0, $t1
syscall # prints first letter of word
Part I
This is pretty straightforward, when we start executing the program counter will be set to the address of the main label. It loads the value 4 into $v0 (that seems to be the print string system call number), and then loads the address of the Prompt string into the first argument register $a0. The last bit just performs the system call that puts the string on the screen.
Part II
Now that the "Enter a word: " string has been printed on screen, we want to actually read what the user is typing. It looks like here we're using system call #8 (probably read string), so we load that value into $v0 in preparation for the syscall. Then, since we want to read the user string into userString, we load the address of that label into $a0 (the first argument for the read string function) then, since we are savvy programmers, we give the upper bound of how many bytes userString can hold (256) in $a1. Then we perform the system call, you type in a string at the keyboard and hit enter and we return to the next line of code.
That line is jr $ra, which means "jump to the location stored in register $ra (return address)". You probably don't want this, because it marks the end of the main function and likely you program exits back to the command line at this point, probably best to remove it.
Part III
Again, you're loading the address of userString into $a0 (and also moving it into $t0 in the next line). Now it gets weird, you load the first byte 0($t0) of userString into $t1. This is a ASCII character value (like 72 or something). Then you start up the system call stuff again with the print string system call (#4) and the argument of $t1. Which you think will print the first letter of the word, which I disagree with. Here's why. If the user types the string, "Hello, world!" this is what it looks like in memory:
userString: H e l l o , w o r l d !
offset: 0 1 2 3 4 5 6 7 8 9 10 11 12
So, loading 0($t0) moves the letter H into register $t1, then when you perform the system call it tries to print the string starting at H to the screen. However, there is not a string starting at letter H, it starts at the address of userString. So if you just move the address of userString into register $a0, then do system call #4 it should print userString to the screen.
#mjshultz
i've changed it up a little. Didnt think i needed 2 loops. Also i've increment it by four because i thought each character is 4 bytes so to go to the next letter i need to increment the offset by four.
.data # Data declaration section
userString: .space 256
Prompt: .asciiz "\nEnter a word: "
newSpace: .asciiz " "
newLine: .asciiz "\n"
.text
main: # Start of code section
li $v0, 4
la $a0, Prompt
syscall
la $a0, userString
li $a1, 256
li $v0, 8
syscall
la $a0, userString
move $s0, $a0
loop:
lb $t1, 0($s0)
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, newSpace
syscall
addi $s0, $s0, 4
blt $s0, 256, loop