Extended Bootloader Stupid Stack Problem

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.
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Extended Bootloader Stupid Stack Problem

Post by yonami »

Hi All.

Can I ask you for help :wink:

I am loading Extanded Bootloader into: 0x7E00. And I am jumping to: 0x0000:0x7E00 for exec my code.

And here is a problem.
What value I should set as stack pointer in Extanded Bootloader code? 0x7E00?

Extanded Bootloader (part)

Code: Select all

[BITS 16] 
[ORG 0x00007E00] << what here?

EBOOT_MAIN:
	cli
	xor bx,bx      ;BX = 0
  	mov ss,bx      ;Stack Segment = 0
  	mov sp,0x00007E00  << and what here?
  	sti            ;Enable Interrupts
	mov si, [ DB_Message_Starting ]
	call EBOOT_PRINTMSG
	jmp $
And here is a Extended Bootloader reading code:

Code: Select all

fReadFloppy:
	mov ah,0x02       ;BIOS Interrupt Function 0x02 (Read Sectors int Memory)
  	mov al,0x01       ;Load 1 sector from floppy
  	mov es,bx         ;Zeroed-out above, sets the ES Segment Register
  	mov bx,0x7E00     ;Set BX offset to our desired load location 
  	mov cx,0x0002     ;Set Cylinder Offset to 0, Set Sector offset to 2
  	xor dx,dx         ;Set Head Offset to 0, Set Drive Number to 0 (First Floppy Disk)
  	int 0x13          ;Execute BIOS Interrupt:
	ret
for mov al,0x01 0x01 is a oryginal value. I have change it to 2, and the same problem. The result of booting is a "$" sign, but should be "OK" string.

I do not know what I am doing wrong :(

Thanks a lot :-)
nirvana111
Posts: 20
Joined: Wed Jun 21, 2006 7:55 am
Location: China

Post by nirvana111 »

hello,yonami, i think you should put all of the code here.

nirvana111
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

OK. Here is a code

Post by yonami »

OK. Here is a code:

boot.asm - MBR Bootloader

Code: Select all

[BITS 16]      
[ORG 0x00007C00]  

BOOT_MAIN:
	cli
	xor bx,bx			;BX = 0
  	mov ss,bx		;Stack Segment = 0
  	mov sp,0x7C00  	;Stack Pointer = 0x7C00
  	sti            			;Enable Interrupts
	call fClrScr
	mov si, [ DB_MSG_BOOTSTART ]
	call fPrint
	call fReadFloppy
	call fExecExtendBoot
	jmp $

fPrint:
	mov ah,0x0E
 	mov bh,0x00
 	mov bl,0x07    			; Normal text attribute
	.fPrintChar:       
 		lodsb
 		or al,al
 		jz .fPrintReturn
 		int 0x10
 		jmp .fPrintChar
	.fPrintReturn:
 		ret

fClrScr:
	mov ax,	3
	int 10h
	ret		
	
fReadFloppy:
	mov ah,0x02	 	 ;Read disk sectors
	mov al,0x01      	 ;Read ONE sector
  	mov ch,0x00      	 ;Track 0
  	mov cl,0x02      	 ;Sector 2
	mov dh,0x00	 	 ;Head 0
	mov dl,0x00	 	 ;Drive 0
  	mov bx,0x7E00     	;Set BX offset to our desired load location 
  	mov es,bx         	;Set Head Offset to 0, Set Drive Number to 0 (First Floppy Disk)
	mov bx, 0x0000;
  	int 0x13          		;Execute BIOS Interrupt:
	ret
	
fReadFloppy2:
	mov ah,0x02      	 ;BIOS Interrupt Function 0x02 (Read Sectors int Memory)
  	mov al,0x02      	 ;Load 1 sector from floppy
  	mov es,bx        	 ;Zeroed-out above, sets the ES Segment Register
  	mov bx,0x7E00    	 ;Set BX offset to our desired load location 
  	mov cx,0x0002    	 ;Set Cylinder Offset to 0, Set Sector offset to 2
  	xor dx,dx        		 ;Set Head Offset to 0, Set Drive Number to 0 (First Floppy Disk)
  	int 0x13         		 ;Execute BIOS Interrupt:
	ret
	
fExecExtendBoot:
	jmp 0x7E00:0x0000
	ret
			
		
DB_MSG_BOOTSTART	db	13, 10, ' Booting MBR... ', 0
DB_MSG_OK				db	'[OK]', 13, 10, 0
DB_MSG_FAILURE		db	'[FAILURE]', 13, 10, 0	
DB_MSG_ERROR			db	13, 10, '   **** [ERROR]: ', 0	
		
times 510-($-$$) db 0   ; Fill the rest with zeros
dw 0xAA55               ; Boot loader signature
fReadFloppy and fReadFloppy2 dosn't works.

Extended Bootloader

Code: Select all

[BITS 16] 
[ORG 0x00007E00]

EBOOT_MAIN:
	cli
	xor bx,bx      		;BX = 0
  	mov ss,bx      		;Stack Segment = 0
  	mov sp,0x7E00  	;Stack Pointer = 0x7C00
  	sti            			;Enable Interrupts
	mov si, [ DB_Message_Starting ]
	call fPrint
	jmp $

fPrint:
 	mov ah,0x0E    ; The function to display a chacter (teletype)
 	mov bh,0x00    ; Page number
 	mov bl,0x07    ; Normal text attribute
	.fPrintNextChar       
 		lodsb
 		or al,al
 		jz .fPrintReturn
 		int 0x10
 		jmp .fPrintNextChar
	.fPrintReturn:
 		ret

fClrScr:
	mov ax,	3
	int 10h
	ret	
	
DB_Message_Starting	db	'OK',13,10,0 
I can't understand how to read floppy, 'cose It is not works anyway :(

Thanks
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

What is ds equal to?
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

chase is right -- you need to set DS (and CS unless you are careful to only use short jumps)

but this is not your only problem:
I am loading Extanded Bootloader into: 0x7E00. And I am jumping to: 0x0000:0x7E00 for exec my code.
no your not:

your loading your second sector to 0000:7E00, and jumping to 7E00:0000 -- i think you wanted either:
jmp 0:7E00
or
jmp 07E0:0
And here is a problem.
What value I should set as stack pointer in Extanded Bootloader code? 0x7E00?
you are setting your stack just below your bootsector, which should be ok -- leave it there as there is no reason to change it -- you dont have to change it for every piece of code -- unless you expect that it might be called from somewhere other than your first sector
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Post by yonami »

What is ds equal to?
Done. DS is equal 0x7E00
your loading your second sector to 0000:7E00, and jumping to 7E00:0000 -- i think you wanted either:
jmp 0:7E00
or
jmp 07E0:0
Here http://www.osdever.net/tutorials/loadin ... ?the_id=86 , is an example. He is using jmp DSvalue:0x0000

Here is code of reading:

Code: Select all

fReadFloppy:
	mov ah,0x02	  ;Read disk sectors
	mov al,0x01       ;Read ONE sector
  	mov ch,0x00       ;Track 0
  	mov cl,0x02       ;Sector 2
	mov dh,0x00	  ;Head 0
	mov dl,0x00	  ;Drive 0
  	mov bx,0x7E00     ;Set BX offset to our desired load location 
  	;mov cx,0x0002     ;Set Cylinder Offset to 0, Set Sector offset to 2
  	mov es,bx         ;Set Head Offset to 0, Set Drive Number to 0 (First Floppy Disk)
	mov bx, 0x0000;
  	int 0x13          ;Execute BIOS Interrupt:
	mov ax, 0x7E00;
	mov ds, ax;
	ret

fExecExtendBoot:
	jmp 0x7E00:0x0000
	ret
I am not using loop for calling 0x13 interupt, 'cose I am using virtual floppy at now.

The result of booting is a "Booting MBR..." and nothink more :roll: Still dosn't works, but maybe problem is with Extended Bootloader, now? I will thinking...

If I will boot it, I think I will put the source for all people as tutorial.
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

yonami wrote:
your loading your second sector to 0000:7E00, and jumping to 7E00:0000 -- i think you wanted either:
jmp 0:7E00
or
jmp 07E0:0
I think it's confusing because in the code you posted first from the code and comments you hav fReadFloppy that loads to 0x7E00:0x0000 and fReadFloppy2 that loads to 0x000:0x7E00.

I think your ORG statements and your segement values are wrong. Where what memory location are you trying to load the sector to? The 512 bytes right after the boot sector?
nirvana111
Posts: 20
Joined: Wed Jun 21, 2006 7:55 am
Location: China

Post by nirvana111 »

it seems no problem, but before using lodsb, you should set ds. for example:
xor ax,ax
push ax
push ax
pop ds
pop cs
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Post by yonami »

Code: Select all

I think your ORG statements and your segement values are wrong. Where what memory location are you trying to load the sector to? The 512 bytes right after the boot sector?
Yes. That is 200h address. But I am using tutorial values at now.

So if I am loading to 0xFE00:0x0000 than should be (in extanded bootloader)

Code: Select all

[BITS 16] 
[ORG 0x7E00]

EBOOT_MAIN:
	cli
	mov bx,0x7E00;
  	mov ss,bx      
  	mov sp,0x0000 
  	sti            ;Enable Interrupts
Eq. -> 0xFE00:0x0000 just as jmp address :x

If not, so I must read tutorials again :twisted:
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

If you want to load to the next 512 bytes of memory then in your reading code you'd need to set es = 0x07e0 and bx = 0x0000. Then your jump would be to 0x0000:7E00 with your current ORG.

You do understand that 0x07e0:0x0000 == 0x0000:0x7e00 and how the ORG statement effects your code right?

EDIT: I mixed up the segement and offset for the jump the first time around.
Last edited by chase on Wed Jul 12, 2006 7:43 pm, edited 1 time in total.
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Post by yonami »

0x07e0:0x0000 == 0x0000:0x7e00
Answer: Not
[...] ORG statement effects your code right?
Answer: Yes
If you want to load to the next 512 bytes of memory then in your reading code you'd need to set es = 0x07e0 and bx = 0x0000. Then your jump would be to 0x07e0:0000 with your current ORG.
That I have
And when I am booting, as a result I see non-keyboard characters - the trees :|

So problem is in Extended Bootloader code :|
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

Fixed an error in my previous post. Are you setting all your data segement registers (ds and es) to 0x0000?
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Post by yonami »

it seems no problem, but before using lodsb, you should set ds. for example:
xor ax,ax
push ax
push ax
pop ds
pop cs
I think you right. Problem is with Printing Messages. ClrScr is working.
[...] Then your jump would be to 0x0000:7E00 with your current ORG
If I jumping to 0x0000:7E00, Bochs returns in loop:

Code: Select all

using of nonexisting segment register
But why printing messages is working in MBR bootloader? Compiler for AA55 signature is setting up DS? What ever, I am going to read more tutorials now. I must understand this. I am so pure in @$$ :twisted:

Thanks
yonami
Posts: 20
Joined: Tue Mar 07, 2006 12:00 am

Post by yonami »

Can you make it run?

I do not know, what is wrong. I check other tutorials, and kernels examples. Tah should works :[
dave
Member
Member
Posts: 42
Joined: Sat Jul 09, 2005 11:00 pm

Jmp Address

Post by dave »

The problem is not your stack. It appears you do not understand segmented addressing. Your jmp 0x7E00:0x0000 is incorrect based on your ORG statment.

In segmented addressing the segment address (0x7E00 according to your jump instruction) is shifted left 4 times ( or multiplied by 16 = 2^4) and the offset is added (0x0000 according to your jump) so the address you
are jumping to is

0x7E00 * 0x10 + 0x0000 = 0x0007 E000

this is not where you have loaded your extended boot code. you loaded your code at 0x0000 7E00 which is not the same address you are jumping too.

0x07E0 * 0x10 + 0x0000 = 0x0000 7E00

JAAman and Chase pointed this problem out earlier.
Post Reply