Unable to jmp kernel in PMODE

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.
Post Reply
sumit
Posts: 1
Joined: Thu Aug 04, 2011 6:05 am

Unable to jmp kernel in PMODE

Post by sumit »

Hello,
Yet another newbie :) , I have written code for bootloader stage1 and stage2. stage1 loads the stage2 successfully. And kernel binary is also ready.
the code for stage2 :

Code: Select all

[BITS 16]
[ORG 0x500]
	
main:
	popf
	popa	

	mov si, a20msg
	call PutStr
        ;; enable the A20 gate 
	jmp Enable_A20Gate

LoadKernel:
	mov si, done
	call PutStr

        ;; load kernel 
	mov si, ldmsg
	call PutStr

	xor ah, ah
	
	mov si, DAP
        mov dl, 0x80
        mov ah, 0x42				;extended read
	int 13H					;bios interrupt 13H
	jc error

	
	mov si, done
	call PutStr

	jmp $


	mov si, pmmsg
	call PutStr

	cli
	push ds

	mov ax,0x0800
	mov ss,ax
	mov sp,0x1000

        ;; load gdt table
	lgdt [gdtinfo]

        ;; enable protected mode
	mov  eax, cr0   	; switch to pmode by
	or al,1         	; set pmode bit
	mov  cr0, eax

	mov  bx, 0x08   	; select descriptor 1
	mov  ds, bx   	; 8h = 1000b

	pop ds

	;; jmp $
        ;; jmp to kernel in memory
	jmp 0x08:0x01000
	

PutStr:	; Procedure label/start
			;;  Set up the registers for the interrupt call
	mov ah,0x0E    	; The function to display a chacter (teletype)
	mov bh,0x00    	; Page number
	mov bl,0x07    	; Normal text attribute
.nextchar       	; Internal label (needed to loop round for the next character)
	lodsb          	; I think of this as LOaD String Block
	or al,al       	; Sets the zero flag if al = 0

	jz .return     	; If the zero flag has been set go to the end of the procedure.
	;;  Zero flag gets set when an instruction returns 0 as the answer.
	int 0x10       	; Run the BIOS video interrupt
	jmp .nextchar 		; Loop back round tothe top
.return         	; Label at the end to jump to when complete
	ret            	; Return to main program

new_line:
	mov ah, 0EH
	mov al,13
	int 10H
	mov ah, 0EH
	mov al,10
	int 10H
	mov dx, 0000H
    
	ret

clearscreen:			

	;; clear the screen
	mov ah, 06
	mov al, 00
	mov bh, 07
	mov ch, 00
	mov cl, 00
	mov dh, 24
	mov dl, 79
	int 10H
	

	mov ah, 0x02
	mov bh, 0x07
	mov dh, 0x00
	mov dl, 0x00
	int 10H
	
	ret

Empty_KeyboardBuffer:
	xor al, al
	in al, 0x64
	test al, 0x02
	jnz Empty_KeyboardBuffer

	ret


Enable_A20Gate:
	cli

	call Empty_KeyboardBuffer

	mov al, 0xD1
	out 0x64, al

	call Empty_KeyboardBuffer

	mov al, 0xDF
	out 0x60, al

	call Empty_KeyboardBuffer

	sti

	jmp LoadKernel


error:
	call new_line
	mov si, errmsg
	call PutStr
	jmp $

	
DAP:
size: 
	db 0x10        		;Size of the disk address packet
res: 
	db 0				;reserved byte
num_of_sec:
	dw 24				;number of sectors to read
offset:
	dw 0x1000			;read into the address specified by segment:offset
segment:
	dw 0
LBA:
	dq 2


;; readchar:
;; 	mov ah, 01
;; 	int 21H
	
	
	number db "PUCSD>",0
	errmsg db "Unable to load kernel",10,0
	a20msg db "Activating A20Gate",10,0
	ldmsg  db "Loading Kernel",10,0
	pmmsg  db "Activating Protected Mode",10,0
	startmsg db "Starting...",10,0
	done db "Done :)",10,0
	

gdtinfo:
	dw gdt_end - gdt - 1	;last byte in table
	dd gdt         	;start of table
	
	gdt 	        dd 0,0  	; entry 0 is always unused
	flatdesc	db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0
gdt_end:


I executed the code on my ACER ASPIRE5738 laptop. the stage1 works fine, it loads the second stage. But in second stage
if I assemble code with
jmp $ ( see jmp to kernel part)
instruction (just to check the code for activating pmode works or no without jmp to kernel) the stage2 code also works fine.
It prints all the message to screen and goes for jmp $ instruction. But if I change jmp $ to jmp 0x08:0x01000
the loading kernel part fails and it shows "Unable to load kernel" message on the screen.

I am not able to understand how come changing jmp instruction causes not to load kernel??. weird isn't it??
please help me!!!!
-- Tough times never lasts, but tough people do.
-- I love deadlines. I like the whooshing sound they make as they fly by. - D. Adams
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Unable to jmp kernel in PMODE

Post by egos »

You can't jump to data segment.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply