计算汇编语言指令的成本

Calculating cost of an instruction in assembly language

我正在看龙书上的代码生成。它给出了一种将成本与每个目标语言关联相关联的简单方法。

We shall assume each target-language instruction has an associated cost. For simplicity, we take the cost of an in­struction to be one plus the costs associated with the addressing modes of the operands. This cost corresponds to the length in words of the instruction. Addressing modes involving registers have zero additional cost, while those in­volving a memory location or constant in them have an additional cost of one, because such operands have to be stored in the words following the instruction.

一些示例:

我了解前两个示例的成本计算。我不明白第三个。费用如何 3另外我不明白上面引用文本中的粗体部分。

根据我的部分理解,我假设 BLTZ *R3,R0 的成本为 3,就像上面类似的第三个示例一样。但是这样的代价是1。怎么办?

注意 BLTZ r, L 如果寄存器 r 中的值为 小于零,并允许控制传递到下一条机器指令 如果没有。

I don't get the third one. How is the cost 3?

  • 1 条指令
  • 1 用于从下一个单词中获取常量 100
  • 1 用于从 R2+100 取值

cost of BLTZ *R3,R0 to be 3 as it is so for the similar third example above. But the cost of this is 1. How?

因为:涉及寄存器的寻址模式的附加成本为零

I don't understand the bold part in the quoted text above.

显然,此体系结构的机器代码使用一个字来指定操作码以及寄存器操作数,如果需要,还可以使用一个附加字来指定常量。

例如LD R1,*100(R2)需要2个字。第一个指定操作(LD 与寄存器相关的偏移量)以及目标寄存器 R1 和基址寄存器 R2。这些是字中的位域。然后第二个单词包含 100,cpu 知道要获取,因为第一个单词中的操作码。

有些定长架构把常量塞进第一个字,但显然只能有有限的范围。使用单独的词允许全范围。

我相信其他人可以更好地解释这一点。但是,这里有一些灵感:

成本不是真实的,根据龙书的定义假设 要了解 实际 成本,您必须检查目标体系结构的表。请参阅 SO wiki 以获取相应的链接。

所以通过与您上面的引用相关的三个示例:

we take the cost of an in­struction to be one

因此假设每条指令的成本为一个

those in­volving a memory location or constant in them have an additional cost of one

所以例子1到3有

  • 1 条指令 + 0 mem/const = 1
  • 1 条指令 + 1 条内存 = 2
  • 1 条指令 + 1 条内存 + 1 个常量 = 3

内存地址和常量分别编码在指令 OpCode 中,在指令的附加 字中 延长,成为提取,OpCode 字节影响 CPU Op/microOp 处理。

书上有误

成本模型只涉及指令长度

This length corresponds to the length of words of the instruction

内存操作不会增加成本,例如LD R1, *R2 花费了 1,因为

addressing modes involving registers has zero additional cost

并且不涉及常量(偏移量或文字内存地址)。

没有提到因为指令访问内存而产生的额外成本。

这也解释了为什么BLTZ *R3, R0的成本是一个:因为指令长度是一个字,寻址方式(*R3)也不涉及常量。

有一个list of errors in dragon book。你提到的问题在我的书的第 515 页,列表在第 515 页

The cost of the instruction is two, not three

相对而言,自龙之书以来发生了很大变化。乘数从 40 个周期 (int) 下降到 1/2 个周期,从 200-300 个周期 (float) 下降到……好吧,我不记得了,但我认为少于 10 个。

不过,内存访问的成本已经飙升,因为处理器加速了,而内存却跟不上这些增长。现在大量的优化工作是面向缓存行的(如果可能,确保彼此具有局部性的指令从单个缓存行中访问数据),并且由于您无法预测哪些内存访问将是缓存未命中,您可以不再容易从编译器内部预测执行时间。