Skip to content

Commit cb5882e

Browse files
authored
Merge pull request #213 from Miezekatze64/6502-printf
Fix broken 6502 test cases
2 parents c920522 + e6c3fe7 commit cb5882e

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

libb/6502.b

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,30 @@ realloc(ptr, size) {
4040
with the `divmod` test
4141
*/
4242
_div(a, b) {
43-
auto d;
43+
auto d, sign;
44+
sign = 0;
45+
if (a < 0) {
46+
sign = !sign;
47+
a = -a;
48+
}
49+
if (b < 0) {
50+
sign = !sign;
51+
b = -a;
52+
}
53+
4454
d = 0; while(a >= b) {
4555
a = a - b;
4656
d++;
4757
}
58+
if (sign) d = -d;
59+
return (d);
60+
}
61+
_udiv(a, b) {
62+
auto d;
63+
d = 0; while(a >= b | a < 0) {
64+
a = a - b;
65+
d++;
66+
}
4867
return (d);
4968
}
5069

@@ -58,18 +77,29 @@ _rem (a, b) {
5877
}
5978
return (a);
6079
}
80+
_urem(a, b) {
81+
auto d;
82+
while(a >= b | a < 0) {
83+
a = a - b;
84+
}
85+
return (a);
86+
}
6187

6288
printn(n, b, sign) {
63-
auto a, c, d;
89+
auto a, c, d, __div, __rem;
90+
91+
/* use correct div/rem based on sign */
92+
__div = sign ? &_div : &_udiv;
93+
__rem = sign ? &_rem : &_urem;
6494

6595
if (sign & n < 0) {
6696
putchar('-');
6797
n = -n;
6898
}
6999

70-
if(a=_div(n, b)) /* assignment, not test for equality */
100+
if(a=__div(n, b)) /* assignment, not test for equality */
71101
printn(a, b, 0); /* recursive */
72-
c = _rem(n,b) + '0';
102+
c = __rem(n,b) + '0';
73103
if (c > '9') c += 7;
74104
putchar(c);
75105
}
@@ -106,7 +136,7 @@ printf(str, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) {
106136
while (c = char(*arg, j++)) {
107137
putchar(c);
108138
}
109-
} else if (c == 'z') { /* hack for %zu */
139+
} else if ((c == 'z') | (c == 'l')) { /* hack for %zu %lu, % */
110140
c = '%';
111141
goto while_end;
112142
} else {

tests.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,14 @@
576576
"comment": ""
577577
},
578578
"uxn": {
579-
"expected_stdout": "69\n1000000\n123456789987654321\n",
579+
"expected_stdout": "69\n16960\n64177\n",
580580
"state": "Enabled",
581-
"comment": ""
581+
"comment": "Since uxn word size is 16 we expect such big literals to get truncated accordingly. 1000000 == 0xf4240 -> 16960 == 0x4240. 123456789987654321 == 0x1b69b4be052fab1 -> 64177 == 0xfab1"
582582
},
583583
"6502": {
584-
"expected_stdout": "69\r\n1000000\r\n123456789987654321\r\n",
584+
"expected_stdout": "69\r\n16960\r\n64177\r\n",
585585
"state": "Enabled",
586-
"comment": ""
586+
"comment": "Since 6502 word size is 16 we expect such big literals to get truncated accordingly. 1000000 == 0xf4240 -> 16960 == 0x4240. 123456789987654321 == 0x1b69b4be052fab1 -> 64177 == 0xfab1"
587587
},
588588
"gas-x86_64-darwin": {
589589
"expected_stdout": "69\n1000000\n123456789987654321\n",

0 commit comments

Comments
 (0)