OSDev.org

The Place to Start for Operating System Developers
It is currently Sat May 11, 2024 7:28 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: The error by me or nasm
PostPosted: Thu Jan 11, 2007 12:22 am 
Offline

Joined: Thu Jul 06, 2006 10:52 pm
Posts: 21
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:
ex.asm:

[section .text]
global _start

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

jmp $


complied with nasm :
Code:
nasm ex.asm -o ex.bin

complied with nasm and link with ld:
Code:
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:
B8 5A 74 66 BB 00 80 0B 00 67 89 03 EB FE

while link bin:
Code:
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 2:27 am 
Offline
Member
Member

Joined: Tue Nov 21, 2006 3:17 pm
Posts: 29
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 2:34 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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 ]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 3:26 am 
Offline
Member
Member

Joined: Mon Jan 08, 2007 3:19 am
Posts: 30
Location: UK
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'.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 10:22 am 
Offline

Joined: Tue Dec 12, 2006 9:55 am
Posts: 19
Location: Czech Republic Prague
Yes, you are true, include BITS directive to force nasm to produce 32/16 code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 7:12 pm 
Offline

Joined: Thu Jul 06, 2006 10:52 pm
Posts: 21
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group