NASM & bootloader crazy stuff

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.
vl4kn0
Posts: 5
Joined: Wed Feb 10, 2010 1:54 am

NASM & bootloader crazy stuff

Post 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?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM & bootloader crazy stuff

Post 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.
vl4kn0
Posts: 5
Joined: Wed Feb 10, 2010 1:54 am

Re: NASM & bootloader crazy stuff

Post by vl4kn0 »

doesn't work. i run that code using virtualbox, isn't that the problem?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM & bootloader crazy stuff

Post 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".
vl4kn0
Posts: 5
Joined: Wed Feb 10, 2010 1:54 am

Re: NASM & bootloader crazy stuff

Post 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
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: NASM & bootloader crazy stuff

Post 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
Last edited by Combuster on Wed Feb 10, 2010 4:01 pm, edited 1 time in total.
"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
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM & bootloader crazy stuff

Post by qw »

You're using NASM, aren't you? Don't you get "error: symbol `aa55h' undefined"?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM & bootloader crazy stuff

Post by qw »

Combuster wrote:A call/return uses the stack, you should set one up first to prevent issues with that.
#-o That must be it.
vl4kn0
Posts: 5
Joined: Wed Feb 10, 2010 1:54 am

Re: NASM & bootloader crazy stuff

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: NASM & bootloader crazy stuff

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
DednDave
Posts: 18
Joined: Fri Feb 05, 2010 10:40 am
Location: Mesa, Arizona

Re: NASM & bootloader crazy stuff

Post 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
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: NASM & bootloader crazy stuff

Post by Combuster »

Doesn't compile for me. Are you using FASM instead of the NASM mentioned in the subject?
"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 ]
vl4kn0
Posts: 5
Joined: Wed Feb 10, 2010 1:54 am

Re: NASM & bootloader crazy stuff

Post 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

...
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: NASM & bootloader crazy stuff

Post 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
"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
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM & bootloader crazy stuff

Post by qw »

Segmentation keeps confusing people.
Post Reply