; lang.asm ; Josh Holtrop ; Created: 10/23/03 ; Modified: 12/30/04 [bits 32] %macro jzfar 1 jnz %%skip jmp %1 %%skip: %endmacro ;implements a lock [global lockit] lockit: push ebp mov ebp, esp push eax push ebx push esi mov esi, [ebp + 8] _lock_loop0: xor eax, eax mov ebx, 1 cmpxchg [esi], ebx jnz _lock_loop0 ; failed to acquire lock pop esi pop ebx pop eax pop ebp ret ;releases a lock [global unlock] unlock: push ebp mov ebp, esp push esi mov esi, [ebp + 8] mov [esi], dword 0 pop esi 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 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 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 ;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 ; read ss register [global read_ss] read_ss: mov ax, ss 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 words (n*2 bytes) from src to destination ;void memcpyw(void *dest, void *src, dword n); [global memcpyw] memcpyw: 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 movsw 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