add trim_trailing_whitespace

This commit is contained in:
andromeda
2026-03-19 21:58:59 +01:00
parent ad6a79d937
commit a5fd811b3f

View File

@@ -648,13 +648,15 @@ tokenise:
je .operand_break je .operand_break
cmp dl, 0x00 cmp dl, 0x00
je .operand_break je .operand_break
cmp dl, ";"
je .operand_break
inc rax ; inc length counter inc rax ; inc length counter
inc rdi ; inc byte pointer inc rdi ; inc byte pointer
jmp .operand_loop jmp .operand_loop
.operand_break: .operand_break:
pop rdi ; rdi = first byte of operand pop rdi ; rdi -> first byte of operand
push rdi push rdi
push rsi push rsi
mov rsi, rax ; rsi = length of operand in bytes mov rsi, rax ; rsi = length of operand in bytes
@@ -747,7 +749,8 @@ tokenise:
; ;
; | code | rsi contents | notes | ; | code | rsi contents | notes |
; |------|----------------------|-------| ; |------|----------------------|-------|
; | 0x00 | token ID of register | | ; | 0x00 | token ID of register | reg |
; | 0x10 | token ID of register | [reg] |
; | 0xFF | - | error | ; | 0xFF | - | error |
; ;
; parameters: ; parameters:
@@ -760,10 +763,27 @@ tokenise:
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
evaluate_operand: evaluate_operand:
cmp rsi, 4 push rdi
jg .unrecognised
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: .register:
cmp rsi, 4
jg .unrecognised
push rdi push rdi
mov edi, [rdi] ; edi = register to be searched mov edi, [rdi] ; edi = register to be searched
@@ -779,6 +799,10 @@ evaluate_operand:
je .register1 je .register1
.register1: .register1:
and edi, 0xFF and edi, 0xFF
push rsi
mov rsi, .msg
call print.debug
pop rsi
.register2: .register2:
and edi, 0xFFFF and edi, 0xFFFF
.register3: .register3:
@@ -798,6 +822,7 @@ evaluate_operand:
.unrecognised: .unrecognised:
mov dl, 0xFF mov dl, 0xFF
ret ret
.msg db "evaluate_operand", 0x0A, 0x00
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
; evaluate_constant ; evaluate_constant
@@ -1174,6 +1199,47 @@ elemb:
mov rax, 1 ; return 1; dl an element of list mov rax, 1 ; return 1; dl an element of list
ret 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 ; clear_token_table
; ;
@@ -1484,6 +1550,5 @@ program:
db "inc rax ; inline comment", 0x0A db "inc rax ; inline comment", 0x0A
db "; one line comment", 0x0A db "; one line comment", 0x0A
db "mov [ rax ], rdx", 0x0A db "mov [ rax ], rdx", 0x0A
db "hlt" db "hlt", 0x00 ; for the sake of being able to print it, I made it a string
db 0x00 ; just for the sake of being able to print it, I made it a string
.size db $ - program .size db $ - program