From: Daniel Liu Date: Sat, 19 Jun 2021 23:43:47 +0000 (-0400) Subject: writing to file done X-Git-Url: https://git.danieliu.xyz/?p=sped.git;a=commitdiff_plain;h=7a7ed6ae81978b84911dab0b902e77ebb7e9b735 writing to file done --- diff --git a/README.md b/README.md index 0664806..4ea05df 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This is my own stupid and pointless attempt at writing such line editor. #### Build from source -With `gcc` and `nasm` installed, you can simply run +with `gcc` and `nasm` installed, you can simply run ``` make ``` diff --git a/fileutils.S b/fileutils.S index 7ce6be3..5a77864 100644 --- a/fileutils.S +++ b/fileutils.S @@ -4,5 +4,6 @@ extern readFile extern readLine +extern writeFile %endif diff --git a/fileutils.asm b/fileutils.asm index 8596ff3..b9b41e7 100644 --- a/fileutils.asm +++ b/fileutils.asm @@ -1,12 +1,16 @@ +; %include "utils.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 @@ -202,6 +206,75 @@ readLine: ret ; writes contents of string array into file -; arg: filename, string array -; writeFile: +; 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 + jge _writeFile_loop + push eax + push wrongfile_str + call printf + jmp _writeFile_exit + + _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_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 diff --git a/sped.asm b/sped.asm index d7dcb8d..213f3ea 100644 --- a/sped.asm +++ b/sped.asm @@ -33,6 +33,7 @@ section .data section .bss buffer resb 4 buffer_lines resb 4 + buffer_filename resb 4 cur_line resb 4 section .text @@ -57,7 +58,10 @@ main: _main_existing: mov ebx, DWORD [ebp+_ARGV] add ebx, 4 ; first user arg is filename - push DWORD [ebx] + mov ebx, [ebx] + mov [buffer_filename], ebx + + push DWORD [buffer_filename] call readFile mov [buffer], eax @@ -235,6 +239,19 @@ repl: jmp _repl_continue _repl_cmd_change_end: + ; w writes file =-=-=-=-=-=-=-=-=-=-=-=-= + mov eax, DWORD [ebp-CMDSTR] + cmp BYTE [eax], 'w' + jne _repl_cmd_write_end + + push DWORD [buffer_filename] + push DWORD [buffer] + push DWORD [buffer_lines] + call writeFile + + jmp _repl_continue + _repl_cmd_write_end: + jmp _repl_invalid_cmd