Why does this MIPS program do what it does?

In summary, the conversation revolves around a MIPS assembly program that produces an incorrect addition in the second line of output. The code is run in the QtSpim MIPS simulator and the conversation includes a discussion about the purpose and function of certain instructions and memory offsets.
  • #1
37,626
9,861
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
  • #2
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.
 
  • #3
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
  • #4
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.
 
  • #5
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

1. Why is the MIPS program coded in assembly language instead of a high-level language?

Assembly language is a low-level programming language that is closely related to the machine code of the computer's central processing unit (CPU). This allows for more direct control over the hardware, making it a more efficient choice for tasks that require precise control over the computer's resources.

2. How does the MIPS program handle memory management?

MIPS programs use a combination of instructions and registers to manage memory. The program will allocate and deallocate memory as needed, and instructions will allow for data to be stored and retrieved from specific memory locations.

3. What is the purpose of the MIPS program's control flow instructions?

Control flow instructions in the MIPS program allow for the execution of specific sections of code based on conditions or loops. This allows for more complex and flexible programs to be created.

4. How does the MIPS program handle input and output?

MIPS programs use specific instructions to handle input and output. These instructions will read or write data from memory or registers, allowing for communication between the program and external devices or the user.

5. Can a MIPS program be run on any computer?

No, MIPS programs are designed to be run on computers that use the MIPS architecture. This includes certain types of processors and operating systems. The program may need to be modified to run on a different architecture.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
1
Views
7K
Back
Top