与函数调用的SP增量相关的困境 [英] Dilemma related to function call's increment of SP

查看:80
本文介绍了与函数调用的SP增量相关的困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数调用时push的情况下,为什么栈指针移动到a通过减去 4 倍的寄存器数量来获得更小的值压入堆栈?

In case of push during function call, why the stack pointer moves to a smaller value by subtracting 4 times the number of registers to be pushed on the stack?

我在阅读理解堆栈

推荐答案

在同一个页面中,明确提到了栈的内存布局:-

In the same page, it is clearly mentioned about the memory layout of stack :-

考虑堆栈的以下方面很有用.

It's useful to think of the following aspects of a stack.

栈底 栈的最大有效地址.栈初始化时,栈指针指向栈底.

stack bottom The largest valid address of a stack. When a stack is initialized, the stack pointer points to the stack bottom.

堆栈限制 堆栈的最小有效地址.如果堆栈指针变得比这个小,那么就会出现堆栈溢出(这个不应与数学运算溢出混淆).

stack limit The smallest valid address of a stack. If the stack pointer gets smaller than this, then there's a stack overflow (this should not be confused with overflow from math operations).

内存的其他部分用于程序和堆(用于动态内存分配的内存部分).

Other sections of memory are used for the program and for the heap (the section of memory used for dynamic memory allocation).

而且,谈到 PUSH 操作,需要减去 4 倍要压入堆栈的寄存器数量,因为在 MIPS 架构中,顺序字的地址相差 4. 而且,MIPS I 指令集架构 (ISA) 和 II ISA 的寄存器是 32 位(4 字节).

And, talking about the PUSH operation, subtracting 4 times the number of registers to be pushed on the stack is needed because in MIPS architecture, addresses of sequential words differ by 4. And, the registers are 32 bits(4 bytes) for MIPS I instruction set architecture (ISA) and II ISA.

对于我们的 4 字节(全字)数据堆栈,添加一个项目意味着从 $sp 中减去 4 并将该项目存储在该地址中.

For our stack of 4-byte (full word) data, adding an item means subtracting four from $sp and storing the item in that address.

这是代码中的样子.假设要压入堆栈的值在寄存器 $t0 中:

Here is what that looks like in code. Say that the value to push on the stack is in register $t0:

# PUSH the item in $t0:
subu $sp,$sp,4      #   point to the place for the new item,
sw   $t0,($sp)      #   store the contents of $t0 as the new top.

因此,您可以压入一个或多个寄存器,方法是将堆栈指针设置为较小的值(通常是减去要压入堆栈的寄存器数量的 4 倍)并将寄存器复制到堆栈中.

And, so, you can push one or more registers, by setting the stack pointer to a smaller value (usually by subtracting 4 times the number of registers to be pushed on the stack) and copying the registers to the stack.

这篇关于与函数调用的SP增量相关的困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆