summary refs log tree commit diff
path: root/pc-thing/code/memman.a
blob: 2398cd647d5528ad671e27ae82ec4cdd78435a23 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; MEMMAN - memory manager

jmp start

#using code/printer.a

; code from calc.a
; call with length in A
str_to_num:
    ; pointer to current character
    ; ld a len
    ld b [stm_start]
    add
    str c [ptr]
    mov a 1
    str a [mult]
    mov a 0
    str a [num]
    .label zero 48
    stm_loop:
        ;load char, subtract zero, multiply by 10
        ld b [ptr]
        ldr a b
        mov b zero
        sub
        swp c a
        ld b [mult]
        mul

        ; add to num
        swp c b
        ld a [num]
        add
        str c [num]

        ld a [mult]
        mov b 10
        mul
        str c [mult]

        ; move pointer
        ld a [ptr]
        mov b 1
        sub
        str c [ptr]
        ld b [stm_start]
        cmr c b
        jz stm_loop
    ret

get_input:
    mov a 0
    mov b 0
    mov c [input]
    sys
    str a [len]
    ret

parse_input:
    mov a 0
    str a [inp_ptr]

    .macro case(addr, val, ln) \
        ld a @addr\
        cmp a @val\
        jnz @ln
    .macro break() jmp default

    case [input] 'g' case_g
    break

    case_g:
        ; get pointer to number
            mov a [input]
            mov b 1
            add
            str c [stm_start]
        ; get new length
            ld a [len]
            mov b 2 ; subtract 2 for command + \n at the end
            sub
            swp c a 
        jmr str_to_num
        ld a [num]
        dbg 3
        str a 16
        jmr print_num
        ; print the output
            mov a 1 ; write
            mov b 1 ; stdout
            mov c 32 ; address
            sys ; syscall
        mov a 1
        mov b 1
        mov c [colon_space]
        sys
        ldm a [num]
        str a 16
        jmr print_num
        ; print the output
            mov a 1 ; write
            mov b 1 ; stdout
            mov c 32 ; address
            sys ; syscall
        break

    default:
        ret


start:
    mov a 2
    mov b 0
    sys
    jmr get_input
    jmr parse_input
    halt

startup_message:
.str "welcome to mem man the memory manager of the memory of managing the memory"
.hex 0xa
.str "yeah"
.hex 0xa
.hex 0xa
.hex 0
err:
.str "what the fuck is this"
.hex 0xa
.hex 0
ptr:
.hex 0
mult:
.hex 0
num:
.hex 0
; 256 bytes of input
input:
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
.str "0000000000000000000000000000000000000000000000000000000000000000"
len:
.hex 0
inp_ptr:
.hex 0
stm_start:
.hex 0
colon_space:
.str ": "
.hex 0