Why does this MIPS program do what it does?

  • Thread starter Thread starter Mark44
  • Start date Start date
  • Tags Tags
    Mips Program
Click For Summary

Discussion Overview

This discussion revolves around a MIPS assembly program that produces unexpected output during execution. Participants analyze the code to understand why the second line of output shows an incorrect addition result. The scope includes technical explanations of assembly language behavior and the implications of modifying instructions in memory.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant notes that the program outputs "5 + 2 = 7" followed by "5 + 2 = 3," prompting questions about the cause of the discrepancy.
  • Another participant points out that the address 0x400030 is critical to understanding the behavior of the program, suggesting it may contain a value affecting the addition operation.
  • It is mentioned that the lw instruction loads a value from the specified address, which is the machine code for the previous add instruction, and that the subsequent addiu instruction modifies this value.
  • Some participants propose that the modification of the instruction from addition to subtraction explains the unexpected output.
  • One participant expresses confusion about the original question, indicating that the output seems consistent with their expectations based on the code behavior.

Areas of Agreement / Disagreement

Participants express differing views on the implications of the code's behavior. While some agree on the mechanics of how the instruction modification leads to the output, others question the necessity of the original inquiry, indicating a lack of consensus on the purpose of the discussion.

Contextual Notes

There is uncertainty regarding the contents of the memory address 0x400030 and its role in the program's execution. The discussion does not resolve the implications of modifying instructions in memory or the exact nature of the values at that address.

Messages
38,107
Reaction score
10,661
A recent thread asking what a Python program does induced me to post this MIPS assembly program, and ask why we get the incorrect addition in the second line of output.
Here's the output from this code:
Code:
5 + 2 = 7
5 + 2 = 3
And here's the code that produces that output.
Edit: The first line of code after the Loop label adds the values in the $a1 and $a2 registers, and puts the sum in the $a3 register.
The code here was run in the QtSpim MIPS simulator, producing the output you see above.
Code:
AddAddr = 0x400030
         .data
Msg: .asciiz "5 + 2 = "
         .text
        .globl  main
main:
 li $a1, 5
 li $a2, 2
 li $t0, 2
 
Loop:
   add $a3, $a1, $a2
   lw $s0, AddAddr
   addiu $s0, $s0, 2
   sw $s0, AddAddr
   la $a0, Msg
   li $v0, 4
   syscall
   move $a0, $a3
   li $v0, 1
   syscall
   li $a0, '\n'
   li $v0, 11
   syscall
   sub $t0, $t0, 1
   bgt $t0, $0, Loop 
Done:
   li $v0, 10
   syscall
.end main
 
Last edited:
  • Like
Likes   Reactions: jedishrfu
Technology news on Phys.org
Code:
AddAddr = 0x400030  ;Constant memory position... what is at this position?
         .data
Msg: .asciiz "5 + 2 = "
         .text
        .globl  main
main:
 li $a1, 5  ;One of the values to add
 li $a2, 2  ;The other value to add
 li $t0, 2   ;A loop counter
 
Loop:
   add $a3, $a1, $a2   ;Add a1 and a2 and put the result in a3
   lw $s0, AddAddr      ;Load whatever is at address 0x40030  to s0...  Why?
   addiu $s0, $s0, 2     ;Add 2 to the value of s0
   sw $s0, AddAddr     ;Push the results back to the address 0x40030
   la $a0, Msg              ;Load the text of your output into a0 (which is required for output)
   li $v0, 4                    ;Put the integer 4 in v0 (this is an interrupt value for print string that will cause syscall)
   syscall                      ;Will print a0 to the screen because v0 is 4
   move $a0, $a3         ;Copy the results of a3 to a0 to be ready to print it
   li $v0, 1                    ;A value of 1 in v0 will do a print int
   syscall                      ;print the int
   li $a0, '\n'                  ;Put a newline in the print buffer
   li $v0, 11                   ;Tell it it's printing a character
   syscall                      ;Do the print
   sub $t0, $t0, 1          ;Subtract one from the loop
   bgt $t0, $0, Loop      ;Loop if counter is not zero
Done:
   li $v0, 10                  ;10 is the syscall to exit
   syscall                      ;Actually exit
.end main
[/QUOTE]

So it's looping twice because of the $t0 variable. Why you're getting anything other than 7 must have something to do with the 0x400030 offset. I have no idea what that is.
 
newjerseyrunner said:
Why you're getting anything other than 7 must have something to do with the 0x400030 offset. I have no idea what that is.
It's the offset of the first add instruction in the loop. The lw (load word) instuction that follows loads the 32-bit integer at that address, which happens to be the machine code for the previous add instruction. The addiu instruction increments that value by 2, and the sw (store word) instuction inserts the new value into the code segment. The value that is inserted is the machine code for sub $a3, $a1, $a2.
 
  • Like
Likes   Reactions: newjerseyrunner
Gotcha, so it changes the add to a sub. It'll first add the two values together, then subtract one from the other. So what's the question? You seem to get exactly the output I would expect.
 
newjerseyrunner said:
Gotcha, so it changes the add to a sub. It'll first add the two values together, then subtract one from the other. So what's the question? You seem to get exactly the output I would expect.
No question -- I just submitted it for people's "enjoyment".
 
  • Like
Likes   Reactions: newjerseyrunner

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
1
Views
2K