summaryrefslogtreecommitdiff
path: root/twasm/asm/tests.asm
blob: 96276c7030c333442f90f2fdcbcd5bdcbf8f0c26 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
; ------------------------------------------------------------------------------
; tests
; ------------------------------------------------------------------------------

; ------------------------------------------------------------------------------
; run_tests
;
; description:
; runs all tests
; ------------------------------------------------------------------------------

run_tests:
  mov rsi, .msg
  call print.test

  call clear_test_arena
  call test_copy_byte

  call clear_test_arena
  call test_copy_token

  call clear_test_arena
  call test_elemb

  call clear_test_arena
  call test_identify_token

  call clear_test_arena
  call test_identify_next_token

  call clear_test_arena
  call test_get_tte_type

  call clear_test_arena
  call test_get_tte_typed_metadata

  call clear_test_arena
  call test_get_direct_addressing_ModRM

  call clear_test_arena
  call test_get_opcode

  call clear_test_arena
  call test_get_reg_bits

  ret
  .msg db "running test suite...", 0x0A, 0x00

; ------------------------------------------------------------------------------
; test_copy_byte
;
; description:
; tests copy_byte described functionality
; ------------------------------------------------------------------------------

test_copy_byte:
  mov rsi, .msg
  call print.test

  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.test

  ; test case: space terminated

  mov rdi, test_token_space ; 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_space + 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_space] ; check if copied token matches expected token
  jne .fail                   ; if not, fail

  ; test case: null terminated

  mov rdi, test_token_null ; 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_null + 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_null] ; check if copied token matches expected token
  jne .fail                  ; if not, fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_copy_token...", 0x00

; ------------------------------------------------------------------------------
; test_elemb
;
; description:
; tests elemb described functionality
; ------------------------------------------------------------------------------

test_elemb:
  mov rsi, .msg
  call print.test

  ; [0]
  mov rdi, 5
  mov rsi, test_elemb_5
  mov dl, [test_elemb_5]
  call elemb
  cmp al, 1
  jne .fail

  ; [n - 1]
  mov rdi, 5
  mov rsi, test_elemb_5
  mov dl, [test_elemb_5 + 4]
  call elemb
  cmp al, 1
  jne .fail

  ; [1]
  mov rdi, 5
  mov rsi, test_elemb_5
  mov dl, [test_elemb_5 + 1]
  call elemb
  cmp al, 1
  jne .fail

  ; not present
  mov rdi, 5
  mov rsi, test_elemb_5
  mov dl, 0xDA
  call elemb
  cmp al, 0
  jne .fail

  ; 0 length list
  mov rdi, 0
  mov rsi, test_elemb_0
  mov dl, 0x34
  call elemb
  cmp al, 0
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_elemb...", 0x00

; ------------------------------------------------------------------------------
; test_identify_token
;
; description:
; tests identify_token described functionality
; ------------------------------------------------------------------------------

test_identify_token:
  mov rsi, .msg
  call print.test

  ; length1 token that exists
  mov byte [TEST_ARENA_ADDR], "*"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 1
  call identify_token
  cmp ax, 0x0064
  jne .fail

  ; length1 token that doesn't exist
  mov byte [TEST_ARENA_ADDR], " "
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 1
  call identify_token
  cmp ax, 0xFFFF
  jne .fail

  ; length2 token that exists
  mov word [TEST_ARENA_ADDR], "sp"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 2
  call identify_token
  cmp ax, 0x0026
  jne .fail

  ; length2 token that doesn't exist
  mov word [TEST_ARENA_ADDR], "QQ"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 2
  call identify_token
  cmp ax, 0xFFFF
  jne .fail

  ; length3 token that exists
  mov dword [TEST_ARENA_ADDR], "rax"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 3
  call identify_token
  cmp ax, 0x0000
  jne .fail

  ; length3 token that exists
  mov dword [TEST_ARENA_ADDR], "cr0"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 3
  call identify_token
  cmp ax, 0x004A
  jne .fail

  ; length3 token that doesn't exist
  mov dword [TEST_ARENA_ADDR], "r16"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 3
  call identify_token
  cmp ax, 0xFFFF
  jne .fail

  ; length4 token that exists
  mov dword [TEST_ARENA_ADDR], "r10d"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 4
  call identify_token
  cmp ax, 0x001A
  jne .fail

  ; length4 token that exists
  mov dword [TEST_ARENA_ADDR], "r15b"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 4
  call identify_token
  cmp ax, 0x003F
  jne .fail

  ; length4 token that doesn't exist
  mov dword [TEST_ARENA_ADDR], "r15q"
  mov rdi, TEST_ARENA_ADDR
  mov rsi, 4
  call identify_token
  cmp ax, 0xFFFF
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_identify_token...", 0x00

; ------------------------------------------------------------------------------
; test_identify_next_token
;
; description:
; tests identify_next_token described functionality
; ------------------------------------------------------------------------------

test_identify_next_token:
  mov rsi, .msg
  call print.test

  ; length1 token that exists
  mov word [TEST_ARENA_ADDR], "* "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x0064
  jne .fail

  ; length1 token that doesn't exist
  mov word [TEST_ARENA_ADDR], "  "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0xFFFF
  jne .fail

  ; length2 token that exists
  mov dword [TEST_ARENA_ADDR], "sp  "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x0026
  jne .fail

  ; length2 token that doesn't exist
  mov dword [TEST_ARENA_ADDR], "QQ  "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0xFFFF
  jne .fail

  ; length3 token that exists
  mov dword [TEST_ARENA_ADDR], "rax "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x0000
  jne .fail

  ; length3 token that exists
  mov dword [TEST_ARENA_ADDR], "cr0 "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x004A
  jne .fail

  ; length3 token that doesn't exist
  mov dword [TEST_ARENA_ADDR], "r16 "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0xFFFF
  jne .fail

  ; length4 token that exists
  mov dword [TEST_ARENA_ADDR], "r10d"
  mov byte [TEST_ARENA_ADDR + 4], " "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x001A
  jne .fail

  ; length4 token that exists
  mov dword [TEST_ARENA_ADDR], "r15b"
  mov byte [TEST_ARENA_ADDR + 4], " "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0x003F
  jne .fail

  ; length4 token that doesn't exist
  mov dword [TEST_ARENA_ADDR], "r15q"
  mov byte [TEST_ARENA_ADDR + 4], " "
  mov rdi, TEST_ARENA_ADDR
  call identify_next_token
  cmp ax, 0xFFFF
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_identify_next_token...", 0x00


; ------------------------------------------------------------------------------
; test_get_tte_type
;
; description:
; tests get_tte_type described functionality
; ------------------------------------------------------------------------------

test_get_tte_type:
  mov rsi, .msg
  call print.test

  mov di, 0x0053    ; xor
  call get_tte_type
  cmp al, 0x01      ; operator
  jne .fail

  mov di, 0x0003    ; rdx
  call get_tte_type
  cmp al, 0x02      ; register
  jne .fail

  mov di, 0x0056    ; mov
  call get_tte_type
  cmp al, 0x01      ; operator
  jne .fail

  mov di, 0xFFFF    ; unrecognised token
  call get_tte_type
  cmp al, UNRECOGNISED_ID_TYPE
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_get_tte_type...", 0x00

; ------------------------------------------------------------------------------
; test_get_tte_typed_metadata
;
; description:
; tests get_tte_typed_metadata described functionality
; ------------------------------------------------------------------------------

test_get_tte_typed_metadata:
  mov rsi, .msg
  call print.test

  mov di, 0x0053              ; xor
  call get_tte_typed_metadata
  cmp al, 0x02                ; # operands
  jne .fail

  mov di, 0x0003              ; rdx
  call get_tte_typed_metadata
  cmp al, 00001011b           ; reg: 010b
                              ; width: 11b (64 bits)
  jne .fail

  mov di, 0x0056              ; mov
  call get_tte_typed_metadata
  cmp al, 0x02                ; # operands
  jne .fail

  mov di, 0xFFFF              ; unrecognised token
  call get_tte_typed_metadata
  cmp al, UNRECOGNISED_ID_METADATA
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_get_tte_typed_metadata...", 0x00

; ------------------------------------------------------------------------------
; test_get_direct_addressing_ModRM
;
; description:
; tests get_direct_addressing_ModRM described functionality
; ------------------------------------------------------------------------------

test_get_direct_addressing_ModRM:
  mov rsi, .msg
  call print.test

  mov di, 0x0000 ; rax
  mov si, 0x0000 ; rax
  call get_direct_addressing_ModRM
  cmp al, 11000000b ; Mod Reg R/M: 11b 000b 000b
  jne .fail

  mov di, 0x0000 ; rax
  mov si, 0x0003 ; rdx
  call get_direct_addressing_ModRM
  cmp al, 11000010b ; Mod Reg R/M: 11b 000b 010b
  jne .fail

  mov di, 0x0003 ; rdx
  mov si, 0x0000 ; rax
  call get_direct_addressing_ModRM
  cmp al, 11010000b ; Mod Reg R/M: 11b 010b 000b
  jne .fail

  mov di, 0x0003 ; rdx
  mov si, 0x0003 ; rdx
  call get_direct_addressing_ModRM
  cmp al, 11010010b ; Mod Reg R/M 11b 010b 010b
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_get_direct_addressing_ModRM...", 0x00

; ------------------------------------------------------------------------------
; test_get_opcode
;
; description:
; tests get_opcode described functionality
; ------------------------------------------------------------------------------

test_get_opcode:
  mov rsi, .msg
  call print.test

  mov di, 0x0053 ; xor
  call get_opcode
  cmp al, 0x31
  jne .fail

  mov di, 0x0054  ; inc
  call get_opcode
  cmp al, 0xFF
  jne .fail

  mov di, 0x004F  ; hlt
  call get_opcode
  cmp al, 0xF4
  jne .fail

  mov di, 0x0003  ; rdx (not an operator)
  call get_opcode
  cmp al, UNRECOGNISED_ID_OPCODE
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_get_opcode...", 0x00

; ------------------------------------------------------------------------------
; test_get_reg_bits
;
; description:
; tests get_reg_bits described functionality
; ------------------------------------------------------------------------------

test_get_reg_bits:
  mov rsi, .msg
  call print.test

  mov di, 0x0000    ; rax
  call get_reg_bits
  cmp al, 000b
  jne .fail

  mov di, 0x0010    ; eax
  call get_reg_bits
  cmp al, 000b
  jne .fail

  mov di, 0x0003    ; rdx
  call get_reg_bits
  cmp al, 010b
  jne .fail

  .pass:
    mov rsi, msg_pass
    call print
    ret
  .fail:
    mov rsi, msg_fail
    call print
    ret
  .msg db "test_get_reg_bits...", 0x00

msg_pass:
  db 0x0A
  times (TEST_LINE_LENGTH + .start - .end) db " ", ; right align
  .start db "passed."
  .end db 0x0A, 0x00
msg_fail:
  db 0x0A
  times (TEST_LINE_LENGTH + .start - .end) db " ",
  .start db "failed."
  .end db 0x0A, 0x00

test_byte db "Q" ; unterminated, just a byte chillin
test_token_null db "TestTokn", 0x00 ; followed by null terminator. Quad word
test_token_space db "TestTokn " ; followed by space. Quad word
test_elemb_0: ; [This Page Intentionally Left Blank]
test_elemb_5 db 0x54, 0x00, 0x21, 0x20, 0x34