flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Mistery... Help me please?

Author
Thread Post new topic Reply to topic
A$M



Joined: 29 Feb 2012
Posts: 94
A$M
Please help me! I made a simple code that shows a sentence (which is stored in memory) on the screen in protected mode 32-bit. It runs at boot (0000:7c00). It worked in bochs. But then i did a kernel equivalent, but it did not work. IDT loaded, but it did not work. I do not think the reasons for this error. The code:
Code:
include "..\includes\structures.inc"

use16
org 0x0000
 
start:
cli
 
push cs
pop  ds

call a20.check
cmp        ax, 1
je     a20_already_enabled

call a20.enable

a20_already_enabled:

lgdt [gdt_desc]
lidt [idt_desc]
 
mov eax, cr0
or eax, 1
mov cr0, eax
 
jmp 0x08:pmode

a20:

; a20.check
; AX = A20 gate status
.check:
       pushf
       push    ds
  push    es
  push    di
  push    si

      cli

     xor     ax, ax
      mov     es, ax

  not     ax
  mov     ds, ax

  mov     di, 0x0500
  mov     si, 0x0510

      mov     al, byte[es:di]
 push    ax

      mov     al, byte[ds:si]
 push    ax

      mov     byte[es:di], 0x00
       mov     byte[ds:si], 0xFF

   cmp     byte[es:di], 0xFF

   pop     ax
  mov     byte[ds:si], al

     pop     ax
  mov     byte[es:di], al

     mov     ax, 0
       je      .check_a20_exit

 mov     ax, 1

.check_a20_exit:
   pop     si
  pop     di
  pop     es
  pop     ds
  popf

    ret

.enable:
     cli

     call    .a20wait
    mov     al, 0xAD
    out     0x64, al

        call    .a20wait
    mov     al, 0xD0
    out     0x64, al

        call    .a20wait2
   in      al, 0x60
    push    eax

     call    .a20wait
    mov     al, 0xD1
    out     0x64, al

        call    .a20wait
    pop     eax
 or      al, 2
       out     0x60, al

        call    .a20wait
    mov     al, 0xAE
    out     0x64, al

        call    .a20wait
    ret

.a20wait:
    in      al, 0x64
    test    al, 2
       jnz     .a20wait
    ret

.a20wait2:
   in      al, 0x64
    test    al, 1
       jz      .a20wait
    ret

use32
pmode:
org $ + 0x20000
 
mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov esp, 0x090000

sti
 
call ClrScr32

mov esi, string
mov edi, 0x0B8000
mov ecx, 52
rep movsb
 
hlt
 
ClrScr32:
 pushad
 
 cld
 mov edi, 0xB8000
 mov cx, 80 * 25
 mov ah, 0x0F
 mov al, ' '
 rep stosw
 
 popad
 ret

string db 'H'
       db 0x0F
       db 'E'
       db 0x0F
       db 'L'
       db 0x0F
       db 'L'
       db 0x0F
       db 'O'
       db 0x0F
       db ' '
       db 0x0F
       db 'F'
       db 0x0F
       db 'R'
       db 0x0F
       db 'O'
       db 0x0F
       db 'M'
       db 0x0F
       db ' '
       db 0x0F
       db 'P'
       db 0x0F
       db 'R'
       db 0x0F
       db 'O'
       db 0x0F
       db 'T'
       db 0x0F
       db 'E'
       db 0x0F
       db 'C'
       db 0x0F
       db 'T'
       db 0x0F
       db 'E'
       db 0x0F
       db 'D'
       db 0x0F
       db ' '
       db 0x0F
       db 'M'
       db 0x0F
       db 'O'
       db 0x0F
       db 'D'
       db 0x0F
       db 'E'
       db 0x0F
       db '!'
       db 0x0F

Divide_by_zero_Error:
    mov     eax, 0              ; Division result: 0
        iret

Debug:
      iret

Non_maskable_Interrupt:
     iret

Breakpoint:
 iret

Overflow:
   iret

Bound_Range_Exceeded:
       iret

Invalid_Opcode:

 iret

Device_Not_Available:
       iret

Double_Fault:
       mov word[0xB87B6], "DF"
   iret

Coprocessor_Segment_Overrun:
        iret

Invalid_TSS:
        mov word[0xB87B6], "TS"
   iret

Segment_Not_Present:
        mov word[0xB87B6], "NP"
   iret

Stack_Segment_Fault:
        mov word[0xB87B6], "SS"
   iret

General_Protection_Fault:
   mov word[0xB87B6], "GP"
   iret

Page_Fault:
 mov word[0xB87B6], "PF"
   iret

x87_Floating_Point_Exception:
       iret

Alignment_Check:
    mov word[0xB87B6], "AC"
   iret

Machine_Check:
      iret

SIMD_Floating_Point_Exception:
      iret

Security_Exception:
 iret

Reserved:
   iret

idt:

; INT 0x00 - Divide-by-zero Error
INT_00 idt_entry Divide_by_zero_Error, 8, 10001110b
; INT 0x01 - Debug
INT_01 idt_entry Debug, 8, 10001110b
; INT 0x02 - Non-maskable Interrupt
INT_02 idt_entry Non_maskable_Interrupt, 8, 10001110b
; INT 0x03 - Breakpoint
INT_03 idt_entry Breakpoint, 8, 10001110b
; INT 0x04 - Overflow
INT_04 idt_entry Overflow, 8, 10001110b
; INT 0x05 - Bound Range Exceeded
INT_05 idt_entry Bound_Range_Exceeded, 8, 10001110b
; INT 0x06 - Invalid Opcode
INT_06 idt_entry Invalid_Opcode, 8, 10001110b
; INT 0x07 - Device Not Available
INT_07 idt_entry Device_Not_Available, 8, 10001110b
; INT 0x08 - Double Fault
INT_08 idt_entry Double_Fault, 8, 10001110b
; INT 0x09 - Coprocessor Segment Overrun
INT_09 idt_entry Coprocessor_Segment_Overrun, 8, 10001110b
; INT 0x0A - Invalid TSS
INT_0A idt_entry Invalid_TSS, 8, 10001110b
; INT 0x0B - Segment Not Present
INT_0B idt_entry Segment_Not_Present, 8, 10001110b
; INT 0x0C - Stack-Segment Fault
INT_0C idt_entry Stack_Segment_Fault, 8, 10001110b
; INT 0x0D - General Protection Fault
INT_0D idt_entry General_Protection_Fault, 8, 10001110b
; INT 0x0E - Page Fault
INT_0E idt_entry Page_Fault, 8, 10001110b
; INT 0x0F - Reserved
INT_0F idt_entry Reserved, 8, 11100110b
; INT 0x10 - x87 Floating-Point Exception
INT_10 idt_entry x87_Floating_Point_Exception, 8, 10001110b
; INT 0x11 - Alignment Check
INT_11 idt_entry Alignment_Check, 8, 10001110b
; INT 0x12 - Machine Check
INT_12 idt_entry Machine_Check, 8, 10001110b
; INT 0x13 - SIMD Floating-Point Exception
INT_13 idt_entry SIMD_Floating_Point_Exception, 8, 10001110b
; INT 0x14 - Reserved
INT_14 idt_entry Reserved, 8, 10001110b
; INT 0x15 - Reserved
INT_15 idt_entry Reserved, 8, 10001110b
; INT 0x16 - Reserved
INT_16 idt_entry Reserved, 8, 10001110b
; INT 0x17 - Reserved
INT_17 idt_entry Reserved, 8, 10001110b
; INT 0x18 - Reserved
INT_18 idt_entry Reserved, 8, 10001110b
; INT 0x19 - Reserved
INT_19 idt_entry Reserved, 8, 10001110b
; INT 0x1A - Reserved
INT_1A idt_entry Reserved, 8, 10001110b
; INT 0x1B - Reserved
INT_1B idt_entry Reserved, 8, 10001110b
; INT 0x1C - Reserved
INT_1C idt_entry Reserved, 8, 10001110b
; INT 0x1D - Reserved
INT_1D idt_entry Reserved, 8, 10001110b
; INT 0x1E - Security Exception
INT_1E idt_entry Security_Exception, 8, 10001110b
; INT 0x1F - Reserved
INT_1F idt_entry Reserved, 8, 10001110b

idt_end:

idt_desc:
 dw idt_end - idt - 1
 dd idt

gdt:
 
gdt_null:
 dd 0
 dd 0
 
gdt_code:
 dw 0FFFFH
 dw 0
 db 0
 db 10011010B
 db 11011111B
 db 0
 
gdt_data:
 dw 0FFFFH
 dw 0
 db 0
 db 10010010B
 db 11011111B
 db 0
 
gdt_end:
 
gdt_desc:
 dw gdt_end - gdt - 1
 dd gdt
    
(2000h:0000h)
Post 07 Oct 2012, 20:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17658
Location: In your JS exploiting you and your system
revolution
I don't see where you are moving the code in memory to match the ORG addresses.
Quote:
It runs at boot (0000:7c00).
Code:
org 0x0000    
Code:
org $ + 0x20000    


BTW: We can't assemble your code since you have omitted at least one file.
Post 08 Oct 2012, 04:01
View user's profile Send private message Visit poster's website Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M
You do not understand. The bootloader test runs at 0000:07c00h. This kernel test runs at 2000h:0000h.

'structures.inc' code:
Code:
struc idt_entry offset, selector, atributes
 {
  .low    dw offset and 0xFFFF
        .sel    dw selector
 .zero   db 0
        .atrib  db atributes
        .high   dw offset shr 16
 }    
Post 08 Oct 2012, 18:28
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos
Reprogram PIC/APIC before you enable interrupts. Take into account that some exception handlers receive error code in the stack.
Post 08 Oct 2012, 18:56
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.