From aa00959024643a33f766ef3f060a934006fe7725 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 23 Sep 2018 09:23:21 -0700 Subject: 4504 --- transect/ex4.k2 | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 transect/ex4.k2 (limited to 'transect/ex4.k2') diff --git a/transect/ex4.k2 b/transect/ex4.k2 new file mode 100644 index 00000000..9a7ddee7 --- /dev/null +++ b/transect/ex4.k2 @@ -0,0 +1,34 @@ +## read a character from stdin, save it to a global, write it to stdout + +# variables are always references +# read their address with their names: x (can't write to their address) +# read/write their contents with a lookup: *x +var x : char + +fn main [ + call read 0/stdin, x, 1/size + result/EAX <- call write 1/stdout, x, 1/size + call exit, result/EAX +] + +fn read fd : int, x : (address array byte), size : int [ + EBX <- copy fd + ECX <- copy x + EDX <- copy size + EAX <- copy 3/read + syscall +] + +fn write fd : int, x : (address array byte), size : int [ + EBX <- copy fd + ECX <- copy x + EDX <- copy size + EAX <- copy 4/write + syscall +] + +fn exit x : int [ + code/EBX <- copy x + code/EAX <- copy 1/exit + syscall +] -- cgit 1.4.1-2-gfad0