2018-12-01 00:45:15 +00:00
|
|
|
# Read a character from stdin, save it to a global, write it to stdout.
|
2018-07-24 18:53:45 +00:00
|
|
|
#
|
2019-08-20 06:45:04 +00:00
|
|
|
# To run:
|
2021-03-30 01:47:52 +00:00
|
|
|
# $ bootstrap/bootstrap translate ex4.subx -o ex4
|
2021-03-04 08:24:24 +00:00
|
|
|
# $ bootstrap/bootstrap run ex4
|
2018-07-08 06:38:59 +00:00
|
|
|
|
2019-09-12 02:04:49 +00:00
|
|
|
== data
|
2019-05-18 07:00:18 +00:00
|
|
|
|
|
|
|
# the global variable we save to
|
|
|
|
X:
|
|
|
|
0/imm32 # space for read() to write to
|
|
|
|
|
2019-09-12 02:04:49 +00:00
|
|
|
== code
|
2018-11-26 08:26:20 +00:00
|
|
|
|
2019-07-17 07:47:11 +00:00
|
|
|
Entry:
|
2019-09-12 02:04:49 +00:00
|
|
|
# read(stdin, X, 1)
|
2018-12-01 00:45:15 +00:00
|
|
|
# . fd = 0 (stdin)
|
2019-08-26 18:55:26 +00:00
|
|
|
bb/copy-to-ebx 0/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . data = X (location to write result to)
|
2019-08-26 18:55:26 +00:00
|
|
|
b9/copy-to-ecx X/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . size = 1 character
|
2019-08-26 18:55:26 +00:00
|
|
|
ba/copy-to-edx 1/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . syscall
|
2019-09-12 02:04:49 +00:00
|
|
|
e8/call syscall_read/disp32
|
2018-07-16 18:03:08 +00:00
|
|
|
|
2019-09-12 02:04:49 +00:00
|
|
|
# write(stdout, X, 1)
|
2018-12-01 00:45:15 +00:00
|
|
|
# . fd = 1 (stdout)
|
2019-08-26 18:55:26 +00:00
|
|
|
bb/copy-to-ebx 1/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . initialize X (location to read from)
|
2019-08-26 18:55:26 +00:00
|
|
|
b9/copy-to-ecx X/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . size = 1 character
|
2019-08-26 18:55:26 +00:00
|
|
|
ba/copy-to-edx 1/imm32
|
2018-12-01 00:45:15 +00:00
|
|
|
# . syscall
|
2019-09-12 02:04:49 +00:00
|
|
|
e8/call syscall_write/disp32
|
2018-07-08 06:38:59 +00:00
|
|
|
|
2019-09-12 02:04:49 +00:00
|
|
|
# exit(ebx)
|
|
|
|
e8/call syscall_exit/disp32
|
2018-07-08 06:38:59 +00:00
|
|
|
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . vim:nowrap:textwidth=0
|