1

I have a simple calculator program in MIPS assembly, but for some reason it is just returning zeros. Can someone help me out?

I think I may have messed up the frame storing although I am not sure. Also, I am not sure if I am using the beq command correctly.

.include "./cs47_proj_macro.asm"
.text
.globl au_normal
# TBD: Complete your project procedures
# Needed skeleton is given
#####################################################################
# Implement au_normal
# Argument:
#   $a0: First number
#   $a1: Second number
#   $a2: operation code ('+':add, '-':sub, '*':mul, '/':div)
# Return:
#   $v0: ($a0+$a1) | ($a0-$a1) | ($a0*$a1):LO | ($a0 / $a1)
#   $v1: ($a0 * $a1):HI | ($a0 % $a1)
# Notes:
#####################################################################
au_normal:
# TBD: Complete it
#store frame
addi    $sp, $sp, -24
sw      $fp, 24($sp)
sw      $ra, 20($sp)
sw      $a0, 16($sp)
sw      $a1, 12($sp)
sw  $a2, 8($sp)
addi    $fp, $sp, 24

sw $t0, '+'
sw $t1, '-'
sw $t2, '*'
sw $t3, '/'

beq $a2, $t0 Addition
beq $a2, $t1 Subtraction
beq $a2, $t2 Multiplication
beq $a2, $t3 Division

Addition:
    add $v0, $a0, $a1
    j exit

Subtraction:
    sub $v0, $a0, $a1
    j exit

Multiplication:
    mul $v0, $a0, $a1
    mfhi $v1
    j exit

Division:
    div $a0, $a1
    mflo $v0
    mfhi $v1
    j exit

exit:
    lw      $fp, 24($sp)
    lw      $ra, 20($sp)
    lw      $a0, 16($sp)
    lw      $a1,  12($sp)
    lw  $a2,  8($sp)
    addi    $sp, $sp, 20

    #return
    jr  $ra
Acorn
  • 24,970
  • 5
  • 40
  • 69
C Sand
  • 11
  • 1
  • 3
    What does `sw $t0, '+'` assemble to with the assembler you're using? MIPS doesn't have a store-immediate instruction, only `sw $reg, offset($reg)`. If you want to set `$t0` to have the integer value of the ASCII code for `'+'`, use `li $t0, '+'`. If your `sw` assembles at all, perhaps you're getting `sw $t0, '+'($0)`, i.e. storing `$t0` to absolute address 0x2B. **Use your debugger to single-step your code and look at register values**. – Peter Cordes Apr 29 '18 at 06:25
  • Thanks Peter. I used li instead of sw and the program works. Appreciate it! – C Sand Apr 29 '18 at 22:18

0 Answers0