1 # Read a character from stdin, save it to a local on the stack, write it to stdout. 2 # 3 # To run (from the subx directory): 4 # $ ./subx translate examples/ex5.subx -o examples/ex5 5 # $ ./subx run examples/ex5 6 7 == code 8 # instruction effective address register 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 Entry: 13 14 # allocate x on the stack 15 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # subtract from ESP 16 17 # syscall(read, stdin, x, 1) 18 # . fd = 0 (stdin) 19 bb/copy-to-EBX 0/imm32 20 # . data = x (location to write result to) 21 8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 1/r32/ECX 4/disp8 . # copy ESP+4 to ECX 22 # . size = 1 character 23 ba/copy-to-EDX 1/imm32 24 # . syscall 25 b8/copy-to-EAX 3/imm32/read 26 cd/syscall 0x80/imm8 27 28 # syscall(write, stdout, x, 1) 29 # . fd = 1 (stdout) 30 bb/copy-to-EBX 1/imm32 31 # . data = x (location to read from) 32 8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 1/r32/ECX 4/disp8 . # copy ESP+4 to ECX 33 # . size = 1 character 34 ba/copy-to-EDX 1/imm32 35 # . syscall 36 b8/copy-to-EAX 4/imm32/write 37 cd/syscall 0x80/imm8 38 39 # syscall(exit, EBX) 40 b8/copy-to-EAX 1/imm32/exit 41 cd/syscall 0x80/imm8 42 43 # . . vim:nowrap:textwidth=0