Advertisement
Aslai

BF Compiler

Oct 8th, 2011
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stack>
  3.  
  4. int main(){
  5.     //Initialise the butters
  6.     std::stack<int> s;         //Stack to hold '[' locations
  7.     char in[100000];           //input butter
  8.     printf( ": "); gets( in ); //Get the BF program
  9.     FILE* a = fopen( "out.bin", "wb" );
  10.     int p = 0x9D93; //This is the Texas Instruments magic number for where programs are loaded in memory
  11.  
  12.     fprintf( a, "%c%c", 0xBB, 0x6D ); p+=2;     //.DB BBh,6Dh       ;Declare an ASM program
  13.     fprintf( a, "%c%c%c", 0x21, 0xEC, 0x86 ); p+=3; //LD HL,86ECh   ;Safe RAM
  14.  
  15.     fprintf( a, "%c%c%c", 0x11, 0x00, 0x03 ); p+=3;         //LD DE,0300h      ;Set the counter to 0xFF
  16.     fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
  17.             0x36, 0,                    //LD (HL),0         ;Clear the next 768 bytes
  18.             0x23,                       //INC HL            ;...
  19.             0x1B,                       //DEC DE            ;...
  20.             0x7B,                       //LD A,E            ;...
  21.             0xFE, 0,                    //CP 0              ;...
  22.             0xC2, p&0xFF,(p>>8)&0xFF,   //JP NZ,(BEGINLOOP) ;...
  23.             0x7A,                       //LD A,D            ;...
  24.             0xFE, 0x00,                 //CP 0              ;...
  25.             0xC2, p&0xFF,(p>>8)&0xFF ); //JP NZ,(BEGINLOOP) ;...
  26.             p+=16;
  27.     char getAnstoDE[] = {   0x21, 0x78, 0x84,   //LD HL,OP1         ;Spell out the Ans variable
  28.                             0x36, 0x04,         //LD (HL),StrngObj  ;...
  29.                             0x23,               //INC HL            ;...
  30.                             0x36, 0x72,         //LD (HL),tAns      ;...
  31.                             0x23,               //INC HL            ;...
  32.                             0x36, 0x00,         //LD (HL),0         ;...
  33.                             0x23,               //INC HL            ;...
  34.                             0x36, 0x00,         //LD (HL),0         ;...
  35.                             0xEF, 0xF4, 0x42,   //B_CALL(_FindSym)  ;Load the pointer to a string in to DE
  36.                             0x1A, 0x6f,         //LD L,(DE)         ;Get the strings length in to HL
  37.                             0x13,               //INC DE            ;...
  38.                             0x1A, 0x67,         //LD H,(DE)         ;...
  39.                             0x13,               //INC DE            ;...
  40.                             0x19,               //ADD HL,DE         ;Move HL to the end of the string
  41.                             0x36, 0x00          //LD (HL),0         ;Null-terminate it
  42.                             };
  43.     fwrite( getAnstoDE, 26, 1, a ); p += 26;
  44.  
  45.     fprintf( a, "%c%c%c%c%c%c",
  46.             0x21, 0xEC, 0x86,           //LD HL,86ECh       ;Jump back to the start of the safe RAM
  47.             0x01, 0x00, 0x00 ); p+=6;   //LD BC,0            ;Set the bounds checker to 0
  48.  
  49.     for( int i = 0; in[i] != 0; i ++ ){
  50.         switch( in[i] ){
  51.             case '+':
  52.                 fprintf( a, "%c", 0x34 ); p++;      //INC (HL)
  53.                 break;
  54.             case '-':
  55.                 fprintf( a, "%c", 0x35 ); p++;      //DEC (HL)
  56.                 break;
  57.             case '>':
  58.                 fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c",
  59.                     0x23,           //INC HL    ;Move the pointer forwards
  60.                     0x03,           //INC BC    ;Increase the bounds checker
  61.                     0x78,           //LD A,B    ;Move the Bounds Checker high byte to the Accumulator
  62.                     0xFE, 0x03,     //CP 3      ;Check if we're at max
  63.                     0x20, 0x06,     //JR NZ,6    ;If it is 3, then move the pointer back by 768
  64.                     0x25,           //DEC H     ;...
  65.                     0x25,           //DEC H     ;...
  66.                     0x25,           //DEC H     ;...
  67.                     0x01, 0x00, 0x00//LD BC,0   ;Reset the bounds checker
  68.                      );p+=13;
  69.                 break;
  70.             case '<':
  71.                 fprintf( a, "%c%c%c%c%c%c%c%c%c%c%c%c%c",
  72.                     0x2B,           //DEC HL    ;Move the pointer backwards
  73.                     0x0B,           //DEC BC    ;Decrease the bounds checker
  74.                     0x78,           //LD A,B    ;Move the Bounds Checker high byte to the Accumulator
  75.                     0xFE, 0xFF,     //CP FFh    ;Check if we're at -1
  76.                     0x20, 0x06,     //JR NZ,6   ;If it is -1, then move the pointer forward by 768
  77.                     0x24,           //INC H     ;...
  78.                     0x24,           //INC H     ;...
  79.                     0x24,           //INC H     ;...
  80.                     0x01, 0xFF, 0x02//LD BC,02FFh ;Reset the bounds checker
  81.                      );p+=13;
  82.                 break;
  83.             case '[':
  84.             {
  85.                 s.push( p+6 );  //Track the location of this for jumping back
  86.                 int pp = p+6;     //Temp pointer
  87.                 printf("PP: %i;", pp );
  88.                 for( int j = i+1, depth = 1; in[j] != 0; j++ ){     //Find the matching ']'
  89.                     if( in[j] == '[' ) depth ++;
  90.                     if( in[j] == ']' ) depth --;
  91.                     if( in[j] == '+' || in[j] == '-' ){
  92.                         pp++;
  93.                         putc('+',stdout);
  94.                     }
  95.                     if( in[j] == '<' || in[j] == '>' ){
  96.                         pp+=13;putc('>',stdout);
  97.                     }
  98.  
  99.                     if( in[j] == '[' || in[j] == ']' ){
  100.                         pp+=6;putc('[',stdout);
  101.                     }
  102.                     if( in[j] == '.'){
  103.                         pp+=4;putc('.',stdout);
  104.                     }
  105.                     if( in[j] == ','){
  106.                         pp+=7;
  107.                         putc(',',stdout);
  108.                     }
  109.  
  110.                     if( depth <= 0 ) break;
  111.                 }
  112.                 printf(" PP: %i; diff: %i\n", pp, pp - p - 6 );
  113.  
  114.                 fprintf( a, "%c%c%c%c%c%c",
  115.                         0x7E,                       //LD A,(HL)         ;Grab the byte at the pointer and jump to the matching ']' if zero
  116.                         0xFE, 0x00,                 //CP 0              ;...
  117.                         0xCA, pp&0xFF, (pp>>8)&0xFF //JP Z,(MATCHING ]) ;...
  118.                          ); p+=6;
  119.             } break;
  120.             case ']':
  121.             {
  122.                 int pp = s.top( );
  123.                 s.pop();
  124.                 fprintf( a, "%c%c%c%c%c%c",
  125.                         0x7E,                       //LD A,(HL)             ;Grab the byte at the pointer and jump to the matching '[' if not zero
  126.                         0xFE, 0x00,                 //CP 0                  ;...
  127.                         0xC2, pp&0xFF, (pp>>8)&0xFF //JP NZ,(MATCHING [)    ;...
  128.                         ); p+=6;
  129.             } break;
  130.             case '.':
  131.             {
  132.                 fprintf( a, "%c%c%c%c",
  133.                         0x7E,               //LD A,(HL)     ;Print the character being pointed at
  134.                         0xEF, 0x04, 0x45    //B_CALL(PutC)  ;...
  135.                         ); p+=4;
  136.             } break;
  137.             case ',':
  138.             {
  139.                 fprintf( a, "%c%c%c%c%c%c%c",
  140.                         0x1A,       //LD A,(DE) ;Get the next char on the input stream
  141.                         0x77,       //LD (HL),A ;Stick it in to the memory at pointer
  142.                         0xFE, 0x00, //CP 0      ;If it's zero, then don't increase the input stream pointer
  143.                         0x28, 0x01, //JR Z,1    ;...
  144.                         0x13);p+=7; //INC DE    ;...
  145.             } break;
  146.  
  147.         }
  148.     }
  149.     fprintf( a, "%c", 0xC9 ); p+=1; //RET   ;Return control to the TI-OS
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement