flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Entering long mode - simple examples

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
Mac2004



Joined: 15 Dec 2003
Posts: 314
Mac2004 28 Jun 2007, 15:54
Quote:
Where's the instruction that actually prints the text? I cannot get it to work under QEMU. It hangs where it is (nothing is printed).


The code just places textmode characters and their color values on screen. It's like putting the letters directly on screen. This is the fastest way of accesing display memory. Smile

Your code seem to be valid. There might be a bug in QEMU?

regards,
Mac2004
Post 28 Jun 2007, 15:54
View user's profile Send private message Reply with quote
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla 09 Jul 2007, 11:26
In the code below, why are the offsets multiplied by 16 and not 8? Aren't the address locations for the IRQ handlers supposed to be 64-bit (8 byte) values?

Code:
MK_IGATES: ;MAKE INTERRUPT GATES (Gates for other interrupts)
        MOV     ESI,IGATE
        MOVSQ
        MOVSQ
        LOOP    MK_IGATES
        MOV     WORD [0x80*16],CLOCK    ;Set IRQ 0 Handler
        MOV     WORD [0x81*16],KEYBOARD ;Set IRQ 1 Handler
        LIDT    [IDTR]                  ;Load IDT Register
        STI                             ;Now we may enable the interrupts    
Post 09 Jul 2007, 11:26
View user's profile Send private message Reply with quote
Dr. Mario



Joined: 17 Aug 2007
Posts: 8
Dr. Mario 17 Nov 2007, 17:24
I have tried running it in MS-DOS 7.1 - guess what?
It just freezes. I also have tried it on Windows ME disk - froze like a popsicile.

Anything?

_________________
Mama-mia, tha' the Athlon 64, Peach!
Post 17 Nov 2007, 17:24
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 17 Nov 2007, 18:59
didn't you happen to try it inside NTVDM emulator (under windows) ?
Post 17 Nov 2007, 18:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 24 Jan 2009, 18:49
Code:
GDTR:                                   ; Global Descriptors Table Register
  dw 4*8-1                              ; limit of GDT (size minus one)
  dq GDT                                ; linear address of GDT
    

Is it possible that the "dq" is unneeded? Since the LGDT instruction is executed in real-mode with operand size override, shouldn't the processor just load up to 6 bytes instead of 10?
Post 24 Jan 2009, 18:49
View user's profile Send private message Reply with quote
jim3001



Joined: 25 Jul 2013
Posts: 1
jim3001 14 Aug 2013, 10:47
I want to know if accessing memory over 4GB(Ex. access the physical memory address 0x100001000 ) is ok in long mode ?

Ex.
mov eax, 5A5A5A5Ah
mov [100001000h],eax

Is it ok ?
Post 14 Aug 2013, 10:47
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Aug 2013, 16:37
jim3001,

Why not? 0xA3 opcode in 64-bit mode uses 64-bit moffs32 (immediate absolute address, though 32 suffix may be misleading). When in doubt, RTFM (both manuals explicitly state that fact).

If you want to force 64-bit immediate address, use mov [qword addr], eax (otherwise fasm may choose shorter rip-relative encoding for that instruction).

Gravedigging? Wink
Post 14 Aug 2013, 16:37
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 15 Aug 2013, 05:10
Hi jim3001
before writing somewhere under long mode ensure that the memory is present in translation tables (whether it is mapped, else you raise pagefault exception). You can't write into RAM directly under long mode, you must create entries in translation tables and then write into virtual memory.
Post 15 Aug 2013, 05:10
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
spandexyfronts



Joined: 15 Sep 2013
Posts: 6
spandexyfronts 07 Oct 2013, 03:44
Many thanks for such a clear example.

I appended your code to some mbr code, compiled, burnt to usb drive and the whole lot worked first time!
Post 07 Oct 2013, 03:44
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 23 Oct 2013, 14:59
Adam Kachwalla wrote:
Hang on a sec... Where's the instruction that actually prints the text? I cannot get it to work under QEMU. It hangs where it is (nothing is printed).

Are you using QEMU x86-64?

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 23 Oct 2013, 14:59
View user's profile Send private message Reply with quote
c.j.gowett



Joined: 31 Dec 2013
Posts: 12
c.j.gowett 16 Jan 2014, 21:03
If you are in 32-bit mode or 64-bit mode, the memory range for video memory is at 0xC0000000 and goes to 0xCFFFFFFF, at least for my video card on my laptop and main computer. If you want to know ALL video ranges for your computer, go to device manager in Windows, go to your video card and right-click on it for properties, then go to the resources tab. It will list all the memory ranges for your video card. All you have to do is write to the video memory (fill the screen) with:

Code:
; 32-bit mode
; when in interrupt mode (16-bit):
; mov ax,4F02h
; mov bx,105h
; int 10h
; this video mode is 1024x768x256
; graphics mode

mov ebx,0xC0000000
fill:
    mov byte [ebx],42    ;color orange
    inc ebx
    cmp ebx,0xC00C0000
    jge eFill
    jmp fill
eFill:
    ; done
    


Code:
; 64-bit mode
; when in interrupt mode (16-bit):
; mov ax,4F02h
; mov bx,105h
; int 10h
; this video mode is 1024x768x256
; graphics mode

mov rbx,0x00000000C0000000
fill:
    mov byte [rbx],42    ;color orange
    inc rbx
    cmp rbx,0x00000000C00C0000
    jge eFill
    jmp fill
eFill:
    ; done
    



EDIT:
This is aside from the 0xA0000 and 0xB8000 video memory ranges.
i also fixed little mistakes I had in the sample code above.


Last edited by c.j.gowett on 16 Jan 2014, 22:43; edited 4 times in total
Post 16 Jan 2014, 21:03
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 16 Jan 2014, 21:20
The VBE get info function 0x4F00 provides the VRAM size in 64k blocks (up to 4GB) in the info block structure and the get mode info function 0x4F01 will give you the base pointer in the mode info block structure. This is useful because I've seen it at 0xC0..0, 0xD0..0 and 0xE0..0 and knowing the size for long mode page mapping can be handy.
Post 16 Jan 2014, 21:20
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 17 Jan 2014, 08:15
CJ: The memory ranges of devices such as video cards are variable. they can be changed by the OS (and typically change when you change hardware) so don't trust Windows Device Manager data in your OS.
Post 17 Jan 2014, 08:15
View user's profile Send private message Visit poster's website Reply with quote
c.j.gowett



Joined: 31 Dec 2013
Posts: 12
c.j.gowett 22 Jan 2014, 16:28
I had a feeling that was the case, because I was wondering what the addresses would be if I had less than 4 GB of memory.
Post 22 Jan 2014, 16:28
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 19 Mar 2018, 09:51
Sorry for necropost, but when i test this code to enter on 64 bits mode on floppy.img on virtualbox, i have crash. I have just added 0xAA55 signature for boot on device:

Code:
ORG     1600h

        USE16

        cli                             ; disable the interrupts, just in
                                        ; case they are not disabled yet

        lgdt    [cs:GDTR]               ; load GDT register

        mov     eax,cr0                 ; switch to protected mode
        or      al,1
        mov     cr0,eax

        jmp     CODE_SELECTOR:pm_start


NULL_SELECTOR = 0
DATA_SELECTOR = 1 shl 3                 ; flat data selector (ring 0)
CODE_SELECTOR = 2 shl 3                 ; 32-bit code selector (ring 0)
LONG_SELECTOR = 3 shl 3                 ; 64-bit code selector (ring 0)

GDTR:                                   ; Global Descriptors Table Register
  dw 4*8-1                              ; limit of GDT (size minus one)
  dq GDT                                ; linear address of GDT

GDT rw 4                                ; null desciptor
    dw 0FFFFh,0,9200h,08Fh              ; flat data desciptor
    dw 0FFFFh,0,9A00h,0CFh              ; 32-bit code desciptor
    dw 0FFFFh,0,9A00h,0AFh              ; 64-bit code desciptor

        USE32

pm_start:

        mov     eax,DATA_SELECTOR       ; load 4 GB data descriptor
        mov     ds,ax                   ; to all data segment registers
        mov     es,ax
        mov     fs,ax
        mov     gs,ax
        mov     ss,ax

        mov     eax,cr4
        or      eax,1 shl 5
        mov     cr4,eax                 ; enable physical-address extensions

        mov     edi,70000h
        mov     ecx,4000h shr 2
        xor     eax,eax
        rep     stosd                   ; clear the page tables

        mov     dword [70000h],71000h + 111b ; first PDP table
        mov     dword [71000h],72000h + 111b ; first page directory
        mov     dword [72000h],73000h + 111b ; first page table

        mov     edi,73000h              ; address of first page table
        mov     eax,0 + 111b
        mov     ecx,256                 ; number of pages to map (1 MB)
  make_page_entries:
        stosd
        add     edi,4
        add     eax,1000h
        loop    make_page_entries

        mov     eax,70000h
        mov     cr3,eax                 ; load page-map level-4 base

        mov     ecx,0C0000080h          ; EFER MSR
        rdmsr
        or      eax,1 shl 8             ; enable long mode
        wrmsr

        mov     eax,cr0
        or      eax,1 shl 31
        mov     cr0,eax                 ; enable paging

        jmp     LONG_SELECTOR:long_start

        USE64

long_start:

        mov     rax,'L O N G '
        mov     [0B8000h],rax

        jmp     long_start

db 510-($-$$) dup 0x90
dw 0xAA55    


Why ?
Post 19 Mar 2018, 09:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 19 Mar 2018, 10:00
That code wasn't intended to be a boot sector.

You might need to use org 0x7c00 or similar.
Post 19 Mar 2018, 10:00
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 19 Mar 2018, 10:26
Okay, thanks, it's work Cool

I have little question, why is it necessary to do
Code:
mov eax,cr0    
before do
Code:
or al,1
mov cr0,eax    
?

Just do that is not sufficient, whatever the value of cr0 ?
Code:
or al,1
mov cr0,eax    


Last edited by Fulgurance on 19 Mar 2018, 10:30; edited 1 time in total
Post 19 Mar 2018, 10:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 19 Mar 2018, 10:29
If you only have or al,1 the other bits in eax are not properly defined. They will have whatever was there before.
Post 19 Mar 2018, 10:29
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 19 Mar 2018, 10:29
Fulgurance wrote:
Okay, thanks, it's work Cool

I have little question, why is it necessary to do
Code:
mov eax,cr0    
before do
Code:
or al,1
mov cr0,eax    
?

Just do that is not sufficient ?
Code:
or al,1
mov cr0,eax    

Think about register sizes and initialization.
Post 19 Mar 2018, 10:29
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.