about summary refs log tree commit diff stats
path: root/transect/ex7.k2
blob: 3ede2c5e4efbd3df15c8d21e7628211e98669630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
]