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:
  7 #   $ subx translate ex8.subx ex8
  8 #   $ subx run ex8
  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   ## creat(filename)
 19   bb/copy                         .               .             .           .             .           .           .               filename/imm32          # copy to EBX
 20   b9/copy                         .               .             .           .             .           .           .               0x180/imm32/fixed-perms # copy to ECX
 21   b8/copy                         .               .             .           .             .           .           .               8/imm32/creat           # copy 8 to EAX
 22   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 23 
 24   ## stream = open(filename, O_WRONLY, 0)  # we can't use 'fd' because it looks like a hex byte
 25   bb/copy                         .               .             .           .             .           .           .               filename/imm32          # copy to EBX
 26   b9/copy                         .               .             .           .             .           .           .               1/imm32/wronly          # copy 1 to ECX
 27   ba/copy                         .               .             .           .             .           .           .               0x180/imm32/fixed-perms # copy 0 to EDX
 28   b8/copy                         .               .             .           .             .           .           .               5/imm32/open            # copy 5 to EAX
 29   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 30   # save stream
 31   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 32   89/copy                         0/mod/indirect  3/rm32/EBX                                          0/r32/EAX                                           # copy EAX to *EBX
 33 
 34   ## write(stream, "a", 1)
 35   # load stream
 36   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 37   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
 38   #
 39   b9/copy                         .               .             .           .             .           .           .               a/imm32                 # copy to ECX
 40   ba/copy                         .               .             .           .             .           .           .               1/imm32/size            # copy 1 to EDX
 41   b8/copy                         .               .             .           .             .           .           .               4/imm32/write           # copy 4 to EAX
 42   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 43 
 44   ## close(stream)
 45   # load stream
 46   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 47   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
 48   #
 49   b8/copy                         .               .             .           .             .           .           .               6/imm32/close           # copy 6 to EAX
 50   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 51 
 52   ## stream = open(filename, O_RDONLY, 0)
 53   bb/copy                         .               .             .           .             .           .           .               filename/imm32          # copy to EBX
 54   b9/copy                         .               .             .           .             .           .           .               0/imm32/rdonly          # copy 0 to ECX
 55   ba/copy                         .               .             .           .             .           .           .               0x180/imm32/fixed-perms # copy to EDX
 56   b8/copy                         .               .             .           .             .           .           .               5/imm32/open            # copy 5 to EAX
 57   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 58   # save stream
 59   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 60   89/copy                         0/mod/indirect  3/rm32/EBX                                          0/r32/EAX                                           # copy EAX to *EBX
 61 
 62   ## read(stream, b, 1)
 63   # load stream
 64   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 65   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
 66   #
 67   b9/copy                         .               .             .           .             .           .           .               b/imm32                 # copy to ECX
 68   ba/copy                         .               .             .           .             .           .           .               1/imm32/size            # copy 1 to EDX
 69   b8/copy                         .               .             .           .             .           .           .               3/imm32/read            # copy 3 to EAX
 70   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 71 
 72   ## close(stream)
 73   # load stream
 74   bb/copy                         .               .             .           .             .           .           .               stream/imm32            # copy to EBX
 75   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
 76   #
 77   b8/copy                         .               .             .           .             .           .           .               6/imm32/close           # copy 6 to EAX
 78   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 79 
 80   ## unlink(filename)
 81   bb/copy                         .               .             .           .             .           .           .               filename/imm32          # copy to EBX
 82   b8/copy                         .               .             .           .             .           .           .               0xa/imm32/unlink        # copy 10 to EAX
 83   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 84 
 85   ## exit(b)
 86   # load b
 87   bb/copy                         .               .             .           .             .           .           .               b/imm32                 # copy to EBX
 88   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
 89   #
 90   b8/copy                         .               .             .           .             .           .           .               1/imm32/exit            # copy 1 to EAX
 91   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
 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:ft=subx:nowrap:tw&