; asmfuncs.asm ; Josh Holtrop ; Created: 10/23/03 ; Modified: 12/25/03 [extern _putc] [extern _console_memory] [extern _cursorPosition] [extern _video_drawConsole] [extern _videoMode] %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 CR2 register ;extern dword read_cr2(); [global _read_cr2] _read_cr2: mov eax, cr2; ret ;returns the value in the CR3 register ;extern dword read_cr3(); [global _read_cr3] _read_cr3: mov eax, cr3; ret ;compares one string to another ;returns 0 if the strings are different ;extern dword strcmp(char *str1, char *str2); [global _strcmp] _strcmp: push ebp mov ebp, esp push esi push edi mov esi, [ebp+8] mov edi, [ebp+12] strcmp_loop1: lodsb mov ah, [edi] inc edi cmp ah, al jnz strcmp_ne or al, al jz strcmp_e jmp strcmp_loop1 strcmp_e: mov eax, 1 jmp short strcmp_done strcmp_ne: xor eax, eax strcmp_done: pop edi pop esi pop ebp ret ;copies a string from the source to the destination parameter ;extern void strcpy(char *dest, char *src); [global _strcpy] _strcpy: push ebp mov ebp, esp push esi push edi mov edi, [ebp+8] mov esi, [ebp+12] strcpyloop: lodsb stosb or al, al jnz strcpyloop pop edi pop esi pop ebp ret ;copies memory of n bytes from src to destination ;extern void memcpy(dword dest, dword src, dword n); [global _memcpy] _memcpy: push ebp mov ebp, esp push esi push edi push ecx mov edi, [ebp+8] mov esi, [ebp+12] mov ecx, [ebp+16] rep movsb pop ecx pop edi pop esi pop ebp ret ;copies memory of n dwords (n*4 bytes) from src to destination ;extern void memcpyd(dword dest, dword src, dword n); [global _memcpyd] _memcpyd: push ebp mov ebp, esp push esi push edi push ecx mov edi, [ebp+8] mov esi, [ebp+12] mov ecx, [ebp+16] rep movsd pop ecx pop edi pop esi pop ebp ret ;returns the number of characters in a string ;extern dword strlen(char *str); [global _strlen] _strlen: push ebp mov ebp, esp push esi push ebx mov esi, [ebp+8] xor ebx, ebx strlenloop: lodsb or al, al jz strlendone inc ebx jmp strlenloop strlendone: mov eax, ebx pop ebx pop esi pop ebp 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, _console_memory+160 mov edi, _console_memory mov ecx, 960 ;(2000-80)/2 rep movsd mov ax, 0x0720 mov ecx, 80 rep stosw mov esi, _console_memory mov edi, 0xC00B8000 mov ecx, 1000 rep movsd mov eax, [_videoMode] cmp eax, 0 jz _console_scroll_end call _video_drawConsole _console_scroll_end: popa ret ; ;void console_cls() ; [global _console_cls] _console_cls: pusha mov edi, _console_memory mov ax, 0x0720 mov ecx, 2000 rep stosw push dword 0 call _writeCursorPosition add esp, 4 mov [_cursorPosition], dword 0 mov esi, _console_memory mov edi, 0xC00B8000 mov ecx, 1000 rep movsd 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