寄托天下
查看: 2847|回复: 10

请教CS问题,关于activation record的 [复制链接]

Rank: 5Rank: 5

声望
1
寄托币
3052
注册时间
2005-5-6
精华
2
帖子
7
发表于 2005-11-8 21:54:31 |显示全部楼层
下面这个问题怎么也看不明白,虽然有解释,尤其是不明白红色标注的代码是什么用的,有谁知道的麻烦解释一下吧,谢谢啦~~

一个activation record的格式为:
0      %fp
-4     return address
-8     static link
-12    arguments (if any) followed by local variables

程序段procedure P:
var a : integer;
    procedure Q(var x : integer)
        begin
           x := x + a
        end
begin
    Q(a);
end

(1)在Q中,读取x值的操作将译为下列哪段机器代码
(A) mov t0, -12(%fp)
(B) mov t0, -12(%fp)
     mov t0, 0(%t0)
(C) mov t0, -12(%fp)
    mov to, -4(%t0)
Answer: B
x是过程Q的局部变量,所以位置在-12。

(2)在Q中,读取a值的操作将译为:
(A) mov t1 -8(%fp)
     mov t1 0(%t1)
(B) mov t1 -8(%fp)
     mov t1 -4(%t1)
(C) mov t1 -8(%fp)
     mov t1 -8(%t1)
(D) mov t1 -8(%fp)
    mov t1 -12(%t1)
Answer: D
a是过程Q的外部变量,通常通过activation record中的static link返回调用者的活动记录查找该变量的地址。

[ Last edited by @amy@ on 2005-11-8 at 21:56 ]

使用道具 举报

Rank: 2

声望
0
寄托币
73
注册时间
2005-6-26
精华
0
帖子
0
发表于 2005-11-9 05:06:41 |显示全部楼层

我的感觉

1> A should be good. we really don't  need mov t0, 0(%t0)
2> Make sense to me, mov t1 -8(%fp) will give you the address of activation record for P through Q's activation record. Then inside P, you call  mov t1 -12(%t1) to get a. This step is the same as 1>

Check with AutoSea.

使用道具 举报

Rank: 5Rank: 5

声望
1
寄托币
3052
注册时间
2005-5-6
精华
2
帖子
7
发表于 2005-11-9 20:03:20 |显示全部楼层
谢谢楼上~~

使用道具 举报

Rank: 4

声望
0
寄托币
1521
注册时间
2005-1-12
精华
1
帖子
4
发表于 2005-11-9 22:08:24 |显示全部楼层
1,Q的x是变参,Pascal中的变参的值可以在过程中改变,所以在activation record中应该记录变参的地址,对于这个程序来说就是a的地址,这样才可以在Q中实现对a的修改。所以要先取得x的地址,然后在再访问这个地址的值。

不知道说清楚没有,呵呵
GRE作文互动论坛 -> GRE考试综合论坛 -> TOEFL考试讨论专版  -> GRE_SUB -> 美国留学 -> VISA 美国签证 -> 行前准备::飞跃同期声 -> 异乡岁月※海外申请

使用道具 举报

Rank: 5Rank: 5

声望
1
寄托币
3052
注册时间
2005-5-6
精华
2
帖子
7
发表于 2005-11-10 00:24:37 |显示全部楼层
谢谢楼上,你的意思是(1)还是选B吗?意思是
(B) mov t0, -12(%fp)   取得x的地址
     mov t0, 0(%t0)   访问这个地址的值吗?

汗,似乎还是想不太明白,这种形式的机器代码第一次接触到,汗~~~

使用道具 举报

Rank: 2

声望
0
寄托币
73
注册时间
2005-6-26
精华
0
帖子
0
发表于 2005-11-10 05:23:03 |显示全部楼层

Ask autosea.

Then how about 2.

1>'s answer  is contrary with 2>'s answer.

2>'s D should add one more line too.

mov t1 0(%t1)

使用道具 举报

Rank: 5Rank: 5

声望
1
寄托币
3052
注册时间
2005-5-6
精华
2
帖子
7
发表于 2005-11-10 08:39:36 |显示全部楼层
同问 同问,呵呵

使用道具 举报

Rank: 2

声望
0
寄托币
73
注册时间
2005-6-26
精华
0
帖子
0
发表于 2005-11-10 10:14:07 |显示全部楼层

其实这个回忆题有点问题.

Activation record is stored in memory. So, firstly, it shouldn't be MOV instruction, I would think for II,

It should be

lw t1 -8(%fp)
lw t1 -12(%t1)



lw is load.

For 1> In the activation record, the argument is pushed on the stack before the call. I don't think the argument is stored at the same place as local variable as the question indicated.

So, if fp is referring to P's frame pointer, we firstly use
lw t0, -12(%fp)   to load the a's value into register to, when calling Q, we need to push it onto the stack.

It will be
sw to (sp). sp is stack pointer.

使用道具 举报

Rank: 4

声望
0
寄托币
1521
注册时间
2005-1-12
精华
1
帖子
4
发表于 2005-11-10 11:41:36 |显示全部楼层
关于2, 就是选D
mov t1 -8(%fp)    -8(%fp) 这个内存单元中存放的是P的栈帧首地址,所以执行完这条语句后t1的值就是P的栈帧首地址。
mov t1 -12(%t1)  根据栈的布局-12(%t1)这个内存单元就是P的局部变量(不是局部变量的地址)。而a在P中是局部变量,不是变参,所以可以直接取出。

这道题在指南最后的模拟题中有,表述得要好一些。

[ Last edited by Autosea on 2005-11-10 at 11:44 ]
GRE作文互动论坛 -> GRE考试综合论坛 -> TOEFL考试讨论专版  -> GRE_SUB -> 美国留学 -> VISA 美国签证 -> 行前准备::飞跃同期声 -> 异乡岁月※海外申请

使用道具 举报

Rank: 5Rank: 5

声望
0
寄托币
1397
注册时间
2005-11-1
精华
1
帖子
2
发表于 2005-11-10 12:01:28 |显示全部楼层
我想区别在于 a是局部变量,直接存储的值,而 x是形式参数,存的是地址

不知道说的对不对

使用道具 举报

Rank: 5Rank: 5

声望
1
寄托币
3052
注册时间
2005-5-6
精华
2
帖子
7
发表于 2005-11-10 13:07:00 |显示全部楼层
噢~~  谢谢各位的解答,呵呵

使用道具 举报

RE: 请教CS问题,关于activation record的 [修改]

问答
Offer
投票
面经
最新
精华
转发
转发该帖子
请教CS问题,关于activation record的
https://bbs.gter.net/thread-360360-1-1.html
复制链接
发送
回顶部