本文介绍了NASM:“期望在操作数之后的逗号,冒号,修饰符或行尾"声明字符串时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NASM来编写汇编代码程序,但是由于某种原因,它总是给我一个错误.它说我声明一个字符串后期望逗号,冒号,修饰符或行尾,但是我看不出怎么可能是个问题,请告知.

I'm trying to use NASM to make an assembly code program, but for some reason it keeps giving me an error. It says it expects a comma, colon, decorator, or end of line after I declare a string, but I don't see how it can be an issue Please advise.

section .text
        global main
main:
        mov edi,str
lab3:
        cmp [edi],' '
        je lab1

        cmp [edi],0x0
        je lab2

        mov eax,4
        mov ebx,1
        mov ecx,edi
        mov edx,1
        int 0x80
        inc edi
        jmp lab3
lab1:
        inc edi
        mov eax,4
        mov ebx,1
        mov ecx,nwln
        mov edx,1
        int 0x80
        jmp lab3

lab2:
        mov eax,1
        int 0x80

section .data
str db 'this is a test',0x0     ;this is the line giving the error
nwln db 0xa

推荐答案

STR (存储任务寄存器)是指令助记符.您将其用作不带冒号的标签. str: db ...本来可以.

STR (Store Task Register) is an instruction mnemonic. You're using it as a label without a colon. str: db ... would have worked.

YASM在此处提供了更有用的错误消息:string.asm:33: error: unexpected DB/DW/etc. after instruction

YASM gives a more useful error message here: string.asm:33: error: unexpected DB/DW/etc. after instruction

无论您是在标记代码还是在数据上,始终在标签名称后使用: 是一个很好的做法.对于人类读者来说,这更清楚,并且可以防止将来的指令助记符或汇编程序指令对未来产生影响.

It's good practice to always use a : after a label name, whether you're labelling code or data. It's clearer for human readers, and more future-proof against future instruction mnemonics or assembler directives.

使用 -Worphan-labels ,因此如果您自己在一行上写类似cqde(不是cqde:)的内容,则会收到警告.如果没有该选项,它将在该行放置一个标签.使用该选项,您将得到警告并注意到您键入cdqe! (或其他任何非操作数x86指令.)

It's also a good idea to build with -Worphan-labels so you get a warning if you write something like cqde (not cqde:) on a line by itself. Without that option, it puts a label at that line. With that option, you'll get a warning and notice that you typoed cdqe! (Or any other no-operand x86 instructions.)

顺便说一句,在使用带有立即数和内存操作数的指令时,请不要忘记使用cmp byte [edi],' '操作数大小修饰符,因为它不会与模棱两可的操作数大小组合在一起.

BTW, don't forget to use cmp byte [edi],' ' operand-size modifiers when using instructions with an immediate and a memory operand, because it won't assemble with an ambiguous operand-size.

此外,使用有意义的标签名称.像.space_found而不是lab1.

Also, use meaningful label-names. Like .space_found instead of lab1.

这篇关于NASM:“期望在操作数之后的逗号,冒号,修饰符或行尾"声明字符串时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:21