; asmfuncs.asm ; Josh Holtrop ; Created: 10/23/03 ; Modified: 07/11/04 %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 ;void memcpy(void *dest, void *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] cld rep movsb pop ecx pop edi pop esi pop ebp ret ;copies memory of n dwords (n*4 bytes) from src to destination ;void memcpyd(void *dest, void *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] cld rep movsd pop ecx pop edi pop esi pop ebp ret ;sets num bytes at buffer to the value of c ;void *memset(void *buffer, int c, int num); [global _memset] _memset: push ebp mov ebp, esp push edi push ecx mov edi, [ebp+8] push edi ;save for return address mov eax, [ebp+12] mov ecx, [ebp+16] rep stosb pop eax pop ecx pop edi pop ebp ret ;sets num words at buffer to the value of c ;void *memsetw(void *buffer, int c, int num); [global _memsetw] _memsetw: push ebp mov ebp, esp push edi push ecx mov edi, [ebp+8] push edi ;save for return address mov eax, [ebp+12] mov ecx, [ebp+16] rep stosw pop eax pop ecx pop edi pop ebp ret ;sets num dwords at buffer to the value of c ;void *memsetd(void *buffer, int c, int num); [global _memsetd] _memsetd: push ebp mov ebp, esp push edi push ecx mov edi, [ebp+8] push edi ;save for return address mov eax, [ebp+12] mov ecx, [ebp+16] rep stosd pop eax pop ecx pop edi 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