summary refs log tree commit diff
path: root/pc-thing/code/strlib.a
blob: a18da3bbcd6e15889b480da4fcb0dde1ed13321b (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
; number to string
; a - pointer to output
; b - number to print
num_to_str:
    str a out_pointer
    str a out_start
    str b [number]
    ; non-address definitions
    .label zero 48
    ; define stuff in memory
    ; .label number 16
    ; .label mult 17
    ; mov a 1
    ; str a mult
    ; .label currnum 18
    ; .label tmp1 19
    ; .label swap_counter 21

    loop_start:
        ; get num-(num%mult)
            ld a [number]
            ld b [mult]
            mod
            swp c b
            sub
            str c [tmp1]

        ; get num-(num%(mult*10))
            mov a 10
            ld b [mult]
            mul
            swp c b
            ld a [number]
            mod
            swp c b
            sub
        
        ; subtract the thingies to get the current digit
        swp c b
        ld a [tmp1]
        sub
        swp c a
        ld b [mult]
        div
        str c [currnum]

        ; add zero to currnum and store at out_pointer
        swp a c ; currnum -> A
        mov b zero
        add
        ld a [out_pointer]
        srr c a ; store c to address at a (which is out_pointer)

        ; continue loop
            ; increment out_pointer
            ld a [out_pointer]
            mov b 1
            add
            str c [out_pointer]
            ; increment mult
            ld a [mult]
            mov b 10
            mul
            str c [mult]
        
            swp c b ; mult -> b
            ld a [number] ; get number
            mod ; get mod, will be equal to number if reached end
            cmr c a ; compare result of mod and number (which is still in a)
            jz loop_start ; jump if not equal
    ; escaped from loop; swapping around memory to flip the text
    ld a [out_pointer]
    ld b [out_start]
    sub
    swp c a
    mov b 2
    div
    str c [swap_counter]
    cmp c 0
    jnz end_swap
    start_swap:
        ; ptr - counter
            ld b [swap_counter]
            ld a [out_pointer]
            sub
            str c [tmp1]
        ; start + counter
            ld a [swap_counter] ; 1
            mov b 1
            sub ; 1
            swp c b ; 1 -> b
            ld a [out_start] ; 32
            add ; 32 + 1 = 33
        swp c b
        ld a [tmp1]
        dbg 304
        swpm a b
        ld a [swap_counter]
        mov b 1
        sub
        str c [swap_counter]
        cmp c 0
        jz start_swap
    end_swap:
    ; add \n\0
    ld b [out_pointer]
    mov a 10 ; \n
    srr a b
    mov a 1
    add
    mov a 0 ; \0
    srr a c
    ret

out_pointer:
.hex 0
out_start:
.hex 0
number:
.hex 0
mult:
.hex 1
currnum:
.hex 0
tmp1:
.hex 0
swap_counter:
.hex 0