Search code examples
mathmipsinteger-arithmetic

MIPS calculator not outputting properly


I am trying to implement a MIPS calculator that takes 3 ints as input and returns an output. Currently, the code outputs "The sum of int1, int2, and int3 is 1" and I don't understand why. Perhaps I haven't written the addition subroutine properly?

        .data
welc:   .asciiz "Enter an integer:\n"
sum:    .asciiz "The sum of "
prod:   .asciiz "The product of "
divi:   .asciiz "The quotient of "
subt:   .asciiz "The difference of "
also:   .asciiz " and "
comma:  .asciiz ", "
rem:    .asciiz "remainder:"
is:     .asciiz " is "

int1:   .word 1 #space to hold first int
int2:   .word 1 #space to hold second int
int3:   .word 1 #space to hold third int
input:  .space 1 #space to hold raw input
out:    .word 1 #space to hold output
remain: .word 1 #space to hold remainder

        .text
        .globl main
main :  li $v0, 4       #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 1
        syscall
        la $s1, int1    #load int1 into $s1
        sw $v0, 0($s1)  #copy int from $v0 to int 1

        li $v0, 4   #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 2
        syscall
        la $s2, int2    #load int2 into $s2
        sw $v0, 0($s2)  #copy int from $v0 to int 2

        li $v0, 4   #syscall 4, print string
        la $a0, welc    #give argument: string "Enter an integer:"
        syscall

        li $v0, 5   #tell syscall we want to read int 3
        syscall
        la $s3, int3    #load int3 into $s3
        sw $v0, 0($s3)  #copy int from $v0 to int 3

        la $s0, out #load output to $s0

        j plus  #jump to calc

plus:   add $s0, $s1, $s2   #add int1 and int2 and put in out
        add $s0, $s0, $s3   #add out and int3

        li $v0, 4   #tell syscall to print string
        la,$a0, sum #print sum string
        syscall
        li $v0, 1   #tell syscall to print int
        la $s1, int1    #tell syscall to print int1
        lw $a0, 0($s1)  #load int 1 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, comma   #tell syscall to print comma
        syscall
        li $v0, 1   #tell syscall to print int
        la $s2, int2    #tell syscall to print int2
        lw $a0, 0($s2)  #load int 2 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, comma   #tell syscall to print comma
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, also    #tell syscall to print comma
        syscall
        li $v0, 1   #tell syscall to print int
        la $s3, int3    #tell syscall to print int3
        lw $a0, 0($s3)  #load int 3 into $a0 and print
        syscall
        li $v0, 4   #tell syscall to print string
        la,$a0, is  #tell syscall to print is
        syscall
        li $v0, 1   #tell syscall to print int
        la $s0, out #tell syscall to print output
        lw $a0, 0($s0)
        syscall

        li $v0, 10  #exit code
        syscall

Solution

  • In $s0, $s1, and $s2 you have addresses. Therefore in $s0 you have the sum of addresses which is not want you want. Also, you're outputting the value at out, which you never changed.

    Before you add the numbers, you must load their value with lw. Then, if you want to print the value at out, you have to store $s0 at that address first.