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", "w" );
- 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
- 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", 0x21, 0xEC, 0x86 ); p+=3; //LD HL,86ECh ;Safe RAM
- fprintf( a, "%c%c", 0x3E, 0xFF ); p+=2; //LD A,FFh ;Set the counter to 0xFF
- fprintf( a, "%c%c%c%c%c%c%c%c%c",
- 0x36, 0, //LD (HL),0 ;Clear the next 256 bytes
- 0x23, //INC HL ;...
- 0x3D, //DEC A ;...
- 0xFE, 0, //CP 0 ;...
- 0xC2, p&0xFF,(p>>8)&0xFF ); //JP NZ,(BEGINLOOP) ;...
- p+=9;
- fprintf( a, "%c%c%c%c%c",
- 0x21, 0xEC, 0x86, //LD HL,86ECh ;Jump back to the start of the safe RAM
- 0x06, 0x00 ); p+=5; //LD B,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",
- 0x23, //INC HL ;Move the pointer forwards
- 0x78, //LD A,B ;Move the Bounds Checker to the Accumulator
- 0xC6, 0x01, //ADD A,1 ;Increase the bounds checker
- 0x30,0x01, //JR NC,1 ;If there was an overflow, then move the pointer back by 256
- 0x25, //DEC H ;...
- 0x47 );p+=8;//LD B,A ;Move the Accumulator to the Bounds Checker
- break;
- case '<':
- fprintf( a, "%c%c%c%c%c%c%c%c%c",
- 0x2B, //DEC HL ;Move the pointer backwards
- 0x78, //LD A,B ;Move the Bounds Checker to the Accumulator
- 0xD6, 0x01, //SUB A,1 ;Decrease the bounds checker
- 0x30, 0x01, //JR NC,1 ;If there was an overflow, then move the pointer forward by 256
- 0x24, //INC H ;...
- 0x47 );p+=8;//LD B,A ;Move the Accumulator to the Bounds Checker
- break;
- case '[':
- {
- s.push( p+6 ); //Track the location of this for jumping back
- int pp = p; //Temp pointer
- 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++;
- }
- if( in[j] == '<' || in[j] == '>' ){
- pp+=8;
- }
- if( in[j] == '[' || in[j] == ']' ){
- pp+=6;
- }
- if( in[j] == '.'){
- pp+=4;
- }
- if( in[j] == ','){
- pp+=7;
- }
- 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 ']':
- {
- 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