1 # Rudimentary test harness
 2 
 3 == code
 4 #   instruction                     effective address                                                   register    displacement    immediate
 5 # . op          subop               mod             rm32          base        index         scale       r32
 6 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 7 
 8 # main:  (manual test if this is the last file loaded)
 9     # check-ints-equal(34, 34)
10     # . . push args
11     68/push  "error in check-ints-equal"/imm32
12     68/push  34/imm32
13     68/push  34/imm32
14     # . . call
15     e8/call  check-ints-equal/disp32
16     # . . discard args
17     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
18     # syscall(exit, 0)
19     bb/copy-to-EBX  0/imm32
20     b8/copy-to-EAX  1/imm32/exit
21     cd/syscall  0x80/imm8
22 
23 # print msg to stderr if a != b, otherwise print "."
24 check-ints-equal:  # (a : int, b : int, msg : (address array byte)) -> boolean
25     # . prolog
26     55/push-EBP
27     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
28     # . save registers
29     51/push-ECX
30     53/push-EBX
31     # load first 2 args into EAX and EBX
32     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           0/r32/EAX   0x8/disp8       .                 # copy *(EBP+8) to EAX
33     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
34     # if EAX == EBX success
35     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX and EBX
36     75/jump-if-unequal  $check-ints-equal:else/disp8
37     # . _write(2/stderr, '.')
38     # . . push args
39     68/push  "."/imm32
40     68/push  2/imm32/stderr
41     # . . call
42     e8/call  _write/disp32
43     # . . discard args
44     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
45     # . return
46     eb/jump  $check-ints-equal:end/disp8
47     # otherwise print error message
48 $check-ints-equal:else:
49     # . _write(2/stderr, msg)
50     # . . push args
51     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           1/r32/ECX   0x10/disp8      .                 # copy *(EBP+16) to ECX
52     51/push-ECX
53     68/push  2/imm32/stderr
54     # . . call
55     e8/call  _write/disp32
56     # . . discard args
57     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
58     # . _write(2/stderr, Newline)
59     # . . push args
60     68/push  Newline/imm32
61     68/push  2/imm32/stderr
62     # . . call
63     e8/call  _write/disp32
64     # . . discard args
65     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
66     # increment Num-test-failures
67     ff          0/subop/increment   0/mod/indirect  5/rm32/.disp32            .             .           .           Num-test-failures/disp32          # increment *Num-test-failures
68 $check-ints-equal:end:
69     # . restore registers
70     5b/pop-to-EBX
71     59/pop-to-ECX
72     # . epilog
73     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
74     5d/pop-to-EBP
75     c3/return
76 
77 == data
78 
79 Newline:
80     # size
81     01 00 00 00
82     # data
83     0a/newline
84 
85 Num-test-failures:
86     00 00 00 00
87 
88 # . . vim:nowrap:textwidth=0