From 2e222c304abd45dc33c252da757119e588652100 Mon Sep 17 00:00:00 2001 From: andromeda Date: Wed, 18 Mar 2026 20:26:30 +0100 Subject: add evaluate_operand, fix operator tokenising --- twasm/asm/main.asm | 102 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 28 deletions(-) (limited to 'twasm/asm/main.asm') diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm index 4b23759..8b2d7ef 100644 --- a/twasm/asm/main.asm +++ b/twasm/asm/main.asm @@ -578,8 +578,8 @@ tokenise: pop rsi push rax - mov dword [.pending_operator], 0 ; zero pending operator - xor eax, eax ; eax = number of bytes in operator + xor eax, eax ; eax = number of bytes in operator + mov [.pending_operator], eax ; zero pending operator .operator_loop: ; TODO give this its own error @@ -606,35 +606,19 @@ tokenise: jmp .operator_loop ; and loop .operator_break: - pop rax - - push rcx - mov rcx, tokens.operators ; rcx -> entry in lookup table - - .operator_id_loop: - cmp rcx, tokens.operators_end ; check if index still in range - ; TODO give own error - jg .unexpected_operator ; if not, error - - ; TODO use something other than r8 and r9 - mov r8d, [rcx] - mov r9d, [.pending_operator] - cmp r8d, r9d - je .found_id - - add rcx, 6 ; next entry - - jmp .operator_id_loop + ; rax already pushed from .operator + push rdi - .found_id: - push rdx - mov dx, [rcx + 4] ; dx = token id + mov edi, [.pending_operator] ; edi = operator to be searched + call identify_operator + ; ax = operator's token ID + mov cx, ax ; cx = operator's token ID for safe keeping - mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], dx ; write to token - inc rax ; table + pop rdi ; rdi = byte counter + pop rax ; rax = tokens processed - pop rdx - pop rcx + mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], cx + inc rax ; plus 1 token processed mov byte [.expecting], E_COMMENT | E_NEWLINE | E_WHITESPACE | E_OPERAND jmp .loop @@ -650,6 +634,7 @@ tokenise: test byte [.expecting], E_OPERAND ; make sure an operand was expected jz .unexpected_operand ; if not, error + .operand_loop: mov dl, [rdi] @@ -720,6 +705,67 @@ tokenise: .msg_operand db "operand.", 0x0A, 0x00 .pending_operator dd 0 ; the operator token that is pending processing +; ------------------------------------------------------------------------------ +; evaluate_operand +; +; description: +; takes the location and length of an operand and evaluates it into binary data +; and a return code to interpret the binary data. +; +; | code | rsi contents | notes | +; |------|----------------------|-------| +; | 0x00 | token ID of register | | +; | 0xFF | - | error | +; +; parameters: +; rdi -> first byte of operand +; rsi = size of operand in bytes +; +; returned: +; rax = binary data corresponding to the operand +; dl = return code +; ------------------------------------------------------------------------------ + +evaluate_operand: + cmp rsi, 4 + jg .unrecognised + + .register: + push rdi + mov edi, [rdi] ; edi = register to be searched + + ; TODO figure out how to mask elegantly :/ + ; mask edi for lower rsi bits + cmp rsi, 4 + je .register4 + cmp rsi, 3 + je .register3 + cmp rsi, 2 + je .register2 + cmp rsi, 1 + je .register1 + .register1: + and edi, 0xFF + .register2: + and edi, 0xFFFF + .register3: + and edi, 0xFFFFFF + .register4: + + call identify_register + ; ax = register's token ID or UNRECOGNISED_TOKEN_ID + pop rdi + + cmp ax, UNRECOGNISED_TOKEN_ID + je .unrecognised + + mov dl, 0x00 + ret + + .unrecognised: + mov dl, 0xFF + ret + ; ------------------------------------------------------------------------------ ; evaluate_constant ; -- cgit v1.3.1