Can't size instruction

这一生的挚爱 提交于 2021-01-28 02:20:50

问题


I was wondering why some assembly instructions can be inferred, but others cannot. For example, in the following program I have:

.globl main
main:
    push %rbp
    mov %rsp, %rbp
    mov $8, -8(%rbp)
    mov -8(%rbp), %rax
    pop %rbp
    ret

I get the following error:

stack.s:5: Error: no instruction mnemonic suffix given and no register operands; can't size instruction

However, if I change: the 5th line to:

movq $8, -8(%rbp)

It runs without error. How can all the other operations infer the size, but that particular instruction cannot?


回答1:


The hint is in the message: "no register operands".

All the other instructions have a register as an operand, and the assembler knows how large the registers are. So for instance, mov %rax, -8(%rbp) must be a quadword move (8 bytes) because %rax is a 64-bit register. Likewise, mov %eax, -8(%rbp), mov %ax, -8(%rbp), and mov %al, -8(%rbp) will be longword / word / byte moves (4 / 2 / 1 bytes). As such, the size suffix is optional for those instructions.

But the immediate constant $8 has no inherent size, so the assembler has no way to know what size you wanted unless you tell it.



来源:https://stackoverflow.com/questions/63369102/cant-size-instruction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!