# Mu - subx/examples/ex7.subx

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