What The HECK is problem in this little source code??

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
LEEJAEBEOM
Posts: 4
Joined: Thu Feb 18, 2010 7:34 am

What The HECK is problem in this little source code??

Post by LEEJAEBEOM »

The code below is source of boot.asm

Code: Select all

[org 0]
	jmp 07C0h:start
	
start:
	mov ax, cs
	mov ds, ax	
	mov es, ax	

	mov ax, 0xB800	
	mov es, ax	
	mov di, 0 
	mov ax, word [msgBack]	
	mov cx, 0x7FF	

paint:
	mov word [es:di], ax	; fill the screen with "orange background and dots"
	add di, 2
	dec cx			
	jnz paint

read:
	mov ax, 0x1000		
	mov es, ax
	mov bx, 0

	mov ah, 2
	mov al, 1
	mov ch, 0
	mov cl, 2
	mov dh, 0
	mov dl, 0				
	int 0x13		
	jc read		

	jmp 0x1000:0000		

msgBack db '*' , 0x67

times 510-($-$$) db 0
dw 0AA55h
And the Code below is source of kernel.asm

Code: Select all

[org 0]
[bits 16]

start:
	mov ax, cs
	mov ds, ax
	xor ax, ax
	mov ss, ax
	
	cli
	
	lgdt[gdtr] ; load GDT
	
	mov eax, cr0
	or eax, 0x00000001
	mov cr0, eax
	
	jmp $+2 ; jumps to the 32 bit mode
	nop
	nop
	
	db 0x66
	db 0x67
	db 0xEA
	dd PM_Start
	dw SysCodeSelector
	
[bits 32]
PM_Start:
	
	mov bx, SysDataSelector
	mov ds, bx
	mov es, bx
	mov fs, bx
	mov gs, bx
	mov ss, bx
	
	xor eax, eax
	mov ax, VideoSelector
	mov es, ax
	mov edi, 80*2*10+2*10
	lea esi, [ds:msgPMode]
	call printf ; print the message (We are in protected MODE)

	jmp $
	
printf:
	push eax
	
printf_loop:

	or al, al
	jz printf_end
	mov al, byte [esi]
	mov byte [es:edi], al
	inc edi
	mov byte [es:edi], 0x06
	inc esi
	inc edi
	jmp printf_loop
	
printf_end:
	pop eax
	ret
	
msgPMode db "We are in Protected MODE", 0
;---------------------------------------
; Global Descriptor Table
;----------------------------------------

gdtr:
	dw gdt_end - gdt - 1
	dd gdt+0x10000
	
gdt:
;Dummy Segment desc
	dw 0
	dw 0
	db 0 
	db 0
	db 0
	db 0
	
; Code seg
SysCodeSelector equ 0x08
	dw 0xffff
	dw 0x0000
	db 0x01
	db 0x9a
	db 0xcf
	db 0x00
	
; Data seg 
SysDataSelector equ 0x10
	dw 0xffff
	dw 0x0000
	db 0x01
	db 0x92
	db 0xcf
	db 0x00
	
; Video Seg
VideoSelector equ 0x18
	dw 0xffff
	dw 0x8000
	db 0x0B
	db 0x92
	db 0x40
	db 0x00
gdt_end:
And I used those commands to create kernel.img

nasm -f bin -o boot.bin boot.asm
nasm -f bin -o kernel.bin kernel.asm
copy boot.bin+kernel.bin kernel.img

The thing that I'm very annoyed is kernel.img is not working on VMware. It just shows up "orange background filled with dots". It looks like boot.asm is working well but not kernel.asm. I cannot check whether this is problem of vmware or the problem of source code itself because i don't have floppy drive in my computer.

Anybody can find the problem from the source code above?

Plz Help MEEEE :cry:
xiphias
Posts: 7
Joined: Mon Jul 23, 2007 5:45 am
Location: Sweden

Re: What The HECK is problem in this little source code??

Post by xiphias »

I'm not sure why you are using an address prefix in the jmp instruction? (direct jmp doesn't reference any memory).
Try using

Code: Select all

jmp SysCodeSelector:PM_Start
for the jmp to the 32 bit code, and let nasm generate the instruction.
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Re: What The HECK is problem in this little source code??

Post by FlashBurn »

You have this:

Code: Select all

   jmp $+2 ; jumps to the 32 bit mode
   nop
   nop
   
   db 0x66
   db 0x67
   db 0xEA
   dd PM_Start
   dw SysCodeSelector
Try this instead:

Code: Select all

db 0x66
db 0xEA
dd PM_Start
dw SysCodeSelector
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: What The HECK is problem in this little source code??

Post by quanganht »

First, I don't really get how would you use these lines
[quote="LEEJAEBEOM"]

Code: Select all

	
	db 0x66
	db 0x67
	db 0xEA
	dd PM_Start
	dw SysCodeSelector
[/quote]

And IMO, after you set cr0, you can just jump imediately to the 32-bit code label. Like this
[code]
   lgdt[gdtr] ; load GDT
   
   mov eax, cr0
   or eax, 0x00000001
   mov cr0, eax
   
   jmp SysCodeSelector:PM_Start  ;<---- jump

[bits 32]
PM_Start:
;whatever 32-bit code you want goes here
"Programmers are tools for converting caffeine into code."
User avatar
quanganht
Member
Member
Posts: 301
Joined: Fri May 16, 2008 7:13 pm
Location: Hanoi, Vietnam

Re: What The HECK is problem in this little source code??

Post by quanganht »

First, I don't really get how would you use these lines
LEEJAEBEOM wrote:

Code: Select all

	
	db 0x66
	db 0x67
	db 0xEA
	dd PM_Start
	dw SysCodeSelector
EDIT: Ok I get it now. You want a far jump. But it just don't have to be this complicated

And IMO, after you set cr0, you can just jump imediately to the 32-bit code label. Like this

Code: Select all

   lgdt[gdtr] ; load GDT
   
   mov eax, cr0
   or eax, 0x00000001
   mov cr0, eax
   
   jmp SysCodeSelector:PM_Start  ;<---- jump

[bits 32]
PM_Start:
;whatever 32-bit code you want goes here
"Programmers are tools for converting caffeine into code."
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: What The HECK is problem in this little source code??

Post by neon »

I cannot check whether this is problem of vmware or the problem of source code itself because i don't have floppy drive in my computer.
Chances are its your code at fault. It sounds like it goes into protected mode fine but fails to display the message correctly. If so then the problem is with printf or how you are using it. Have no way of knowing with the lack of information given.

Try it in Bochs and narrow down whats causing the problem.

*edit: Also whats with the inline machine code? I dont think thats the problem but its so much easier just using a far jmp mnemonic...
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: What The HECK is problem in this little source code??

Post by qw »

This may not be the cause of your problem, but if int 13h/ah=2 fails to read, you'd better reset the drive (int 13h/ah=0) before trying to read again.
LEEJAEBEOM
Posts: 4
Joined: Thu Feb 18, 2010 7:34 am

Re: What The HECK is problem in this little source code??

Post by LEEJAEBEOM »

Thx for all the replies guys :D

But I just found that when I assemble the source code by nasm ver 0.98, it works greatly. However, when I assemble it by nasm ver 2.0.7 (the newest one), it just does not work.

Do u guys know any difference between 0.98 and 2.07 that might effect?
xiphias
Posts: 7
Joined: Mon Jul 23, 2007 5:45 am
Location: Sweden

Re: What The HECK is problem in this little source code??

Post by xiphias »

The problem is your jmp into the 32 bit code. First of all, jmp $+2 will not jmp to the 32 bit code. $ is a label to the instruction itself, and since nasm 2.07 seems to be generating a near jmp with a 16 bit offset you are jumping into the middle of the instruction. (I guess the older version is generating a short jmp with a signed relative offset which is only one byte). You can verify this by adding the word short before the $+2, then it will work as you expect since a short jmp is only two bytes, whereas a near jmp is bigger. The other thing is the 0x67 address prefix you are adding to the far jmp, why is it there? I would recommend you to let nasm do the machine instruction assembling and simply make a far jmp the normal way as I suggested in my first post. Things will probably work better then.

/Mikael
xiphias
Posts: 7
Joined: Mon Jul 23, 2007 5:45 am
Location: Sweden

Re: What The HECK is problem in this little source code??

Post by xiphias »

You should also make sure to set esp to some known value since you are using the stack. The preferred way is to set esp right after ss, or you can use the lss instruction to load ss and esp at the same time.
Post Reply