简体   繁体   English

cmp汇编语言指令-gas格式

[英]cmp assembly language instruction - gas format

I am converting 32-bit and 64-bit assembly language files from gas to MASM format, and ran across an instruction in my code that seems completely problematic.我正在将 32 位和 64 位汇编语言文件从 gas 格式转换为 MASM 格式,并且在我的代码中遇到了一条似乎完全有问题的指令。 What I mean is, I see no way the assembler can know what size the operand is, whether the instruction fetches 8, 16, 32 or 64 bits from memory to perform the compare.我的意思是,我认为汇编器无法知道操作数的大小,无论指令是从内存中获取 8、16、32 还是 64 位来执行比较。 Here is the instruction:这是指令:

  • cmp $0, 8(%rsp) cmp $0, 8(%rsp)

If the instruction had been one of the following, the assembler could have figured out the size of the memory operand to fetch and compare based upon the register:如果指令是以下之一,则汇编器可以根据寄存器计算出要获取和比较的内存操作数的大小:

  • cmp %rax, 8(%rsp) cmp %rax, 8(%rsp)
  • cmp %eax, 8(%rsp) cmp %eax, 8(%rsp)
  • cmp %ax, 8(%rsp) cmp %ax, 8(%rsp)

I would have thought that the instruction needs to be cmpb, cmpw, cmpl, cmpq... but no, my program assembles it fine with just cmp instruction.我原以为指令需要是 cmpb、cmpw、cmpl、cmpq……但不,我的程序只用 cmp 指令就可以很好地组装它。 Unfortunately, I don't remember what this code is doing, and it is very non-obvious... so the easier way to resolve this is to know what the instruction is doing.不幸的是,我不记得这段代码在做什么,而且它非常不明显……所以解决这个问题的更简单的方法是知道指令在做什么。

Anyone know?有人知道吗? Anyone understand why this syntax (with operand size/type unspecified) is even allowed with an immediate operand?任何人都明白为什么这种语法(操作数大小/类型未指定)甚至允许立即操作数?

It must have some kind of default.它必须有某种默认值。 I tried on my machine, which says as is:我在我的机器上试过,它说as是:

Apple Inc version cctools-836, GNU assembler version 1.38

And I get these results:我得到了这些结果:

$ cat example.s 
    cmp $0, 8(%rsp)
$ make example.o
as   -o example.o example.s
$ otool -tV example.o
example.o:
(__TEXT,__text) section
0000000000000000    cmpl    $0x00,0x08(%rsp)

So on my machine, it picked cmpl .所以在我的机器上,它选择了cmpl Using gcc rather than as gave me the same results, but using clang produced the error you expect:使用gcc而不是as给了我相同的结果,但使用clang产生了你期望的错误:

$ clang -c example.s 
example.s:1:2: error: ambiguous instructions require an explicit suffix (could be 'cmpb', 'cmpw', 'cmpl', or 'cmpq')
 cmp $0, 8(%rsp)
 ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM