I can't write os becouse I don't now how to create interrupts.
please help me.
Who can explain how to create interrupts with NASM
Software interrupts
Are you talking about a normal software interrupt? It's really easy. First write your interrupt's code (I'll use int 21h as an example, interrupts range from 0-FF (0-255), 0-1F (0-31) are reserved by Intel for exceptions):
_int21h:
pusha ; Save registers (if you choose to do so)
... code ... ; Your code
popa ; Restore registers
iret ; Return from interrupt
Then you need to tell the CPU that INT 21h refers to _int21h. At 0x00000 is a table that tells the CPU this. It has 256 4-byte entries. Each entry consists of a address:segment combination. So, in NASM, you could do:
cli ; Disable interrupts
xor ax, ax ; Set ES to the real-mode interrupt table at 0x00000
mov es, ax
mov ax, _int21h
mov [es:0x21*4], ax ; Set the address
mov ax, cs
mov [es:0x21*4+2], ax ; Set the segment
sti ; Reenable interrupts
That's all! Hope this helps.
_int21h:
pusha ; Save registers (if you choose to do so)
... code ... ; Your code
popa ; Restore registers
iret ; Return from interrupt
Then you need to tell the CPU that INT 21h refers to _int21h. At 0x00000 is a table that tells the CPU this. It has 256 4-byte entries. Each entry consists of a address:segment combination. So, in NASM, you could do:
cli ; Disable interrupts
xor ax, ax ; Set ES to the real-mode interrupt table at 0x00000
mov es, ax
mov ax, _int21h
mov [es:0x21*4], ax ; Set the address
mov ax, cs
mov [es:0x21*4+2], ax ; Set the segment
sti ; Reenable interrupts
That's all! Hope this helps.
Keyboard IRQ
Here is how to write a keyboard IRQ (Interrupt Request). When you push a key (or let go of a key) on the keyboard, the CPU checks the interrupt flag, which can be enabled or disabled by CLI or STI. If it is set, then the CPU calls INT 9. So, by reprogramming INT 9, you can take control of the keyboard, change Ctrl-Alt-Del, etc... Okay, here's the code:
_int9h:
; Don't touch the program's registers
push eax
push ebx
; Read the key from port 0x60 (the keyboard)
in al, 0x60
; Save the scancode, adding it to the stack
xor bh, bh ; Check if the buffer is full via. BX
mov bl, [keyCtr]
cmp bl, 32
jz .bufferFull
mov [keyBuffer+BX], al ; Store the key in the buffer
inc bl ; Advance the buffer pointer
mov [keyCtr], bl ; Store it, and return!
.bufferFull:
mov al, 0x20 ; Tell the keyboard we got the key
out 0x20, al
pop ebx
pop eax
iret
keyCtr db 0
keyBuffer resb 32
---------
Note that with this code, you would also need to reprogram INT 16h, to tell it to pop the key from your buffer, not the BIOS's. Note that the values stored in the buffer are not ASCII codes, but scancodes. Look around for a scancode<->ASCII chart, and then program your interrupt to use XLAT or something like that for translation. C--'s docs have scancodes, though the code is hard to read (KEYCODES.H--). If you want to make Ctrl-Alt-Del reboot, or do something else, you should put it in the int 9 handler.
_int9h:
; Don't touch the program's registers
push eax
push ebx
; Read the key from port 0x60 (the keyboard)
in al, 0x60
; Save the scancode, adding it to the stack
xor bh, bh ; Check if the buffer is full via. BX
mov bl, [keyCtr]
cmp bl, 32
jz .bufferFull
mov [keyBuffer+BX], al ; Store the key in the buffer
inc bl ; Advance the buffer pointer
mov [keyCtr], bl ; Store it, and return!
.bufferFull:
mov al, 0x20 ; Tell the keyboard we got the key
out 0x20, al
pop ebx
pop eax
iret
keyCtr db 0
keyBuffer resb 32
---------
Note that with this code, you would also need to reprogram INT 16h, to tell it to pop the key from your buffer, not the BIOS's. Note that the values stored in the buffer are not ASCII codes, but scancodes. Look around for a scancode<->ASCII chart, and then program your interrupt to use XLAT or something like that for translation. C--'s docs have scancodes, though the code is hard to read (KEYCODES.H--). If you want to make Ctrl-Alt-Del reboot, or do something else, you should put it in the int 9 handler.
RE:Software interrupts
I trying to get interrupts to work in pmode and Im not having any luck. Will this still work in pmode or will I have to either switch to real mode or use v86 mode?
for right now I don't mind switching to real mode since I not ready for v86 mode yet considering I haven't learned taskswitching from ring0 to ring 3 and back.
for right now I don't mind switching to real mode since I not ready for v86 mode yet considering I haven't learned taskswitching from ring0 to ring 3 and back.
RE:Software interrupts
In pmode you'll have to create your own IDT (Interrupt Descriptor Table) to set up your interrupts, but you would still call them the same way. There should be some (good) pmode interrupt tutorials here:
http://www.nondot.org/sabre/os/articles/ProtectedMode/
I don't really program in pmode, so I can't help much, but good luck!
http://www.nondot.org/sabre/os/articles/ProtectedMode/
I don't really program in pmode, so I can't help much, but good luck!