From a5fd811b3f14a4fc888f390d4ff48ccac5664054 Mon Sep 17 00:00:00 2001 From: andromeda Date: Thu, 19 Mar 2026 21:58:59 +0100 Subject: [PATCH] add trim_trailing_whitespace --- twasm/asm/main.asm | 77 ++++++++++++++++++++++++++++++++++++++++++---- 1 file 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 @@ -1174,6 +1199,47 @@ elemb: mov rax, 1 ; return 1; dl an element of list 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 ; @@ -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