Page 1 of 1

The error by me or nasm

Posted: Thu Jan 11, 2007 12:22 am
by tresss
I want write something complied by nasm,gcc and linked by ld.

But i find some error.

I try it by nasm and ld:

Code: Select all

ex.asm:

[section .text]
global _start

_start:
mov ax,0x745a
mov ebx,0xb8000
mov [ebx],ax

jmp $
complied with nasm :

Code: Select all

nasm ex.asm -o ex.bin
complied with nasm and link with ld:

Code: Select all

nasm -f elf ex.asm -o ex.o
ld ex.o -o ex.bin -s -x -e _start -Ttext=0x8000 -oformat=binary -no-omagic
Then I open the two bin by hex:
when just compling (no link) ,

Code: Select all

B8 5A 74 66 BB 00 80 0B 00 67 89 03 EB FE
while link bin:

Code: Select all

66 B8 5A 74 BB 00 80 0B 00 66 89 03 EB FE
I think they should have no difference.but i'm wrong.

Why?
Why they are different?[/code]

Posted: Thu Jan 11, 2007 2:27 am
by smbogan
Logically, those instructions are not the same thing...it looks like the machine code you wrote is using the incorrect operand sizes in the second example, but I could be wrong.

Somehow I'm guessing flat binary output was what you had in mind...but if I'm wrong correct me.

Try changing it to:
nasm -f aout ex.asm -o ex.o
ld ex.o -o ex.bin -oformat=binary

I'm also confused as to why you have all of the command line switches to remove symbols...I'm not sure if it can do the jump $ without them, but maybe it can. I've never before used ELF as an output format from nasm, and it does not work on my system as ld won't take it as an input file. My knowledge of nasm and ld are limited, so I could be wrong...

Also, if this is related to osdev, try reading Bran's kernel tutorial (google it), it might help you get started.

Posted: Thu Jan 11, 2007 2:34 am
by Combuster
what you are missing is a "bits 32" directive. If its not present it causes nasm to guess the operation size, which ends up being different for both targets.

The differences you see are the various prefixes to force 16 or 32 bit code operation.

Posted: Thu Jan 11, 2007 3:26 am
by Jules
Just to clarify...

the '-f elf' directive, among other things, tells NASM to default to 32 bit code.
the '-f bin' (default) directive tells it to default to 16 bit code.

The fact that the changes are '66's is a dead giveaway - '66' is a prefix that tells the CPU 'the next instruction uses a 16 bit register if we're running 32 bit code or a 32 bit register if we're running 16 bit code'.

Posted: Thu Jan 11, 2007 10:22 am
by ntfs
Yes, you are true, include BITS directive to force nasm to produce 32/16 code.

Posted: Thu Jan 11, 2007 7:12 pm
by tresss
Thanks.I'm understand.

It's should include "bits 16".
And i use --oformat only when ld now.