stage2 reboots my system

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.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

stage2 reboots my system

Post by extremecoder »

i have written two small codes for testing / learning stage1 and stage2 loaders ..

here is what the code is

stage1:

Code: Select all

[bits 16]
[org 0x07c00]

	< print sample string >

	mov bx, 0x1000
	mov es, bx
	mov bx, 0x0000

;      read 512 bytes from sector 1 - assuming sector 0 will have stage 1
	mov ah, 0x02
	mov al, 0x01
	mov ch, 0x00
	mov cl, 0x01
	mov dh, 0x00
	mov dl, 0x00
	int 0x13

	JMP 0x10000

TIMES 510 - ($ - $$) DB 0
dw 0XAA55
stage2:

Code: Select all

[bits 16]
[org 0x10000]

	< print sample string >

TIMES 512 - ($ - $$) DB 0
i have used dd to copy the entire bootloader (which is 1024 bytes) into floppy .. i have also used cat boot1.bin boot2.bin > bl.bin for merging both the bin files, as i don't have option to combine the DOS way :(

but when I run the OS, it's only printing the string from stage1 and not printing the string from stage2 or otherwise after printing the string from stage1 it reboots ...
Last edited by quok on Tue Apr 14, 2009 3:12 am, edited 1 time in total.
Reason: Changed to use [code]...[/code] blocks
djmauretto
Member
Member
Posts: 116
Joined: Wed Oct 22, 2008 2:21 am
Location: Roma,Italy

Re: stage2 reboots my system

Post by djmauretto »

try this:

Code: Select all


stage1:
[bits 16]
[org 0x07c00]

xor ax,ax
mov ds,ax
mov ss,ax
or ax,0x7c00
mov sp,ax

< print sample string >

mov cx, 0x1000
mov es, cx
xor bx,bx

; read 512 bytes from sector 1 - assuming sector 0 will have stage 1
mov ax, 0x0201
mov cx,1
xor dx,dx
int 0x13

mov ax,0x1000
mov ds,ax
jmp  0x1000:0000

TIMES 510 - ($ - $$) DB 0
dw 0xAA55


stage2:
[bits 16]
[org 0]

< print sample string >

TIMES 512 - ($ - $$) DB 0
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

extremecoder wrote:stage1:
[bits 16]
[org 0x07c00]

< print sample string >

mov bx, 0x1000
mov es, bx
mov bx, 0x0000

; read 512 bytes from sector 1 - assuming sector 0 will have stage 1
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13

JMP 0x10000
Boy, you are being in RM with 16-bit segments... Replace JUMP 0x10000 with jmp 0x1000:0... and initialize the stack.
If you have seen bad English in my words, tell me what's wrong, please.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

djmauretto, respect :)
If you have seen bad English in my words, tell me what's wrong, please.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: stage2 reboots my system

Post by extremecoder »

now it's continuously printing the stage1 one string :(
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

extremecoder wrote:now it's continuously printing the stage1 one string :(
Show the code, especially < print sample string > :)

Edited: Check on errors after calling int 13h. And maybe needs to use Reset function (0) before reading...
If you have seen bad English in my words, tell me what's wrong, please.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: stage2 reboots my system

Post by extremecoder »

here goes the code :)

Stage1:

Code: Select all

[bits 16]  
[org 0x07C00]   

; ----------------------------------------------------------------   
;			code section STARTS      
; ----------------------------------------------------------------          

start:
	cli

	mov ax, 0x0000    
	mov ds, ax    
	mov ss, ax

	mov ax, 0x7c00
	mov sp, ax

	mov si, rstring
	call printstring
	call newline  

	mov si, stagestring
	call printstring
	call newline  

	mov ax, 0
	mov bx, 0
	mov cx, 0
	mov dx, 0

	mov bx, 0x1000  
	mov es, bx
	mov bx, 0x0000        

reset:
	mov ax, 0
	mov dl, 0  
	int 0x13
	jc reset

readsector:
	
;	mov ah, 0x02 		; service      
;	mov al, 0x03 		; number of sectors to read 
;	mov ch, 0x00     	; cylinder number    
;	mov cl, 0x01 		; sector number    
;	mov dh, 0x00 		; head number   
;	mov dl, 0x00		; drive number   

	mov ax, 0x0201
	mov cx, 1
	xor dx, dx	

	int 0x13  
	jc readsector


	mov ax, 0x1000
	mov ds, ax
	JMP 0x1000:0000  



; ---------------------------------------------------------------  
;			code section ENDS
; ---------------------------------------------------------------   

; ----------------------------------------------------------------
;			core functions STARTS
; ----------------------------------------------------------------
printstring:
next_printstring:   
	lodsb 
	or al, al 

	jz exit_printstring

	call printchar
	jmp next_printstring    

exit_printstring:
	ret

printchar:
	mov ah, 0x0e   
	mov bh, 0x0  
	mov bl, 0x07

	int 0x10
	ret 

newline:
	mov al, 0xa	; 10 - new line   
	call printchar	
	mov al, 0xd	; 13 - line feed   
	call printchar  	
	ret	

; --------------------------------------------------------------------
;			core functions ENDS
; --------------------------------------------------------------------


; ------------------------------------------------------------------  
;			data section STARTS    
; ------------------------------------------------------------------ 

rstring db 'rloader version 0.5. ', 0   
stagestring db 'stage1 . ', 0

;-------------------------------------------------------------------
;			data section ENDS	
;------------------------------------------------------------------- 



TIMES 510 - ($ - $$) db 0    
DW 0xAA55    
Stage2:

Code: Select all

[bits 16]
[org 0]


	mov si, stage2
	call printstring

	JMP $

printstring:
next_printstring:
	lodsb
	or al, al
	jz exit_printstring
	call printchar
	jmp next_printstring
exit_printstring:
	ret

printchar:
	mov ah, 0x0E
	mov bh, 0x00
	mov bl, 0x07

	int 0x10 
	ret

stage2 db "stage 2 loader ! .", 13, 10, 0

TIMES 512 - ($ - $$) db 0
;DW 0x5577  
Last edited by quok on Tue Apr 14, 2009 3:11 am, edited 1 time in total.
Reason: Changed to use [code]...[/code] blocks
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

Reset function can destroy es:bx pointer. Move setting up of this pointer immediately before calling the Read function. When Read function returns an error, you must call Reset function before reading anew.

Edited: And remove unbreakable cycles from your code.
If you have seen bad English in my words, tell me what's wrong, please.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: stage2 reboots my system

Post by extremecoder »

still no use :(
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

:mrgreen: You are reading and running boot sector again and again. Read the sector 0, 0, 2 ;)
If you have seen bad English in my words, tell me what's wrong, please.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: stage2 reboots my system

Post by extremecoder »

ooops ... you are right, because it's working now .

but tell me one thing ... why are we changing the ds to 0x1000 ...

mov ax, 0x1000
mov ds, ax
jmp 0x1000:0000

why can't we directly jump to 0x1000:0000
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

extremecoder wrote:ooops ... you are right, because it's working now .

but tell me one thing ... why are we changing the ds to 0x1000 ...

mov ax, 0x1000
mov ds, ax
jmp 0x1000:0000

why can't we directly jump to 0x1000:0000
It's needs for the printstring function placed in stage2, not for transfer control. You can set up ds register in stage2 boot loader. It's even better.
If you have seen bad English in my words, tell me what's wrong, please.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: stage2 reboots my system

Post by extremecoder »

correct .. I have set ds in stage2 ..

in stage1 we are setting ds to 0x0000 and in stage2 we are setting ds to 0x1000 ... why is that we can't set value 0x0000 in stage2 also ?

once stage1 loads the stage2, in theory stage2 can sit in same place in memory ... so ds of stage2 can also be 0x0000 ... but when I do that in stage2, my stage2 is not working. but when I change ds to 0x1000 in stage2, then it's working ...

am I confused ... ??
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: stage2 reboots my system

Post by egos »

extremecoder wrote:correct .. I have set ds in stage2 ..

in stage1 we are setting ds to 0x0000 and in stage2 we are setting ds to 0x1000 ... why is that we can't set value 0x0000 in stage2 also ?
Ask yourself :)
extremecoder wrote:once stage1 loads the stage2, in theory stage2 can sit in same place in memory ... so ds of stage2 can also be 0x0000 ... but when I do that in stage2, my stage2 is not working. but when I change ds to 0x1000 in stage2, then it's working ...

am I confused ... ??
I'm confused too :) Probably because now stage2 places at 0x10000 ;)

Edited: If you will use my method, you could use zero segment address only. Load stage2 boot loader at 0x8000 linear address, then stage1 and stage2 will placed in same 64k-segment.
Last edited by egos on Tue Apr 14, 2009 5:12 am, edited 1 time in total.
If you have seen bad English in my words, tell me what's wrong, please.
kay10
Member
Member
Posts: 30
Joined: Mon Apr 13, 2009 6:10 am
Location: Oldenburg, Germany

Re: stage2 reboots my system

Post by kay10 »

I hope I explain it right, correct me if it's wrong :wink:

DS has to be 0x1000 because the whole code of the stage2 loader starts there.
Your "printstring" uses the segment in DS and the offset in SI, if DS was 0x0000, it would print something totally different,
because the adress of your stage2 message is 0x1000:stage2 (It was loaded there) and not 0x0000:stage2.

EDIT: egos were faster :mrgreen:
Post Reply