aboutsummaryrefslogtreecommitdiff
path: root/examples/memory-print.asm
blob: cebd6e773e316741393b5bc839ec2fdd9b369a30 (plain)
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
65
;;; memory-print: An example program that features a subroutine for
;;; printing a memory buffer, of any length, as characters.

  ;; Setup label for entrypoint
  global main
main:
  ;; Allocate a buffer of 3 characters
  malloc.byte 3
  mov.word 0
  ;; Setup the buffer to be equivalent to "abc"
  push.reg.word 0
  push.byte 'a'
  mset.byte 0
  push.reg.word 0
  push.byte 'b'
  mset.byte 1
  push.reg.word 0
  push.byte 'c'
  mset.byte 2

  ;; Save buffer to W[8] because the first 8 registers should be
  ;; reserved for library routines as it may be overwritten
  push.reg.word 0
  mov.word 8
  ;; Call the routine
  call print_cptr

  ;; Delete allocated buffer
  push.reg.word 8
  mdelete

  halt

;;; print_cptr: Prints pointer to a buffer of characters.  Pointer
;;; should be on the stack as a word.
print_cptr:
  ;; iterator I -> W[1]
  push.word 0
  mov.word 1
  ;; (W[0])[W[1]] -> P[I]
loopback:
  push.reg.word 0
  push.reg.word 1
  mget.stack.byte
  print.char

  ;; I += 1
  push.reg.word 1
  push.word 1
  plus.word
  mov.word 1

  ;; if I != |P| ...
  push.reg.word 1
  push.reg.word 0
  msize
  eq.word
  not.byte
  ;; then go to `loopback`
  jump.if.byte loopback
  ;; else print a newline
  push.byte '\n'
  print.char
  ;; return back to the caller
  ret