在MIPS过程中创建和删除堆栈空间

时间:2019-05-11 22:43:23

标签: assembly stack mips procedure

#===========================================
# PrintStr - prints a string
# arguments:
#   $a0 = arress of the string
# return value:
#   n/a
#===========================================
PrintStr:   
    # backup return address
    addiu $sp, $sp, -8 #  create space for 2 words
                       # (4*2=8 bytes) on the stack 
    sw $ra, 0($sp) # backup return address $ra  
    # protect arguments from change
    sw $v0, 4($sp) # protect string address

    li $v0, 4
    syscall

    lw $ra, 0($sp) # backup return address $ra
    lw $v0, 4($sp) # protect string address

    # restore stack pointer     
    addiu $sp, $sp, 8 # return the space on the stack(pop)

    # return 
    jr $ra

在以上代码中,通过使用代码addiu $sp, $sp, -8在堆栈上创建了8个字节以保存两个寄存器。

对吗?

不是addiu $sp, $sp, -8代表一个空格,addiu $sp, $sp, -12代表两个空格,依此类推吗?

0 个答案:

没有答案
相关问题