added simple string_builder
This commit is contained in:
parent
30fb9231d8
commit
390dd10dca
2
Makefile
2
Makefile
@ -120,7 +120,7 @@ GLOBALSRC := $(addprefix $(GLOBALDIR)/, $(addsuffix .s, \
|
||||
))
|
||||
|
||||
SBSRC := $(addprefix $(SBDIR)/, $(addsuffix .s, \
|
||||
string_builder\
|
||||
string_builder sb_append \
|
||||
))
|
||||
|
||||
# Collect all sources and objects
|
||||
|
||||
BIN
lib/core.a
BIN
lib/core.a
Binary file not shown.
@ -2,15 +2,17 @@
|
||||
|
||||
section .text
|
||||
extern strlen
|
||||
extern strcpy
|
||||
extern memcpy
|
||||
extern malloc
|
||||
extern err_malloc
|
||||
|
||||
%define SB [rbp - 16]
|
||||
%define SB_LEN [rbp - 4]
|
||||
%define SB_CAP [rbp - 8]
|
||||
%define SB_LEN dword [rbp - 4]
|
||||
%define SB_CAP dword [rbp - 8]
|
||||
%define APPENDIX [rbp - 24]
|
||||
%define APP_LEN dword [rbp - 28]
|
||||
|
||||
global sb_append
|
||||
sb_append: ; (rdi: *sb, rsi: char*)
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
@ -26,26 +28,28 @@ sb_append: ; (rdi: *sb, rsi: char*)
|
||||
|
||||
; get sb len
|
||||
mov eax, dword [rdi + STR_LEN]
|
||||
mov dword SB_LEN, eax
|
||||
mov SB_LEN, eax
|
||||
|
||||
mov eax, dword [rdi + STR_CAP]
|
||||
mov dword SB_CAP, eax
|
||||
mov SB_CAP, eax
|
||||
|
||||
push rdi
|
||||
mov rdi, rsi
|
||||
call strlen
|
||||
|
||||
add rax, SB_LEN
|
||||
cmp rax, SB_CAP
|
||||
mov APP_LEN, eax
|
||||
|
||||
add eax, SB_LEN
|
||||
cmp eax, SB_CAP
|
||||
jl .copy_string
|
||||
|
||||
mov r9, SB_CAP
|
||||
mov r9d, SB_CAP
|
||||
; new string will be to large for current cap, need to realloc
|
||||
.get_new_len:
|
||||
imul r9, 2
|
||||
cmp r9, rax
|
||||
jl .get_new_len
|
||||
mov SB_CAP, r9
|
||||
mov SB_CAP, r9d
|
||||
push r9
|
||||
push rax
|
||||
mov rdi, r9
|
||||
@ -55,11 +59,12 @@ sb_append: ; (rdi: *sb, rsi: char*)
|
||||
mov rdi, rax
|
||||
mov rsi, SB
|
||||
mov rsi, [rsi + STR_DATA]
|
||||
call strcpy
|
||||
mov edx, SB_LEN
|
||||
call memcpy
|
||||
pop rax
|
||||
mov rsi, SB
|
||||
pop r9
|
||||
mov [SB + STR_CAP], r9
|
||||
mov [rsi + STR_CAP], r9
|
||||
|
||||
.copy_string:
|
||||
pop rdi
|
||||
@ -67,9 +72,10 @@ sb_append: ; (rdi: *sb, rsi: char*)
|
||||
mov dword [r9 + STR_LEN], eax
|
||||
mov rdi, [r9 + STR_DATA]
|
||||
mov eax, dword [rbp - 4]
|
||||
lea rdi, [rdi + eax]
|
||||
mov rsi, [rbp - 24]
|
||||
call strcpy
|
||||
lea rdi, [rdi + rax]
|
||||
mov rsi, APPENDIX
|
||||
mov edx, APP_LEN
|
||||
call memcpy
|
||||
|
||||
.done:
|
||||
mov rsp, rbp
|
||||
@ -10,7 +10,6 @@ section .text
|
||||
global sb_new
|
||||
sb_new: ; rax: str*(rdi: char* || NULL, rsi: *hidden_copy_ptr)
|
||||
push rbx
|
||||
push rdi
|
||||
push rsi
|
||||
|
||||
mov rbx, STRING_INIT_CAP
|
||||
@ -31,7 +30,6 @@ sb_new: ; rax: str*(rdi: char* || NULL, rsi: *hidden_copy_ptr)
|
||||
cmp r9, rbx
|
||||
jg .calc_init_len
|
||||
|
||||
|
||||
.alloc_string:
|
||||
push rdi
|
||||
mov rdi, rbx
|
||||
@ -46,9 +44,10 @@ sb_new: ; rax: str*(rdi: char* || NULL, rsi: *hidden_copy_ptr)
|
||||
mov [rsi + STR_DATA], rax
|
||||
test r9, r9
|
||||
jz .done
|
||||
push rdi
|
||||
mov rdi, rax
|
||||
mov rdx, r9
|
||||
pop rsi
|
||||
mov rdx, r9
|
||||
call memcpy
|
||||
|
||||
.done:
|
||||
|
||||
24
src/start.s
24
src/start.s
@ -1,5 +1,8 @@
|
||||
%include "./src/core/string_builder/sb.s"
|
||||
|
||||
section .data
|
||||
usage: db "Usage: ./debug <file>.lang", 0xa, 0
|
||||
example_data: db "Do you know Ligma?"
|
||||
|
||||
section .text
|
||||
global _start
|
||||
@ -14,6 +17,7 @@ section .text
|
||||
extern putchar
|
||||
extern vec_pop
|
||||
extern sb_new
|
||||
extern sb_append
|
||||
|
||||
print_usage:
|
||||
mov rdi, usage
|
||||
@ -25,9 +29,17 @@ _start:
|
||||
mov rbp, rsp
|
||||
sub rsp, 16
|
||||
mov rdi, usage
|
||||
lea rsi, [rsp - 16]
|
||||
lea rsi, [rbp - 16]
|
||||
call sb_new
|
||||
|
||||
mov rdi, [rbp - 16 + STR_DATA]
|
||||
call putstr
|
||||
lea rdi, [rbp - 16]
|
||||
mov rsi, example_data
|
||||
call sb_append
|
||||
mov rdi, [rbp - 16 + STR_DATA]
|
||||
call putstr
|
||||
|
||||
; pop rdi
|
||||
; cmp rdi, 2
|
||||
; jne err_args
|
||||
@ -44,9 +56,9 @@ _start:
|
||||
; mov rdi, rax
|
||||
; call lex
|
||||
;
|
||||
; mov rsp, rbp
|
||||
; pop rbp
|
||||
;
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
|
||||
done:
|
||||
; xor rdi, rdi
|
||||
; call exit
|
||||
xor rdi, rdi
|
||||
call exit
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user