about summary refs log tree commit diff stats
path: root/archive/2.transect/ex8.k2
blob: dfef03b0bb5f139c8961447a56ea64a2ac47d7bd (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
# Example reading commandline arguments: compute length of first arg.

fn main argc : int, argv : (array (ref array char)) -> [
  var tmp : (index char)
  tmp <- index 1, %size(ref array char)
  var tmp2 : (address (ref array char))
  tmp2 <- advance argv, tmp
  var s/EBX : (ref array char)
  s/EBX <- copy *tmp2
  var result/EAX : int
  result/EAX <- ascii_length s/EBX
  call exit, result/EAX
]

fn ascii_length s : (ref array char) -> result : int [
  var result/EBX : int
  result/EBX <- copy 0
  {
    var tmp0/EDI : (offset char)
    tmp0/EDI <- index result/EBX, %size(char)
    var tmp/EDX : (address char)
    tmp/EDX <- advance *s, tmp0/EDI
    var c/ECX : char
    c/ECX <- copy *tmp
    compare c/ECX, 0
    break-if-equal
    loop
  }
  return result/EBX
]

fn exit x : int [
  code/EBX <- copy x
  code/EAX <- copy 1/exit
  syscall
]