diff options
Diffstat (limited to 'transect/ex7.k2')
-rw-r--r-- | transect/ex7.k2 | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/transect/ex7.k2 b/transect/ex7.k2 new file mode 100644 index 00000000..3ede2c5e --- /dev/null +++ b/transect/ex7.k2 @@ -0,0 +1,64 @@ +# example showing file syscalls +# +# Create a file, open it for writing, write a character to it, close it, open +# it for reading, read a character from it, close it, delete it, and return +# the character read. + +var stream : int = 0 +var a : char = 97 +var b : char = 0 +var filename : (array char) = ".foo" + +fn main [ + call create, filename + *stream <- call open, filename, 1/wronly + call write, *stream, a, 1 + call close, *stream + stream <- call open, filename, 0/rdonly + call read, *stream, b, 1 + call close, *stream + call unlink, filename + var result/EBX : char + result/EBX <- copy b # TODO: copy char to int? + call exit-EBX +] + +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 open name : (address array byte) [ + EBX <- copy name + EAX <- copy 5/open + syscall +] + +fn close name : (address array byte) [ + EBX <- copy name + EAX <- copy 6/close + syscall +] + +fn unlink name : (address array byte) [ + EBX <- copy name + EAX <- copy 10/unlink + syscall +] + +# like exit, but assumes the code is already in EBX +fn exit-EBX [ + code/EAX <- copy 1/exit + syscall +] |