Advertisement
Aslai

Brainfuck to z80 compiler

Oct 9th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stack>
  3. #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;}}
  4. #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;}}
  5. int main(){std::stack<int> s;char in[100000];printf(": ");gets( in );FILE*a=fopen("out.bin","wb");int errors=0;int p=0x9D93;fprintf(a,"%c%c",0xBB,0x6D);p+=2;fprintf(a,"%c%c%c",0x21,0xEC,0x86);p+=3;fprintf(a,"%c%c%c",0x11,0x00,0x03);p+=3;fprintf(a,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",0x36,0,0x23,0x1B,0x7B,0xFE,0,0xC2,p&0xFF,(p>>8)&0xFF,0x7A,0xFE,0x00,0xC2,p&0xFF,(p>>8)&0xFF );p+=16;fprintf(a,"%c%c%c%c",0xFD,0xCB,0x09,0xA6);p+=4;char getAnstoDE[]={0x21,0x78,0x84,0x36,0x04,0x23,0x36,0x72,0x23,0x36,0x00,0x23,0x36,0x00,0xEF,0xF4,0x42,0x1A,0x6f,0x13,0x1A,0x67,0x13,0x19,0x36,0x00};fwrite(getAnstoDE,26,1,a);p+=26;fprintf(a,"%c%c%c%c%c%c",0x21,0xEC,0x86,0x01,0x00,0x00);p+=6;fprintf(a,"%c%c%c",0xC3,(p+94)&0xFF,((p+94)>>8)&0xFF);p+=3;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,0xDD,0x7E,0,0x3D,0x32,(GoLeft+20)&0xFF,((GoLeft+20)>>8)&0xFF,0xDD,0x23,0xDD,0xE5,0xFE,0x00,0x28,0x04,0xCD,GoLeft&0xFF,(GoLeft>>8)&0xFF,0x00,0x23,0x03,0x78,0xFE,0x03,0x20,0x06,0x25,0x25,0x25,0x01,0x00,0x00,0xC9);p+=35;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,0xDD,0x7E,0,0x3D,0x32,(GoRight+20)&0xFF,((GoRight+20)>>8)&0xFF,0xDD,0x23,0xDD,0xE5,0xFE,0x00,0x28,0x04,0xCD,GoRight&0xFF,(GoRight>>8)&0xFF,0x00,0x2B,0x0B,0x78,0xFE,0xFF,0x20,0x06,0x24,0x24,0x24,0x01,0xFF,0x02,0xC9);p+=35;int GetC = p;fprintf(a,"%c%c%c%c%c%c%c%c",0x1A,0x77,0xFE,0x00,0x28,0x01,0x13,0xC9);p+=8;int OnCheck=p;fprintf(a,"%c%c%c%c%c%c%c%c%c%c%c%c%c",0xFD,0xCB,0x09,0x66,0x28,0x06,0xDD,0xE1,0xFD,0xCB,0x09,0xA6,0xC9);p+=13;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);int pp=p+6;int arithchange=0;int movcnt=0;int depth=1;for(int j=i+1;in[j]!=0;j++){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++;}else if(movcnt!=0){if(movcnt<0)movcnt=-movcnt;while(movcnt>0){pp+=4;movcnt>>=8;}}if(in[j] =='['||in[j]==']'){pp+=9;}if(in[j]=='.'){pp+=4;}if(in[j]==','){pp+=3;}if(depth<=0)break;}if(depth>0){printf( "ERROR: Unmatched '[' at position %i\n", i);errors++;}fprintf(a,"%c%c%c%c%c%c%c%c%c",0xCD,OnCheck&0xff,(OnCheck>>8)&0xff,0x7E,0xFE,0x00,0xCA,pp&0xFF,(pp>>8)&0xFF);p+=9;}break;case ']':{AHAN MHAN if(s.size()>0){int pp=s.top();s.pop();fprintf(a,"%c%c%c%c%c%c%c%c%c",0xCD,OnCheck&0xff,(OnCheck>>8)&0xff,0x7E,0xFE,0x00,0xC2,pp&0xFF,(pp>>8)&0xFF);p+=9;}else{printf("ERROR: Unmatched ']' at position %i\n",i);errors++;}}break;case '.':{AHAN MHAN fprintf(a,"%c%c%c%c",0x7E,0xEF,0x04,0x45);p+=4;}break;case ',':{AHAN MHAN fprintf(a,"%c%c%c",0xCD,GetC&0xff,(GetC>>8)&0xff);p+=3;}break;}}AHAN MHAN fprintf(a,"%c",0xC9);p+=1;fclose( a );if(errors>0){a=fopen("out.bin","wb");fprintf(a,"%c%c%c%c%c%c%c%c%cThere were errors in this program.%c%c",0xBB,0x6D,0x21,0x9C,0x9D,0xEF,0x0A,0x45,0xC9,0,0);fclose(a);}}
  6.  
  7.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement