ASM国米preTER:如何存储本地变量? [英] ASM Interpreter: How are local variables stored?

查看:117
本文介绍了ASM国米preTER:如何存储本地变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功课,我需要写一个非常小的虚拟的16位汇编国米$ P $ C#中PTER。
它模拟了字节阵列(64K)中,用变量(A,B,C,...)的寄存器中的RAM中。
现在,我需要一种方法来保存局部变量,谷歌表示,他们是在堆栈中分配。

for my homework I need to write a very little virtual 16 bit Assembler-Interpreter in C#. It simulates the RAM with a byte-array (64k) and the registers with Variables (A,B,C,...). Now I need a way to save local variables, google says that they are allocated on the Stack.

不过那事我不清楚是,当他们在堆栈中分配(含推...),怎么会是国米preTER访问他们时,他们以后使用?

But the thing thats unclear to me is, when they are allocated on the Stack (with push...), how is the Interpreter accessing them when they are used later?

请参阅以下2行:

pi INT 3
mov A, pi

在第一行,PI被分配在栈中,在第二行,PI被使用,但国米preTER应该怎么知道其中Pi是在堆栈中访问其数据? (我的堆栈是一个字节数组太用2辅助函数(推,流行),还一个指针堆栈的顶部)

In the first line, pi is allocated on the stack, in the second line, pi is used, but how should the Interpreter know where pi is in the stack to access its data? (my Stack is a byte-array too with 2 helper-functions (push, pop), there is also a pointer to the top of the stack)

推荐答案

通常没有独立的堆栈内存,而不是堆栈是在常规的RAM,所以你只有一个跟踪它的堆栈指针。

Typically there is no separate stack memory, instead the stack is in the regular RAM, so you only have the stack pointer that keeps track of it.

此外典型地,局部变量是在子程序的开头由堆栈指针复制到另一个寄存器,然后移动堆栈指针以腾出空间给变量分配

Also typically, local variables are allocated at the beginning of a subroutine by copying the stack pointer to another register, then moving the stack pointer to make room for the variables:

mov bp, sp ;copy stack pointer
sub sp, 4 ;make room for two integer variables

访问局部变量是使用堆栈指针的副本完成的:

Accessing local variables is done using the copy of the stack pointer:

mov A, [bp-2] ;get first integer
mov B, [bp] ;get second integer

当你离开子程序,你恢复堆栈指针释放局部变量:

When you leave the subroutine, you restore the stack pointer to deallocate the local variables:

mov sp, bp ;restore stack
ret ;exit from subroutine

这是你的问题使用的语法通常用来声明全局变量,不是局部变量:

The syntax that you use in the question is usually used to declare global variables, not local variables:

.data
pi int 3 ;declare a label and allocate room for an int in the program
.code
mov A, pi ;use the address of the label to access the int

这篇关于ASM国米preTER:如何存储本地变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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