-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_line.ear
88 lines (73 loc) · 1.27 KB
/
read_line.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
/*
int read_line(char* buffer, size_t size) {
int value, prev_value = 0;
int bytes_read = 0;
while(bytes_read < size && (value = port_read(stdio)) >= 0) {
if(value == '\n') {
*buffer = prev_value;
break;
}
if(bytes_read != 0) {
*buffer++ = prev_value | 0x80;
}
prev_value = value;
++bytes_read;
}
if(value < 0) {
return value;
}
return bytes_read;
}
*/
// A0 = buffer
// A1 = size
// R3 = bytes_read
// R4 = value
// R5 = prev_value
.scope
.export @read_line
@read_line:
// bytes_read = 0;
MOV R3, ZERO
MOV R5, ZERO
BRR @.while_cond
@.while_body:
/*
if(value == '\n') {
*buffer = prev_value;
break;
}
*/
CMP R4, '\n'
STB.EQ [A0], R5
BRR.EQ @.while_after
/*
if(bytes_read != 0) {
*buffer++ = prev_value | 0x80;
}
*/
MOV R3, R3
BRR.ZR @.skip_first_write
ORR R5, 0x80
STB [A0], R5
INC A0
@.skip_first_write:
// prev_value = value;
MOV R5, R4
// ++bytes_read;
INC R3
@.while_cond:
// while(bytes_read < size ...
CMP R3, A1
BRR.GE @.while_after
// ... && (value = port_read(portnum)) >= 0)
RDB R4
BRR.CC @.while_body
// if(value < 0) return value;
MOV RV, R4
BRR @.return
@.while_after:
// return bytes_read;
MOV RV, R3
@.return:
RET