diff --git a/' b/' new file mode 100644 index 0000000..ab0c019 --- /dev/null +++ b/' @@ -0,0 +1,30 @@ +%include "src/constants/sizes.asm" + +extern exit +extern main + +section .text + global _start + global stack_is_top + +_start: + mov rdi, [rsp] + lea rsi, [rsp + SIZE_QWORD] + push rsp ;Can maybe used as a method to find out if we're at top of the stack + push -1 + + call main + + mov rdi, rax + call exit + +stack_is_top: + xor rax, rax + mov rdi, rsp + cmp rdi, [rsp] + je .yes + jmp .q + .yes: + inc al + .q: + ret diff --git a/Makefile b/Makefile index 80e0faf..d7ddbb1 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ DIR_BLD := build DIR_BIN := $(DIR_BLD)/bin OBJS := $(DIR_BLD)/tests.o \ + $(DIR_BLD)/_initializor.o \ $(DIR_BLD)/constants.o \ $(DIR_BLD)/core.o \ $(DIR_BLD)/console.o \ diff --git a/src/_initializor.asm b/src/_initializor.asm new file mode 100644 index 0000000..95cd372 --- /dev/null +++ b/src/_initializor.asm @@ -0,0 +1,25 @@ +%include "src/constants/sizes.asm" + +extern exit +extern main + +section .text + global _start + +_start: + ; Store argc and argv + mov rdi, [rsp] + lea rsi, [rsp + SIZE_QWORD] + + ; Increase stack (fill with zeroes) + ; Some functions(wrappers) like format() will try to read beyond stack when: + ; - No args were passed + ; - Stack is at top + ; This prevents that + push 0 + push 0 + + call main + + mov rdi, rax + call exit diff --git a/src/tests.asm b/src/tests.asm index 1f3c49b..21f198f 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -440,8 +440,11 @@ section .bss statBuff2 resb STATBUFF_SIZE section .text - global _start -_start: + global main +main: + push rbp + mov rbp, rsp + xor r13, r13 ;total tests xor r14, r14 ;OK tests (assert => true) @@ -1691,8 +1694,10 @@ _start: mov rdx, r14 mov rcx, r13 sub rcx, r14 + mov r15, rcx call printf ;--- exit() - xor rdi, rdi - call exit + leave + mov rax, r15 + ret