News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Real Mode

Started by OneX, December 14, 2011, 11:39:56 AM

Previous topic - Next topic

OneX

Hi,
I want to write some asm to allow me to run code in real mode (for VESA, etc.) but I don't know how to switch to real mode and back to protected mode.
Can anyone give me an example of how to switch to real mode from protected mode with MASM?

bomz

Quoteknow how to switch to real mode and back to protected mode.

any need to switch /

Magnum




;
; ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
; º This file is generated by The Interactive Disassembler (IDA)     º
; º Copyright (c) 2006 by DataRescue sa/nv, <ida@datarescue.com>     º
; º Licensed to: Freeware version     º
; ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
;
; File Name   : C:\masm32\SOURCE\NONAME.com
; Format      : MS-DOS COM-file
; Base Address: 1000h Range: 10100h-10146h Loaded length: 46h

.686p
.mmx
.model tiny

; ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ

; Segment type: Pure code
seg000 segment byte public 'CODE' use16
assume cs:seg000
org 100h
assume es:nothing, ss:nothing, ds:seg000, fs:nothing, gs:nothing

; ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ S U B R O U T I N E ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ


public start
start proc near
mov ax, 12h
int 10h ; - VIDEO - SET VIDEO MODE
; AL = mode
mov dx, 3C8h
mov al, 0
out dx, al
mov dx, 3C9h
mov al, 0
out dx, al
mov al, 0
out dx, al
mov al, 3Fh
out dx, al
xor ax, ax
int 33h ; - MS MOUSE - RESET DRIVER AND READ STATUS
; Return: AX = status
; BX = number of buttons
mov ax, 1
int 33h ; - MS MOUSE - SHOW MOUSE CURSOR
; SeeAlso: AX=0002h, INT 16/AX=FFFEh

loc_10120: ; CODE XREF: start+31j start+43j
mov ah, 1
int 16h ; KEYBOARD - CHECK BUFFER, DO NOT CLEAR
; Return: ZF clear if character in buffer
; AH = scan code, AL = character
; ZF set if no character in buffer
jnz short locret_10145
mov ax, 3
int 33h ; - MS MOUSE - RETURN POSITION AND BUTTON STATUS
; Return: BX = button status, CX = column, DX = row
and bx, 1
cmp bx, 0
jz short loc_10120
mov ax, 2
int 33h ; - MS MOUSE - HIDE MOUSE CURSOR
; SeeAlso: AX=0001h, INT 16/AX=FFFFh
mov ah, 0Ch
mov al, 0Fh
int 10h ; - VIDEO - WRITE DOT ON SCREEN
; AL = color of dot, BH = display page
; CX = column, DX = row
mov ax, 1
int 33h ; - MS MOUSE - SHOW MOUSE CURSOR
; SeeAlso: AX=0002h, INT 16/AX=FFFEh
jmp short loc_10120
; ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

locret_10145: ; CODE XREF: start+24j
retn
start endp

seg000 ends


end start

Have a great day,
                         Andy

bomz

QuoteCSEG segment
assume cs:CSEG
org 100h
Begin:
mov ax,12h
INT 10h
mov dx,3c8h
mov al,0
out dx,al
mov dx,3c9h
mov al,0
out dx,al
mov al,0
out dx,al
mov al,63
out dx,al
xor ax,ax
int 33h
mov ax,1h
int 33h
above:
mov ah, 1h
int 16h
jne exit
mov ax,3h
int 33h
and bx, 01h
cmp bx, 00h
je above
mov ax,2h
int 33h
mov ah, 0ch
mov al, 15
int 10h
mov ax,1h
int 33h
jmp above
exit:
ret
CSEG ends
end Begin

bomz

#4
IDA *sorry, in Russian the same word mean: pump and download, so such smile use in sence download, he is pump a file from Internet

I use this free from TASM

BOMZ, do NOT post binaries that you do not own, Sourcer is commercial software protected by COPYRIGHT.

sinsi

Switching from real to protected mode and back is as simple as toggling bit 0 of cr0.
If you want to use VESA in Windows there is no need to switch modes, Windows will emulate real mode for you (except for 64-bit Windows), up to XP I think (for full screen).
Light travels faster than sound, that's why some people seem bright until you hear them.

bomz


OneX

 :naughty: :tdownNOBODY UNDERSTAND!!! I AM WRITING A DRIVER IN MASM TO SWITCH TO REAL MODE FROM PROTECTED MODE BECAUSE I WANT TO USE BIOS INTERRUPTS(VESA) AND IT IS NOT SIMPLE AS TOGGLING THE PE BITS IN THE CR0 REGISTER!!! I NEED TURN OFF PAGING, CREATE A NEW GDT WITH A 16 BIT DATA AND CODE SEGMENT, LOAD IVT, FAR JUMP TO REAL MODE, RELOAD DS, ES, FS, GS, SS WITH APPROPRIATE REAL MODE VALUES, THAT I DON'T KNOW WHAT VALUES, AND SET THE PE BIT IN THE CR0, TO USE BIOS INTERRUPTS AND I NEED I SOURCE CODE TO DO IT, IN MASM!!!

sinsi

Light travels faster than sound, that's why some people seem bright until you hear them.

donkey

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

OneX

donkey, I read the link but at this link I don't know what I put at DataSelector, DataSegment, RMDATA and StackPointer. I want to know what means these fields.

bomz



Just begin from the very begining. What you want??  slowly and very detailed, so that aborigine from the jungle, who never see computer understand you

When you switch on your computer and bios loaded - it's works in real mode. When Windows load ntldr it switch processor to protected mode (real mode may be  emulated ) you can't back processor to real mode under Windows. If you load DOS - processor steal in Real mode. So if you make driver for DOS or under BIOS no need to switch. as for Windows any need for switch and it's impossible
Something like that

May be you mean virtualization - I don't know is it possible real/protected

donkey

Quote from: OneX on December 16, 2011, 11:27:17 AM
donkey, I read the link but at this link I don't know what I put at DataSelector, DataSegment, RMDATA and StackPointer. I want to know what means these fields.

Hi OneX,

I haven't really tried it but I would assume that the loader would set the location of those for you.

http://msdn.microsoft.com/en-US/library/6hzta43h%28v=VS.80%29.aspx

For the selectors I would think they would be allocated in the GDT/LDT.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Twister


sinsi

If you are already in protected mode then some OS has changed it, more than likely it will not allow any sort of mode switch unless there's an API (like DPMI).
There are also certain privileged instructions, these can only be executed in ring 0, whereas most programs run in ring 3, least privileged.
That's one reason it's called protected mode - the controlling program is protected against things like that.

A Windows program can't do it, a DOS program can, depending on himem/emm386/dpmi. It's not easy either for your own OS.

You show me your code, I'll show you mine...
Light travels faster than sound, that's why some people seem bright until you hear them.