summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandromeda <andromeda@lenovo>2026-03-07 12:43:55 +0100
committerandromeda <andromeda@lenovo>2026-03-07 12:43:55 +0100
commit30a7b5cb34e50da86fd7d4326a736709a7b1aab5 (patch)
treee7d46e16dd8a1427191002b9bbebde27e75eb065
parente1822da600607d469ff12be8673cd27bbebf7772 (diff)
add elemb, improve token checking
-rw-r--r--twasm/asm/main.asm184
1 files changed, 166 insertions, 18 deletions
diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm
index 1402364..9805b56 100644
--- a/twasm/asm/main.asm
+++ b/twasm/asm/main.asm
@@ -9,13 +9,17 @@ TEST_ARENA_SIZE equ 0x1000 ; maximum size tests can use
OUTPUT_ADDR equ 0x00070000 ; address of outputed binary
OUTPUT_SIZE equ 0x1000 ; max length of outputed binary
+STACK_ADDR equ 0x00060000 ; address to put the 64-bit stack at
+
[bits 64]
[org LOAD_ADDR]
start:
+ mov rsp, STACK_ADDR ; we might need more stack space, let's just be safe
+
mov rsi, msg_welcome
call print
-
+
call run_tests
jmp halt
@@ -41,21 +45,35 @@ start:
copy_token:
.loop:
- mov al, [rdi] ; check if next char is a space
- cmp al, " "
- je .break
- call copy_byte ; copy the next byte if not
+ mov dl, [rdi] ; move bit to compare to current byte in read buffer
- inc rdi ; increment read- and write-pointers
- inc rsi
- jmp .loop ; and loop again
+ push rdi ; push incrementors to call elemb
+ push rsi ;
+
+ mov rdi, 8 ; length of terminator list
+ mov rsi, token_terminator_8 ; start of terminator list
+ ; dl set before pushing rdi
+ call elemb
+
+ pop rsi ;
+ pop rdi ; pop incrementors after call
+
+ cmp rax, 1 ; check if the next character is a token terminator
+ je .break ; > if so, break the function
+
+ ; rdi and rsi set from previous loop iteration
+ call copy_byte ; if not, copy the current byte in read buffer
+
+ inc rdi ; read pointer
+ inc rsi ; write pointer
+
+ jmp .loop
.break:
- mov rax, rdi ; return values
- mov rdx, rsi
+ mov rax, rdi ; -> last byte read
+ mov rdx, rsi ; -> last byte written
ret
- .msg db "yo loop copy_token", 0x0D, 0x0A, 0x00
; ------------------------------------------------------------------------------
; copy_byte
@@ -119,6 +137,48 @@ halt:
jmp halt
; ------------------------------------------------------------------------------
+; elemb
+;
+; description:
+; checks if given byte is element of the specified list
+;
+; parameters:
+; rdi = size of list
+; rsi -> start of list
+; dl = given byte
+;
+; returned:
+; rax = 0: is not an element
+; 1: is an element
+; ------------------------------------------------------------------------------
+
+elemb:
+ .loop
+ cmp rdi, 0 ; check if remaining length 0
+ je .not_found ; if so, break; dl not an element of list
+
+ mov al, [rsi]
+ cmp al, dl ; check if current byte in list is the desired byte
+ je .found ; if so, break; dl an element of list
+
+ inc rsi ; move to next byte
+ dec rdi ; and reduce remaining length
+
+ jmp .loop
+
+ .not_found
+ xor rax, rax ; return 0; dl not an element of list
+ ret
+
+ .found
+ xor rax, rax
+ mov rax, 1 ; return 1; dl an element of list
+ ret
+
+ .f db "found", 0x0D, 0x0A, 0x00
+ .nf db "not found", 0x0D, 0x0A, 0x00
+
+; ------------------------------------------------------------------------------
; tests
; ------------------------------------------------------------------------------
@@ -135,6 +195,7 @@ run_tests:
call test_copy_byte
call test_copy_token
+ call test_elemb
ret
.msg db "running test suite...", 0x0D, 0x0A, 0x00
@@ -143,7 +204,7 @@ run_tests:
; test_copy_byte
;
; description:
-; tests copy_byte's described functionality
+; tests copy_byte described functionality
; ------------------------------------------------------------------------------
test_copy_byte:
@@ -183,21 +244,40 @@ test_copy_token:
mov rsi, .msg
call print
- mov rdi, test_token ; read buffer
- mov rsi, TEST_ARENA_ADDR ; write buffer
+ ; test case: space terminated
+
+ mov rdi, test_token_space ; read buffer
+ mov rsi, TEST_ARENA_ADDR ; write buffer
call copy_token
; check reported final indicies with the expected final indicies
- cmp rax, test_token + 8 ; last byte read
+ cmp rax, test_token_space + 8 ; last byte read
jne .fail
cmp rdx, TEST_ARENA_ADDR + 8 ; last byte written
jne .fail
mov rsi, TEST_ARENA_ADDR
mov rcx, [rsi]
- cmp rcx, [test_token] ; compare copied token to expected token
+ cmp rcx, [test_token_space] ; check if copied token matches expected token
+ jne .fail ; if not, fail
+
+ ; test case: null terminated
+
+ mov rdi, test_token_null ; read buffer
+ mov rsi, TEST_ARENA_ADDR ; write buffer
+ call copy_token
+
+ ; check reported final indicies with the expected final indicies
+ cmp rax, test_token_null + 8 ; last byte read
+ jne .fail
+ cmp rdx, TEST_ARENA_ADDR + 8 ; last byte written
jne .fail
+ mov rsi, TEST_ARENA_ADDR
+ mov rcx, [rsi]
+ cmp rcx, [test_token_null] ; check if copied token matches expected token
+ jne .fail ; if not, fail
+
.pass:
mov rsi, msg_pass
call print
@@ -209,6 +289,67 @@ test_copy_token:
.msg db "test_copy_token...", 0x00
; ------------------------------------------------------------------------------
+; test_elemb
+;
+; description:
+; tests elemb described functionality
+; ------------------------------------------------------------------------------
+
+test_elemb:
+ mov rsi, .msg
+ call print
+
+ ; [0]
+ mov rdi, 5
+ mov rsi, test_elemb_5
+ mov dl, [test_elemb_5]
+ call elemb
+ cmp al, 1
+ jne .fail
+
+ ; [n - 1]
+ mov rdi, 5
+ mov rsi, test_elemb_5
+ mov dl, [test_elemb_5 + 4]
+ call elemb
+ cmp al, 1
+ jne .fail
+
+ ; [1]
+ mov rdi, 5
+ mov rsi, test_elemb_5
+ mov dl, [test_elemb_5 + 1]
+ call elemb
+ cmp al, 1
+ jne .fail
+
+ ; not present
+ mov rdi, 5
+ mov rsi, test_elemb_5
+ mov dl, 0xDA
+ call elemb
+ cmp al, 0
+ jne .fail
+
+ ; 0 length list
+ mov rdi, 0
+ mov rsi, test_elemb_0
+ mov dl, 0x34
+ call elemb
+ cmp al, 0
+ jne .fail
+
+ .pass:
+ mov rsi, msg_pass
+ call print
+ ret
+ .fail:
+ mov rsi, msg_fail
+ call print
+ ret
+ .msg db "test_elemb...", 0x00
+
+; ------------------------------------------------------------------------------
; data
; ------------------------------------------------------------------------------
@@ -217,5 +358,12 @@ msg_halt db "halted.", 0x0D, 0x0A, 0x00
msg_pass db "passed.", 0x0D, 0x0A, 0x00
msg_fail db "failed.", 0x0D, 0x0A, 0x00
-test_byte db "T"
-test_token db "TestTokn " ; followed by space. Quad word
+test_byte db "Q" ; unterminated, just a byte chillin
+test_token_null db "TestTokn", 0x00 ; followed by null terminator. Quad word
+test_token_space db "TestTokn " ; followed by space. Quad word
+test_elemb_0
+test_elemb_5 db 0x54, 0x00, 0x21, 0x20, 0x34
+
+token_terminator_8 db 0x00, " ", 0x0A, 0x0D, 0x00, 0x00, 0x00, 0x00
+
+debug_string db "debug_string", 0x0D, 0x0A, 0x00