https://github.com/akkartik/mu/blob/master/examples/ex10.subx
 1 # String comparison: return 1 iff the two args passed in at the commandline are equal.
 2 #
 3 # To run:
 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 0x09000000
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 Entry:  # 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   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
26     # . . push argv[1]
27     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/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/exit
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 $argv-equal:loop:
42     # c1/eax, c2/ebx = *s1, *s2
43     b8/copy-to-eax  0/imm32
44     8a/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/AL    .               .                 # copy byte at *ecx to AL
45     bb/copy-to-ebx  0/imm32
46     8a/copy-byte                    0/mod/indirect  2/rm32/edx    .           .             .           3/r32/BL    .               .                 # copy byte at *edx to BL
47     # if (c1 == 0) break
48     3d/compare-eax-and  0/imm32
49     74/jump-if-equal  $argv-equal:break/disp8
50     # if (c1 != c2) return false
51     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
52     75/jump-if-not-equal  $argv-equal:false/disp8
53     # ++s1, ++s2
54     41/increment-ecx
55     42/increment-edx
56     # end while
57     eb/jump  $argv-equal:loop/disp8
58 $argv-equal:break:
59     # if (c2 == 0) return true
60     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0/imm32           # compare ebx
61     75/jump-if-not-equal  $argv-equal:false/disp8
62 $argv-equal:success:
63     b8/copy-to-eax  1/imm32
64     c3/return
65     # return false
66 $argv-equal:false:
67     b8/copy-to-eax  0/imm32
68     c3/return
69 
70 == data 0x0a000000
71 
72 # . . vim:nowrap:textwidth=0