Advertisement
Aslai

BF Compiler

Oct 8th, 2011
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 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", "w" );
  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.     char getAnstoDE[] = {   0x21, 0x78, 0x84,   //LD HL,OP1         ;Spell out the Ans variable
  14.                             0x36, 0x04,         //LD (HL),StrngObj  ;...
  15.                             0x23,               //INC HL            ;...
  16.                             0x36, 0x72,         //LD (HL),tAns      ;...
  17.                             0x23,               //INC HL            ;...
  18.                             0x36, 0x00,         //LD (HL),0         ;...
  19.                             0x23,               //INC HL            ;...
  20.                             0x36, 0x00,         //LD (HL),0         ;...
  21.                             0xEF, 0xF4, 0x42,   //B_CALL(_FindSym)  ;Load the pointer to a string in to DE
  22.                             0x1A, 0x6f,         //LD L,(DE)         ;Get the strings length in to HL
  23.                             0x13,               //INC DE            ;...
  24.                             0x1A, 0x67,         //LD H,(DE)         ;...
  25.                             0x13,               //INC DE            ;...
  26.                             0x19,               //ADD HL,DE         ;Move HL to the end of the string
  27.                             0x36, 0x00          //LD (HL),0         ;Null-terminate it
  28.                             };
  29.     fwrite( getAnstoDE, 26, 1, a ); p += 26;
  30.     fprintf( a, "%c%c%c", 0x21, 0xEC, 0x86 ); p+=3; //LD HL,86ECh   ;Safe RAM
  31.  
  32.     fprintf( a, "%c%c", 0x3E, 0xFF ); p+=2;         //LD A,FFh      ;Set the counter to 0xFF
  33.     fprintf( a, "%c%c%c%c%c%c%c%c%c",
  34.             0x36, 0,                    //LD (HL),0         ;Clear the next 256 bytes
  35.             0x23,                       //INC HL            ;...
  36.             0x3D,                       //DEC A             ;...
  37.             0xFE, 0,                    //CP 0              ;...
  38.             0xC2, p&0xFF,(p>>8)&0xFF ); //JP NZ,(BEGINLOOP) ;...
  39.             p+=9;
  40.     fprintf( a, "%c%c%c%c%c",
  41.             0x21, 0xEC, 0x86,           //LD HL,86ECh       ;Jump back to the start of the safe RAM
  42.             0x06, 0x00 ); p+=5;         //LD B,0            ;Set the bounds checker to 0
  43.  
  44.     for( int i = 0; in[i] != 0; i ++ ){
  45.         switch( in[i] ){
  46.             case '+':
  47.                 fprintf( a, "%c", 0x34 ); p++;      //INC (HL)
  48.                 break;
  49.             case '-':
  50.                 fprintf( a, "%c", 0x35 ); p++;      //DEC (HL)
  51.                 break;
  52.             case '>':
  53.                 fprintf( a, "%c%c%c%c%c%c%c%c%c",
  54.                     0x23,       //INC HL    ;Move the pointer forwards
  55.                     0x78,       //LD A,B    ;Move the Bounds Checker to the Accumulator
  56.                     0xC6, 0x01, //ADD A,1   ;Increase the bounds checker
  57.                     0x30,0x01,  //JR NC,1   ;If there was an overflow, then move the pointer back by 256
  58.                     0x25,       //DEC H     ;...
  59.                     0x47 );p+=8;//LD B,A    ;Move the Accumulator to the Bounds Checker
  60.                 break;
  61.             case '<':
  62.                 fprintf( a, "%c%c%c%c%c%c%c%c%c",
  63.                 0x2B,       //DEC HL    ;Move the pointer backwards
  64.                 0x78,       //LD A,B    ;Move the Bounds Checker to the Accumulator
  65.                 0xD6, 0x01, //SUB A,1   ;Decrease the bounds checker
  66.                 0x30, 0x01, //JR NC,1   ;If there was an overflow, then move the pointer forward by 256
  67.                 0x24,       //INC H     ;...
  68.                 0x47 );p+=8;//LD B,A    ;Move the Accumulator to the Bounds Checker
  69.                 break;
  70.             case '[':
  71.             {
  72.                 s.push( p+6 );  //Track the location of this for jumping back
  73.                 int pp = p;     //Temp pointer
  74.                 for( int j = i+1, depth = 1; in[j] != 0; j++ ){     //Find the matching ']'
  75.                     if( in[j] == '[' ) depth ++;
  76.                     if( in[j] == ']' ) depth --;
  77.                     if( in[j] == '+' || in[j] == '-' ){
  78.                         pp++;
  79.                     }
  80.                     if( in[j] == '<' || in[j] == '>' ){
  81.                         pp+=8;
  82.                     }
  83.  
  84.                     if( in[j] == '[' || in[j] == ']' ){
  85.                         pp+=6;
  86.                     }
  87.                     if( in[j] == '.'){
  88.                         pp+=4;
  89.                     }
  90.                     if( in[j] == ','){
  91.                         pp+=7;
  92.                     }
  93.  
  94.                     if( depth <= 0 ) break;
  95.                 }
  96.                 fprintf( a, "%c%c%c%c%c%c",
  97.                         0x7E,                       //LD A,(HL)         ;Grab the byte at the pointer and jump to the matching ']' if zero
  98.                         0xFE, 0x00,                 //CP 0              ;...
  99.                         0xCA, pp&0xFF, (pp>>8)&0xFF //JP Z,(MATCHING ]) ;...
  100.                          ); p+=6;
  101.             } break;
  102.             case ']':
  103.             {
  104.                 int pp = s.top( );
  105.                 s.pop();
  106.                 fprintf( a, "%c%c%c%c%c%c",
  107.                         0x7E,                       //LD A,(HL)             ;Grab the byte at the pointer and jump to the matching '[' if not zero
  108.                         0xFE, 0x00,                 //CP 0                  ;...
  109.                         0xC2, pp&0xFF, (pp>>8)&0xFF //JP NZ,(MATCHING [)    ;...
  110.                         ); p+=6;
  111.             } break;
  112.             case '.':
  113.             {
  114.                 fprintf( a, "%c%c%c%c",
  115.                         0x7E,               //LD A,(HL)     ;Print the character being pointed at
  116.                         0xEF, 0x04, 0x45    //B_CALL(PutC)  ;...
  117.                         ); p+=4;
  118.             } break;
  119.             case ',':
  120.             {
  121.                 fprintf( a, "%c%c%c%c%c%c%c",
  122.                         0x1A,       //LD A,(DE) ;Get the next char on the input stream
  123.                         0x77,       //LD (HL),A ;Stick it in to the memory at pointer
  124.                         0xFE, 0x00, //CP 0      ;If it's zero, then don't increase the input stream pointer
  125.                         0x28, 0x01, //JR Z,1    ;...
  126.                         0x13);p+=7; //INC DE    ;...
  127.             } break;
  128.  
  129.         }
  130.     }
  131.     fprintf( a, "%c", 0xC9 ); p+=1; //RET   ;Return control to the TI-OS
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement