correctly handle [register]s as operands

This commit is contained in:
andromeda
2026-03-21 21:42:50 +01:00
parent 73ea2bb2b5
commit 55c426631b
2 changed files with 31 additions and 18 deletions

View File

@@ -470,7 +470,7 @@ tokenise:
.loop:
cmp rdi, rsi ; if current byte greater than last byte
jg .break ; then break
jge .break ; then break
mov dl, [rdi] ; dl = current byte
@@ -488,8 +488,8 @@ tokenise:
push rax
push rdx
; TODO probably should not ignore null bytes
mov rsi, whitespace_3 ; rsi -> list of whitespace (ignored) bytes
mov rdi, 3 ; rdi = size of list in bytes
mov rsi, whitespace_2 ; rsi -> list of whitespace (ignored) bytes
mov rdi, 2 ; rdi = size of list in bytes
; dl = current byte
call elemb
; al = 0 if not whitespace, 1 if whitespace
@@ -671,18 +671,32 @@ tokenise:
mov rcx, rax ; rcx = evaluate_operand's return value
pop rax ; rax = number of tokens processed
; operand is a register
; operand is some reg
; cx = token ID
cmp dl, 0x00
je .operand_register
; operand is some [reg]
; cx = token ID
cmp dl, 0x10
je .operand_addr_register
jmp .unexpected_operand
; cx = token ID
.operand_register:
mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], cx
inc rax ; another token processed
jmp .operand_break_continue
; cx = token ID
.operand_addr_register
mov word [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], 0x1000
inc rax ; 0x1000: addr reg token, next token is the register
mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], cx
inc rax ; the register as returned by evaluate_operand
jmp .operand_break_continue
.operand_break_continue:
mov byte [.expecting], E_COMMENT | E_NEWLINE | E_WHITESPACE | E_COMMA
jmp .loop
@@ -833,8 +847,6 @@ evaluate_operand:
mov dl, 0xFF
ret
.msg db "evaluate_operand", 0x0A, 0x00
; ------------------------------------------------------------------------------
; evaluate_constant
;
@@ -1232,8 +1244,8 @@ trim_trailing_whitespace:
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
mov rsi, whitespace_2 ; pointer of whitespace list
mov rdi, 2 ; length of whitespace list
call elemb
pop rsi ; rsi -> start of list
@@ -1553,7 +1565,7 @@ msg_halt db "halted.", 0x0A, 0x00
token_terminator_8 db 0x00, " ", 0x0A, 0x0D, ",", 0x00, 0x00, 0x00
whitespace_3 db " ", 0x0D, 0x00
whitespace_2 db " ", 0x0D
; test program
program:
@@ -1561,5 +1573,5 @@ program:
db "inc rax ; inline comment", 0x0A
db "; one line comment", 0x0A
db "mov [rax], rdx", 0x0A
db "hlt", 0x00 ; for the sake of being able to print it, I made it a string
db "hlt"
.size db $ - program