6

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,

3
  • If you remove the semicolon after the statement with the call of main do the errors occur? Also are white spaces allowed in statement for example move byte ptr[eax], 65? Try to remove the white spaces before 65. Commented Dec 22, 2014 at 9:34
  • How did you compile this with gcc? gcc -S -masm=intel -o your.asm your.c? Commented Dec 22, 2014 at 9:37
  • Yeah i compiled like that way David C.Rankin had shown !
    – user43609
    Commented Dec 22, 2014 at 10:14

2 Answers 2

10

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.

1
  • call ___main: is a syntax error, even if you had done extern ___main. semi-colon ; is the comment character, not : colon. And you don't use : on symbol names you're mentioning, only when using a label to define them. error: invalid combination of opcode and operands because the only syntax for a call that includes a : is a far call to a seg:off address, and main isn't a segment, and there's no offset. Commented Dec 14, 2021 at 1:50
0

It seems that the errors occured due to white spaces after commas. Try to change for example statement

mov     DWORD PTR [esp+12], 753664

to

mov     DWORD PTR [esp+12],753664
1
  • This syntactic adjustment fixed the error in my case.
    – jxmorris12
    Commented Nov 6, 2019 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.