https://github.com/akkartik/mu/blob/main/linux/ex9.subx

     1 # Example showing arg order on the stack.
     2 #
     3 # Show difference between ascii codes of first letter of first arg and first
     4 # letter of second arg.
     5 #
     6 # To run:
     7 #   $ bootstrap/bootstrap translate apps/ex9.subx -o apps/ex9
     8 #   $ bootstrap/bootstrap run ex9 z x
     9 # Expected result:
    10 #   $ echo $?
    11 #   2
    12 #
    13 # At the start of a SubX program:
    14 #   argc: *esp
    15 #   argv[0]: *(esp+4)
    16 #   argv[1]: *(esp+8)
    17 #   ...
    18 # Locals start from esp-4 downwards.
    19
    20 == code
    21 #   instruction                     effective address                                                   register    displacement    immediate
    22 # . op          subop               mod             rm32          base        index         scale       r32
    23 # . 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
    24
    25 Entry:
    26     # . prologue
    27     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    28     # ascii-difference(argv[1], argv[2])
    29     # . . push argv[2]
    30     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    31     # . . push argv[1]
    32     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    33     # . . call
    34     e8/call  ascii-difference/disp32
    35     # . . discard args
    36     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    37     # exit(eax)
    38     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
    39     e8/call  syscall_exit/disp32
    40
    41 ascii-difference:  # (s1, s2): null-terminated ascii strings
    42     # a = first letter of s1 (ecx)
    43     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           0/r32/eax   4/disp8         .                 # copy *(esp+4) to eax
    44     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           0/r32/eax   .               .                 # copy *eax to eax
    45     # b = first letter of s2 (edx)
    46     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           1/r32/ecx   8/disp8                           # copy *(esp+8) to ecx
    47     8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # copy *ecx to ecx
    48     # a-b
    49     29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # subtract ecx from eax
    50     c3/return
    51
    52 # . . vim:nowrap:textwidth=0