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 (Store Task Register) is an instruction mnemonic. You're using it as a label without a colon. str: db ...
would have worked.
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.
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.)
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.
Also, use meaningful label-names. Like .space_found
instead of lab1
.