Page 1 of 1
A little help to get started
Posted: Mon Apr 09, 2012 6:43 am
by sd
Hi All,
I have been playing a bit with nasm to know the details of bootloaders and kernels etc. I tried to test a very basic bootloader that loads another code into memory and executes it. The problem is, the variable declared in the code which is loaded by bootloader does not get created. Here's the code:
Code: Select all
%define mem 0x1000
%define sec 2
[bits 16]
[org 0]
jmp 0x07C0:load
load:
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov al, byte [v1]
mov ah, 0x0e
int 0x10
call loados
jmp mem:0x0000
loados: ; Load 1 sector
mov ax, 0x1000
mov es, ax
mov bx, 0x0000
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, sec
mov dh, 0x00
int 0x13
jc error
ret
error:
jmp loados
v1: db 'A'
times 510 - ($ - $$) db 0
dw 0xAA55
; Second code
jmp main
main:
mov ax, 0x1000
mov ds, ax
mov es, ax
cli
mov ax, 0x7000
mov ss, ax
mov sp, ss
sti
mov ah, 0x0E
mov al, 'B'
int 0x10
mov ah, 0x0E
mov al, byte [v2]
int 0x10
jmp $
v2: db 'C'
times 1024 - ($ - $$) db 0
While I test it with qemu I get "AB " printed, but not "ABC". Is it something to do with the DS not set properly? If I'm missing something a pointer to the theory will be a great help!
Thanks in advance.
Re: A little help to get started
Posted: Mon Apr 09, 2012 8:59 am
by Chandra
sd wrote:jmp mem:0x0000
The jump seems to be misplaced. The segment seems to be okay but the offset isn't. It is clear from the code that the second portion doesn't start at offset 0.
Hint: Use relative far jump.
Re: A little help to get started
Posted: Mon Apr 09, 2012 9:06 am
by Rudster816
Your problem is that the program counter is 512 when it reaches the code for the second sector. This means the address for v2 is 512 bytes higher than where it appears in memory.
One way to fix it:
Code: Select all
times 510 - ($ - $$) db 0
dw 0xAA55
SecondSectorBase equ $
; Second code
jmp main
main:
mov ax, 0x1000
mov ds, ax
mov es, ax
cli
mov ax, 0x7000
mov ss, ax
mov sp, ss
sti
mov ah, 0x0E
mov al, 'B'
int 0x10
mov ah, 0x0E
mov al, [v2 - SecondSectorBase]
int 0x10
; BOCHS will warn you about executing HLT when IF = 0, so you'll know when you've reached here
; Doing info registers in the Qemu monitor will tell you HLT=1 aswell
; This will help you know what has been executed and can act as a rudimentry breakpoint.
cli
hlt
v2: db 'C'
times 1024 - ($ - $$) db 0
You'd have to do "var - SecondSectorBase" anytime you accessed a variable though, so its not suited well for a largish code.
Another way to do it at run time is to set your data\extra segments to 0xFE0 rather than 0x1000, which would 'automagically' subtract 512 from all your variables for you. This is a very ugly\confusing hack though.
The proper (IMO) way to do this is to just load the second sector immediately after the first. This has several advantages, namely you don't have to reset the segment registers after jumping to the second sector. The program counter will also be good, and you can easily access code that lies in the first sector.
Complete Example:
Code: Select all
%define sec 2
[bits 16]
[org 0x0000]
jmp 0x7C0:load
load:
mov ax, cs
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
xor ax, ax
mov ss, ax
mov sp, 0x7C00 ; You need a stack before you call the BIOS
mov al, [v1]
mov ah, 0x0E
int 0x10
call loados
jmp main
loados: ; Load 1 sector
mov bx, 0x200 ; 0x7E00. Remember ES is already set
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, sec
mov dh, 0x00
int 0x13
jc error
ret
error:
mov esp, 0xDEADDEAD
cli
hlt
v1: db 'A'
times 510 - ($ - $$) db 0
dw 0xAA55
; Second code
main:
mov ah, 0x0E
mov al, 'B'
int 0x10
mov ah, 0x0E
mov di, v2
mov al, [v2]
int 0x10
cli
hlt
v2: db 'C'
times 1024 - ($ - $$) db 0
Re: A little help to get started
Posted: Mon Apr 09, 2012 9:19 am
by rdos
The best solution probably is to use two different source files, and let both start at offset 0. That would avoid most of the problems. The two files can then be concatenated into a single file if that is required. It is common practise (at least I think it is) to have one 512 byte file for the boot-sector, and another file for the second stage loader.
Re: A little help to get started
Posted: Mon Apr 09, 2012 10:12 am
by xenos
Just one remark regarding Rudster816's post: The "cli; hlt;" sequence certainly is a good breakpoint. However, don't be surprised if you don't get any screen output on QEMU. It simply freezes the screen when it encounters a "cli; hlt;", and any changes that have not made it to the screen refresh yet (even if the printing instructions have been executed before the "cli; hlt;") will not appear on the screen.
Re: A little help to get started
Posted: Mon Apr 09, 2012 10:56 am
by bubach
off topic:
xchg bx,bx works for breakpoint in bochs too.
edit: ebx->bx
Re: A little help to get started
Posted: Mon Apr 09, 2012 11:34 am
by Rudster816
XenOS wrote:Just one remark regarding Rudster816's post: The "cli; hlt;" sequence certainly is a good breakpoint. However, don't be surprised if you don't get any screen output on QEMU. It simply freezes the screen when it encounters a "cli; hlt;", and any changes that have not made it to the screen refresh yet (even if the printing instructions have been executed before the "cli; hlt;") will not appear on the screen.
I've personally never had a problem with Qemu (or any other emulator) in this regard. I quite frequently do a kprintf() than immediately follow with a cli hlt, and I've never run in to any issues.
Re: A little help to get started
Posted: Mon Apr 09, 2012 1:48 pm
by xenos
Rudster816 wrote:I've personally never had a problem with Qemu (or any other emulator) in this regard. I quite frequently do a kprintf() than immediately follow with a cli hlt, and I've never run in to any issues.
Me neither - I just wanted to mention it since this problem is frequently reported here.
Re: A little help to get started
Posted: Mon Apr 09, 2012 3:35 pm
by invalid
XenOS wrote:any changes that have not made it to the screen refresh yet (even if the printing instructions have been executed before the "cli; hlt;") will not appear on the screen.
Bochs doesn't refresh the screen immediately when stepping with debugger, either.
Re: A little help to get started
Posted: Mon Apr 09, 2012 10:27 pm
by sd
Thanks guys! The solutions worked like a charm!
