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
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
; 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:
jmp _writeFile_loop
+ _writeFile_error:
+ push eax
+ push wrongfile_str
+ call printf
+ jmp _writeFile_exit
+
_writeFile_exit:
; close file
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'
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
; 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