345 lines
4.7 KiB
NASM
345 lines
4.7 KiB
NASM
; asmfuncs.asm
|
|
; Josh Holtrop
|
|
; Created: 10/23/03
|
|
; Modified: 12/25/03
|
|
|
|
[extern _putc]
|
|
|
|
%macro jzfar 1
|
|
jnz %%skip
|
|
jmp %1
|
|
%%skip:
|
|
|
|
%endmacro
|
|
|
|
;stores the parameter to the CR0 register
|
|
;extern dword write_cr0(dword cr0);
|
|
[global _write_cr0]
|
|
_write_cr0:
|
|
push ebp
|
|
mov ebp, esp
|
|
mov eax, [ebp+8]
|
|
mov cr0, eax
|
|
pop ebp
|
|
ret
|
|
|
|
;returns the value in the CR0 register
|
|
;extern dword read_cr0();
|
|
[global _read_cr0]
|
|
_read_cr0:
|
|
mov eax, cr0;
|
|
ret
|
|
|
|
;stores the parameter to the CR3 register
|
|
;extern dword write_cr3(dword cr3);
|
|
[global _write_cr3]
|
|
_write_cr3:
|
|
push ebp
|
|
mov ebp, esp
|
|
mov eax, [ebp+8]
|
|
mov cr3, eax
|
|
pop ebp
|
|
ret
|
|
|
|
;returns the value in the CR3 register
|
|
;extern dword read_cr3();
|
|
[global _read_cr3]
|
|
_read_cr3:
|
|
mov eax, cr3;
|
|
ret
|
|
|
|
;this function invalidates the page directory/table entry that
|
|
; would be used to access the memory address given in the parameter
|
|
;extern void invlpg(dword addr);
|
|
[global _invlpg]
|
|
_invlpg:
|
|
mov eax, [esp+4]
|
|
invlpg [eax]
|
|
ret
|
|
|
|
|
|
;
|
|
;void writeCursorPosition(word pos)
|
|
;
|
|
[global _writeCursorPosition]
|
|
_writeCursorPosition:
|
|
push ebp
|
|
mov ebp, esp
|
|
|
|
push eax
|
|
push ebx
|
|
push edx
|
|
|
|
mov eax, [ebp+8] ;cursor position in ax
|
|
|
|
mov bl, al
|
|
mov dx, 0x03D4
|
|
mov al, 0x0E
|
|
out dx, al
|
|
|
|
inc dx
|
|
mov al, ah
|
|
out dx, al
|
|
|
|
dec dx
|
|
mov al, 0x0F
|
|
out dx, al
|
|
|
|
inc dx
|
|
mov al, bl
|
|
out dx, al
|
|
|
|
pop edx
|
|
pop ebx
|
|
pop eax
|
|
pop ebp
|
|
|
|
ret
|
|
|
|
|
|
;
|
|
;word getCursorPosition()
|
|
;
|
|
[global _getCursorPosition]
|
|
_getCursorPosition:
|
|
push ebx
|
|
push edx
|
|
|
|
xor eax, eax
|
|
mov dx, 0x03D4
|
|
mov al, 0x0E
|
|
out dx, al
|
|
|
|
inc dx
|
|
in al, dx
|
|
mov bl, al
|
|
|
|
dec dx
|
|
mov al, 0x0F
|
|
out dx, al
|
|
|
|
inc dx
|
|
in al, dx
|
|
mov ah, bl
|
|
|
|
pop edx
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
|
|
;
|
|
;void console_scroll()
|
|
;
|
|
[global _console_scroll]
|
|
_console_scroll:
|
|
pusha
|
|
mov esi, 0xc00b8000+160
|
|
mov edi, 0xc00b8000
|
|
mov ecx, 960 ;(2000-80)/2
|
|
rep movsd
|
|
mov ax, 0x0720
|
|
mov ecx, 80
|
|
rep stosw
|
|
popa
|
|
ret
|
|
|
|
|
|
|
|
;
|
|
;void console_cls()
|
|
;
|
|
[global _console_cls]
|
|
_console_cls:
|
|
pusha
|
|
mov edi, 0xc00b8000
|
|
mov ax, 0x0720
|
|
mov ecx, 2000
|
|
rep stosw
|
|
push dword 0
|
|
call _writeCursorPosition
|
|
add esp, 4
|
|
popa
|
|
ret
|
|
|
|
|
|
|
|
|
|
;
|
|
;int puts(char *str)
|
|
;
|
|
[global _puts]
|
|
_puts:
|
|
push ebp
|
|
mov ebp, esp
|
|
push esi
|
|
push eax
|
|
mov esi, [ebp+8] ;esi = to string
|
|
puts_loop:
|
|
lodsb
|
|
cmp al, 0
|
|
jz puts_done
|
|
push eax
|
|
call _putc
|
|
add esp, 4
|
|
jmp puts_loop
|
|
|
|
puts_done:
|
|
pop eax
|
|
pop esi
|
|
pop ebp
|
|
ret
|
|
|
|
|
|
|
|
|
|
[global _putDecu]
|
|
_putDecu:
|
|
push ebp
|
|
mov ebp, esp
|
|
sub esp, 24
|
|
mov DWORD [ebp-4], 1
|
|
mov BYTE [ebp-5], 0
|
|
L2:
|
|
mov edx, DWORD [ebp+8]
|
|
mov eax, -858993459
|
|
mul edx
|
|
mov eax, edx
|
|
shr eax, 3
|
|
cmp eax, DWORD [ebp-4]
|
|
jae L4
|
|
jmp L3
|
|
L4:
|
|
mov eax, DWORD [ebp-4]
|
|
mov edx, eax
|
|
sal edx, 2
|
|
add edx, eax
|
|
lea eax, [edx+edx]
|
|
mov DWORD [ebp-4], eax
|
|
jmp L2
|
|
L3:
|
|
nop
|
|
L5:
|
|
cmp DWORD [ebp-4], 1
|
|
ja L7
|
|
jmp L6
|
|
L7:
|
|
mov edx, DWORD [ebp+8]
|
|
mov eax, edx
|
|
mov edx, 0
|
|
div DWORD [ebp-4]
|
|
mov DWORD [ebp-12], eax
|
|
mov al, BYTE [ebp-12]
|
|
mov BYTE [ebp-5], al
|
|
mov eax, 0
|
|
mov al, BYTE [ebp-5]
|
|
imul eax, DWORD [ebp-4]
|
|
sub DWORD [ebp+8], eax
|
|
mov edx, DWORD [ebp-4]
|
|
mov eax, -858993459
|
|
mul edx
|
|
mov eax, edx
|
|
shr eax, 3
|
|
mov DWORD [ebp-4], eax
|
|
lea eax, [ebp-5]
|
|
add BYTE [eax], 48
|
|
sub esp, 12
|
|
mov eax, 0
|
|
mov al, BYTE [ebp-5]
|
|
push eax
|
|
call _putc
|
|
add esp, 16
|
|
jmp L5
|
|
L6:
|
|
sub esp, 12
|
|
mov al, BYTE [ebp+8]
|
|
add eax, 48
|
|
and eax, 255
|
|
push eax
|
|
call _putc
|
|
add esp, 16
|
|
leave
|
|
ret
|
|
|
|
|
|
|
|
|
|
[global _putDec]
|
|
_putDec:
|
|
push ebp
|
|
mov ebp, esp
|
|
sub esp, 24
|
|
cmp DWORD [ebp+8], 0
|
|
jns L9
|
|
sub esp, 12
|
|
push 45
|
|
call _putc
|
|
add esp, 16
|
|
neg DWORD [ebp+8]
|
|
L9:
|
|
mov DWORD [ebp-4], 1
|
|
mov BYTE [ebp-5], 0
|
|
L10:
|
|
mov eax, DWORD [ebp+8]
|
|
cmp eax, DWORD [ebp-4]
|
|
jae L12
|
|
jmp L11
|
|
L12:
|
|
mov eax, DWORD [ebp-4]
|
|
mov edx, eax
|
|
sal edx, 2
|
|
add edx, eax
|
|
lea eax, [edx+edx]
|
|
mov DWORD [ebp-4], eax
|
|
jmp L10
|
|
L11:
|
|
mov edx, DWORD [ebp-4]
|
|
mov eax, -858993459
|
|
mul edx
|
|
mov eax, edx
|
|
shr eax, 3
|
|
mov DWORD [ebp-4], eax
|
|
L13:
|
|
cmp DWORD [ebp-4], 1
|
|
ja L15
|
|
jmp L14
|
|
L15:
|
|
mov edx, DWORD [ebp+8]
|
|
mov eax, edx
|
|
mov edx, 0
|
|
div DWORD [ebp-4]
|
|
mov DWORD [ebp-12], eax
|
|
mov al, BYTE [ebp-12]
|
|
mov BYTE [ebp-5], al
|
|
mov eax, 0
|
|
mov al, BYTE [ebp-5]
|
|
imul eax, DWORD [ebp-4]
|
|
sub DWORD [ebp+8], eax
|
|
mov edx, DWORD [ebp-4]
|
|
mov eax, -858993459
|
|
mul edx
|
|
mov eax, edx
|
|
shr eax, 3
|
|
mov DWORD [ebp-4], eax
|
|
lea eax, [ebp-5]
|
|
add BYTE [eax], 48
|
|
sub esp, 12
|
|
mov eax, 0
|
|
mov al, BYTE [ebp-5]
|
|
push eax
|
|
call _putc
|
|
add esp, 16
|
|
jmp L13
|
|
L14:
|
|
sub esp, 12
|
|
mov al, BYTE [ebp+8]
|
|
add eax, 48
|
|
and eax, 255
|
|
push eax
|
|
call _putc
|
|
add esp, 16
|
|
leave
|
|
ret
|
|
|
|
|