https://github.com/akkartik/mu/blob/main/linux/apps/ex10.subx
 1 # String comparison: return 1 iff the two args passed in at the commandline are equal.
 2 #
 3 # To run:
 4 #   $ ./translate_subx 000init.subx apps/ex10.subx
 5 #   $ ./a.elf 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 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     # . prologue
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     # exit(eax)
31     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
32     e8/call  syscall_exit/disp32
33 
34 # compare two null-terminated ascii strings
35 # reason for the name: the only place we should have null-terminated ascii strings is from commandline args
36 argv-equal:  # (s1, s2): null-terminated ascii strings -> eax: boolean
37     # initialize s1 (ecx) and s2 (edx)
38     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           1/r32/ecx   4/disp8         .                 # copy *(esp+4) to ecx
39     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           2/r32/edx   8/disp8         .                 # copy *(esp+8) to edx
40 $argv-equal:loop:
41     # c1/eax, c2/ebx = *s1, *s2
42     b8/copy-to-eax  0/imm32
43     8a/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/AL    .               .                 # copy byte at *ecx to AL
44     bb/copy-to-ebx  0/imm32
45     8a/copy-byte                    0/mod/indirect  2/rm32/edx    .           .             .           3/r32/BL    .               .                 # copy byte at *edx to BL
46     # if (c1 == 0) break
47     3d/compare-eax-and  0/imm32/null
48     74/jump-if-=  $argv-equal:break/disp8
49     # if (c1 != c2) return false
50     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
51     75/jump-if-!=  $argv-equal:false/disp8
52     # ++s1, ++s2
53     41/increment-ecx
54     42/increment-edx
55     # end while
56     eb/jump  $argv-equal:loop/disp8
57 $argv-equal:break:
58     # if (c2 == 0) return true
59     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0/imm32/null      # compare ebx
60     75/jump-if-!=  $argv-equal:false/disp8
61 $argv-equal:success:
62     b8/copy-to-eax  1/imm32
63     c3/return
64     # return false
65 $argv-equal:false:
66     b8/copy-to-eax  0/imm32
67     c3/return
68 
69 # . . vim:nowrap:textwidth=0