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