1

Why doesn't NASM compile to an object file with the org directive?

org 0x7C00
nop
mov ax, ax
nop

If I compile this with:

nasm -O0 -fobj temp.asm

NASM gives me an error for whatever reason:

temp.asm:1: error: parser: instruction expected
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74

2 Answers2

3

In this case instead of org you should use resb:

; file: nasmOrg.asm
; assemble: nasm -f obj nasmOrg.asm -o nasmOrg.obj

SEGMENT _TEXT PUBLIC CLASS=CODE USE16

;        resb    0x0100 ; reserve 0x0100 bytes for a .COM program
        resb    0x7C00 ; reserve 0x7C00 bytes for a boot sector
..start:
nop
mov ax, ax
nop

This is how you can compile different parts of a .COM program into separate object files. If you use TLINK as the linker, the next step would be:

tlink.exe /t nasmOrg.obj [any other object files], nasmOrg.bin

Note that if you omit the comma and the following name of the resultant binary (nasmOrg.bin) or if you specify a name with the .COM extension (e.g. nasmOrg.com), TLINK will refuse to link, it will say something like:

Error: Cannot generate COM file : invalid initial entry point address

And you'll have to change 0x7C00 to 0x0100 to make a .COM program.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • I realize that this is a really old thread, but I am trying to do this exact same thing. I would like to create an object file instead of a .com file because I want to use the `-g` option for NASM to have debugging information in Turbo Debugger. So in your example, after linking my object file (`hello.obj`), how would I get the .com file? – wkjagt Jan 08 '23 at 00:46
  • @wkjagt Using tlink exactly or almost exactly as shown? What did you try and what did you get? – Alexey Frunze Jan 08 '23 at 01:19
  • Oh I just found the cause, but without understanding why. What made it work was changing my `start` label to `..start`. There must be some meaning to the double dot? – wkjagt Jan 08 '23 at 15:22
  • @wkjagt See the NASM documentation. – Alexey Frunze Jan 10 '23 at 07:29
2

Using the -obj option will output the assembled file in Object Module Format. Using the org directive is only supported in binary format accoring to the manual .

The reason for this is that the linker should handle relocations for you.

If you want to create free space, maybe the times directive will help you:

times 10   db 0    ; 10 zero bytes
copy
  • 3,301
  • 2
  • 29
  • 36
  • On wikipedia, it says: "There are no file offsets (like a pointer to a symbol table) in the file. Therefore a linker must parse all entries (records) of the object file to get information about it." Is that what your are talking about? – Cole Tobin May 19 '12 at 22:34
  • @ColeJohnson No. That sentence is about creating executable files. You probably want to omit the `-fobj` option if you want to create a bootloader (or see the other response). – copy May 19 '12 at 23:06