Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stack>
- 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
- for( int i = 0; in[i] != 0; i ++ ){
- switch( in[i] ){
- case '+':
- fprintf( a, "%c", 0x34 ); p++; //INC (HL)
- break;
- case '-':
- fprintf( a, "%c", 0x35 ); p++; //DEC (HL)
- break;
- case '>':
- fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c",
- 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
- );p+=13;
- break;
- case '<':
- fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c",
- 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
- );p+=13;
- break;
- case '[':
- {
- s.push( p+6 ); //Track the location of this for jumping back
- int pp = p+6; //Temp pointer
- printf("PP: %i;", pp );
- 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] == '-' ){
- pp++;
- putc('+',stdout);
- }
- if( in[j] == '<' || in[j] == '>' ){
- pp+=13;putc('>',stdout);
- }
- if( in[j] == '[' || in[j] == ']' ){
- pp+=6;putc('[',stdout);
- }
- if( in[j] == '.'){
- pp+=4;putc('.',stdout);
- }
- if( in[j] == ','){
- pp+=7;
- putc(',',stdout);
- }
- if( depth <= 0 ) break;
- }
- printf(" PP: %i; diff: %i\n", pp, pp - p - 6 );
- 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 ']':
- {
- 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 '.':
- {
- 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 ',':
- {
- fprintf( a, "%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);p+=7; //INC DE ;...
- } break;
- }
- }
- fprintf( a, "%c", 0xC9 ); p+=1; //RET ;Return control to the TI-OS
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement