Page 1 of 2

Extended Bootloader Stupid Stack Problem

Posted: Wed Jul 12, 2006 4:56 am
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 :-)

Posted: Wed Jul 12, 2006 5:58 am
by nirvana111
hello,yonami, i think you should put all of the code here.

nirvana111

OK. Here is a code

Posted: Wed Jul 12, 2006 7:49 am
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

Posted: Wed Jul 12, 2006 9:22 am
by chase
What is ds equal to?

Posted: Wed Jul 12, 2006 1:40 pm
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

Posted: Wed Jul 12, 2006 4:29 pm
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.

Posted: Wed Jul 12, 2006 5:05 pm
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?

Posted: Wed Jul 12, 2006 6:16 pm
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

Posted: Wed Jul 12, 2006 7:15 pm
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:

Posted: Wed Jul 12, 2006 7:22 pm
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.

Posted: Wed Jul 12, 2006 7:35 pm
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 :|

Posted: Wed Jul 12, 2006 7:47 pm
by chase
Fixed an error in my previous post. Are you setting all your data segement registers (ds and es) to 0x0000?

Posted: Wed Jul 12, 2006 11:21 pm
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

Posted: Fri Jul 14, 2006 8:11 pm
by yonami
Can you make it run?

I do not know, what is wrong. I check other tutorials, and kernels examples. Tah should works :[

Jmp Address

Posted: Fri Jul 14, 2006 10:17 pm
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.