The error by me or nasm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tresss
Posts: 21
Joined: Thu Jul 06, 2006 10:52 pm

The error by me or nasm

Post 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]
http://english.writeos.com

My English is not very good
smbogan
Member
Member
Posts: 29
Joined: Tue Nov 21, 2006 3:17 pm

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Jules
Member
Member
Posts: 30
Joined: Mon Jan 08, 2007 3:19 am
Location: UK

Post 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'.
ntfs
Posts: 19
Joined: Tue Dec 12, 2006 9:55 am
Location: Czech Republic Prague

Post by ntfs »

Yes, you are true, include BITS directive to force nasm to produce 32/16 code.
tresss
Posts: 21
Joined: Thu Jul 06, 2006 10:52 pm

Post by tresss »

Thanks.I'm understand.

It's should include "bits 16".
And i use --oformat only when ld now.
http://english.writeos.com

My English is not very good
Post Reply