-
Notifications
You must be signed in to change notification settings - Fork 0
/
uadd32.ear
166 lines (135 loc) · 2.4 KB
/
uadd32.ear
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
155
156
157
158
159
160
161
162
163
164
165
166
/*
u32 uadd32(u32 x, u32 y) {
u16 a = LO16(x); //R2
u16 b = HI16(x); //R3
u16 c = LO16(y); //R4
u16 d = HI16(y); //R5
u16 e; //R2
u16 f; //R3
e = a + c;
f = b + d + CARRY;
return __PAIR__(LO16=e, HI16=f);
}
*/
.scope
.export @uadd32
@uadd32: //LEAF
ADD R2, R4
ADC R3, R5
RET
/*
void puts(const char* R2) {
if(R2 == NULL) {
return;
}
char R3;
u16 R4;
u16 R5 = 0x7f;
do {
R3 = *R2++;
R4 = R3 & R5;
WRB(R4);
} while(R3 > R4);
}
*/
.scope
.export @puts
@puts: //LEAF
MOV R2, R2 // NULL check (more lightweight than CMP)
RET.EQ // Return if NULL
MOV R5, 0x7f // Create mask for the ASCII character bits
@.next_char:
LDB R3, [R2] // Load character from string
INC R2 // Increment string pointer
AND R4, R3, R5 // Mask off the continuation bit
WRB R4 // Write ASCII character
CMP R3, R4 // Check for continuation bit
BRR.GT @.next_char // Continuation bit was set, keep iterating through the string
RET
/*
void print_hex16(u16 num) {
u16 R3 = 12;
u16 R4 = 0xF;
while(1) {
u16 R5 = (num >> R3) & R4;
R5 = "0123456789ABCDEF"[R5];
WRB(R5);
if(R3 == 0) {
break;
}
R3 -= 4;
}
}
*/
.scope
.export @print_hex16
@print_hex16: //LEAF
MOV R3, 12
MOV R4, 0xF
@.while_body:
SRU R5, R2, R3
AND R5, R4
ADR R6, @.hexchars
ADD R6, R5
LDB R5, [R6]
WRB R5
@.while_cond:
MOV R3, R3
RET.ZR
SUB R3, 4
BRR @.while_body
@.hexchars:
.db "0123456789ABCDEF"
/*
void print_hex32(u32 num) {
print_hex16(HI16(num)); //R3
print_hex16(LO16(num)); //R2
}
*/
.scope
.export @print_hex32
@print_hex32:
PSH {R8, RA, RD}
MOV R8, R2
MOV R2, R3
FCR @print_hex16
MOV R2, R8
FCR @print_hex16
POP {R8, PC, DPC}
/*
void main(void) {
u32 a = 0x12345678;
u32 b = 0x44332211;
u32 sum = uadd32(a, b);
print_hex32(sum);
}
*/
.scope
.export @main
@main:
WRB '0'
WRB 'x'
MOV R2, 0x2211
MOV R3, 0x4433
FCR @print_hex32
WRB ' '
WRB '+'
WRB ' '
WRB '0'
WRB 'x'
MOV R2, 0x5678
MOV R3, 0x1234
FCR @print_hex32
WRB ' '
WRB '='
WRB ' '
WRB '0'
WRB 'x'
MOV R2, 0x5678
MOV R3, 0x1234
MOV R4, 0x2211
MOV R5, 0x4433
FCR @uadd32
FCR @print_hex32
WRB '\n'
HLT