Wednesday, January 5, 2022

[SOLVED] Comma, colon, decorator or end of line expected after operand

Issue

I am programming in c and i compiled a c code to assembly code but when i re-compile the code with the NASM assembler , it is giving me a error

Expected comma , colon , decorator or end of line expected after operand . This occurs in line number 6 , line number 7 and 8 . Please help me with this .

push    ebp
mov     ebp, esp
and     esp, -16
sub     esp, 16
call    ___main ;
mov     DWORD PTR [esp+12], 753664
mov     eax, DWORD PTR [esp+12]
mov     BYTE PTR [eax], 65
leave
ret

Thanks,


Solution

Syntactically, using NASM, there is no PTR keyword. Removing those allows the code to compile up to the undefined ___main. For example:

push    ebp
mov     ebp, esp
and     esp, -16
sub     esp, 16
call    ___main:     ; semi-colon starts comment (should be colon)
mov     DWORD [esp+12], 753664
mov     eax, DWORD [esp+12]
mov     BYTE [eax], 65
leave
ret

Then compiling with:

$ nasm -felf -o asm_recompile.o asm_recompile.asm

The only error returned is:

asm_recompile.asm:5: error: symbol `___main' undefined

Generally, NASM assembly programs require:

section .text
    global _start

    _start:

Note: Just because you compile to assembly with gcc, do not expect to be able to simply compile the code back to a working elf executable using NASM. gcc by default generates AT&T syntax that is incompatible with NASM. Even telling gcc to output assembly using the -masm=intel option to produce intel format assembly will not compile as-is in NASM. gcc uses as as the assembler. You will have varying luck using as as well, due to the myriad of compiler scripts and options gcc uses by default. The best examination of the process you can get with gcc is to compile your c program to executable using the -v, --verbose option. That will show all of the compiler commands gcc uses to generate the assembly associated with the c code.



Answered By - David C. Rankin