问题描述
我需要在汇编时以字节为单位计算函数的大小.我尝试了各种方法,包括:
I need to compute the size of a function in bytes at assembly time. I've tried various ways, including:
.set chk0_sz, offset chk0_e - offset chk0_s
然后使用mov rcx, offset chk0_sz
来获取值.
但是,它给出了错误:
错误:在内存操作数中不能使用多个符号
.
这里的chk0_e
和chk0_s
是两个标签,分别表示函数的结束和开始.
Here chk0_e
and chk0_s
are two labels denoting the end and start of the function, respectively.
有什么想法吗?
推荐答案
当将地址用作立即数时,您只需要 offset
关键字.在其他上下文中,例如作为数据,无论如何都不能取消引用,因此符号是地址.
You only need the offset
keyword when using an address as an immediate. In other contexts, like as data, it can't be dereferenced anyway so the symbol is the address.
编译器通常使用诸如 .size chk0, 之类的东西.- chk0
.所以你可能想要
Compilers typically use stuff like .size chk0, . - chk0
. So you probably want
.equ chk0_sz, . - chk0 # at the end of chk0
.
是当前位置,如果你把 .equ
放在那个位置,它会使用 chk0_e
替换.
.
is the current position, it replaces using chk0_e
if you put the .equ
at that position.
显然你不能使用 rcx
(一个寄存器)作为汇编时间常数计算的一部分.或者你的意思是 mov rcx, offset chk0_sz
use 大小?
And obviously you can't use rcx
(a register) as part of an assemble-time-constant calculation. Or did you mean mov rcx, offset chk0_sz
to use the size?
如果您使用 .equ
将其定义为汇编时间常量,则只需执行 mov ecx, chk0_sz
.MASM 风格的语法不一致,因此这是一个 mov-immediate 而不是加载,因为 chk0_sz
是如何定义的.
If you define it as an assemble-time constant with .equ
, you'd just do mov ecx, chk0_sz
. MASM-style syntax is inconsistent, so this is a mov-immediate not a load because of how chk0_sz
was defined.
这篇关于如何使用 Intel 语法在 GNU 汇编器中以字节为单位获取函数的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!