为什么NA​​SM在使用有效的指令助记符作为操作数中的符号名称时不会遇到麻烦? [英] Why doesn't NASM have trouble with valid instruction mnemonics as symbol names in operands?

查看:133
本文介绍了为什么NA​​SM在使用有效的指令助记符作为操作数中的符号名称时不会遇到麻烦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下简单程序,但是nasm拒绝对其进行编译.

I wrote the following simple program, but nasm refuses to compile it.

section .text
    global _start

_start:
    mov rax, 0x01 
    mov rdi, 0x01
    mov rsi, str
    mov rdx, 0x03
    syscall

    mov rax, 60
    syscall

segment .data
    str db 'Some string'


nasm -f elf64 main.asm
main.asm:15: error: comma, colon, decorator or end of line expected after operand

我在此 answer 中读到的原因是str是指令助记符.因此,我在str中添加了一个冒号,现在可以正常编译了.但是那行

As I read in this answer this is because str is an instruction mnemonic. So I added a colon to str and now it compiles fine. But what about the line

mov rsi, str

str是此处的指令助记符,但仍可以正常编译.为什么?

str is an instruction mnemonic here, but it still compiles fine. Why?

推荐答案

NASM手册所述,除了宏定义和指令,NASM源代码行的格式具有以下四个字段的组合:

As the NASM manual explains, other than macro definitions and directives, the format of a NASM source line has some combination of these four fields:

label:    instruction operands        ; comment

mov视为助记符后,便不再将其余标记视为可能的指令助记符.汇编语言严格来说是每个语句仅使用一条指令.

After it sees mov as the mnemonic, it's no longer considering the remaining tokens as possible instruction mnemonics. Assembly language is strictly one instruction per statement.

如果您想将编码str ax指令的字节作为mov -sign-extended-imm32的立即操作数,则必须自己使用数字常量来完成. NASM语法没有为您执行此操作的方法,因此一旦发现助记符,其解析器就无需递归到操作数中.

If you wanted the bytes that encode an str ax instruction as the immediate operand for mov-sign-extended-imm32, you'd have to do that yourself with a numeric constant. NASM syntax doesn't have a way to do that for you, so its parser doesn't need to recurse into the operands once its found a mnemonic.

或者使用db发出mov指令的字节,而不是手动编码str.

Or instead of encoding str manually, use a db to emit the bytes of the mov instruction.

db 0x48, 0xc7, 0xc6    ; REX.W prefix, opcode for mov r/m64,imm32,  ModR/M = rsi destination
      str  [rax+1]     ; a disp8 makes this 4 bytes long.


;; same machine code as
mov rsi, strict dword 0x0148000f    ; str [rax+1]

;; nasm optimizes it to mov esi, imm32 without strict dword.
;; I guess I should have used that 5-byte instruction for the DB version...

这篇关于为什么NA​​SM在使用有效的指令助记符作为操作数中的符号名称时不会遇到麻烦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆