1 ## read a character from stdin, save it to a global, write it to stdout
 2 #
 3 # To run:
 4 #   $ subx translate ex4.subx ex4
 5 #   $ subx run ex4
 6 
 7 == 0x08048074  # 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   ## read(stdin, x, 1)
13   # fd = 0 (stdin)
14   bb/copy                                                                                                                         0/imm32           # copy 0 to EBX
15   # initialize x (location to write result to)
16   b9/copy                                                                                                                         0x080490a7/imm32  # copy to ECX
17   # size = 1 character
18   ba/copy                                                                                                                         1/imm32           # copy 1 to EDX
19   # read(fd, x, size)
20   b8/copy                                                                                                                         3/imm32           # copy 3 to EAX
21   cd/syscall                                                                                                                      0x80/imm8         # int 80h
22 
23   ## write(stdout, x, 1)
24   # fd = 1 (stdout)
25   bb/copy                                                                                                                         1/imm32           # copy 1 to EBX
26   # initialize x (location to read from)
27   b9/copy                                                                                                                         0x080490a7/imm32  # copy to ECX
28   # size = 1 character
29   ba/copy                                                                                                                         1/imm32           # copy 1 to EDX
30   # write(fd, x, size)
31   b8/copy                                                                                                                         4/imm32           # copy 4 to EAX
32   cd/syscall                                                                                                                      0x80/imm8         # int 80h
33 
34   ## exit(EBX)
35   b8/copy                                                                                                                         1/imm32           # copy 1 to EAX
36   cd/syscall                                                                                                                      0x80/imm8         # int 80h
37 
38 == 0x080490a7  # data segment
39 00 00 00 00  # space for read() to write to
40 
41 # vim:ft=subx:nowrap