Why does this MIPS program do what it does?

  • Thread starter Thread starter Mark44
  • Start date Start date
  • Tags Tags
    Mips Program
AI Thread Summary
The discussion revolves around a MIPS assembly program that produces unexpected output during a loop. The program is intended to add the values 5 and 2, but it outputs "5 + 2 = 7" followed by "5 + 2 = 3". The issue arises from the manipulation of a memory address (0x400030) that contains the machine code for the addition operation. Each iteration of the loop loads this address, increments its value by 2, and stores it back, effectively changing the addition instruction to a subtraction instruction on subsequent iterations. This results in the second output being the result of a subtraction instead of an addition. The discussion highlights the importance of understanding how memory manipulation can affect program behavior in assembly language.
Messages
38,030
Reaction score
10,506
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 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 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 newjerseyrunner
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top