https://github.com/akkartik/mu/blob/master/examples/ex7.subx
  1 # Example showing file syscalls.
  2 #
  3 # Create a file, open it for writing, write a character to it, close it, open
  4 # it for reading, read a character from it, close it, delete it, and return
  5 # the character read.
  6 #
  7 # To run (from the subx directory):
  8 #   $ ./subx translate examples/ex7.subx -o examples/ex7
  9 #   $ ./subx run examples/ex7
 10 # Expected result:
 11 #   $ echo $?
 12 #   97
 13 
 14 == code 0x09000000
 15 #   instruction                     effective address                                                   register    displacement    immediate
 16 # . op          subop               mod             rm32          base        index         scale       r32
 17 # . 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
 18 
 19 Entry:
 20     # syscall(creat, Filename)
 21     bb/copy-to-EBX  Filename/imm32
 22     b9/copy-to-ECX  0x180/imm32/fixed-perms
 23     b8/copy-to-EAX  8/imm32/creat
 24     cd/syscall  0x80/imm8
 25 
 26     # stream = syscall(open, Filename, O_WRONLY, 0)  # we can't use 'fd' because it looks like a hex byte
 27     bb/copy-to-EBX  Filename/imm32
 28     b9/copy-to-ECX  1/imm32/wronly
 29     ba/copy-to-EDX  0x180/imm32/fixed-perms
 30     b8/copy-to-EAX  5/imm32/open
 31     cd/syscall  0x80/imm8
 32     # save stream
 33     bb/copy-to-EBX  Stream/imm32
 34     89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
 35 
 36     # syscall(write, Stream, "a", 1)
 37     # . load stream
 38     bb/copy-to-EBX  Stream/imm32
 39     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 40     # .
 41     b9/copy-to-ECX  A/imm32
 42     ba/copy-to-EDX  1/imm32/size
 43     b8/copy-to-EAX  4/imm32/write
 44     cd/syscall  0x80/imm8
 45 
 46     # syscall(close, Stream)
 47     # . load stream
 48     bb/copy-to-EBX  Stream/imm32
 49     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 50     # .
 51     b8/copy-to-EAX  6/imm32/close
 52     cd/syscall  0x80/imm8
 53 
 54     # stream = syscall(open, Filename, O_RDONLY, 0)
 55     bb/copy-to-EBX  Filename/imm32
 56     b9/copy-to-ECX  0/imm32/rdonly
 57     ba/copy-to-EDX  0x180/imm32/fixed-perms
 58     b8/copy-to-EAX  5/imm32/open
 59     cd/syscall  0x80/imm8
 60     # . save Stream
 61     bb/copy-to-EBX  Stream/imm32
 62     89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
 63 
 64     # syscall(read, Stream, B, 1)
 65     # . load stream
 66     bb/copy-to-EBX  Stream/imm32
 67     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 68     # .
 69     b9/copy-to-ECX  B/imm32
 70     ba/copy-to-EDX  1/imm32/size
 71     b8/copy-to-EAX  3/imm32/read
 72     cd/syscall  0x80/imm8
 73 
 74     # syscall(close, Stream)
 75     # . load stream
 76     bb/copy-to-EBX  Stream/imm32
 77     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 78     #
 79     b8/copy-to-EAX  6/imm32/close
 80     cd/syscall  0x80/imm8
 81 
 82     # syscall(unlink, filename)
 83     bb/copy-to-EBX  Filename/imm32
 84     b8/copy-to-EAX  0xa/imm32/unlink
 85     cd/syscall  0x80/imm8
 86 
 87     # syscall(exit, b)
 88     # . load b
 89     bb/copy-to-EBX  B/imm32
 90     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 91     #
 92     b8/copy-to-EAX  1/imm32/exit
 93     cd/syscall  0x80/imm8
 94 
 95 == data 0x0a000000
 96 
 97 Stream:
 98     0/imm32
 99 A:
100     61/imm32/A
101 B:
102     0/imm32
103 Filename:
104     2e 66 6f 6f 00 00 00 00
105 #   .  f  o  o  null
106 
107 # . . vim:nowrap:textwidth=0