From: Daniel Liu Date: Sun, 20 Jun 2021 02:00:27 +0000 (-0400) Subject: appending lines after X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=0e7c787c5d6e31bd64fb9348c1cc4704d5b5928e;p=sped.git appending lines after --- diff --git a/fileutils.asm b/fileutils.asm index 6c30ca6..54c9895 100644 --- a/fileutils.asm +++ b/fileutils.asm @@ -14,6 +14,7 @@ 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 @@ -62,13 +63,13 @@ readFile: push DWORD [ebp-FILE_HANDLE] call readLine - mov esi, eax mov [ebp-IS_EOF], ebx ; check if eof was reached cmp DWORD [ebp-IS_EOF], 1 je _readFile_exit + ; push esi ; call printf @@ -233,11 +234,13 @@ writeFile: ; check if file was open successfully cmp eax, 0 - jge _writeFile_loop - push eax - push wrongfile_str - call printf - jmp _writeFile_exit + jl _writeFile_error + + ; truncate file + mov eax, 93 + mov ebx, [ebp-FILE_HANDLE] + mov ecx, 1 + int 0x80 _writeFile_loop: @@ -262,6 +265,12 @@ writeFile: jmp _writeFile_loop + _writeFile_error: + push eax + push wrongfile_str + call printf + jmp _writeFile_exit + _writeFile_exit: ; close file diff --git a/repl.asm b/repl.asm index 738da94..6a1472f 100644 --- a/repl.asm +++ b/repl.asm @@ -201,12 +201,42 @@ repl: push DWORD [buffer_lines] push DWORD [cur_line] call shiftLeft + mov [buffer], eax sub DWORD [buffer_lines], 1 jmp _repl_continue _repl_cmd_delete_end: + ; o appends text after line =-=-=-=-=-=-=-=-= + mov eax, DWORD [ebp-CMDSTR] + cmp BYTE [eax], 'o' + jne _repl_cmd_appendup_end + + ; make room first + push DWORD [buffer] + push DWORD [buffer_lines] + push DWORD [cur_line] + call shiftRight + mov [buffer], eax + + ; input text + push 0 + call readLine + mov esi, eax + + ; insert new string + mov eax, [cur_line] + mov ecx, 4 + mul ecx + add eax, DWORD [buffer] + mov [eax], esi + + add DWORD [buffer_lines], 1 + + jmp _repl_continue + _repl_cmd_appendup_end: + ; w writes file =-=-=-=-=-=-=-=-=-=-=-=-= mov eax, DWORD [ebp-CMDSTR] cmp BYTE [eax], 'w' diff --git a/utils.asm b/utils.asm index 9feb498..441efc1 100644 --- a/utils.asm +++ b/utils.asm @@ -55,10 +55,20 @@ shiftLeft: call memmove ; realloc to shrink the array + mov eax, DWORD [ebp+_BUFFER_LEN] + sub eax, 1 + mov ecx, 4 + mul ecx + push eax + push DWORD [ebp+_BUFFER] + call realloc + mov [ebp-NEW_BUFFER], eax %undef _BUFFER %undef _BUFFER_LEN %undef _SHIFT_POS + + mov eax, [ebp-NEW_BUFFER] mov esp, ebp pop ebp @@ -66,12 +76,55 @@ shiftLeft: ; grows array by shifting blocks right ; args: buffer, buffer_len, shift_pos (new uninitalized index) -; return: location of new buffer +; return: +; eax: location of new buffer shiftRight: + %define _BUFFER 16 + %define _BUFFER_LEN 12 + %define _SHIFT_POS 8 + %define SHIFT_LEN 4 + %define BLOCK_OFFSET 8 ; mem location of block to be destroyed + %define NEW_BUFFER 12 + push ebp mov ebp, esp - + sub esp, 12 + + ; realloc to make memory bigger first + mov eax, DWORD [ebp+_BUFFER_LEN] + add eax, 1 + mov ecx, 4 + mul ecx + push eax + push DWORD [ebp+_BUFFER] + call realloc + mov [ebp-NEW_BUFFER], eax + + ; set vars + mov eax, DWORD [ebp+_BUFFER_LEN] + sub eax, [ebp+_SHIFT_POS] + mov [ebp-SHIFT_LEN], eax + + str_offset [ebp-NEW_BUFFER], [ebp+_SHIFT_POS] + mov [ebp-BLOCK_OFFSET], eax + + ; move the memory + mov eax, DWORD [ebp-SHIFT_LEN] + mov ecx, 4 + mul ecx + push eax + push DWORD [ebp-BLOCK_OFFSET] + mov eax, DWORD [ebp-BLOCK_OFFSET] + add eax, 4 + push eax + call memmove + + %undef _BUFFER + %undef _BUFFER_LEN + %undef _SHIFT_POS + + mov eax, [ebp-NEW_BUFFER] mov esp, ebp pop ebp