Page 1 of 2

problem in booting at int 13h

Posted: Sat Sep 19, 2009 1:37 am
by phoenix07p
hi
I followed tutorials to write a simple bootloader that writes a string to screen and also a simple kernel that writes a string to screen. Bootloader i tested from usb drive that worked and kernel bin path i gave from grub which was present on a linux partition. then i tried chainloading from grub to my bootloader on usb device which also worked.

Now the problem is i want to link my kernel to my bootloader. From my understanding my bootloader should first load the kernel to desired location in memory and then jump there to begin execution. So I used this command

dd if=bootl.bin of=/dev/sdb1 bs=512 count=1

and

dd if=kernel.bin of=/dev/sdb1 ibs=9141 obs=512 count=1 seek=4

to write my kernel whose size is 9141. This i suppose would write kernel to 2048 or 0x800 location on usb drive. so i figured that would be the begining of sector 5 and sectors to read = 9141/512 = 18 approx. and es:bx 0000:0x800
then in my asm i used this block of code

[BITS 16]
[ORG 0X7C00]

CALL setvideomode
CALL setcolor
MOV SI,EnterString
CALL PrintString

MOV AH,02
MOV AL,18d
MOV CH,0
MOV CL,5d
MOV DH,0
MOV DL,81h
MOV BX,0
MOV ES,BX
MOV BX,0x800
INT 13H
JC sad
jmp 0x800

sad:
CALL PrintCharacter
CALL PrintCharacter
CALL PrintCharacter
MOV SI, ExitString
CALL PrintString
hlt
JMP $

the problem is it prints EnterString but gets stuck at int 13h i tried inserting strings and found that it does not progress ahead of int 13h i.e it does not even reach jmp 0x800 or sad.it just gets stuck at int 13h. What's wrong? are the offsets i gave incorrect?


Thanks

Re: problem in booting at int 13h

Posted: Sat Sep 19, 2009 5:36 am
by Combuster
Start with setting up a stack

Re: problem in booting at int 13h

Posted: Sat Sep 19, 2009 11:45 am
by SamW
I too am having the exact same problem... butI thought i had set up the stack? (or is this just a pointer...?)

boot.asm:

Code: Select all

[BITS 16]
[ORG 0x7C00]

MOV  ESP, 0x105000  ; Set the stack pointer


MOV AL, 65
MOV AH, 0x0E	;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00	;Page no.
MOV BL, 0x07	;Text attribute 0x07 is lightgrey font on black background

INT 0x10	;Call video interrupt


read:

MOV AL, 67
MOV AH, 0x0E	;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00	;Page no.
MOV BL, 0x07	;Text attribute 0x07 is lightgrey font on black background

INT 0x10	;Call video interrupt

	mov bx, 0x2000	; Destination Location
	mov es, bx	; Into bx
	mov bx, 0	; Offset to read
	mov ah, 02	; Read funct.
	mov al, 01	; Read 1 sector
	mov ch,	01	; First track
	mov cl,	02	; Second sector
	mov dh,	01	; First head
	mov dl,	00	; Drive number 0
	int 0x13	; Call

jc read             ; ERROR => Try again


jmp 2000h:0000      ; Jump to the program


TIMES 510 - ($ - $$) db 0
DW 0xAA55
and the file its jumping to is....

Code: Select all

[BITS 16]
[ORG 0X2000]

MOV AL, 69
MOV AH, 0x0E	;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00	;Page no.
MOV BL, 0x07	;Text attribute 0x07 is lightgrey font on black background

INT 0x10	;Call video interrupt

jmp $
The output of this is...

Code: Select all

AC
But the 'E' character never fires...

Thanks in advanced for any help.

Sam

Re: problem in booting at int 13h

Posted: Sat Sep 19, 2009 2:13 pm
by Combuster
SamW wrote:I too am having the exact same problem... butI thought i had set up the stack? (or is this just a pointer...?)
(...)
[BITS 16]
(...)
MOV ESP, 0x105000 ; Set the stack pointer
Ok, we are in Real Mode. Lets ignore the segment registers and the 16-bit addressing... :roll:

Re: problem in booting at int 13h

Posted: Sat Sep 19, 2009 8:51 pm
by phoenix07p
well i still didn't get. stack set up yet its stuck at int13h. what is the problem? are inputs to int13 correct and also will mov dl,81h work on usb?? also in the offsets do the sector numbers and head numbers start from 0 or 1. help plz!!!

Re: problem in booting at int 13h

Posted: Sat Sep 19, 2009 9:03 pm
by neon
AH gives you the status code after the INT call (Status codes) that can be used for further troubleshooting. Please post the error code here if you continue to have problems.

Also, your stack isnt set up correctly. I don't even see where you set your segment registers up at...

Also, please don't use "plz" like that - spell out the words.

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 6:46 am
by qw
Combuster wrote:Ok, we are in Real Mode. Lets ignore the segment registers and the 16-bit addressing...
neon wrote:Also, your stack isnt set up correctly. I don't even see where you set your segment registers up at...
Phoenix07p, you are familiar with real mode segmented addressing, aren't you?

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 9:42 am
by CopperMan
Try to insert this at begining of your code:

Code: Select all

[bits 16]
[org 0x7c00]

     jmp 0:start  ; setup cs:ip
start:
     xor ax, ax    ; setup segment registers
     mov ds, ax
     mov es, ax
     mov ss, ax   ; setup stack
     xor sp, sp

     ; - your code here -
And it would be better if you load your kernel to address 1000:0000 (0x10000) just to ensure that your kernel did'nt overwrites boot loader code

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 10:37 am
by phoenix07p
ok i will try that and will get back shortly. meanwhile just a question why would I want to initialize segment register if i am not even using them. In this case i just needed es segment register so i initialized it to 00.cause apart from the block of code relating to int 13h, the bootloader was working fine as it is. or r they needed for implicit purposes?

and neon i said in my first post that the program is stuck at int 13h i printed above and below the int command and found that program actually hangs at int 13h line. it just prints entering. so i can't even get the error code.

and yes i am familiar with real mode addressing. but a bit loss in memory related to segments.

thanks.

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 11:17 am
by neon
Hello,
phoenix07p wrote:ok i will try that and will get back shortly. meanwhile just a question why would I want to initialize segment register if i am not even using them.
True. BIOS Interrupts do use them however.
phoenix07p wrote:and neon i said in my first post that the program is stuck at int 13h i printed above and below the int command and found that program actually hangs at int 13h line. it just prints entering. so i can't even get the error code.
Okay. I highly suspect that it is not hanging at the int call as you think it is. I suspect it returns an error, however do to your jc instruction, it continues to loop forever thus never getting past it.

Rather then using jc - grab the reason for it failing from AH.
phoenix07p wrote:and yes i am familiar with real mode addressing. but a bit loss in memory related to segments
If you are somewhat confused about how memory is referenced in relation to the segments, then you don't have an understanding of segment:offset addressing.

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 11:55 am
by phoenix07p
Ok that problem taken care of int 13h working fine and sectors are read without error but kernel is not loading. This is the code right now :

[BITS 16]
[ORG 0X7C00]

JMP 0:start

start:
XOR AX,AX
MOV DS,AX
MOV ES,AX
MOV SS,AX
XOR SP,SP

MOV AL,65
CALL setvideomode
CALL setcolor
MOV SI, EnterString
CALL PrintString

MOV AH,02
MOV AL,18d
MOV CH,0
MOV CL,5d
MOV DH,0
MOV DL,81h
MOV BX,0
MOV ES,BX
MOV BX,0X800
INT 13H
JC sad

MOV SI, ConfirmString
CALL PrintString
JMP 0x800

sad:

MOV SI, HelloString
CALL PrintString
hlt
JMP $


setcolor:
MOV AH,0X0B
MOV BH,0X00
MOV BL,0x02
INT 0X10
RET

setvideomode:
MOV AH,0X00
MOV AL,0X12
INT 0X10


MOV AH,0X02
MOV BH,0X00
MOV DH,100
MOV DL,100
RET

PrintCharacter:

MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07
INT 0x10
RET

PrintString:

next_character:
MOV AL, [SI]
INC SI
OR AL, AL
JZ exit_function
CALL PrintCharacter
JMP next_character
exit_function:
RET

HelloString db 'Hello !!! Can't boot',0
ConfirmString db 'jumping to kernel',0
EnterString db 'Entering ......',0
ExitString db 'Exited......',0
TIMES 510 - ($ - $$) db 0
DW 0xAA55

and its not going to 'sad ' label now its printing ConfirmString and jumping to 0x800 but kernel's not getting invoked. the kernel also prints characters on the screen.

like i wrote in my first post i wrote the kernel and bootloader with these:

dd if=bootl.bin of=/dev/sdb1 bs=512 count=1
and
dd if=kernel.bin of=/dev/sdb1 ibs=9141 obs=512 count=1 seek=4

kernel.bin was working fine directly with grub.
what's the problem now??

Thanks

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 12:12 pm
by neon
Hello,

Please use code tags when posting code - it makes it easier to read. Also, it might help if you post your bochs crash log (Only the last 10-20 lines please - not the whole log.)

This is where you knowing how to use the bochs debugger becomes important. If you break right before JMPing to the kernel and single step, it might help answer why it is not working. Also, posting a part (or the entire if its small) kernel might help (I see SamW's kernel, not yours.)

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 12:40 pm
by Love4Boobies
Why do people who don't even know how the CPU works (and of course, assembly) try to write OSes?

Code: Select all

[BITS 16]
[ORG 0X7C00]

JMP 0:start ; WHAT IS THIS!?

start:
XOR AX,AX
MOV DS,AX
MOV ES,AX
MOV SS,AX
XOR SP,SP ; THE STACK GROWS TO THE BOTTOM OF THE MEMORY!!!
Okay, I think I've read enough. I will not look through the rest of your "code".

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 1:03 pm
by CopperMan
Love4Boobies wrote:Why do people who don't even know how the CPU works (and of course, assembly) try to write OSes?

Code: Select all

JMP 0:start ; WHAT IS THIS!?
start:
because some bogus BIOSes jump to 07C0:0000 instead of 0000:7C00.
Love4Boobies wrote:

Code: Select all

XOR SP,SP ; THE STACK GROWS TO THE BOTTOM OF THE MEMORY!!!
Before pushing something on the stack SP gets decreased.

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 1:24 pm
by gravaera
Before pushing something on the stack SP gets decreased.
I think that's the reason why there's something wrong with zeroing off SP :wink: .