0

I've assembled this a short MIPS assembly code using QtSPIM simulator. I am appending the code for completion:

.text
.globl main
main:
 subu $sp,$sp,32 # Stack frame is 32 bytes long
 sw $ra,20($sp) # Save return address
 sw $fp,16($sp) # Save old frame pointer
 addiu $fp,$sp,28 # Set up frame pointer
 li $a0,10 # Put argument (10) in $a0
 jal fact # Call factorial function
 la $a0,$LC # Put format string in $a0
 move $a1,$v0 # Move fact result to $a1
 lw $ra,20($sp) # Restore return address
 lw $fp,16($sp) # Restore frame pointer
 addiu $sp,$sp,32 # Pop stack frame
 jr $ra # Return to caller
 
 .rdata
$LC:
.ascii "The factorial of 10 is %d\n\000"
 
 .text
fact:
 subu $sp,$sp,32 # Stack frame is 32 bytes long
 sw $ra,20($sp) # Save return address
 sw $fp,16($sp) # Save frame pointer
 addiu $fp,$sp,28 # Set up frame pointer
 sw $a0,0($fp) # Save argument (n)
 lw $v0,0($fp) # Load n
 bgtz $v0,$L2 # Branch if n > 0
 li $v0,1 # Return 1
 jr $L1 # Jump to code to return

$L2:
  lw $v1,0($fp) # Load n
  subu $v0,$v1,1 # Compute n - 1
  move $a0,$v0 # Move value to $a0
  jal fact # Call factorial function
  lw $v1,0($fp) # Load n
  mul $v0,$v0,$v1 # Compute fact(n-1) * n
  
$L1: # Result is in $v0
  lw $ra, 20($sp) # Restore $ra
  lw $fp, 16($sp) # Restore $fp
  addiu $sp, $sp, 32 # Pop stack
  jr $ra # Return to caller

This is the output of the QtSPIM simluator

The book I am reading on MIPS, says that the initial stack pointer when the program is run should point to location 0x7FFFFFFF in memory. Stack pointer ($sp) in the MIPS simulator is first access at instruction number 2. $sp points to an address 0x7ffffe10 as indicated by the $a1 register (0x7ffffe10 = 7ffffe14 - 4 ). How come the stack pointer address is 0x7ffffe10 and not 0x7FFFFFFF? What code changed it?

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • Keep in mind the value of the stack pointer when your program is run depends on the hardware. I wouldn't expect it to be at the same place across different implementations of MIPS. PlayStation isn't going to adhere to the same stack pointer starting value as QtSPIM for instance. – puppydrum64 Nov 22 '22 at 18:06

1 Answers1

2

First, 0x7FFFFFFF is not a reasonable address for a MIPS stack pointer, because it is odd.  A MIPS stack pointer points to words, so should be an even multiple of 4.

Second, QtSPIM sets up the simulation's initial stack in a similar manner to a UNIX process — it puts command line parameters and environment variables on the stack.

Suggest you take a look at the stack in the data section and you'll most likely see strings of the environment.  (Click on the Data tab, before running the first instruction of a simulation, then view the User Stack memory area.)

Running QtSPIM on windows, for example, I see the same strings in that I see when doing set command from the command line shell cmd (cmd.exe).

If you add "Command-line arguments to pass to program" using "Run Parameters" menu item (from QtSPIM menu: "Simulator"), any strings you type there will also appear on the stack in front of the environment strings.  That will also change the initial stack pointer value used by the simulation.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53