0

例如,下面是一段 C 代码及其由 cc 编译器生成的汇编代码。

// C code (pre K&R C)    
foo(a, b) {
    int c, d;
    c = a;
    d = b;
    return c+d;
}
// corresponding assembly code generated by cc
.global _foo
.text
_foo:
~~foo:
~a=4
~b=6
~c=177770
~d=177766
jsr r5, csv
sub $4, sp
mov 4(r5), -10(r5)
mov 6(r5), -12(r5)
mov -10(r5), r0
add -12(r5), r0
jbr L1
L1: jmp cret

我可以理解大部分代码。但我不知道有什么作用~~foo:。以及 和 中的神奇数字从何~c=177770而来~d=177766。硬件是 pdp-11/40。

4

1 回答 1

0

波浪线看起来像决定堆栈使用的数据。您可能会发现回忆 pdp-11 使用 16 位整数以及 DEC 更喜欢八进制数而不是十六进制数会很有帮助。

jsr r5, csv

是一种使寄存器 5 (r5) 指向某些数据(可能是偏移量列表)的方法。

这些数字对应于八进制堆栈上的偏移量。假设调用者做类似的事情

  • 将 a 和 b 压入堆栈(正偏移)
  • 将返回地址压入堆栈(偏移量=0)
  • 可能会在csv函数中推送其他内容
  • c 和 d 是局部变量(负偏移,因此是“17777x”)

那条线

~d=177776

看起来很奇怪 - 我希望

~d=177766

因为它应该c在堆栈的下方。寄存器操作数中的-10-12偏移量看起来也是八进制数。您应该能够根据上下文将偏移量与变量匹配。

这只是一个有根据的猜测:我在text-editor中改编了 jsr+r5 习语。

带有波浪线的线是符号定义。DECUS C Compiler Reference中有一个线索,位于

ftp://ftp.update.uu.se/pub/pdp11/rsx/lang/decusc/2.19/005003/CC.DOC

它说

  3.3  Global Symbols Containing Radix-50 '$' and '.' 
         ______ _______ __________ ________     ___

    With  this  version  of  Decus C, it is possible to generate and
    access global symbols which contain the Radix-50  '.'  and  '$'.
    The  compiler allows identifiers to contain the Ascii '$', which
    becomes a Radix-50 '$' in the object code.  The AS assembly code
    shows  this  character as a tilde (~).  The underscore character
    (_) in a C program  becomes  a  '.'  in  both  the  AS  assembly
    language  and  in  the  object  code.  This allows C programs to
    access all global symbols:  

            extern int $dsw;  
            .  .  .  
            printf("Directive status = %06o\n", $dsw);  

    The  above  prints  the current contents of the task's directive
    status word.

所以你可以阅读

~a=4

作为

$a=4

并且看到这$a是一个(或多或少)传统符号。

于 2017-04-02T22:45:08.957 回答