Added basic putuint to the standard library, i need to implement proper 0 handling but that probably needs and if statement

This commit is contained in:
cannoli-fruit 2026-07-26 01:48:43 -04:00
commit 5b7dd220ae
6 changed files with 94 additions and 2 deletions

View file

@ -6,6 +6,22 @@ proc getchar
0xCFFFFFFFFFFFFFFE ld8
endproc
proc putuint
@val
1 @divisor
while &val &divisor u/ 9 u> do
10 &divisor u* @divisor
endwhile
while &divisor 0 u> do
&val &divisor u/ 10 u%
48 u+ :putchar
&divisor 10 u/ @divisor
endwhile
endproc
proc puts
while dup ld8 0 == not do
dup ld8 :putchar

View file

@ -85,6 +85,9 @@ static inline bool isKW(Lz_SB *sb) {
|| SBeq(sb, "u-")
|| SBeq(sb, "u*")
|| SBeq(sb, "u/")
|| SBeq(sb, "u%")
|| SBeq(sb, "u>")
|| SBeq(sb, "u<")
|| SBeq(sb, "pop")
|| SBeq(sb, "$inc")
;
@ -276,6 +279,47 @@ codegen:
printf(" pushstack 98\n");
break;
}
if (SBeq(&tokenAccumulator, "u-")) {
printf(" popstack 97\n");
printf(" popstack 98\n");
printf(" SubR64 98, 97\n");
printf(" pushstack 98\n");
break;
}
if (SBeq(&tokenAccumulator, "u*")) {
printf(" popstack 97\n");
printf(" popstack 98\n");
printf(" MulR64 98, 97\n");
printf(" pushstack 98\n");
break;
}
if (SBeq(&tokenAccumulator, "u/")) {
printf(" popstack 97\n");
printf(" popstack 98\n");
printf(" DivR64 98, 97\n");
printf(" pushstack 98\n");
break;
}
if (SBeq(&tokenAccumulator, "u%")) {
printf(" popstack 97\n");
printf(" popstack 98\n");
printf(" ModR64 98, 97\n");
printf(" pushstack 98\n");
break;
}
if (SBeq(&tokenAccumulator, "u>")) {
printf(" popstack 97\n");
printf(" popstack 98\n");
printf(" Cmp64 98, 97\n");
printf(" JmpIP __Cmp%d\n", counter);
printf(" MovI64 112, 0\n");
printf(" Jmp __DoneCmp%d\n", counter);
printf("__Cmp%d:\n", counter);
printf(" MovI64 112, 1\n");
printf("__DoneCmp%d:\n", counter);
printf(" pushstack 112\n");
break;
}
if (SBeq(&tokenAccumulator, "pop")) {
printf(" popstack 113\n");
break;
@ -354,7 +398,7 @@ codegen:
if (tokenAccumulator.cnt < 128) {
memcpy(varName, tokenAccumulator.data+1,
tokenAccumulator.cnt-1);
varName[tokenAccumulator.cnt] = 0;
varName[tokenAccumulator.cnt-1] = 0;
} else {
memcpy(varName, tokenAccumulator.data+1, 127);
varName[127] = 0;
@ -367,7 +411,7 @@ codegen:
}
}
if (addr == 0) {
fprintf(stderr, "Undeclared variable %s in function %s",
fprintf(stderr, "Undeclared variable %s in function %s\n",
varName, funcName);
exit(1);
}

6
flagpole/tests/div.fp Normal file
View file

@ -0,0 +1,6 @@
$inc fvmstd.fp
proc main
8 2 u/
48 u+ :putchar :newline
endproc

19
flagpole/tests/gt.fp Normal file
View file

@ -0,0 +1,19 @@
$inc fvmstd.fp
proc main
5 3 u>
48 u+
"5 3 u>: " :puts
:putchar :newline
3 3 u>
48 u+
"3 3 u>: " :puts
:putchar :newline
3 5 u>
48 u+
"3 5 u>: " :puts
:putchar :newline
endproc

View file

@ -0,0 +1,6 @@
$inc fvmstd.fp
proc main
"Hello World!" :puts :newline
23 :putuint
endproc

View file

@ -251,6 +251,7 @@ int main(int argc, char **argv) {
case 0x72: { // DivR64
unsigned char dst = inputs[0];
unsigned char src = inputs[1];
if (registers[src] == 0) break; // Div by 0 smh
registers[dst] /= registers[src];
break;
}