0

I am following a tutorial and learning x64 Intel Syntax Assembly and I have this code sample that suppose to compile without issues but I am getting errors, so I cannot get started.

Code sample:

.code
ASM_Debug proc
    mov rax, 55
    ret
ASM_Debug endp
end

I am using NASM assembly compiler, using MinGW (G++) code compiler, if I compile using inline assembly using x64 Intel Syntax MASM I don't get any errors.

Could someone tell me what do I need to do?, what is the correct NASM command line to compile this code so I could get started learning. Thanks

My current command line is: nasm.exe -f elf64 Foo.asm -o Foo.o

PS: I am NOT using Visual Studio IDE for development, I don't want to hear anything about it.

PS2: I am developing/working on/for Windows OS, NOT Linux.

Compilation errors:

CoreASM.asm:1: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
CoreASM.asm:1: error: attempt to define a local label before any non-local labels
CoreASM.asm:2: error: parser: instruction expected
CoreASM.asm:5: error: symbol `ASM_Debug' redefined
CoreASM.asm:5: error: parser: instruction expected
CoreASM.asm:6: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
CodeMaster12
  • 15
  • 1
  • 5
  • Related / probable duplicate: [NASM Error Parsing, Instruction Expected](https://stackoverflow.com/q/11572307), except it's about ancient 16-bit MASM code, not x86-64 MASM – Peter Cordes Mar 06 '21 at 08:32

1 Answers1

1

The code you've written is in MASM syntax, which NASM doesn't support. You can tell because of the PROC/ENDP keywords, which are only in MASM (and TASM, which is mostly syntax-compatible with MASM).

So, this is MASM syntax:

.code
ASM_Debug proc
    mov rax, 55
    ret
ASM_Debug endp
end

And this is the same code translated to NASM syntax:

SECTION .text

ASM_Debug:
    mov rax, 55
    ret

Notice, in particular, that:

  • The .code directive has been replaced by SECTION .text.
  • The PROC/ENDP keywords have been eliminated, and a simple label is used to name the procedure. Procedures don't repeat their names at the end.
  • There is no END directive at the end of the code file.

You can find multiple guides to syntactical differences between MASM and NASM online. Here is a blog post by Dara Hazeghi that looks pretty comprehensive.

Your command line for running NASM is also wrong because it's trying to build a Linux-format binary (elf64 format). (You can build these in NASM on Windows, of course, but you won't be able to run them.) For 64-bit Windows, you want win64. So, it would be:

nasm.exe -f win64 Foo.asm -o Foo.obj

(Note that .obj is conventional for object files on Windows, not .o, although you can use either. The entire -o switch is actually optional; if you omit it, NASM will automatically name the object file with the standard extension and the same name as our input source file.)

But, if you're trying to learn how to program in assembly, you really need to find a tutorial that uses syntax identical to what your assembler expects. If you want to use NASM as your assembler, then find a tutorial that also uses NASM. Or, if you want to use the MASM tutorial, then use MASM as your assembler. Version 8 of MASM can be downloaded here for non-commercial use; it isn't the newest version, but it's perfectly suitable for learning. However, that's the 32-bit version. If you want to assemble 64-bit code, you'll need the x86-64 version, with the filename ml64.exe. I don't know anywhere you can download this separately, but it is included with the Windows SDK. At least, I know it was included with the Windows 7 SDK; I'm not sure if it's still there in the latest Windows 10 SDKs, but I think so.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574