0

My task was to translate the following c code to MIPS assembly.

int compare(int a, int b) {
if ( add(b, a) >= 100) 
    return a;
else
    return b; }

int add(int x, int y){
    return (x+y); } 

This is what I managed to write but somehow it does not return the value. It should return 5 in the case of the arguments I provided to test the program. I'm guessing the problem has to do with the stack pointer and return address. The MARS simulator tells gives me: "-- program is finished running (dropped off bottom) --"

The MIPS code compiled on MARS:

.data
.text
.globl main

main:

    addiu $a0, $zero, 10
    addiu $a1, $zero, 5

    jal compare

    addu $s1, $v0, $zero

    addu $a0, $zero, $s1 # print value
    li $v0, 1
    syscall

    li $v0, 10
    syscall

compare:
    addiu $sp, $sp, -12 #adjusting stac for 3 items

    sw $ra, 0($sp)
    sw $a0, 4($sp)      #saving return address and arguments
    sw $a1, 8($sp)

    lw $a0, 8($sp)      #swaping arguments
    lw $a1, 4($sp)

    jal addition        #calling addition procedure

    blt $v0, 100, return_b      #if add(b,a) < 100 return b

    lw $v0, 4($sp)          #else

    j return_value          

return_b:
    lw $v0, 8($sp)

return_value:   
    lw $ra, 0($sp)
    lw $a0, 4($sp)      #restoring arguemtns and return address
    lw $a1, 8($sp)

    addiu $sp, $sp, 12

    jr $ra

addition:
    addu $v0, $a0, $a1
    jr $ra
tessa
  • 1
  • 1
    Are you sure that you're running the code that you posted? I get the output `5` followed by `-- program is finished running --`. – Michael Mar 17 '15 at 16:59
  • really?? must be something wrong with my computer/MARS software because I get this error a lot. Thank You for make it clear. – tessa Mar 17 '15 at 19:01

0 Answers0