1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
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
; and returns metadata about that token
;
; parameters:
; rdi -> start of asm command to be read
; rsi -> start of buffer to be written
;
; returned:
copy_token:
mov rax, rdi
.loop:
cmp al, " "
je .continue
jne .break
.continue:
call copy_byte
.break:
inc rax
ret
; 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
copy_byte:
mov al, [rdi]
mov [rsi], al
ret
;
; utilities
;
; print
;
; description:
; prints a null-terminated string
;
; parameters:
; rsi -> start of null-terminated string
; dx = port to output to
print:
.loop:
mov al, [rsi]
test al, al
jz .done
out dx, al
inc rsi
jmp .loop
.done:
ret
; halt
;
; description:
; halts the program, silly :)
halt:
mov rsi, msg_halt
call print
hlt
jmp halt
;
; tests
;
; run_tests
;
; description:
; runs all tests
;
; parameters:
; dx = port to output to
run_tests:
mov rsi, .msg
call print
call test_copy_byte
ret
.msg db "running test suite...", 0x0D, 0x0A, 0x00
; test_copy_byte
;
; description:
; makes sure copy_byte function works by testing the following
; - 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:
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]
cmp ax, cx ; compare returned byte to copied byte
jne .fail
je .pass
.pass:
mov rsi, msg_pass
call print
ret
.fail:
mov rsi, msg_fail
call print
ret
.msg db "test_copy_byte...", 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 "Test Token" ; two tokens, one followed by a space and one by nothing
|