an odd phenomena: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
user4

an odd phenomena:pmode

Post by user4 »

Following is a boot program(writed in TASM32) that will enter pmode.
The odd phenomena is that when I enable A20,
the line "mov byte ptr es:2,'@'" will resets CPU,and if I change 'byte'
to 'word',it will OK.
By contraries,If I disable A20, the line "mov word ptr es:2,'@'" will OK
and 'byte' will resets.
who knows why?
 org 7c00h
 jmp start
 start:                      
   mov ax,0              
   mov ss,ax
   mov sp,0600h
   mov es,ax
   mov ds,ax
   mov ax,3
   int 10h    
;    in al,92h  ;|
;    or al,2    ;|enable A20
;    out 92h,al ;|
;================================
   mov bx,cs
   shl ebx,4
   mov eax,ebx
   shr eax,16
   mov word ptr [gdtcs+2],bx
   mov byte ptr [gdtcs+4],al
   mov byte ptr [gdtcs+7],ah
   mov di,900h
   lea si,gdt
   mov cx,24
   cld
   rep movsb
   mov ax,0b800h
   mov es,ax
   push dword 0
   popfd
;=====above from Chris Giese'code
   lgdt qword ptr gdtr
   mov eax,cr0
   or al,1
   mov cr0,eax
   mov byte ptr es:0,'1' ;this works
   db 0eah
   dw do_pm
   dw gdt_cs
 do_pm:
   mov byte ptr es:2,'@'
   jmp $
 gdt:  
   dq 0      ;NULL descriptor
 gdt_cs equ $-gdt
 gdtcs:
   dw 0ffffh ;limit 4G
   dw 0      ;base addr L16
   db 0      ;base addr M8
   db 9ah    ;(cs)code descriptor
   db 0cfh   ;G/D
   db 0      ;base addr H8
 gdt_ds equ $-gdt
   dw 0ffffh
   dw 0
   db 0
   db 92h    ;(ds)data descriptor
   db 0cfh
   db 0
 gdtr:
   dw $-gdt-1
   dd 900h
Chris Giese

Re: an odd phenomena:pmode

Post by Chris Giese »

>  ;=====above from Chris Giese'code

Oh sure, blame me for it :)

>     db 0eah
>     dw do_pm
>     dw gdt_cs

This is the far JMP, where you enter 32-bit pmode. But you didn't tell TASM32 about it.

>   do_pm:
>     mov byte ptr es:2,'@'
>     jmp $

This code is assembled in 16-bit mode to

26C606020040      mov byte [es:0x2],0x40
EBFE              jmp short 0x6

But the CPU is now in 32-bit mode, so it sees these instructions instead:

26C60602          mov byte [es:esi],0x2
0040EB            add [eax-0x15],al
FE                db 0xFE

If I remember right, the 32-bit code must go into a separate USE32 code segment if you use TASM.
user4

Re: an odd phenomena:pmode

Post by user4 »

Thanks for Chris Giese.
I've been changed my code(also using tasm32),but some questions.see below please.
code16 segment use16
 assume cs:code16,ds:code16
 org 7c00h
 boot:
   jmp short start
 start:                      
   mov ax,0              
   mov ss,ax
   mov sp,0600h
   mov es,ax
   mov ds,ax
   mov ax,3
   int 10h    
   in al,92h
   or al,2  
   out 92h,al
   mov di,900h
   lea si,gdt
   mov cx,24
   cld
   rep movsb
   mov ax,0b800h
   mov es,ax
   push dword 0
   popfd
   mov byte ptr es:0,'@'
   lgdt qword ptr gdtr
   mov ax,1
   lmsw ax
   db 0eah
   dw do_pm
   dw gdt_cs
 do_pm:
   push 7d70h ;address of 'jmp $' in code32 segment
   retf       ;but it just resets the CPU! ?
 gdt:  
   dq 0      
 gdt_cs equ $-gdt
   dw 0ffffh
   dw 0      
   db 0      
   db 9ah    
   db 0cfh  
   db 0      
 gdt_ds equ $-gdt
   dw 0ffffh
   dw 0
   db 0
   db 92h    
   db 0cfh
   db 0
 gdtr:
   dw 256
   dd 900h
code16 ends
code32 segment use32
 assume cs:code32,ds:code32
   jmp $
   mov byte ptr es:2,'@'  ;as you said,this line changed to
                          ;mov byte ptr [es:esi],02
                          ;add [bx+si+66],al
                          ;but I've been added 'use32' in here,why?
                          ;and, how can I jmp to code32 from code16?
code32 ends
end boot
Chris Giese

Re: an odd phenomena:pmode

Post by Chris Giese »

Now I remember why I switched from TASM to NASM...
because TASM is a pain in the @$$.

Here is some TASM code I put together; hope it helps:
http://www.execpc.com/~geezer/temp/pm.asm
user4

Re: an odd phenomena:pmode

Post by user4 »

what a wonderful world!
i now truned to NASM,all things goes well,
and anyway,very thanks to Chris Giese.
before using nasm,i tried to change the codes about 'segment',according to your pm.asm,
but still failed.
Post Reply