https://github.com/akkartik/mu/blob/master/subx/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
 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     # syscall(creat, Filename)
 20     bb/copy-to-EBX  Filename/imm32
 21     b9/copy-to-ECX  0x180/imm32/fixed-perms
 22     b8/copy-to-EAX  8/imm32/creat
 23     cd/syscall  0x80/imm8
 24 
 25     # stream = syscall(open, Filename, O_WRONLY, 0)  # we can't use 'fd' because it looks like a hex byte
 26     bb/copy-to-EBX  Filename/imm32
 27     b9/copy-to-ECX  1/imm32/wronly
 28     ba/copy-to-EDX  0x180/imm32/fixed-perms
 29     b8/copy-to-EAX  5/imm32/open
 30     cd/syscall  0x80/imm8
 31     # save stream
 32     bb/copy-to-EBX  Stream/imm32
 33     89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
 34 
 35     # syscall(write, Stream, "a", 1)
 36     # . load stream
 37     bb/copy-to-EBX  Stream/imm32
 38     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 39     # .
 40     b9/copy-to-ECX  A/imm32
 41     ba/copy-to-EDX  1/imm32/size
 42     b8/copy-to-EAX  4/imm32/write
 43     cd/syscall  0x80/imm8
 44 
 45     # syscall(close, Stream)
 46     # . load stream
 47     bb/copy-to-EBX  Stream/imm32
 48     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 49     # .
 50     b8/copy-to-EAX  6/imm32/close
 51     cd/syscall  0x80/imm8
 52 
 53     # stream = syscall(open, Filename, O_RDONLY, 0)
 54     bb/copy-to-EBX  Filename/imm32
 55     b9/copy-to-ECX  0/imm32/rdonly
 56     ba/copy-to-EDX  0x180/imm32/fixed-perms
 57     b8/copy-to-EAX  5/imm32/open
 58     cd/syscall  0x80/imm8
 59     # . save Stream
 60     bb/copy-to-EBX  Stream/imm32
 61     89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
 62 
 63     # syscall(read, Stream, B, 1)
 64     # . load stream
 65     bb/copy-to-EBX  Stream/imm32
 66     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 67     # .
 68     b9/copy-to-ECX  B/imm32
 69     ba/copy-to-EDX  1/imm32/size
 70     b8/copy-to-EAX  3/imm32/read
 71     cd/syscall  0x80/imm8
 72 
 73     # syscall(close, Stream)
 74     # . load stream
 75     bb/copy-to-EBX  Stream/imm32
 76     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 77     #
 78     b8/copy-to-EAX  6/imm32/close
 79     cd/syscall  0x80/imm8
 80 
 81     # syscall(unlink, filename)
 82     bb/copy-to-EBX  Filename/imm32
 83     b8/copy-to-EAX  0xa/imm32/unlink
 84     cd/syscall  0x80/imm8
 85 
 86     # syscall(exit, b)
 87     # . load b
 88     bb/copy-to-EBX  B/imm32
 89     8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # copy *EBX to EBX
 90     #
 91     b8/copy-to-EAX  1/imm32/exit
 92     cd/syscall  0x80/imm8
 93 
 94 == data
 95 
 96 Stream:
 97     00 00 00 00
 98 A:
 99     61 00 00 00
100 B:
101     00 00 00 00
102 Filename:
103     2e 66 6f 6f 00 00 00 00
104 
105 # . . vim:nowrap:textwidth=0