flat assembler
Message board for the users of flat assembler.

Index > Main > pmode code

Author
Thread Post new topic Reply to topic
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
I put there few tracings. When i comment everything bellow trace2, it works fine,
i see "012" in the left top corner, but when compile whole code(which is bellow trace2),
i get a reboot. Can anybody help me ?
Code:
use16
_start:
        mov     ax,0x9000               
        mov     ss,ax
        mov     sp,0xffff               
    
        mov     ax,cs
        mov     ds,ax
        in      al,0x92         
        test    al,2                    ; just for case
        jnz     no92
        or      al,2
        out     0x92, al                ; enable A20
no92:

        xor     eax,eax
        mov     es,ax
        mov     [es:0xb8000], byte '0'  ; nothing else but tracing 0 (write 0 in l.t corner)
        
        xor     eax,eax
        mov     ax, cs
        shl     eax, 4
        add     ax, pm_entry
        mov     dword [PM], eax 
        
        mov     ax,cs
        shl     eax,4
        add     ax, GDT
        mov     dword [GDTR+2], eax
        lgdt    fword ptr GDTR
        
        mov     [es:0xb8002], byte '1'  ; trace 1 (write 1 in left top corner)
        cli                             ; forbit interrupts
        in      al,0x70                 ; forbit nonmaskable interrupts
        or      al,0x80
        out     0x70,al
        mov     [es:0xb8004], byte '2'  ; trace 2 (write 2 in left top corner)

        mov     eax,cr0
        or      eax,1
        mov     cr0,eax                 ; BOOM!!!!!!!   
                
        db      0x66
        db      0xEA            
PM      dd      ?                       ; far jump to pm_entry
        dw      00001000b       


GDT:    times 8 db 0                    ; NULL descriptor
        code_des db 0xff, 0xff, 0x00, 0x00, 0x00, 10011010b, 11001111b, 0x00 
        data_des db 0xff, 0xff, 0x00, 0x00, 0x00, 10010010b, 11001111b, 0x00 
        gdt_size equ $-GDT

GDTR    dw      gdt_size - 1            ; /______ This will be loaded to gdtr
        dd      ?                       ; \ 

org 0x1000
use32
pm_entry:    
        
        mov     [es:0xb8006], byte '3'  ;trace 3 
        mov     ax,00010000b
        mov     ds,ax
        mov     es,ax

        mov     [es:0xb8008], byte '4'  ; trace 4
        
;********** Enable paging ************************      
        mov     edi,0x00100000          ; Page table entry at 1Mb
        mov     eax,0x00101007   
        stosd                           
        mov     ecx,1023
        rep     stosd                   ; fill all PDE with 0x00101007  
                                        ; edi == 0x00101000    1Mb+4096
        mov     eax,0x00000007;Page Descriptor Table located 1MB + 4096 == 0x00101000
        mov     ecx,1024
L_1:
        stosd                        ; fill PDT with 0x00001007, 0x00002007,..., 0x01024007
        add     eax,0x00001000
        loop    L_1

        mov     eax,0x00100000          ; cr3 == 1Mb (PDE adress)
        mov     cr3,eax

        mov     eax,cr0                   
        or      eax,0x80000000
        mov     cr0,eax                 ; Enable paging 
    

_________________
Sorry for my bad English Sad
Post 09 Apr 2005, 22:07
View user's profile Send private message Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
forgot to write,
boot loader loads this code to memory, then jumps on.

_________________
Sorry for my bad English Sad
Post 09 Apr 2005, 22:09
View user's profile Send private message Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius
I don't know if that will help, but try to use or al,1 instead of or eax,1.

_________________
Better to rule in hell, than to be a slave in heaven...
Post 10 Apr 2005, 10:38
View user's profile Send private message Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
did'nt help.
I dont know much about how to read from disk,
maby problem in bootloader?
Code:
KERNEL_START equ 1; Disk block where kernel starts
KERNEL_SIZE equ 1; Kernel size in disk blocks
KERNEL_SEGMENT equ 10000h; Segment where kernel will be loaded

mov ax, 200h + KERNEL_SIZE
push word KERNEL_SEGMENT
pop es
xor bx, bx
mov cx, KERNEL_START + 1
mov dx, 0
int 13h
jnc ok
jmp $
ok:

jmp KERNEL_SEGMENT:0

times 510 - ($ - $$) db 0
db 55h
db 0aah
    

_________________
Sorry for my bad English Sad
Post 10 Apr 2005, 10:56
View user's profile Send private message Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen
Giedrius wrote:
I don't know if that will help, but try to use or al,1 instead of or eax,1.

Those two instructions are semanticaly identical - you always set the least significant bit.

_________________
x86asm.net
Post 10 Apr 2005, 12:50
View user's profile Send private message Visit poster's website Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
GOT it!!
i put org 0x07c00 on top of code. But iam stil a bit confused witch this "org" directive,
can anybody tell me more about it

_________________
Sorry for my bad English Sad
Post 10 Apr 2005, 14:52
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
BTW, you can do it without manual opcodes:
Code:
        jmp     pword 00001000b:0
        label   PM dword at $-6    
Post 10 Apr 2005, 15:04
View user's profile Send private message Visit poster's website Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
X-0
What is this all about?

_________________
Sorry for my bad English Sad
Post 10 Apr 2005, 19:09
View user's profile Send private message Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu
ORG directive tells the assembler where to consider that the code will be loaded and executed aka at what addres will the code normaly run?

ORG 0x7C00 simply means that the code is expected to be loaded and run at address 7C00 hexa.

For a boot code the BIOS will get it there and run it
But that is another story: how does it get there? and who starts it's execution?

It makes sense mainly for binary output. You do have to tell the assembler because otherwise how will it know the value of your data labels?

Privalov decided that FASM will not move the location inside the output file acordingly. So as a consequence the ORG directive only affects the $ assembler location counter but not the position inside the output code.

Luckyly this behaviour is exactly what is expected/needed for OS boot code.

So your output code will still have only 512bytes and will not be filled by 07C00 x zeroes at start Very Happy

FYI TASM on MASM move the file pointer.
Post 10 Apr 2005, 21:45
View user's profile Send private message Visit poster's website Reply with quote
othername



Joined: 08 Apr 2005
Posts: 26
Location: Lithuania
othername
now it's clear. thanks for help

_________________
Sorry for my bad English Sad
Post 11 Apr 2005, 16:25
View user's profile Send private message Reply with quote
PopeInnocent



Joined: 01 Jan 2004
Posts: 18
Location: USA
PopeInnocent
Quote:
mov sp,0xffff


I might be drifting off-topic here, but since this is the general assembly-stuff forum, we should point out that it's a very bad idea to store the value 0xffff in SP. In 16-bit code, SP should be a multiple of 2; in 32-bit code, ESP should be a multiple of 4. An odd value in ESP means that the processor will have to split memory reads and write when a push or pop crosses a cache line boundary: a major performance hit.

The following line would work well:

mov sp,0xfffc

Smile
Post 13 Apr 2005, 00:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.