Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stack>
- #define AHAN \
- {\
- if( arithtrack != 0 ){\
- bool neg = arithtrack < 0;\
- if( arithtrack < 0 ) arithtrack = -arithtrack;\
- arithtrack = arithtrack & 0xFF;\
- if( !neg && arithtrack < 4 )\
- while( arithtrack-- > 0 )\
- {fprintf( a, "%c", 0x34 ); p++;} \
- else if( neg && arithtrack < 4 )\
- while( arithtrack-- > 0 )\
- {fprintf( a, "%c", 0x35 ); p++;} \
- else if( !neg )\
- {fprintf( a, "%c%c%c%c", \
- 0x7E, \
- 0xC6, arithtrack&0xFF, \
- 0x77 \
- ); p+=4;} \
- else if( neg )\
- {fprintf( a, "%c%c%c%c", \
- 0x7E, \
- 0xD6, (arithtrack)&0xFF, \
- 0x77 \
- ); p+=4;}\
- arithtrack = 0;\
- }\
- }
- #define MHAN\
- {\
- if( movtrack != 0 )\
- {\
- while( movtrack > 0 ){\
- fprintf( a, "%c%c%c%c",\
- 0xCD, GoRight&0xff, (GoRight>>8)&0xff, \
- movtrack&0xff \
- ); p += 4;\
- movtrack >>= 8;\
- }\
- if( movtrack < 0 ){\
- movtrack = -movtrack;\
- while( movtrack > 0 ){\
- fprintf( a, "%c%c%c%c",\
- 0xCD, GoLeft&0xff, (GoLeft>>8)&0xff, \
- movtrack&0xff \
- ); p += 4;\
- movtrack >>= 8;\
- }\
- }\
- movtrack = 0;\
- }\
- }
- int main(){
- //Initialise the butters
- std::stack<int> s; //Stack to hold '[' locations
- char in[100000]; //input butter
- printf( ": "); gets( in ); //Get the BF program
- FILE* a = fopen( "out.bin", "wb" );
- int p = 0x9D93; //This is the Texas Instruments magic number for where programs are loaded in memory
- fprintf( a, "%c%c", 0xBB, 0x6D ); p+=2; //.DB BBh,6Dh ;Declare an ASM program
- fprintf( a, "%c%c%c", 0x21, 0xEC, 0x86 ); p+=3; //LD HL,86ECh ;Safe RAM
- fprintf( a, "%c%c%c", 0x11, 0x00, 0x03 ); p+=3; //LD DE,0300h ;Set the counter to 0xFF
- fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
- 0x36, 0, //LD (HL),0 ;Clear the next 768 bytes
- 0x23, //INC HL ;...
- 0x1B, //DEC DE ;...
- 0x7B, //LD A,E ;...
- 0xFE, 0, //CP 0 ;...
- 0xC2, p&0xFF,(p>>8)&0xFF, //JP NZ,(BEGINLOOP) ;...
- 0x7A, //LD A,D ;...
- 0xFE, 0x00, //CP 0 ;...
- 0xC2, p&0xFF,(p>>8)&0xFF ); //JP NZ,(BEGINLOOP) ;...
- p+=16;
- char getAnstoDE[] = { 0x21, 0x78, 0x84, //LD HL,OP1 ;Spell out the Ans variable
- 0x36, 0x04, //LD (HL),StrngObj ;...
- 0x23, //INC HL ;...
- 0x36, 0x72, //LD (HL),tAns ;...
- 0x23, //INC HL ;...
- 0x36, 0x00, //LD (HL),0 ;...
- 0x23, //INC HL ;...
- 0x36, 0x00, //LD (HL),0 ;...
- 0xEF, 0xF4, 0x42, //B_CALL(_FindSym) ;Load the pointer to a string in to DE
- 0x1A, 0x6f, //LD L,(DE) ;Get the strings length in to HL
- 0x13, //INC DE ;...
- 0x1A, 0x67, //LD H,(DE) ;...
- 0x13, //INC DE ;...
- 0x19, //ADD HL,DE ;Move HL to the end of the string
- 0x36, 0x00 //LD (HL),0 ;Null-terminate it
- };
- fwrite( getAnstoDE, 26, 1, a ); p += 26;
- fprintf( a, "%c%c%c%c%c%c",
- 0x21, 0xEC, 0x86, //LD HL,86ECh ;Jump back to the start of the safe RAM
- 0x01, 0x00, 0x00 ); p+=6; //LD BC,0 ;Set the bounds checker to 0
- fprintf( a, "%c%c%c",
- 0xC3, (p+81)&0xFF,((p+81)>>8)&0xFF //JP (After defs) ;Skip the definition code
- ); p += 3;
- //Define <
- int GoLeft = p;
- fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
- 0xDD, 0xE1, //POP IX
- 0xDD, 0x7E, 0, //LD A,(IX+0)
- 0x3D, //DEC A
- 0x32, (GoLeft+20)&0xFF, ((GoLeft+20)>>8)&0xFF, //LD (ArgSpot),A ;Set up arg for recursion
- 0xDD, 0x23, //INC IX
- 0xDD, 0xE5, //PUSH IX
- 0xFE, 0x00, //CP 0
- 0x28, 0x04, //JR Z,4
- 0xCD, (GoLeft)&0xFF, (GoLeft>>8)&0xFF, //CALL GoLeft
- 0x00,
- 0x23, //INC HL ;Move the pointer forwards
- 0x03, //INC BC ;Increase the bounds checker
- 0x78, //LD A,B ;Move the Bounds Checker high byte to the Accumulator
- 0xFE, 0x03, //CP 3 ;Check if we're at max
- 0x20, 0x06, //JR NZ,6 ;If it is 3, then move the pointer back by 768
- 0x25, //DEC H ;...
- 0x25, //DEC H ;...
- 0x25, //DEC H ;...
- 0x01, 0x00,0x00,//LD BC,0 ;Reset the bounds checker
- 0xC9 //RET
- );p+=35;
- //define >
- int GoRight = p;
- fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
- 0xDD, 0xE1, //POP IX
- 0xDD, 0x7E, 0, //LD A,(IX+0)
- 0x3D, //DEC A
- 0x32, (GoRight+20)&0xFF, ((GoRight+20)>>8)&0xFF, //LD (ArgSpot),A ;Set up arg for recursion
- 0xDD, 0x23, //INC IX
- 0xDD, 0xE5, //PUSH IX
- 0xFE, 0x00, //CP 0
- 0x28, 0x04, //JR Z,4
- 0xCD, (GoRight)&0xFF, (GoRight>>8)&0xFF, //CALL GoRight
- 0x00,
- 0x2B, //DEC HL ;Move the pointer backwards
- 0x0B, //DEC BC ;Decrease the bounds checker
- 0x78, //LD A,B ;Move the Bounds Checker high byte to the Accumulator
- 0xFE, 0xFF, //CP FFh ;Check if we're at -1
- 0x20, 0x06, //JR NZ,6 ;If it is -1, then move the pointer forward by 768
- 0x24, //INC H ;...
- 0x24, //INC H ;...
- 0x24, //INC H ;...
- 0x01, 0xFF,0x02,//LD BC,02FFh ;Reset the bounds checker
- 0xC9 //RET
- );p+=35;
- //Define ,
- int GetC = p;
- fprintf( a, "%c%c%c%c%c%c%c%c",
- 0x1A, //LD A,(DE) ;Get the next char on the input stream
- 0x77, //LD (HL),A ;Stick it in to the memory at pointer
- 0xFE, 0x00, //CP 0 ;If it's zero, then don't increase the input stream pointer
- 0x28, 0x01, //JR Z,1 ;...
- 0x13, //INC DE ;...
- 0xC9 //RET
- );p+=8;
- int arithtrack = 0;
- int movtrack = 0;
- for( int i = 0; in[i] != 0; i ++ ){
- switch( in[i] ){
- case '+':{
- MHAN
- arithtrack++;
- }break;
- case '-':{
- MHAN
- arithtrack--;
- }break;
- case '>':{
- AHAN
- movtrack++;
- }break;
- case '<':{
- AHAN
- movtrack--;
- }break;
- case '[':
- {
- AHAN
- MHAN
- s.push( p+6 ); //Track the location of this for jumping back
- int pp = p+6; //Temp pointer
- int arithchange = 0;
- int movcnt = 0;
- for( int j = i+1, depth = 1; in[j] != 0; j++ ){ //Find the matching ']'
- if( in[j] == '[' ) depth ++;
- if( in[j] == ']' ) depth --;
- if( in[j] == '+' || in[j] == '-'){
- if( in[j] == '+' ) arithchange ++;
- if( in[j] == '-' ) arithchange --;
- }
- else{
- if( arithchange < 0 ) arithchange = - arithchange;
- if( arithchange >=4 ) pp += 4;
- else pp += arithchange;
- arithchange = 0;
- }
- if( in[j] == '<' || in[j] == '>' ){
- if( in[j] == '<' ) movcnt--;
- if( in[j] == '>' ) movcnt++;
- //pp += 3;
- }
- else if( movcnt != 0 ){
- if( movcnt < 0 ) movcnt = -movcnt;
- while( movcnt > 0 ){
- pp += 4;
- movcnt >>= 8;
- }
- }
- if( in[j] == '[' || in[j] == ']' ){
- pp+=6;
- }
- if( in[j] == '.'){
- pp+=4;
- }
- if( in[j] == ','){
- pp += 3;
- }
- if( depth <= 0 ) break;
- }
- fprintf( a, "%c%c%c%c%c%c",
- 0x7E, //LD A,(HL) ;Grab the byte at the pointer and jump to the matching ']' if zero
- 0xFE, 0x00, //CP 0 ;...
- 0xCA, pp&0xFF, (pp>>8)&0xFF //JP Z,(MATCHING ]) ;...
- ); p+=6;
- } break;
- case ']':
- {
- AHAN
- MHAN
- int pp = s.top( );
- s.pop();
- fprintf( a, "%c%c%c%c%c%c",
- 0x7E, //LD A,(HL) ;Grab the byte at the pointer and jump to the matching '[' if not zero
- 0xFE, 0x00, //CP 0 ;...
- 0xC2, pp&0xFF, (pp>>8)&0xFF //JP NZ,(MATCHING [) ;...
- ); p+=6;
- } break;
- case '.':
- {
- AHAN
- MHAN
- fprintf( a, "%c%c%c%c",
- 0x7E, //LD A,(HL) ;Print the character being pointed at
- 0xEF, 0x04, 0x45 //B_CALL(PutC) ;...
- ); p+=4;
- } break;
- case ',':
- {
- AHAN
- MHAN
- fprintf( a, "%c%c%c",
- 0xCD, GetC&0xff, (GetC>>8)&0xff //CALL GetC
- ); p += 3;
- } break;
- }
- }
- AHAN
- MHAN
- fprintf( a, "%c", 0xC9 ); p+=1; //RET ;Return control to the TI-OS
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement