about summary refs log blame commit diff stats
path: root/archive/2.transect/ex7.k2
blob: 3ede2c5e4efbd3df15c8d21e7628211e98669630 (plain) (tree)































































                                                                             
# 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
]