222 lines
5.2 KiB
NASM
222 lines
5.2 KiB
NASM
LOAD_ADDR equ 0x00010000 ; address this program is loaded at
|
|
|
|
TOKEN_TABLE_ADDR equ 0x00060000 ; address the token table is loaded at
|
|
TOKEN_TABLE_SIZE equ 0x1000 ; max length of table
|
|
|
|
TEST_ARENA_ADDR equ 0x00060000 ; address to run tests at
|
|
TEST_ARENA_SIZE equ 0x1000 ; maximum size tests can use
|
|
|
|
OUTPUT_ADDR equ 0x00070000 ; address of outputed binary
|
|
OUTPUT_SIZE equ 0x1000 ; max length of outputed binary
|
|
|
|
[bits 64]
|
|
[org LOAD_ADDR]
|
|
|
|
start:
|
|
mov rsi, msg_welcome
|
|
call print
|
|
|
|
call run_tests
|
|
|
|
jmp halt
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; tokenising
|
|
; ------------------------------------------------------------------------------
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; copy_token
|
|
;
|
|
; description:
|
|
; copies a token from one spot in memory to another
|
|
;
|
|
; parameters:
|
|
; rdi -> start of buffer to be read
|
|
; rsi -> start of buffer to be written
|
|
;
|
|
; returned:
|
|
; rax -> last byte read
|
|
; rdx -> last byte written
|
|
; ------------------------------------------------------------------------------
|
|
|
|
copy_token:
|
|
.loop:
|
|
mov al, [rdi] ; check if next char is a space
|
|
cmp al, " "
|
|
je .break
|
|
|
|
call copy_byte ; copy the next byte if not
|
|
|
|
inc rdi ; increment read- and write-pointers
|
|
inc rsi
|
|
jmp .loop ; and loop again
|
|
|
|
.break:
|
|
mov rax, rdi ; return values
|
|
mov rdx, rsi
|
|
ret
|
|
.msg db "yo loop copy_token", 0x0D, 0x0A, 0x00
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; copy_byte
|
|
;
|
|
; description:
|
|
; copies a byte from one spot in memory to another
|
|
;
|
|
; parameters:
|
|
; rdi -> word to be read
|
|
; rsi -> word to be written
|
|
;
|
|
; returned:
|
|
; al = byte that was read; the rest of rax is zeroed
|
|
; ------------------------------------------------------------------------------
|
|
|
|
copy_byte:
|
|
xor rax, rax ; zero out so it returns fine
|
|
mov al, [rdi]
|
|
mov [rsi], al
|
|
ret
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; utilities
|
|
; ------------------------------------------------------------------------------
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; print
|
|
;
|
|
; description:
|
|
; prints a null-terminated string
|
|
;
|
|
; parameters:
|
|
; rsi -> start of null-terminated string
|
|
; ------------------------------------------------------------------------------
|
|
|
|
print:
|
|
push rdx
|
|
mov rdx, 0x3F8
|
|
.loop:
|
|
mov al, [rsi]
|
|
test al, al
|
|
jz .done
|
|
out dx, al
|
|
inc rsi
|
|
jmp .loop
|
|
.done:
|
|
pop rdx
|
|
ret
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; halt
|
|
;
|
|
; description:
|
|
; halts the program, silly :)
|
|
; ------------------------------------------------------------------------------
|
|
|
|
halt:
|
|
mov rsi, msg_halt
|
|
call print
|
|
hlt
|
|
jmp halt
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; tests
|
|
; ------------------------------------------------------------------------------
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; run_tests
|
|
;
|
|
; description:
|
|
; runs all tests
|
|
; ------------------------------------------------------------------------------
|
|
|
|
run_tests:
|
|
mov rsi, .msg
|
|
call print
|
|
|
|
call test_copy_byte
|
|
call test_copy_token
|
|
|
|
ret
|
|
.msg db "running test suite...", 0x0D, 0x0A, 0x00
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; test_copy_byte
|
|
;
|
|
; description:
|
|
; tests copy_byte's described functionality
|
|
; ------------------------------------------------------------------------------
|
|
|
|
test_copy_byte:
|
|
mov rsi, .msg
|
|
call print
|
|
|
|
mov rdi, test_byte ; byte to be copied
|
|
mov rsi, TEST_ARENA_ADDR ; location of test
|
|
call copy_byte
|
|
|
|
mov cx, [rsi]
|
|
and ax, 0xFF ; only compare bottom byte
|
|
and cx, 0xFF
|
|
cmp ax, cx ; compare returned byte to copied byte
|
|
jne .fail
|
|
cmp al, [test_byte] ; compare returned byte to expected byte
|
|
jne .fail
|
|
|
|
.pass:
|
|
mov rsi, msg_pass
|
|
call print
|
|
ret
|
|
.fail:
|
|
mov rsi, msg_fail
|
|
call print
|
|
ret
|
|
.msg db "test_copy_byte...", 0x00
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; test_copy_token
|
|
;
|
|
; description:
|
|
; tests copy_token described functionality
|
|
; ------------------------------------------------------------------------------
|
|
|
|
test_copy_token:
|
|
mov rsi, .msg
|
|
call print
|
|
|
|
mov rdi, test_token ; read buffer
|
|
mov rsi, TEST_ARENA_ADDR ; write buffer
|
|
call copy_token
|
|
|
|
; check reported final indicies with the expected final indicies
|
|
cmp rax, test_token + 8 ; last byte read
|
|
jne .fail
|
|
cmp rdx, TEST_ARENA_ADDR + 8 ; last byte written
|
|
jne .fail
|
|
|
|
mov rsi, TEST_ARENA_ADDR
|
|
mov rcx, [rsi]
|
|
cmp rcx, [test_token] ; compare copied token to expected token
|
|
jne .fail
|
|
|
|
.pass:
|
|
mov rsi, msg_pass
|
|
call print
|
|
ret
|
|
.fail:
|
|
mov rsi, msg_fail
|
|
call print
|
|
ret
|
|
.msg db "test_copy_token...", 0x00
|
|
|
|
; ------------------------------------------------------------------------------
|
|
; data
|
|
; ------------------------------------------------------------------------------
|
|
|
|
msg_welcome db "Welcome to Twasm", 0x0D, 0x0A, 0x00
|
|
msg_halt db "halted.", 0x0D, 0x0A, 0x00
|
|
msg_pass db "passed.", 0x0D, 0x0A, 0x00
|
|
msg_fail db "failed.", 0x0D, 0x0A, 0x00
|
|
|
|
test_byte db "T"
|
|
test_token db "TestTokn " ; followed by space. Quad word
|