From 6cf67300c03d92a9cc0c5888b33cdb21a852eb69 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 19 Jun 2021 21:14:15 -0400 Subject: [PATCH] deleting line --- fileutils.S | 9 -------- fileutils.asm | 10 ++++----- utils.S => macros.S | 11 +++++++--- makefile | 2 +- repl.S | 7 ------ repl.asm | 17 ++++++++++++--- sped.asm | 16 ++++---------- utils.asm | 52 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 84 insertions(+), 40 deletions(-) delete mode 100644 fileutils.S rename utils.S => macros.S (69%) delete mode 100644 repl.S diff --git a/fileutils.S b/fileutils.S deleted file mode 100644 index 5a77864..0000000 --- a/fileutils.S +++ /dev/null @@ -1,9 +0,0 @@ - -%ifndef __FILEUTILS_S__ -%define __FILEUTILS_S__ - -extern readFile -extern readLine -extern writeFile - -%endif diff --git a/fileutils.asm b/fileutils.asm index 1d78439..6c30ca6 100644 --- a/fileutils.asm +++ b/fileutils.asm @@ -1,5 +1,5 @@ -%include "utils.S" +%include "macros.S" extern printf extern malloc @@ -60,15 +60,15 @@ 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 + + ; check if eof was reached + cmp DWORD [ebp-IS_EOF], 1 + je _readFile_exit ; push esi ; call printf diff --git a/utils.S b/macros.S similarity index 69% rename from utils.S rename to macros.S index 5c09a23..afde07d 100644 --- a/utils.S +++ b/macros.S @@ -2,6 +2,14 @@ %ifndef __UTILS_S__ %define __UTILS_S__ +%macro write_str 2 + mov eax, 4 + mov ebx, 1 + mov ecx, %1 + mov edx, %2 + int 0x80 +%endmacro + ; gets the nth string ptr in a str array ; of the form: str_offset buffer, n ; result is in eax @@ -12,7 +20,4 @@ add eax, DWORD %1 %endmacro -extern shiftLeft -extern shiftRight - %endif diff --git a/makefile b/makefile index 18c299f..e77e686 100644 --- a/makefile +++ b/makefile @@ -10,7 +10,7 @@ make: sped %.o: %.asm $(ASM) $(ASMFLAGS) -o $@ $^ -sped: sped.o fileutils.o repl.o +sped: sped.o fileutils.o repl.o utils.o $(CC) $(CFLAGS) -o $@ $^ clean: diff --git a/repl.S b/repl.S deleted file mode 100644 index 18805a6..0000000 --- a/repl.S +++ /dev/null @@ -1,7 +0,0 @@ - -%ifndef __REPL_S__ -%define __REPL_S__ - -extern repl - -%endif diff --git a/repl.asm b/repl.asm index 3c7f429..738da94 100644 --- a/repl.asm +++ b/repl.asm @@ -1,11 +1,14 @@ -%include "fileutils.S" - extern printf extern fflush extern stdout extern free +extern readLine +extern writeFile +extern shiftLeft +extern shiftRight + global repl section .data @@ -190,8 +193,16 @@ repl: mov eax, DWORD [ebp-CMDSTR] cmp BYTE [eax], 'd' jne _repl_cmd_delete_end - + ; check to make sure we don't have only one line + + ; delete the line + push DWORD [buffer] + push DWORD [buffer_lines] + push DWORD [cur_line] + call shiftLeft + + sub DWORD [buffer_lines], 1 jmp _repl_continue _repl_cmd_delete_end: diff --git a/sped.asm b/sped.asm index a14dbd4..544ec3c 100644 --- a/sped.asm +++ b/sped.asm @@ -1,21 +1,13 @@ ; sped - the stupidly pointless editor -; written by pinosaur -%include "fileutils.S" -%include "repl.S" +%include "macros.S" extern printf -global main +extern readFile +extern repl -; macros -%macro write_str 2 - mov eax, 4 - mov ebx, 1 - mov ecx, %1 - mov edx, %2 - int 0x80 -%endmacro +global main section .data banner_str db `SPED - the stupidly pointless editor\n`, 0x00 diff --git a/utils.asm b/utils.asm index fd8529d..9feb498 100644 --- a/utils.asm +++ b/utils.asm @@ -1,21 +1,72 @@ +%include "macros.S" + extern memmove +extern free +extern realloc global shiftLeft global shiftRight section .text +; shrinks array (4byte) by shifting blocks left +; args: buffer, buffer_len, shift_pos (index that gets destroyed) +; return: +; eax: location of new buffer +; issues: shiftLeft: + %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 + + ; set vars + mov eax, DWORD [ebp+_BUFFER_LEN] + sub eax, [ebp+_SHIFT_POS] + sub eax, 1 + mov [ebp-SHIFT_LEN], eax + + str_offset [ebp+_BUFFER], [ebp+_SHIFT_POS] + mov [ebp-BLOCK_OFFSET], eax + ; free string to be destoryed first + mov eax, DWORD [ebp-BLOCK_OFFSET] + mov eax, [eax] + push eax + call free + + ; move the memory + mov eax, DWORD [ebp-SHIFT_LEN] + mov ecx, 4 + mul ecx + push eax + mov eax, DWORD [ebp-BLOCK_OFFSET] + add eax, 4 + push eax + push DWORD [ebp-BLOCK_OFFSET] + call memmove + ; realloc to shrink the array + + %undef _BUFFER + %undef _BUFFER_LEN + %undef _SHIFT_POS mov esp, ebp pop ebp ret +; grows array by shifting blocks right +; args: buffer, buffer_len, shift_pos (new uninitalized index) +; return: location of new buffer shiftRight: push ebp mov ebp, esp @@ -25,3 +76,4 @@ shiftRight: mov esp, ebp pop ebp ret + -- 2.20.1