https://github.com/akkartik/mu/blob/master/subx/examples/ex10.subx
 1 # String comparison: return 1 iff the two args passed in at the commandline are equal.
 2 #
 3 # To run (from the subx directory):
 4 #   $ subx translate examples/ex10.subx -o examples/ex10
 5 #   $ subx run examples/ex10 abc abd
 6 # Expected result:
 7 #   $ echo $?
 8 #   0  # false
 9 
10 == code
11 #   instruction                     effective address                                                   register    displacement    immediate
12 # . op          subop               mod             rm32          base        index         scale       r32
13 # . 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
14 
15 # main: return argv-equal(argv[1], argv[2])
16 #       At the start of a SubX program:
17 #         argc: *ESP
18 #         argv[0]: *(ESP+4)
19 #         argv[1]: *(ESP+8)
20 #         ...
21     # . prolog
22     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
23     # argv-equal(argv[1], argv[2])
24     # . . push argv[2]
25     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
26     # . . push argv[1]
27     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       .                 # push *(EBP+8)
28     # . . call
29     e8/call argv-equal/disp32
30     # syscall(exit, EAX)
31     89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
32     b8/copy-to-EAX  1/imm32
33     cd/syscall  0x80/imm8
34 
35 # compare two null-terminated ascii strings
36 # reason for the name: the only place we should have null-terminated ascii strings is from commandline args
37 argv-equal:  # (s1, s2) : null-terminated ascii strings -> EAX : boolean
38     # initialize s1 (ECX) and s2 (EDX)
39     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   4/disp8         .                 # copy *(ESP+4) to ECX
40     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           2/r32/EDX   8/disp8         .                 # copy *(ESP+8) to EDX
41     # while (true)
42 $argv-equal:loop:
43     # c1/EAX, c2/EBX = *s1, *s2
44     b8/copy-to-EAX  0/imm32
45     8a/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy byte at *ECX to lower byte of EAX
46     bb/copy-to-EBX  0/imm32
47     8a/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           3/r32/EBX   .               .                 # copy byte at *EDX to lower byte of EBX
48     # if (c1 == 0) break
49     3d/compare-EAX  0/imm32
50     74/jump-if-equal  $argv-equal:break/disp8
51     # if (c1 != c2) return false
52     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX with EBX
53     75/jump-if-not-equal  $argv-equal:false/disp8
54     # ++s1, ++s2
55     41/inc-ECX
56     42/inc-EDX
57     # end while
58     eb/jump  $argv-equal:loop/disp8
59 $argv-equal:break:
60     # if (c2 == 0) return true
61     81          7/subop/compare     3/mod/direct    3/rm32/EBX    .           .             .           .           .               0/imm32           # compare EBX
62     75/jump-if-not-equal  $argv-equal:false/disp8
63 $argv-equal:success:
64     b8/copy-to-EAX  1/imm32
65     c3/return
66     # return false
67 $argv-equal:false:
68     b8/copy-to-EAX  0/imm32
69     c3/return
70 
71 # . . vim:nowrap:textwidth=0