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 == code
 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 # 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/read      # copy 3 to EAX
28   cd/syscall  0x80/imm8
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/write     # copy 4 to EAX
39   cd/syscall  0x80/imm8
40 
41   # exit(EBX)
42   b8/copy                                                                                                                         1/imm32/exit      # copy 1 to EAX
43   cd/syscall  0x80/imm8
44 
45 # vim:ft=subx:nowrap