kang
kang

Reputation: 79

MIPS assembly, stacks and basic addition

I am currently working on an assignment for class that requires us to accept user input values for two variables (a and b) and solve the equation [a - ab + 8a - 10b + 19] while using only the stack and registers $t0 and $t1.

I understand what I need to do, but I cannot figure out why my program displays incorrect solutions when I compile it. With stacks am I supposed to refer to specific locations [4($sp), for example] when I pop and push? I'm getting inconsistent answers from googling.

My apologies for any strange methods you see taken here, I have been fidgeting with it for awhile to no avail.

results: [a=4, b=3, solution = 51] [a=6, b=4, solution = 67]

    .data

aprompt:    .asciiz "Input a value for a: "
bprompt:    .asciiz "Input a value for b: "
eq:         .asciiz "a - ab + 8a - 10b + 19 = "

    .text

main:

    la $a0, aprompt         # print the prompt for a
    li $v0, 4
    syscall

    li $v0, 5               # read input as integer
    syscall
    move $t0, $v0           # store variable a in $t0

    la $a0, bprompt         # print the prompt for b
    li $v0, 4
    syscall

    li $v0, 5               # read input as integer
    syscall
    move $t1, $v0           # store variable b in $t1

    addi $sp,$sp,-4         # decrement stack pointer by 4
    sw $t0,($sp)            # push a on the stack at 0

    mult $t0,$t1            # a*b
    mflo $t0                # store result in $t0
    addi $sp,$sp,-4         # decrement stack pointer by 4
    sw $t0,($sp)            # push a*b on the stack at 4

    li $t0,10               # load 10 into $t0
    mult $t0,$t1            # 10*b
    mflo $t0                # store result in $t0

    lw $t1,($sp)            # pop the stack and store in $t1
    addi $sp,$sp,4          # increment stack pointer by 4

    add $t0,$t0,$t1         # add a*b and 10*b
    lw $t1,($sp)            # pop the stack and store in $t1
    addi $sp,$sp,-4         # decrement stack pointer
    sw $t0,($sp)            # push a*b+10*b on the stack at 0
    addi $sp,$sp,4          # increment stack pointer

    li $t0,8                # store 8 in $t0
    mult $t0,$t1            # 8*a
    mflo $t0                # store in $t0

    addi $t1,$t1,19         # a + 19

    add $t0,$t0,$t1         # store (a + 19) + 8*a in $t0

    lw $t1,($sp)            # pop the stack and store in $t1
    sub $t0,$t0,$t1         # (a + 19 + 8a) - (ab + 10b)

    move $a0,$t0            # print solution as integer
    li $v0,1
    syscall

    li $v0, 10              # exit
    syscall

Upvotes: 1

Views: 1906

Answers (1)

m0skit0
m0skit0

Reputation: 25874

lw $t1,($sp)            # pop the stack and store in $t1
sub $t0,$t0,$t1         # (a + 19 + 8a) - (ab + 10b)

lw $t1,($sp) is actually popping a, not ab + 10b.

You can fix it by popping ab + 10b instead from $sp - 4.

add $t0,$t0,$t1         # store (a + 19) + 8*a in $t0
addi $sp,$sp,-4         # decrement stack pointer
lw $t1,($sp)            # pop the stack and store in $t1

sub $t0,$t0,$t1         # (a + 19 + 8a) - (ab + 10b)

Upvotes: 2

Related Questions