处理器型号I
● Registers
PC – Program Counter
Single data register (accumulator) without name
We will use symbol A to describe operations on this register
● Stack with an unspecified implementation
● Data specified by name or value
More realistic model will follow later
Instruction Action Description
---------------------------------------------------------------------
load data A = data Load data to accumulator
store data data = A Store data from accumulator to memory
add data A += data Add data to accumulator
push data Push data onto stack
call addr Push PC Jump and store return trace on the stack
PC = addr
Pop PC
return Pop PC Return – jump to the previously stored trace addr
create data Allocate data on stack
destroy data Deallocate data from stack
处理器型号II
● More realistic version of model I
● Stack explicitly implemented in memory
● Stack Pointer register - SP
● Stack operations defined explicitly using SP
● Allocation and deallocation of local data implemented
by changing the value of SP (no data transfer occurs)
● Arguments and local variables accessed by names
Still unrealistic
Instruction Action Description
---------------------------------------------------------------------
load data A = data Load data to accumulator (constant or variable)
store data data = A Store data from accumulator to memory
add data A += data Add data to accumulator (constant of variable)
push data *--SP = data Place data on the stack (constant of variable)
call addr *--SP = data Jump storing the return trace on the stack
PC = addr
return PC = *SP++ Return to the previously stored trace address
Create data SP -= sizeof(data) Allocate data on the stack
Destroy data SP += sizeof(data) Deallocate data from the stack
上述两张幻灯片是在x86 / MIPS的演讲中提出的。我不太了解。
我唯一理解的是,有两种使用堆栈调用函数的模型,以及分配/释放堆。一个使用PC而另一个使用SP。
它是在谈论两种不同处理器(x86 / MIPS)的型号吗?哪一个适合谁?
任何人都可以解释一下吗?
两者都是类似x86的,其中call
将返回地址(PC)推入堆栈。注意,在执行call
指令期间,PC指向指令/下一个指令的开始结束,因此PC是应该从ret
执行call
之后执行的指令的地址。
第二个模型更明确地说明了堆栈是如何工作的,匹配x86 push
/ pop
/ call
/ ret
。这一切都改变了。
MIPS jal
将返回地址放入寄存器(链接寄存器$lr
是MIPS上32个通用整数寄存器之一)。软件可以手动将其推入堆栈(例如,在非叶功能中),但ISA不知道/不关心它。
MIPS甚至在架构上没有一个“堆栈指针”,它隐含地用于任何事情。 callstack是一种软件约定,虽然非常有用,但基本上所有软件都使用与x86基本相同的方式,只是没有组合修改SP和加载或存储的单个指令。
我唯一理解的是,有两种使用堆栈调用函数的模型,以及分配/释放堆。一个使用PC而另一个使用SP。
不,这在所有级别都是完全错误的。
首先,没有一个示例显示堆内存的任何内容,只为堆栈中的局部变量(自动存储)保留空间。函数返回时将释放的存储空间。
“堆”记忆是分开的。它通常不是一件事,例如在现代操作系统中,静态和动态分配通常是分开的。但无论如何,像malloc
这样的动态堆分配会给你一个指向内存的指针,该指针在add sp, 16
之后仍然有效,以及ret
来拆除当前函数的堆栈帧。
第二,PC在存储分配方面根本不参与。详细信息仅显示PC作为返回地址读取,并由跳转/调用/ ret指令写入。它也被称为IP,即指令指针。在x86上,32位和64位版本的IP寄存器分别是EIP / RIP。