summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandromeda <andromeda@lenovo>2026-03-19 21:58:59 +0100
committerandromeda <andromeda@lenovo>2026-03-19 21:58:59 +0100
commita5fd811b3f14a4fc888f390d4ff48ccac5664054 (patch)
treeb18d159904925462c0230961f979d02407d9aecd
parentad6a79d9376144cf6261f0d3c3558637ba3e8d19 (diff)
add trim_trailing_whitespace
-rw-r--r--twasm/asm/main.asm77
1 files changed, 71 insertions, 6 deletions
diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm
index 29095c0..fe08d48 100644
--- a/twasm/asm/main.asm
+++ b/twasm/asm/main.asm
@@ -648,13 +648,15 @@ tokenise:
je .operand_break
cmp dl, 0x00
je .operand_break
+ cmp dl, ";"
+ je .operand_break
inc rax ; inc length counter
inc rdi ; inc byte pointer
jmp .operand_loop
.operand_break:
- pop rdi ; rdi = first byte of operand
+ pop rdi ; rdi -> first byte of operand
push rdi
push rsi
mov rsi, rax ; rsi = length of operand in bytes
@@ -747,7 +749,8 @@ tokenise:
;
; | code | rsi contents | notes |
; |------|----------------------|-------|
-; | 0x00 | token ID of register | |
+; | 0x00 | token ID of register | reg |
+; | 0x10 | token ID of register | [reg] |
; | 0xFF | - | error |
;
; parameters:
@@ -760,10 +763,27 @@ tokenise:
; ------------------------------------------------------------------------------
evaluate_operand:
- cmp rsi, 4
- jg .unrecognised
+ push rdi
+
+ push rsi
+ mov rsi, rdi ; rsi -> start of operand
+ pop rdi ; rdi = size of operand
+ call trim_trailing_whitespace
+
+ pop rdi ; rdi -> first byte of operand
+ mov rsi, rax ; rsi = size of operand w/o trailing whitespace
+
+ cmp [rdi + 1], '['
+ je .address
+
+ jmp .register
+
+ .address:
+ jmp halt
.register:
+ cmp rsi, 4
+ jg .unrecognised
push rdi
mov edi, [rdi] ; edi = register to be searched
@@ -779,6 +799,10 @@ evaluate_operand:
je .register1
.register1:
and edi, 0xFF
+ push rsi
+ mov rsi, .msg
+ call print.debug
+ pop rsi
.register2:
and edi, 0xFFFF
.register3:
@@ -798,6 +822,7 @@ evaluate_operand:
.unrecognised:
mov dl, 0xFF
ret
+ .msg db "evaluate_operand", 0x0A, 0x00
; ------------------------------------------------------------------------------
; evaluate_constant
@@ -1175,6 +1200,47 @@ elemb:
ret
; ------------------------------------------------------------------------------
+; trim_trailing_whitespace
+;
+; description:
+; trims whitespace from the start and end of the given byte array.
+;
+; parameters:
+; rdi = size of list
+; rsi -> start of list
+;
+; returned:
+; rax = new size of list
+; ------------------------------------------------------------------------------
+
+trim_trailing_whitespace:
+ cmp rdi, 0 ; list of length zero
+ je .done ; already trimmed
+
+ push rdi
+ push rsi
+
+ mov dl, [rsi + rdi - 1] ; last element of given list
+ mov rsi, whitespace_3 ; pointer of whitespace list
+ mov rdi, 3 ; length of whitespace list
+ call elemb
+
+ pop rsi ; rsi -> start of list
+ pop rdi ; rdi = size of list
+
+ cmp al, 0 ; if last element whitespace
+ je .done ; then break
+
+ .trim ; otherwise one shorter
+ dec rdi
+ call trim_trailing_whitespace
+ ret
+
+ .done
+ mov rax, rdi
+ ret
+
+; ------------------------------------------------------------------------------
; clear_token_table
;
; description:
@@ -1484,6 +1550,5 @@ program:
db "inc rax ; inline comment", 0x0A
db "; one line comment", 0x0A
db "mov [ rax ], rdx", 0x0A
- db "hlt"
- db 0x00 ; just for the sake of being able to print it, I made it a string
+ db "hlt", 0x00 ; for the sake of being able to print it, I made it a string
.size db $ - program