Page 1 of 1

About offset error using nasm in linux

Posted: Fri Nov 29, 2013 4:37 am
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.

Re: About offset error using nasm in linux

Posted: Fri Nov 29, 2013 5:12 am
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.

Re: About offset error using nasm in linux

Posted: Fri Nov 29, 2013 5:16 pm
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

Re: About offset error using nasm in linux

Posted: Fri Nov 29, 2013 5:47 pm
by Octocontrabass
Or, ignore BMW and use [ORG 0] instead. (Sorry, Combuster. I know you wanted leetow2003 to figure it out.)

Re: About offset error using nasm in linux

Posted: Fri Nov 29, 2013 5:56 pm
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.

Re: About offset error using nasm in linux

Posted: Fri Nov 29, 2013 8:17 pm
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.