Interrupts do not work in real mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

Interrupts do not work in real mode

Post by gabemaiberger »

Hello, I cannot use interrupts in real mode after switching from protected mode :cry:. I followed the instructions at Real Mode but I fear I have overwritten the bios in memory. Here is the code for switching to real mode:

Code: Select all

realmode:
mov ebx, msgRealMode
call DisplayMessage
mov ebx, msgNWLN
call DisplayMessage
cli
jmp 0x28:switchrealmode
realidtr:
dw 0x3FF
dq 0

BITS 16

switchrealmode:
mov eax, 0x30
mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
mov ss, eax
lidt [realidtr]
mov eax, cr0
and eax, 0
mov cr0, eax
jmp 0:gorealmode
gorealmode:
mov ax, 0
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov sp, 0xFFFF
sti
ret
Also, here is my gdt:

Code: Select all

NULL_DESC:
dd 0
dd 0

CODE_DESC:
dw 0xFFFF
dw 0
db 0
db 10011010b
db 11001111b
db 0

DATA_DESC:
dw 0xFFFF
dw 0
db 0
db 10010010b
db 11001111b
db 0

USERCODE_DESC:
dw 0xFFFF
dw 0
db 0
db 11111010b
db 11001111b
db 0

USERDATA_DESC:
dw 0xFFFF
dw 0
db 0
db 11110010b
db 11001111b
db 0

SIXTEENCODE_DESC:
dw 0xFFFF
dw 0
db 0
db 10011010b
db 00001111b
db 0

SIXTEENDATA_DESC:
dw 0xFFFF
dw 0
db 0
db 10010010b
db 00001111b
db 0

gdtend:

gdtr:
Limit dw gdtend-NULL_DESC-1
Base dd NULL_DESC

Code: Select all

var myCat="marshmallow"
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Interrupts do not work in real mode

Post by AJ »

Hi,

Have you reprogrammed the PIC's for protected mode and not changed them back? Have you got a null pointer somewhere that is causing the real mode IVT to be overwritten?

Cheers,
Adam
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

Re: Interrupts do not work in real mode

Post by gabemaiberger »

No, I have not programmed the PIC's. Is my code to switch from pm to rm okay?

Code: Select all

var myCat="marshmallow"
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Interrupts do not work in real mode

Post by Mikemk »

It's best not to rely on real mode. The preferable method of interacting with hardware in pmode (in most cases) is direct hardware programming via registers. In some cases, such as for VBE, v8086 mode can be used, but those should be minimized.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Interrupts do not work in real mode

Post by Gigasoft »

Try keeping SP even. You should also make sure that you haven't overwritten the IVT, BDA or EBDA.
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: Interrupts do not work in real mode

Post by freecrac »

gabemaiberger wrote:

Code: Select all

realmode:
BITS 16

switchrealmode:
mov eax, 0x30
If we are using the directive for 16 bit code, then all followling instructions use a operandsize and/or addressize prefix for to to access 32 Bit register/operands/addresses.
Example: 66 B8 30 00 00 00 "mov eax, 0x30"

Code: Select all

mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
mov ss, eax
But segmentregister have only a size of 16 bit, so it is recommended to use a 16 bit register for to move a 16 bit address to our segmentregisters like your instructions below in you listing.
Note: This is only a cosmetik thing and it does not change any byte of our code, so it give the same bytes "8E D8" if we use "mov ds,ax", or if we use a "mov ds,eax" .

Code: Select all

mov eax, cr0
and eax, 0
mov cr0, eax

Code: Select all

mov eax, cr0
and eax, not 1
mov cr0, eax
Dirk
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

Re: Interrupts do not work in real mode

Post by gabemaiberger »

Hello again, nasm is giving me an error for the code

Code: Select all

and eax, not 1
and that error is:

Code: Select all

./Code/System/kernel.asm:181: error: comma, colon or end of line expected
where line 181 is:

Code: Select all

and eax, not 1
.

Code: Select all

var myCat="marshmallow"
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Interrupts do not work in real mode

Post by Brendan »

Hi,
gabemaiberger wrote:Hello again, nasm is giving me an error for the code

Code: Select all

and eax, not 1
Try:

Code: Select all

and eax, ~1
Or (to reduce code size):

Code: Select all

and al, ~1

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: Interrupts do not work in real mode

Post by freecrac »

gabemaiberger wrote:Hello again, nasm is giving me an error for the code

Code: Select all

and eax, not 1
and that error is:

Code: Select all

./Code/System/kernel.asm:181: error: comma, colon or end of line expected
where line 181 is:

Code: Select all

and eax, not 1
.
Ups, i use this code with MASM and assemble it without an error. Good to know that NASM do not like it.

Dirk
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Interrupts do not work in real mode

Post by Mikemk »

freecrac wrote:Ups, i use this code with MASM and assemble it without an error. Good to know that NASM do not like it.

Dirk
NASM is not MASM
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

Re: Interrupts do not work in real mode

Post by gabemaiberger »

Yay! My OS can shut down by me telling it to! The code works!

Code: Select all

var myCat="marshmallow"
Post Reply