pmode woes
Posted: Sat Jul 05, 2003 11:00 pm
hi
i am trying to learn about protected mode kernel programmming and it seems that i have landed into a problem that i can't rectify.
the source attached below initialises the gdtr and enters protected mode . now after that a search routine is used to search the
whole memory for word 'mary'. and it is not able to detect it.mary is defined in the [section.data] (buf1)
the program is loaded from dos
the program looks big but please do have a look most of is just copied from pm.zip ( http://my.execpc.com/~geezer/os/pm.zip )
believe me that if i could ave solved this problem or even had the slightesthunch on something i would not have posted the big source
thanks in advance
bhaskar sen
[BITS 16] ; Set 16 bit code generation
[ORG 0x0100] ; Set code start address to 100h (COM file)
[SECTION .text] ; Text section containing code
start:
xor ebx,ebx
mov bx,cs ; BX=segment
shl ebx,4 ; BX="linear" address of segment base
mov eax,ebx
mov [gdt2 + 2],ax ; set base address of 32-bit segments
mov [gdt3 + 2],ax
mov [gdt4 + 2],ax ; set base address of 16-bit segments
mov [gdt5 + 2],ax
shr eax,16
mov [gdt2 + 4],al
mov [gdt3 + 4],al
mov [gdt4 + 4],al
mov [gdt5 + 4],al
mov [gdt2 + 7],ah
mov [gdt3 + 7],ah
mov [gdt4 + 7],ah
mov [gdt5 + 7],ah
lea eax,[gdt + ebx] ; EAX=PHYSICAL address of gdt
mov [gdtr + 2],eax
mov ax,0xB800
mov gs,ax ; point gs to video memory
mov word [gs:0],0x641 ;display brown 'a'
mov ah,00h
int 16h ;wait until keypress
cli
o32 lgdt [cs:gdtr]
mov eax,cr0
or al,1
mov cr0,eax
.5: in al, 0x64 ;Enable A20 {4A} {5}
test al, 2
jnz .5
mov al, 0xD1
out 0x64, al
.6: in al, 0x64
test al, 2
jnz .6
mov al, 0xDF
out 0x60, al
jmp dword SYS_CODE_SEL:do_pm ; jumps to do_pm
[BITS 32]
do_pm:
mov ax,SYS_DATA_SEL
mov ds,ax
mov ss,ax
mov ax,LINEAR_SEL
mov es,ax
; questionable PM code here
mov byte [es:dword 0xB8000],'0'
mov byte [es:dword 0xb8001],00000111b
mov byte [es:dword 0x200000],'m'
mov byte [es:dword 0x200001],'a'
mov byte [es:dword 0x200002],'r'
mov byte [es:dword 0x200003],'y'
mov edi,0x0500 ;offset to video memory ie [LINEAR_SEL : 0XB8000 + EDI ]
mov esi,0
mov ax,LINEAR_SEL
mov es,ax
loo1: ;SEARCH ROUTINE
mov al,[es:esi]
cmp al,'m'
jnz tryagain
mov al,[es:esi+1]
cmp al,'a'
jnz tryagain
mov al,[es:esi+2]
cmp al,'r'
jnz tryagain
mov al,[es:esi+3]
cmp al,'y'
jnz tryagain
;PRINT VALUE OF ESI -> WHERE MARY WAS FOUND
print_hex:
mov cx, 8 ; 4 hex digits
mov edx, esi ; load word into dx
print_digit:
rol edx, 4 ; rotate so that lowest 4 bits are used
mov al, dl ; mask off so we have only next nibble
and al, 0xf ; to clear carry flag
add al, '0' ; convert to 0-based digit
cmp al, '9' ; check for overflow
jbe good_digit
add al, 'A' - '0' - 10
good_digit:
mov byte [es:edi],al ; DISPLAY DIGIT
inc edi
mov byte [es:edi+1],00000111b ; ATTRIBUTE FOR CHARACTER DISPLAYED
inc edi
loop print_digit
add edi , 80*2
add esi,3
tryagain :
inc esi
cmp esi,0x800000 ; REPEAT MEMORY SEARCH TO 8MB RAM
jz loopex
jmp loo1
loopex: ; INDICATES COMPLETION OF SEARCH
mov byte [es:dword 0xB8002],'1'
mov byte [es:dword 0xb8003],00000111b
spin:
jmp spin
[BITS 16]
[SECTION .data] ; Initialised variables
buf1 db 'halumary halaluyia maryrose'
gdtr: dw gdt_end - gdt - 1 ; GDT limit
dd gdt ; (GDT base gets set above)
; null descriptor
gdt: dw 0 ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0 ; type
db 0 ; limit 19:16, flags
db 0 ; base 31:24
LINEAR_SEL equ $-gdt
dw 0xFFFF ; limit 0xFFFFF
dw 0 ; base 0
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0
SYS_CODE_SEL equ $-gdt
gdt2: dw 0xFFFF ; limit 0xFFFFF
dw 0 ; (base gets set above)
db 0
db 0x9A ; present, ring 0, code, non-conforming, readable
db 0xCF ; page-granular, 32-bit
db 0
SYS_DATA_SEL equ $-gdt
gdt3: dw 0xFFFF ; limit 0xFFFFF
dw 0 ; (base gets set above)
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0
REAL_CODE_SEL equ $-gdt
gdt4: dw 0xFFFF
dw 0 ; (base gets set above)
db 0
db 0x9A ; present, ring 0, code, non-conforming, readable
db 0 ; byte-granular, 16-bit
db 0
REAL_DATA_SEL equ $-gdt
gdt5: dw 0xFFFF
dw 0 ; (base gets set above)
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0 ; byte-granular, 16-bit
db 0
gdt_end:
i am trying to learn about protected mode kernel programmming and it seems that i have landed into a problem that i can't rectify.
the source attached below initialises the gdtr and enters protected mode . now after that a search routine is used to search the
whole memory for word 'mary'. and it is not able to detect it.mary is defined in the [section.data] (buf1)
the program is loaded from dos
the program looks big but please do have a look most of is just copied from pm.zip ( http://my.execpc.com/~geezer/os/pm.zip )
believe me that if i could ave solved this problem or even had the slightesthunch on something i would not have posted the big source
thanks in advance
bhaskar sen
[BITS 16] ; Set 16 bit code generation
[ORG 0x0100] ; Set code start address to 100h (COM file)
[SECTION .text] ; Text section containing code
start:
xor ebx,ebx
mov bx,cs ; BX=segment
shl ebx,4 ; BX="linear" address of segment base
mov eax,ebx
mov [gdt2 + 2],ax ; set base address of 32-bit segments
mov [gdt3 + 2],ax
mov [gdt4 + 2],ax ; set base address of 16-bit segments
mov [gdt5 + 2],ax
shr eax,16
mov [gdt2 + 4],al
mov [gdt3 + 4],al
mov [gdt4 + 4],al
mov [gdt5 + 4],al
mov [gdt2 + 7],ah
mov [gdt3 + 7],ah
mov [gdt4 + 7],ah
mov [gdt5 + 7],ah
lea eax,[gdt + ebx] ; EAX=PHYSICAL address of gdt
mov [gdtr + 2],eax
mov ax,0xB800
mov gs,ax ; point gs to video memory
mov word [gs:0],0x641 ;display brown 'a'
mov ah,00h
int 16h ;wait until keypress
cli
o32 lgdt [cs:gdtr]
mov eax,cr0
or al,1
mov cr0,eax
.5: in al, 0x64 ;Enable A20 {4A} {5}
test al, 2
jnz .5
mov al, 0xD1
out 0x64, al
.6: in al, 0x64
test al, 2
jnz .6
mov al, 0xDF
out 0x60, al
jmp dword SYS_CODE_SEL:do_pm ; jumps to do_pm
[BITS 32]
do_pm:
mov ax,SYS_DATA_SEL
mov ds,ax
mov ss,ax
mov ax,LINEAR_SEL
mov es,ax
; questionable PM code here
mov byte [es:dword 0xB8000],'0'
mov byte [es:dword 0xb8001],00000111b
mov byte [es:dword 0x200000],'m'
mov byte [es:dword 0x200001],'a'
mov byte [es:dword 0x200002],'r'
mov byte [es:dword 0x200003],'y'
mov edi,0x0500 ;offset to video memory ie [LINEAR_SEL : 0XB8000 + EDI ]
mov esi,0
mov ax,LINEAR_SEL
mov es,ax
loo1: ;SEARCH ROUTINE
mov al,[es:esi]
cmp al,'m'
jnz tryagain
mov al,[es:esi+1]
cmp al,'a'
jnz tryagain
mov al,[es:esi+2]
cmp al,'r'
jnz tryagain
mov al,[es:esi+3]
cmp al,'y'
jnz tryagain
;PRINT VALUE OF ESI -> WHERE MARY WAS FOUND
print_hex:
mov cx, 8 ; 4 hex digits
mov edx, esi ; load word into dx
print_digit:
rol edx, 4 ; rotate so that lowest 4 bits are used
mov al, dl ; mask off so we have only next nibble
and al, 0xf ; to clear carry flag
add al, '0' ; convert to 0-based digit
cmp al, '9' ; check for overflow
jbe good_digit
add al, 'A' - '0' - 10
good_digit:
mov byte [es:edi],al ; DISPLAY DIGIT
inc edi
mov byte [es:edi+1],00000111b ; ATTRIBUTE FOR CHARACTER DISPLAYED
inc edi
loop print_digit
add edi , 80*2
add esi,3
tryagain :
inc esi
cmp esi,0x800000 ; REPEAT MEMORY SEARCH TO 8MB RAM
jz loopex
jmp loo1
loopex: ; INDICATES COMPLETION OF SEARCH
mov byte [es:dword 0xB8002],'1'
mov byte [es:dword 0xb8003],00000111b
spin:
jmp spin
[BITS 16]
[SECTION .data] ; Initialised variables
buf1 db 'halumary halaluyia maryrose'
gdtr: dw gdt_end - gdt - 1 ; GDT limit
dd gdt ; (GDT base gets set above)
; null descriptor
gdt: dw 0 ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0 ; type
db 0 ; limit 19:16, flags
db 0 ; base 31:24
LINEAR_SEL equ $-gdt
dw 0xFFFF ; limit 0xFFFFF
dw 0 ; base 0
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0
SYS_CODE_SEL equ $-gdt
gdt2: dw 0xFFFF ; limit 0xFFFFF
dw 0 ; (base gets set above)
db 0
db 0x9A ; present, ring 0, code, non-conforming, readable
db 0xCF ; page-granular, 32-bit
db 0
SYS_DATA_SEL equ $-gdt
gdt3: dw 0xFFFF ; limit 0xFFFFF
dw 0 ; (base gets set above)
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0
REAL_CODE_SEL equ $-gdt
gdt4: dw 0xFFFF
dw 0 ; (base gets set above)
db 0
db 0x9A ; present, ring 0, code, non-conforming, readable
db 0 ; byte-granular, 16-bit
db 0
REAL_DATA_SEL equ $-gdt
gdt5: dw 0xFFFF
dw 0 ; (base gets set above)
db 0
db 0x92 ; present, ring 0, data, expand-up, writable
db 0 ; byte-granular, 16-bit
db 0
gdt_end: