From 7328af20a1921d9258a60803ee5367da97a6082e Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 13 Aug 2018 21:25:22 -0700 Subject: 4521 --- html/subx/ex8.subx.html | 159 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 html/subx/ex8.subx.html (limited to 'html/subx/ex8.subx.html') diff --git a/html/subx/ex8.subx.html b/html/subx/ex8.subx.html new file mode 100644 index 00000000..96bfb430 --- /dev/null +++ b/html/subx/ex8.subx.html @@ -0,0 +1,159 @@ + + + + +Mu - subx/ex8.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:
+ 7 #   $ subx translate ex8.subx ex8
+ 8 #   $ subx run ex8
+ 9 # Expected result:
+10 #   $ echo $?
+11 #   97
+12 
+13 == 0x08048074  # code segment, after leaving room for ELF header and segment headers
+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                         .               .             .           .             .           .           .               0x08049131/imm32/fname  # copy to EBX
+20   b9/copy                         .               .             .           .             .           .           .               0x180/imm32/fixed-perms # copy 0 to ECX
+21   b8/copy                         .               .             .           .             .           .           .               8/imm32/creat           # copy 8 to EAX
+22   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
+23 
+24   ## fd = open(filename, O_WRONLY, 0)
+25   bb/copy                         .               .             .           .             .           .           .               0x08049131/imm32/fname  # 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 fd
+31   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # copy to EBX
+32   89/copy                         0/mod/indirect  3/rm32/EBX                                          0/r32/EAX                                           # copy EAX to *EBX
+33 
+34   ## write(fd, "a", 1)
+35   # load fd
+36   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # copy to EBX
+37   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
+38   #
+39   b9/copy                         .               .             .           .             .           .           .               0x08049129/imm32/a      # 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(fd)
+45   # load fd
+46   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # 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   ## fd = open(filename, O_RDONLY, 0)
+53   bb/copy                         .               .             .           .             .           .           .               0x08049131/imm32/fname  # copy to EBX
+54   b9/copy                         .               .             .           .             .           .           .               0/imm32/rdonly          # copy 0 to ECX
+55   ba/copy                         .               .             .           .             .           .           .               0x180/imm32/fixed-perms # copy 0 to EDX
+56   b8/copy                         .               .             .           .             .           .           .               5/imm32/open            # copy 5 to EAX
+57   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
+58   # save fd
+59   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # copy to EBX
+60   89/copy                         0/mod/indirect  3/rm32/EBX                                          0/r32/EAX                                           # copy EAX to *EBX
+61 
+62   ## read(fd, b, 1)
+63   # load fd
+64   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # copy to EBX
+65   8b/copy                         0/mod/indirect  3/rm32/EBX                                          3/r32/EBX                                           # copy *EBX to EBX
+66   #
+67   b9/copy                         .               .             .           .             .           .           .               0x0804912d/imm32/b      # 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(fd)
+73   # load fd
+74   bb/copy                         .               .             .           .             .           .           .               0x08049125/imm32/fd     # 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 8 to EAX
+78   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
+79 
+80   ## unlink(filename)
+81   bb/copy                         .               .             .           .             .           .           .               0x08049131/imm32/fname  # copy to EBX
+82   b8/copy                         .               .             .           .             .           .           .               0xa/imm32/unlink        # copy 8 to EAX
+83   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8               # int 80h
+84 
+85   ## exit(b)
+86   # load b
+87   bb                              .               .             .           .             .           .           .               0x0804912d/imm32/b      # 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 == 0x08049125  # data segment
+94 00 00 00 00  # fd
+95 61 00 00 00  # a: string to write to file: 'a'
+96 00 00 00 00  # b: space for string read from file
+97 2e 66 6f 6f 00 00 00 00  # filename: '.foo'
+98 
+99 # vim:ft=subx:nowrap:tw&
+
+ + + -- cgit 1.4.1-2-gfad0