From bb2b6ba7fd72bbebc3a4490d090f080e7b8628cc Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 4 Aug 2018 17:00:27 -0700 Subject: 4478 --- html/subx/ex5.subx.html | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 html/subx/ex5.subx.html (limited to 'html/subx/ex5.subx.html') diff --git a/html/subx/ex5.subx.html b/html/subx/ex5.subx.html new file mode 100644 index 00000000..297f0ab2 --- /dev/null +++ b/html/subx/ex5.subx.html @@ -0,0 +1,104 @@ + + + + +Mu - subx/ex5.subx + + + + + + + + + + +
+ 1 ## read a character from stdin, save it to a local on the stack, write it to stdout
+ 2 #
+ 3 # To run:
+ 4 #   $ subx translate ex5.subx ex5
+ 5 #   $ subx run ex5
+ 6 
+ 7 == 0x08048054  # code segment, after leaving room for ELF header and segment headers
+ 8 # instruction                     effective address                                                   operand     displacement    immediate
+ 9 # op          subop               mod             rm32          base        index         scale       r32
+10 # 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
+11 
+12 ## function main
+13   # prolog
+14   55/push                                                                                                                                           # push EBP
+15   89/copy                         3/mod/direct    5/rm32/EBP                                          4/r32/ESP                                     # copy ESP to EBP
+16   # allocate x on the stack
+17   81          5/subop/subtract    3/mod/direct    4/rm32/ESP                                                                      4/imm32           # subtract 4 bytes from ESP
+18 
+19   ## read(stdin, x, 1)
+20   # fd = 0 (stdin)
+21   bb/copy                                                                                                                         0/imm32           # copy 0 to EBX
+22   # initialize x (location to write result to)
+23   89/copy                         3/mod/direct    1/rm32/ECX                                          5/r32/EBP                                     # copy EBP to ECX
+24   # size = 1 character
+25   ba/copy                                                                                                                         1/imm32           # copy 1 to EDX
+26   # read(fd, x, size)
+27   b8/copy                                                                                                                         3/imm32           # copy 3 to EAX
+28   cd/syscall                                                                                                                      0x80/imm8         # int 80h
+29 
+30   ## write(stdout, x, 1)
+31   # fd = 1 (stdout)
+32   bb/copy                                                                                                                         1/imm32           # copy 1 to EBX
+33   # initialize x (location to read from)
+34   89/copy                         3/mod/direct    1/rm32/ECX                                          5/r32/EBP                                     # copy EBP to ECX
+35   # size = 1 character
+36   ba/copy                                                                                                                         1/imm32           # copy 1 to EDX
+37   # write(fd, x, size)
+38   b8/copy                                                                                                                         4/imm32           # copy 4 to EAX
+39   cd/syscall                                                                                                                      0x80/imm8         # int 80h
+40 
+41   ## exit(EBX)
+42   b8/copy                                                                                                                         1/imm32           # copy 1 to EAX
+43   cd/syscall                                                                                                                      0x80/imm8         # int 80h
+44 
+45 # vim:ft=subx:nowrap
+
+ + + -- cgit 1.4.1-2-gfad0