Page 1 of 1

pmode woes

Posted: Sat Jul 05, 2003 11:00 pm
by bhaskar_sen81
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:

RE:pmode woes

Posted: Sat Jul 05, 2003 11:00 pm
by VE3MTM
If I read your code right, then I think this might help

How I handle video memory is that I put an entry in my GDT pointing to the base of the colour VGA text buffer (0xB80000), and then I loaded that offset into the gs register so I could write stuff to video memory like:

mov byte gs:0, 'M'
mov byte gs:2, 'a'
mov byte gs:4, 'r'
mov byte gs:6, 'y'
...etc...

In addition, since the GDT is defined in the boot sector, I don't have to remember the segment offset in the GDT elsewhere, I just use the value of gs

RE:pmode woes

Posted: Mon Jul 07, 2003 11:00 pm
by bhaskar_sen81
hi
the problem is not in accessing the video memory. video memory writes work fine.
the selector is linear big and the offset is the 0xb8000. the real problem is why can't the program detect when it searches(sweeps ) the memory from 0mb to 8mb the word 'mary' which is defined in 2 distinct places in the memory in buf1

by doing that i was checking weather i can really access the whole memory or not. the program output should be like first 0 is output at 0,0 location
then the memory location (given by esi) and the 1 is printed at 0,1

the video output is working fine but not the search. i also tried the problem in real mode but it works there.
please help
thanks in advance
bhaskar sen

RE:pmode woes.....solved

Posted: Tue Jul 08, 2003 11:00 pm
by bhaskar_sen81
hi
it was a silly mistake.

wrong code
       mov edi,0x0500

correct code
       mov edi,0xb8000+0x0500 ;offset to video ie [LINEAR_SEL : 0XB8000 + EDI ]