Sample Code Sample

I found this spim sample code on the web

.data COUNT: .word 10 TEXT: .asciiz "The number is " EOL: .asciiz "\n" .text .globl main main: addiu $sp, $sp, -32 # Adjust stack sw $ra, 24($sp) sw $fp, 16($sp) # save old frame pointer addiu $fp, $sp, 28 # load new frame pointer la $t0, COUNT lw $t1, 0($t0) li $t0, 0 # init index to 0 loop: sw $t0, 12($sp) # save caller saved registers sw $t1, 8($sp) # move $a0, $t0 # setup parameter for fn call jal print_num # call subroutine lw $t1, 8($sp) # restore caller saved values lw $t0, 12($sp) # addiu $t0, $t0, 1 # increment index; blt $t0, $t1, loop # lw $fp, 16($sp) # restore frame pointer lw $ra, 24($sp) # restore return address addiu $sp, $sp, 32 # restore stack pointer jr $ra 

UPDATE
I can't get it: if the main function needs to save four registers on the stack ($ ra $ fp $ t0 $ t1), then how does it allocate space for 8 registers (32 bytes instead of 16 bytes)?

thank you for your time

+4
source share
2 answers

The frame pointer should point to the beginning (bottom) of the stack. Since the stack elements are 4 bytes, the beginning of the bottom element is 4 less than the size of the stack.

+3
source

This is due to the use of an agreement, in particular an agreement for calls . In MIPS, calleรฉ saves some registers on the stack, if they need to use them, and registers need to be saved and where on the stack they are stored, it is indicated in some agreement. The IDK is about SPIM, but SGI IRIX has two different conventions: o32 and n32 (for "old" and "new"), you may have some luck for Google.

+2
source

Source: https://habr.com/ru/post/1347767/


All Articles