Page 1 of 2
NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 1:22 pm
by vl4kn0
I have this code in asm:
Code: Select all
bits 16
org 7c00h
mov al,41h
mov ah,0eh
mov bl,07h
mov bh,00h
int 10h
jmp $
times 510 - ($ - $$) db 0
dw aa55h
it works fine, prints 'A'
but if I change it to this
Code: Select all
bits 16
org 7c00h
jmp start
start:
mov al,41h
call PrintChar
jmp $
PrintChar:
mov ah,0eh
mov bl,07h
mov bh,00h
int 10h
ret
times 510 - ($ - $$) db 0
dw aa55h
it prints just cursor not the 'A'. why?
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 1:59 pm
by qw
Probably your code was loaded to 07C0:0000H instead of 0000:7C00H. This makes your ORG directive incorrect.
See topic
[ORG 0x7C00] vs mov ax, 0x07C0 vs jmp 0x07C0:start.
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:09 pm
by vl4kn0
doesn't work. i run that code using virtualbox, isn't that the problem?
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:29 pm
by qw
Since the only differences are a jmp and a call, I still suspect it's an address issue. Try "jmp 0:start" instead of "jmp start".
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:48 pm
by vl4kn0
I tried this
Code: Select all
[bits 16]
[org 0]
jmp 0x07C0:main
main:
;; Reset all segment registers
mov ax,0x07C0
mov ds,ax
mov es,ax
and this
Code: Select all
[bits 16]
[org 0x7C00]
jmp 0:main
main:
;; Reset all segment registers
xor ax,ax
mov ds,ax
mov es,ax
doesn't work neither
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:53 pm
by Combuster
A call/return uses the stack, you should set one up first to prevent issues with that.
Also, do you use any image building program? They might overwrite parts of your bootsector with the information required for a FAT filesystem. Your first code snippet is short enough to fit before that table
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:53 pm
by qw
You're using NASM, aren't you? Don't you get "error: symbol `aa55h' undefined"?
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 3:54 pm
by qw
Combuster wrote:A call/return uses the stack, you should set one up first to prevent issues with that.
That must be it.
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 4:09 pm
by vl4kn0
anything after main label doesn't work if there is jump. if i comment jump it still doesn't work because there is call. i tried to set stack segment and stack pointer but still doesn't work.
Combuster: i use bfi.exe for creating floppy image
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 4:26 pm
by neon
Hello,
Hobbes is right - your original code should have given you an error on your last line. Change dw aa55h to dw 0aa55h or dw 0xaa55. Also you should add a BPB table to the code as well along with a stack being properly set up.
Re: NASM & bootloader crazy stuff
Posted: Wed Feb 10, 2010 7:44 pm
by DednDave
you may try this to zero the unused area (which will probably be 0 anyway)
Code: Select all
bits 16
org 7C00h
db 512 dup(0)
org 7C00h
;put your code here
org 7DFEh
dw 0AA55h
Re: NASM & bootloader crazy stuff
Posted: Thu Feb 11, 2010 4:43 am
by Combuster
Doesn't compile for me. Are you using FASM instead of the NASM mentioned in the subject?
Re: NASM & bootloader crazy stuff
Posted: Thu Feb 11, 2010 6:50 am
by vl4kn0
I know first post is confusing but I wrote that code here on the forum and I didn't realize that aa55h wouldn't work, I use 0xaa55 instead, there wasn't problem.
I have already fixed the issue. Problem was that I hadn't defined BPB table. Here's working program:
Code: Select all
[bits 16]
[org 0]
jmp short CorrectRegisters
nop ;; I need to start bpb table at 0x03 so i need to step one adress
;; Here comes BPB table
welcome db "Welcome",0x0
main:
mov si,welcome
call PrintString
jmp $
;; Set all segments to 0x07C0 and set up stack pointer
CorrectRegisters:
mov ax,0x07C0
mov es,ax
mov ds,ax
mov ss,ax
mov sp,(0x07C0 - 16)
jmp short main
...
Re: NASM & bootloader crazy stuff
Posted: Thu Feb 11, 2010 8:11 am
by Combuster
mov ax,0x07C0
mov ss,ax
mov sp,(0x07C0 - 16)
I doubt this does what you intended - it places the stack at 07C0:07B0 downward = 0x000083B0 - ~0x00007FB0 physical
Re: NASM & bootloader crazy stuff
Posted: Thu Feb 11, 2010 8:52 am
by qw
Segmentation keeps confusing people.