MIPS return address in main

Posted by Alexander on Stack Overflow See other posts from Stack Overflow or by Alexander
Published on 2010-04-06T22:41:09Z Indexed on 2010/04/06 22:43 UTC
Read the original article Hit count: 587

Filed under:
|

I am confused why in the code below I need to decrement the stack pointer and store the return address again. If I don't do that... then PCSpim keeps on looping.. Why is that?

########################################################################################################################
### main
########################################################################################################################

.text

.globl main

main: addi  $sp, $sp, -4   # Make space on stack
   sw $ra, 0($sp)    # Save return address

   # Start test 1 
   ############################################################
   la $a0, asize1    # 1st parameter: address of asize1[0]
   la $a1, frame1    # 2nd parameter: address of frame1[0]
   la $a2, window1   # 3rd parameter: address of window1[0] 

   jal   vbsme       # call function

   # Printing $v0
   add $a0, $v0, $zero     # Load $v0 for printing
   li $v0, 1            # Load the system call numbers
   syscall

   # Print newline.
   la $a0, newline         # Load value for printing
   li $v0, 4            # Load the system call numbers
   syscall

   # Printing $v1
   add $a0, $v1, $zero     # Load $v1 for printing
   li $v0, 1            # Load the system call numbers
   syscall

   # Print newline.
   la $a0, newline         # Load value for printing
   li $v0, 4            # Load the system call numbers
   syscall

   # Print newline.
   la $a0, newline         # Load value for printing
   li $v0, 4            # Load the system call numbers
   syscall
   ############################################################
   # End of test 1   


   lw $ra, 0($sp)       # Restore return address
   addi  $sp, $sp, 4    # Restore stack pointer
   jr $ra               # Return



########################################################################################################################
### vbsme
########################################################################################################################

#.text
.globl  vbsme


vbsme:  addi    $sp, $sp, -4        # create space on the stack pointer
    sw  $ra, 0($sp)     # save return address   

exit:   add $v1, $t5, $zero     # (v1) x coordinate of the block in the frame with the minimum SAD
    add $v0, $t4, $zero     # (v0) y coordinate of the block in the frame with the minimum SAD
    lw  $ra, 0($sp)     # restore return address
    addi    $sp, $sp, 4     # restore stack pointer   
    jr $ra              # return    

If I delete:

addi    $sp, $sp, -4      # create space on the stack pointer
    sw  $ra, 0($sp)       # save return address   

and

lw  $ra, 0($sp)     # restore return address
addi    $sp, $sp, 4     # restore stack pointer   

on vbsme: PCSpim keeps on running... Why??? I shouldn't have to increment/decrement the stack pointer on vbsme and then do the jr again right? The jal in main is supposed to handle that

© Stack Overflow or respective owner

Related posts about assembly

Related posts about mips