X-Git-Url: https://git.danieliu.xyz/?p=sped.git;a=blobdiff_plain;f=fileutils.asm;h=32dcef6fd579a9610fd45dc1a3f89cf9cabf2f60;hp=1d96aa05eae9e75b012d83cd66cc094f90113cd3;hb=HEAD;hpb=fb894149783dd18e6ecf1271153193a342e80433 diff --git a/fileutils.asm b/fileutils.asm index 1d96aa0..32dcef6 100644 --- a/fileutils.asm +++ b/fileutils.asm @@ -1,14 +1,23 @@ +; sped - the stupidly pointless editor +; written by pinosaur +; fileutils.asm: file i/o + +%include "macros.S" extern printf extern malloc extern realloc extern free extern memset +extern strlen global readFile +global readLine +global writeFile section .data wrongfile_str db `unable to open file, error code: %i\n`, 0x00 + returnvalue_str db `system call return was %i\n`, 0x00 section .text @@ -55,18 +64,14 @@ readFile: _readFile_loop: - ; check if eof was reached - cmp DWORD [ebp-IS_EOF], 1 - je _readFile_exit - push DWORD [ebp-FILE_HANDLE] call readLine - mov esi, eax mov [ebp-IS_EOF], ebx - - push esi - call printf + + ; check if eof was reached + cmp DWORD [ebp-IS_EOF], 1 + je _readFile_exit ; make string buffer bigger mov eax, DWORD [ebp-LINES_READ] @@ -79,10 +84,14 @@ readFile: mov DWORD [ebp-BUF_PTR], eax ; write string to buffer - mov eax, DWORD [ebp-BUF_PTR] + mov eax, [ebp-LINES_READ] mov ecx, 4 mul ecx - mov eax, esi + add eax, DWORD [ebp-BUF_PTR] + mov [eax], esi + + ; push DWORD [eax] + ; call printf add DWORD [ebp-LINES_READ], 1 @@ -113,6 +122,7 @@ readFile: ; return: ; eax: location to buffer ; ebx: contains eof +; ecx: number of chars read readLine: %define _FILE_HANDLE 8 %define CHAR_COUNT 4 ; count number of characters read @@ -149,13 +159,6 @@ readLine: add ecx, [ebp-CHAR_COUNT] mov edx, 1 int 0x80 - - ; mov eax, 4 - ; mov ebx, 1 - ; mov ecx, [ebp-STR_PTR] - ; add ecx, [ebp-CHAR_COUNT] - ; mov edx, 1 - ; int 0x80 ; check for eof cmp eax, 0 ; eax has zero on eof @@ -178,6 +181,12 @@ readLine: _readLine_exit: + mov eax, [ebp-BLOCK_COUNT] + mov ecx, 63 + mul ecx + add eax, [ebp-CHAR_COUNT] + mov ecx,eax + mov eax, DWORD [ebp-STR_PTR] %undef _FILE_HANDLE @@ -189,3 +198,84 @@ readLine: pop ebp ret +; writes contents of string array into file +; arg: filename, string array, number of lines +writeFile: + + %define _FILE_NAME 16 + %define _STR_ARR 12 + %define _STR_ARR_LEN 8 + %define FILE_HANDLE 4 + %define LINES_WRITTEN 8 + + push ebp + mov ebp, esp + + ; allocate vars + sub esp, 8 + mov DWORD [ebp-FILE_HANDLE], 0x00 + mov DWORD [ebp-LINES_WRITTEN], 0x00 + + ; open existing file + mov eax, 5 + mov ebx, [ebp+_FILE_NAME] + mov ecx, 2 + mov edx, 0777 + int 0x80 + mov [ebp-FILE_HANDLE], eax + + ; check if file was open successfully + cmp eax, 0 + jl _writeFile_error + + ; truncate file + mov eax, 93 + mov ebx, [ebp-FILE_HANDLE] + mov ecx, 1 + int 0x80 + + _writeFile_loop: + + ; check if we are done writing + mov eax, [ebp+_STR_ARR_LEN] + cmp eax, [ebp-LINES_WRITTEN] + je _writeFile_exit + + ; get length of string to write + str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN] + mov esi, eax + push DWORD [esi] + call strlen + + mov edx, eax + mov eax, 4 + mov ebx, [ebp-FILE_HANDLE] + mov ecx, [esi] + int 0x80 + + add DWORD [ebp-LINES_WRITTEN], 1 + + jmp _writeFile_loop + + _writeFile_error: + push eax + push wrongfile_str + call printf + jmp _writeFile_exit + + _writeFile_exit: + + ; close file + mov eax, 6 + mov ebx, [ebp-FILE_HANDLE] + int 0x80 + + %undef _FILE_NAME + %undef _STR_ARR + %undef _STR_ARR_LEN + %undef FILE_HANDLE + + mov esp, ebp + pop ebp + ret +