0

Got a homework question asking me to take two user inputs, compare them, and input them into an equation.

This is my template: ($t1 + 5) - ($t2 * 2) = result

However, I seem to be getting a return value of 5 (or 05) every time I run it. I'm not too sure what I'm doing wrong.

Here's the code:

.text

# First Input - Saved to $t1
la  $a0, input
li  $v0, 4
syscall

li  $v0, 5
move    $a0, $t1
syscall

# Second Input - Saved to $t2
la  $a0, input2
li  $v0, 4
syscall

li  $v0, 5
move    $a0, $t2
syscall

# Compare the two Inputs
bgt $t1, $t2, Bigger
blt $t1, $t2, Smaller

# If the 1st is greater
# ($t1 + 5) - ($t2 * 2) = result
Bigger:
    add $t4, $t1, 5 # $t4 = $t1 + 5
    mul $t5, $t2, 2 # $t5 = $t2 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5
    syscall

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall

# If the 1st is smaller
Smaller: 
    add $t4, $t2, 5 # $t4 = $t2 + 5
    mul $t5, $t1, 2 # $t5 = $t1 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall


.data

input:  .asciiz "Enter the First Integer: "

input2: .asciiz "Enter the Second Integer: "

Halt:   li  $v0, 10
        syscall

Any help? Thanks!

Saigo no Akuma
  • 147
  • 1
  • 1
  • 11
  • 2
    You should not be using the temporary registers `$t1, $t2` to save values. These registers are not preserved across calls or syscalls. Use `$s0,$s1,...` instead. – markgz Nov 30 '15 at 23:40
  • I'm still getting a return value of 0.05 (If I input 5 and 2). – Saigo no Akuma Nov 30 '15 at 23:49

1 Answers1

2

The read_int syscall simply needs the function code 5 in $v0 and returns the input value in there too. So instead of:

li  $v0, 5
move    $a0, $t1
syscall

You should do:

li  $v0, 5
syscall
move    $t1, $v0

Similarly for the other number of course.

Incorporating @markgz's comment about $t registers being caller saved, the whole code could look like:

.text
# First Input - Saved to $t1
la  $a0, input
li  $v0, 4
syscall

li  $v0, 5
syscall
move    $s0, $v0  # save to $s0

# Second Input - Saved to $t2
la  $a0, input2
li  $v0, 4
syscall

li  $v0, 5
syscall
move    $t1, $s0 # restore 1st number
move    $t2, $v0

# Compare the two Inputs
bgt $t1, $t2, Bigger
blt $t1, $t2, Smaller

# If the 1st is greater
# ($t1 + 5) - ($t2 * 2) = result
Bigger:
    add $t4, $t1, 5 # $t4 = $t1 + 5
    mul $t5, $t2, 2 # $t5 = $t2 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall

# If the 1st is smaller
Smaller:
    add $t4, $t2, 5 # $t4 = $t2 + 5
    mul $t5, $t1, 2 # $t5 = $t1 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall


.data

input:  .asciiz "Enter the First Integer: "

input2: .asciiz "Enter the Second Integer: "

Halt:   li  $v0, 10
        syscall
Jester
  • 56,577
  • 4
  • 81
  • 125
  • I'm still getting a return value of 0.05 (If I input 5 and 2) – Saigo no Akuma Nov 30 '15 at 23:50
  • 1
    You have a stray `syscall` instruction on the 4th line after `Bigger:`. – Jester Nov 30 '15 at 23:58
  • Removed it and inputted 10 and 9, still getting 5. Edit: Put in a 1 and 0 and still getting 5. I think it might be an operation problem. – Saigo no Akuma Dec 01 '15 at 00:03
  • 1
    Works fine here. Also consider what @markgz said, your system might not be preserving the `$t` registers. – Jester Dec 01 '15 at 00:19
  • I checked the registers, and it says $t4 = 5 and $t7 = 5. I think this is where I'm getting the repeat 5 from. It looks like $t1 (the input) is not being saved. I'm going to do what @markgz suggested and test it out. *Edit* I changed the input register from $t to $s, and I'm still getting 5 as a return value. This is driving me insane! – Saigo no Akuma Dec 01 '15 at 00:28
  • 1
    Also check that your assembler understands your use of pseudoinstructions - for example there is no `mul` that takes an immediate, the assembler has to synthesize that. But if it didn't produce an error, I expect correct code. Oh, and double check that you are actually running the updated code (maybe you forgot to build it or used a different name). – Jester Dec 01 '15 at 00:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96617/discussion-between-saigo-no-akuma-and-jester). – Saigo no Akuma Dec 01 '15 at 00:55