About offset error using nasm in linux

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
leetow2003
Member
Member
Posts: 70
Joined: Fri Nov 19, 2010 6:54 pm

About offset error using nasm in linux

Post by leetow2003 »

I want to write the file bootsect.s in linux 0.11 using nasm,
Look:

Code: Select all

;bootsect.asm
[BITS 16]
[ORG 0x07C00]

jmp start
                
BOOTSEG  EQU 0x07c0                        
INITSEG  EQU 0x9000                
        
start:
      mov ax,BOOTSEG
      mov ds,ax
      mov ax,INITSEG
      mov es,ax
      mov cx,20
      sub si,si  
      sub di,di
      rep
      movsw        
      nop
      nop 
      jmp  INITSEG:go ;I find the go offset address is error
go:   mov        ax,cs
      mov        ds,ax
      mov        es,ax
      mov        ss,ax
      mov        sp,0xFF00

      nop
      nop
      call DispStr   
      jmp $
DispStr:
      mov ax,BootMessage  ;this BootMessage offset address is error,too
      mov bp,ax
      mov cx,16
      mov ax,01301h
      mov bx,000ch
      mov dl,0
      int 10h
      ret

BootMessage:db"Hello, OS world!"
        
times 510-($-$$) db 0
dw  0xaa55
I compile the codes:nasm bootsect.asm -o bootsect.bin,
and start using bochs.when I debug it,I find the label go and
BootMessage offset address is error,How to solve it?
Thanks.
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:

Re: About offset error using nasm in linux

Post by Combuster »

BootMessage offset address is error
What is it? How was it calculated? What should it be?


Only if you understand the basics, does it make sense to deal with the dozen other errors in there.
"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 ]
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: About offset error using nasm in linux

Post by BMW »

All data read from memory is accessed by an offset from the base address of the data. The base address of the data is equal to DS:ORG. You have not set DS correctly, you should set it to 0x0000. This will give the data base address 0x0000:0x7C00 = 0x7C00 which is correct.

You might also want to make sure your stack segment (SS) is correctly set up.

And there is some pointless code in there which I have removed (see below)

Try this:

Code: Select all

;bootsect.asm
[BITS 16]
[ORG 0x07C00]

start:
      mov ax, 0x0000
      mov ds, ax
      mov es, ax
      mov fs, ax
      mov gs, ax
      mov ss, ax
      mov sp, 0xFF00
      mov si, BootMessage

printloop:
	mov ah, 0x0E
	lodsb
	cmp al, byte 0x00
	je finprint
	int 0x10
	jmp printloop

finprint:
      cli
      hlt

BootMessage db "Hello, bootloader world!", 0x00
       
times 510-($-$$) db 0
dw  0xAA55
Last edited by BMW on Fri Nov 29, 2013 6:08 pm, edited 3 times in total.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: About offset error using nasm in linux

Post by Octocontrabass »

Or, ignore BMW and use [ORG 0] instead. (Sorry, Combuster. I know you wanted leetow2003 to figure it out.)
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: About offset error using nasm in linux

Post by BMW »

Octocontrabass wrote:Or, ignore BMW and use [ORG 0] instead. (Sorry, Combuster. I know you wanted leetow2003 to figure it out.)
Or, ignore Octocontrabass and fix up the rest of the code as well. In his original code he isn't even using the print BIOS interrupt (0x10) correctly so it won't work.

EDIT: I think it makes more sense to use ORG 0x7C00 - it is meant to refer to the origin, which is 0x7C00. It may also help the OP to understand what ORG does.

@leetow2003 I think you have just copied code from one or more poorly written tutorials and expected it to work. The best thing for you to do would be to make sure you have a good understanding of the assembly language and segmentation (which I don't think you do, no offense), then follow some bootloader tutorials or research how bootloaders work. You are doing yourself a disservice by trying to whack together a bootloader from copied code.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: About offset error using nasm in linux

Post by Octocontrabass »

BMW wrote:I think it makes more sense to use ORG 0x7C00 - it is meant to refer to the origin, which is 0x7C00.
And I think it makes more sense to use ORG 0, since the origin is relative to the segment, not the physical address. I think it would be better to just ignore both of us and answer Combuster's questions, though.
Post Reply