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
Related
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.
I have this program I just wrote:
countzeroes:
li $v0, 0 # count = 0
li $t0, 0 # int i = 0
li $v1, 1 # compare bit = 1
cz_loop:
bge $t0, 32, cz_exit # exit loop if i >= 32
andi $t1, $a0, 1 # bit = arg0 & 1
beq $t1, $v1, cz_skip # skip if bit = 1
addi $v0, $v0, 1 # adds 1 to count
cz_skip:
srl $a0, $a0, 1 # shifts input right by 1
add $t0, $t0, 1 # i++
j cz_loop
cz_exit:
jr $ra
Pretty simple, just computes the number of zeroes in a 32 bit word. I was wondering how the program knows how to return $v0 at the end? I know v0 and v1 are return registers, but I was wondering if those two are always returned. If not, how does the program know to return v0?
In addition, I know jr $ra jumps to the return address- but what does that mean?
Thanks for your help.
"how the program knows how to return $v0 at the end?"
It doesn't know, you're writing the "return" value in $v0, in fact you could return the "result or return values" in any available register such as the temporals, it's just a convention to use the $v0 register to return values (in MIPS).
"I was wondering if those two are always returned"
Remember that in any subroutine in your program you always have access to all registers, so there's not "restriction" about what register store values that can be semantically called "return values", so I could easily create a method that returns 3 numbers in $t0, $t1, $t2 but that's my choice, you can return values in the stack also, there are a lot of possibilities, and this depends and lays down on the good programming practices and also the calling conventions, here you can find the MIPS calling convention: https://courses.cs.washington.edu/courses/cse410/09sp/examples/MIPSCallingConventionsSummary.pdf
" jr $ra jumps to the return address- but what does that mean?"
The program is executed instruction by instruction(the program has an instruction pointer aka program counter), when you call a subroutine the address of the next instruction is being stored in the $ra register, then when you make jr $ra, the program execution returns to that address (the instruction pointer gets the value of $ra).
In MIPS there are three different jumps you'll see. j, jr & jal.
j: it is considered an unconditional jump. Simply just do:
j function
jr:aka jump to register. Exactly as the name sounds you jump to register. This is when you have a register already saved somewhere in your program you want to jump back to. It will usually look like so:
jr $ra
$ra being the register which had been previously set aside before your jal (see below) which will jump the program back to that address.
jal: aka Jump and link copies the address of the next instruction into the register and then jumps to the address label. In other words, both jal and jr are used in conjunction with each other mainly for going to functions which are usually placed after the exit calls.
Ex:
main:
#program
jal function
#continue with program
function:
#
#do something
#
jr $ra
Also, most helpful site when I started learning: http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm
Some other quick hints that I wish someone told me when I started:
Always start with "main:"
Be wary of the difference between high/low registers in multiplying
and dividing integers
Always keep track of your registers because with so many $s and $t
and $f and $p and $a and $v registers working at one time things can
get messy very quickly.
I have a function that takes a memory address as $a0 and I access the (variable) number of words by using x($a0), where x is multiples of 8. I need to store these in the $sp register so I can use the $a0 register for passing arguments to other functions. Completely new to MIPS assembly, so any pointers here would help!
Multiples of 8 i assume you are using mips-64
first u make a loop and increment a0 by 8 each time:
loop: lw $t0, 0($a0) ;fetch data and store in t0
addi $sp,$sp,-8 ;increase stack
sw $t0, 0($sp) ;store data fetched
addi $a0,$a0,8 ;increment a0 to go to next entry
;here you check that you haven't reached x yet
;let's say 8*x+$a0(initial) is stored in $t1 (this is easy to do just use sll by 3 to multiply by 8 then add a0 before loop)
bne $a0,$t1,loop
;now you can use $a0
The following is my code in MIPS to calculate the sum of odd integers using a while loop.
.data
num: .space 4
.text
.globl main
main:
li $t1, 1
li $t2, 9 # make $t2 9 to break the loop
li $t3, 1
loop:
beq $t3, 11, Exit # check to see if $t3 = 11 if so exit
addi $t3, $t3, 2 # change $t3 to next odd number by adding 2
add $t1, $t1, $t3 # add $t3 to $t1 (1+3,...3+5...etc...)
j loop #jump back to the start of the loop
Exit:
li $v0, 1 # system call code to print an int
lw $a0, num # address of int to print
syscall # print the int
jr $ra #exit
This is my first real experience with MIPS and I'm not sure what is going wrong in this code. I put the print inside the while loop to see if it was ever calculating, but the result is always 1.
So, my result at the end is just 111111.
Edit: Removed the prints inside of the loop with the same result.
And OS is Windows 7 64x
Update: Having num as a variable was over complicating things. Code has been revised as follow and works. Thank you for the help!
enter code here
.data
.text
.globl main
main:
addi $t1, $0, 1
addi $t2, $0, 3
loop: bge $t2, 11, Exit # check to see if $t3 >= 11 if so exit
add $t1, $t1, $t2 # add $t2 to $t1 (1+3,...3+5...etc...)
addi $t2, $t2, 2 # change $t2 to next odd number by adding 2
j loop #jump back to the start of the loop
Exit:
li $v0, 1 # system call code to print an int
move $a0,$t1 # address of int to print
syscall # print the int
jr $ra #exit
la $t1, num
You're clearly getting into trouble here, since you're overwriting your accumulator with the address of num every time you're making the syscall. You're losing the current state of your calculation each time.
You'll need to either save your registers, or simply use different ones. Since I don't know what OS it is that you're using, I don't know if you more generally need to save registers over a syscall, but that could also be a source of errors.
I did similar problems for an architecture class and this seemed to be a recurrent problem among all students. When facing problems similar to this our professor's recommendation was to use a different register to temporarily store the register's address to avoid overwriting other desired values from our most commonly used regs.
I'm new in MIPS language and I don't understand how the functions (procedures) in the MIPS assembly language work. Here are but I will specify my problem :
What does:
jal
jr
$ra
mean in mips language and the important thing
How can we use them when we want to create a function or (procedure)?
Firstly, you might want to check this quick MIPS reference. It really helped me.
Secondly, to explain jal, jr and $ra. What jal <label> does is jump to the label label and store the program counter (think of it as the address of the current instruction) in the $ra register. Now, when you want to return from label to where you initially were, you just use jr $ra.
Here's an example:
.text
main:
li $t0, 1
jal procedure # call procedure
li $v0, 10
syscall
procedure:
li $t0, 3
jr $ra # return
You will notice when running this in a SPIM emulator that the value left in $t0 is 3, the one loaded in the so-called procedure.
Hope this helps.
1.the first two are instructions,the third it's kind of special register
jal=jump and link (Address of following instruction put in $ra,and jump to target address)
jr=jump to specify register
$ra=return address
we often use the instruction like this ...
jr $ra (Copy $ra to program counter)
it means return(jump) to the address saved in $ra .
2.
Here's an example function (procedure) in C
int main(){
x=addthem(a,b);
}
int addthem(int a, int b){
return a+b;
}
function in MIPS
.text
main: #assume value a is already in $t0, b in $t1
add $a0,$0,$t0 # it's the same function as move the value
add $a1,$0,$t1
jal addthem # call procedure
add $t3,$0,$v0 # move the return value from $v0 to where we want
syscall
addthem:
addi $sp,$sp,-4 # Moving Stack pointer
sw $t0, 0($sp) # Store previous value
add $t0,$a0,$a1 # Procedure Body
add $v0,$0,$t0 # Result
lw $t0, 0($sp) # Load previous value
addi $sp,$sp,4 # Moving Stack pointer
jr $ra # return (Copy $ra to PC)
You will want to read the System V Application Binary Interface, MIPS RISC Processor Supplement. This describes the conventions used for calling functions, in particular how the stack is managed and parameters are exchanged (there is no hardware stack in MIPS, everything is a matter of software conventions, and the ABI defines those conventions).
The document above assumes some basic knowledge of what MIPS instructions do, so you will also need the MIPS32 Architecture for Programmers, in particular volume II (instruction set), which describes the detailed effect of each instruction. But, do yourself a favor, download and read volume I (introduction) first.
The jal instruction is the "jump and link" opcode. It jumps at the target address (which is the address of the first opcode of the called procedure) while saving the current instruction pointer into the link register, which is register 31 (to be precise, it saves in register 31 the value x+8, where x is the address of the jal opcode itself).
jal: aka jump and link against any function name will redirect you to the required function.
jr $ra: It returns the value from a function that was called.
main function:
.data
x: .word 3 # initializing x to be 3
y: .word 5 # initializing y to be 5
answer: .word 0 # intialzing answer to be 0
prompt: .asciiz "Add X to Y: " # console prompt for final answer
.text
.globl main
.ent main
main:
lw $a0, x # pass arguments to function $a0 - x, $a1 - y
lw $a1, y
jal adds # adds function is called
sw $v0, answer # answer from the returned value
la $a0, prompt # displaying prompt on the console
li $v0, 4
syscall
lw $a0, answer # displaying final answer on the console
li $v0, 1
syscall
li $v0, 10 # end program
syscall
.end main
adds function:
.globl adds
.ent adds
adds: # adds function
li $v0, 0
add $v0, $a0, $a1 # adding arguments to the callee
jr $ra # return
.end adds