2

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Robert Salay
  • 21
  • 1
  • 2
  • nasm requires colons after labels, i.e. `str: db 'this is a test',0x0` – Jester Nov 12 '16 at 04:08
  • @Jester: no it doesn't, only when they're also valid mnemonics :P That's why the `-Worphan-labels` option exists. The OP's program assembles when changing `str` to `str1`. (Except of course for the missing operand-sizes on the `cmp mem, immediate` instructions) – Peter Cordes Nov 12 '16 at 04:10
  • 3
    Better to always use it, one less headache. Also, what happens if next year intel introduces a new instruction that collides with your label name... – Jester Nov 12 '16 at 04:11
  • @Jester: Totally agree that it fixes the problem and is a good idea for style / future-proofing, just not from a language-rules perspective. Will update my answer to suggest it. – Peter Cordes Nov 12 '16 at 04:12

1 Answers1

6

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847