Druzyek wrote:
This sounds really neat! Could you see how compiled code compares when you do loops and calculations? How about something along these lines:
Code:
int i,j,result;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
result=(i*10+j)*9/5+32;
printf("%d\n",result);
}
}
Actually, this is where C02 diverges from regular C. Because C02 is designed to compile directly to 6502 assembly, variables are limited to a single byte and mathematical operators are limited to available machine operations. And, because of the limits of addressing modes, the stack is not used during mathematical operations. Because of this multipluy and divide aren't available as normal operators, and functions may only appear as the first term of an argument, however functions may be nested.
Basically, the parser converts the expression into an LDA or JSR(s) followed by a series of ADC, SBC, AND, ORA, and EOR instructions.
To profile the above code, I will need to make the numbers a bit smaller so that interim results will fit in the range 0-255, and refactor the expression to work within the limits of C02. The parameters to printf are also reversed because of C02's calling convention for functions. In addition, escapes in strings are handled literally, so '\n' will not produce a newline on most 6502 systems. The current solution is to call the system-specific newlin() function.
So this is what the C02 code ends up looking like
Code:
char i,j,result;
for (i=0;i<5;i++) {
for (j=0;j<5;j++) {
result=div(mult(mult(i,5)+j,3),5)+32; //(i*5+j)*3/5+32;
printf(result,"%d");
newlin();
}
}
and the equivalent C code is now
Code:
char i,j,result;
for (i=0;i<5;i++) {
for (j=0;j<5;j++) {
result=(i*5+j)*3/5+32;
printf("%d\n",result);
}
}
The resulting assembly will be in the following reply.