reformat, add copy_token and test, fix bug

This commit is contained in:
andromeda
2026-03-06 22:16:26 +01:00
parent e5c715d54f
commit 00be2cc545
2 changed files with 91 additions and 30 deletions

View File

@@ -20,33 +20,44 @@ start:
jmp halt jmp halt
; ; ------------------------------------------------------------------------------
; tokenising ; tokenising
; ; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; copy_token ; copy_token
; ;
; description: ; description:
; copies a token from one spot in memory to another ; copies a token from one spot in memory to another
; and returns metadata about that token
; ;
; parameters: ; parameters:
; rdi -> start of asm command to be read ; rdi -> start of buffer to be read
; rsi -> start of buffer to be written ; rsi -> start of buffer to be written
; ;
; returned: ; returned:
copy_token: ; rax -> last byte read
mov rax, rdi ; rdx -> last byte written
.loop: ; ------------------------------------------------------------------------------
cmp al, " "
je .continue
jne .break
.continue:
call copy_byte
.break:
inc rax
ret
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 ; copy_byte
; ;
; description: ; description:
@@ -57,16 +68,20 @@ copy_token:
; rsi -> word to be written ; rsi -> word to be written
; ;
; returned: ; returned:
; al = byte that was read ; rax = byte that was read
; ------------------------------------------------------------------------------
copy_byte: copy_byte:
xor rax, rax ; zero out so it returns fine
mov al, [rdi] mov al, [rdi]
mov [rsi], al mov [rsi], al
ret ret
; ; ------------------------------------------------------------------------------
; utilities ; utilities
; ; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; print ; print
; ;
; description: ; description:
@@ -74,8 +89,11 @@ copy_byte:
; ;
; parameters: ; parameters:
; rsi -> start of null-terminated string ; rsi -> start of null-terminated string
; dx = port to output to ; ------------------------------------------------------------------------------
print: print:
push rdx
mov rdx, 0x3F8
.loop: .loop:
mov al, [rsi] mov al, [rsi]
test al, al test al, al
@@ -84,47 +102,50 @@ print:
inc rsi inc rsi
jmp .loop jmp .loop
.done: .done:
pop rdx
ret ret
; ------------------------------------------------------------------------------
; halt ; halt
; ;
; description: ; description:
; halts the program, silly :) ; halts the program, silly :)
; ------------------------------------------------------------------------------
halt: halt:
mov rsi, msg_halt mov rsi, msg_halt
call print call print
hlt hlt
jmp halt jmp halt
; ; ------------------------------------------------------------------------------
; tests ; tests
; ; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; run_tests ; run_tests
; ;
; description: ; description:
; runs all tests ; runs all tests
; ; ------------------------------------------------------------------------------
; parameters:
; dx = port to output to
run_tests: run_tests:
mov rsi, .msg mov rsi, .msg
call print call print
call test_copy_byte call test_copy_byte
call test_copy_token
ret ret
.msg db "running test suite...", 0x0D, 0x0A, 0x00 .msg db "running test suite...", 0x0D, 0x0A, 0x00
; ------------------------------------------------------------------------------
; test_copy_byte ; test_copy_byte
; ;
; description: ; description:
; makes sure copy_byte function works by testing the following ; tests copy_byte's described functionality
; - that it copies one byte to a specified memory address ; ------------------------------------------------------------------------------
; - that it returns the copied byte in al
;
; parameters:
; dx = port to output to
test_copy_byte: test_copy_byte:
mov rsi, .msg mov rsi, .msg
call print call print
@@ -134,9 +155,12 @@ test_copy_byte:
call copy_byte call copy_byte
mov cx, [rsi] mov cx, [rsi]
and ax, 0xFF ; only compare bottom byte
and cx, 0xFF
cmp ax, cx ; compare returned byte to copied byte cmp ax, cx ; compare returned byte to copied byte
jne .fail jne .fail
je .pass cmp al, [test_byte] ; compare returned byte to expected byte
jne .fail
.pass: .pass:
mov rsi, msg_pass mov rsi, msg_pass
@@ -148,9 +172,45 @@ test_copy_byte:
ret ret
.msg db "test_copy_byte...", 0x00 .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 ; data
; ; ------------------------------------------------------------------------------
msg_welcome db "Welcome to Twasm", 0x0D, 0x0A, 0x00 msg_welcome db "Welcome to Twasm", 0x0D, 0x0A, 0x00
msg_halt db "halted.", 0x0D, 0x0A, 0x00 msg_halt db "halted.", 0x0D, 0x0A, 0x00
@@ -158,4 +218,4 @@ msg_pass db "passed.", 0x0D, 0x0A, 0x00
msg_fail db "failed.", 0x0D, 0x0A, 0x00 msg_fail db "failed.", 0x0D, 0x0A, 0x00
test_byte db "T" test_byte db "T"
test_token db "Test Token" ; two tokens, one followed by a space and one by nothing test_token db "TestTokn " ; followed by space. Quad word

View File

@@ -28,6 +28,7 @@ stdenv.mkDerivation {
${qemu}/bin/qemu-system-x86_64 \ ${qemu}/bin/qemu-system-x86_64 \
-nographic \ -nographic \
-s \
-drive file=./.bootle/disk,format=raw,index=0,media=disk -drive file=./.bootle/disk,format=raw,index=0,media=disk
rm ./.bootle -r rm ./.bootle -r