Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdio.h>
- void changeLabels();
- void convert();
- void convertIntToString(char [], int);
- void convertStringOfIntToStringOfBinary(char[], char[]);
- void convertStringOfIntToStringOfBinary2(char[], char[]);
- void convertStringOfIntToStringOfBinary5(char[], char[]);
- void convertIntToBinary(int, char[]);
- void writeFile(char []);
- int main()
- {
- //changeLabels();
- convert();
- return EXIT_SUCCESS;
- }
- void convert() {
- int rs,rd,rt,i;
- char instruction[32];
- for(i = 0; i < 32; i++) {
- instruction[i] = '0';
- }
- FILE *r;
- r = fopen("assembly.txt", "r");
- if(r != NULL) {
- char line[256];
- while(!feof(r)) {
- if(fgets(line, 256, r) != NULL) {
- if((line[0] == 'a' && line[1] == 'd' && line[2] == 'd')
- || (line[0] == 's' && line[1] == 'u' && line[2] == 'b')
- || (line[0] == 'm' && line[1] == 'u' && line[2] == 'l')
- || (line[0] == 's' && line[1] == 'l' && line[2] == 't')
- || (line[0] == 'o' && line[1] == 'r' && line[2] == 32)
- || (line[0] == 'n' && line[1] == 'o' && line[2] == 'r')
- || (line[0] == 'a' && line[1] == 'n' && line[2] == 'd')) {
- rs = (int)line[5] - 48;
- rs = rs * 10;
- rs += line[6] - 48;
- rt = (int)line[9] - 48;
- rt = rt * 10;
- rt += line[10] - 48;
- rd = (int)line[12] - 48;
- rd = rd * 10;
- rd += line[13] - 48;
- for(i = 0; i < 6; i++) {
- instruction[i] = '0';
- }
- char regist1[5], regist2[5], regist3[5];
- convertIntToString(regist3, rs);
- convertIntToString(regist2, rt);
- convertIntToString(regist1, rd);
- for(i = 0; i < 5; i++) {
- instruction[6+i] = regist1[i];
- instruction[11+i] = regist2[i];
- instruction[16+i] = regist3[i];
- instruction[21+i] = '0';
- }
- if(line[0] == 'a' && line[1] == 'd' && line[2] == 'd') {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '0';
- instruction[30] = '0';
- instruction[31] = '0';
- }
- else if(line[0] == 'a' && line[1] == 'n' && line[2] == 'd') {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '1';
- instruction[30] = '0';
- instruction[31] = '0';
- }
- else if(line[0] == 'm' && line[1] == 'u' && line[2] == 'l') {
- instruction[26] = '0';
- instruction[27] = '1';
- instruction[28] = '1';
- instruction[29] = '0';
- instruction[30] = '0';
- instruction[31] = '0';
- }
- else if(line[0] == 'o' && line[1] == 'r' && line[2] == 32) {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '1';
- instruction[30] = '0';
- instruction[31] = '1';
- }
- else if(line[0] == 's' && line[1] == 'l' && line[2] == 't') {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '1';
- instruction[29] = '0';
- instruction[30] = '1';
- instruction[31] = '0';
- }
- else if(line[0] == 's' && line[1] == 'u' && line[2] == 'b') {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '0';
- instruction[30] = '1';
- instruction[31] = '0';
- }
- else if(line[0] == 'n' && line[1] == 'o' && line[2] == 'r') {
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '1';
- instruction[30] = '1';
- instruction[31] = '0';
- }
- }
- else if((line[0] == 'b' && line[1] == 'e' && line[2] == 'q')
- || (line[0] == 'l' && line[1] == 'w' && line[2] == 32)
- || (line[0] == 's' && line[1] == 'w' && line[2] == 32)
- || (line[0] == 'a' && line[1] == 'd' && line[2] == 'i')){
- rs = (int)line[5] - 48;
- rs = rs * 10;
- rs += line[6] - 48;
- rt = (int)line[9] - 48;
- rt = rt * 10;
- rt += line[10] - 48;
- char regist1[5], regist2[5];
- convertIntToString(regist2, rs);
- convertIntToString(regist1, rt);
- for(i = 0; i < 5; i++) {
- instruction[6+i] = regist1[i];
- instruction[11+i] = regist2[i];
- }
- char number[8];
- for(i = 0; i < 8; i++) {
- if(line[12+i] != 10) {
- number[i] = line[12+i];
- }
- }
- char label[16];
- convertStringOfIntToStringOfBinary(number, label);
- for(i = 0; i < 16; i++) {
- instruction[16+i] = label[i];
- }
- if(line[0] == 'b' && line[1] == 'e' && line[2] == 'q') {
- instruction[0] = '0';
- instruction[1] = '0';
- instruction[2] = '0';
- instruction[3] = '1';
- instruction[4] = '0';
- instruction[5] = '0';
- }
- if(line[0] == 'a' && line[1] == 'd' && line[2] == 'i') {
- instruction[0] = '0';
- instruction[1] = '0';
- instruction[2] = '1';
- instruction[3] = '0';
- instruction[4] = '0';
- instruction[5] = '0';
- }
- if(line[0] == 'l' && line[1] == 'w' && line[2] == 32) {
- instruction[0] = '1';
- instruction[1] = '0';
- instruction[2] = '0';
- instruction[3] = '0';
- instruction[4] = '1';
- instruction[5] = '1';
- }
- if(line[0] == 's' && line[1] == 'w' && line[2] == 32) {
- instruction[0] = '1';
- instruction[1] = '0';
- instruction[2] = '1';
- instruction[3] = '0';
- instruction[4] = '1';
- instruction[5] = '1';
- }
- }
- else if((line[0] == 'j' && line[1] == 'm' && line[2] == 'p')
- || (line[0] == 'j' && line[1] == 'a' && line[2] == 'l')){
- char number[12];
- for(i = 0; i < 12; i++) {
- if(line[4+i] != 10) {
- number[i] = line[4+i];
- }
- }
- char label[26];
- convertStringOfIntToStringOfBinary2(number, label);
- for(i = 0; i < 26; i++) {
- instruction[6+i] = label[i];
- }
- if(line[0] == 'j' && line[1] == 'm' && line[2] == 'p') {
- instruction[0] = '0';
- instruction[1] = '0';
- instruction[2] = '0';
- instruction[3] = '0';
- instruction[4] = '1';
- instruction[5] = '0';
- }
- if(line[0] == 'j' && line[1] == 'a' && line[2] == 'l') {
- instruction[0] = '0';
- instruction[1] = '0';
- instruction[2] = '0';
- instruction[3] = '0';
- instruction[4] = '1';
- instruction[5] = '1';
- }
- }
- else if(line[0] == 'h' && line[1] == 'l' && line[2] == 't') {
- for(i = 0; i < 32; i++) {
- instruction[i] = '1';
- }
- }
- else if(line[0] == 'm' && line[1] == 'o' && line[2] == 'v') {
- for(i = 0; i < 6; i++) {
- instruction[i] = '0';
- }
- for(i = 0; i < 6; i++) {
- instruction[16+i] = '0';
- instruction[21+i] = '0';
- }
- instruction[26] = '1';
- instruction[27] = '0';
- instruction[28] = '0';
- instruction[29] = '0';
- instruction[30] = '0';
- instruction[31] = '0';
- rs = (int)line[5] - 48;
- rs = rs * 10;
- rs += line[6] - 48;
- rt = (int)line[9] - 48;
- rt = rt * 10;
- rt += line[10] - 48;
- char regist1[5], regist2[5];
- convertIntToString(regist2, rs);
- convertIntToString(regist1, rt);
- for(i = 0; i < 5; i++) {
- instruction[6+i] = regist1[i];
- instruction[11+i] = regist2[i];
- }
- }
- else if((line[0] == 's' && line[1] == 'l' && line[2] == 'l')
- || (line[0] == 's' && line[1] == 'r' && line[2] == 'l')){
- for(i = 0; i < 6; i++) {
- instruction[i] = '0';
- }
- rs = (int)line[5] - 48;
- rs = rs * 10;
- rs += line[6] - 48;
- rt = (int)line[9] - 48;
- rt = rt * 10;
- rt += line[10] - 48;
- char regist1[5], regist2[5];
- convertIntToString(regist2, rs);
- convertIntToString(regist1, rt);
- for(i = 0; i < 5; i++) {
- instruction[6+i] = regist1[i];
- instruction[11+i] = regist2[i];
- instruction[16+i] = '0';
- instruction[26+i] = '0';
- }
- char number[8];
- for(i = 0; i < 8; i++) {
- if(line[12+i] != 10) {
- number[i] = line[12+i];
- }
- }
- char shift[5];
- convertStringOfIntToStringOfBinary5(number, shift);
- for(i = 0; i < 5; i++) {
- instruction[21+i] = shift[i];
- }
- }
- else if(line[0] == 'j' && line[1] == 'r' && line[2] == 32) {
- for(i = 0; i < 6; i++) {
- instruction[i] = '0';
- }
- rs = (int)line[5] - 48;
- rs = rs * 10;
- rs += line[6] - 48;
- char regist1[5];
- convertIntToString(regist1, rs);
- for(i = 0; i < 5; i++) {
- instruction[6+i] = regist1[i];
- instruction[11+i] = '0';
- instruction[16+i] = '0';
- instruction[21+i] = '0';
- }
- instruction[26] = '0';
- instruction[27] = '0';
- instruction[28] = '1';
- instruction[29] = '0';
- instruction[30] = '0';
- instruction[31] = '0';
- }
- writeFile(instruction);
- }
- }
- }
- }
- void changeLabels() {
- //~ char line[256];
- //~ FILE *p;
- //~ p = fopen("assembly.txt", "r");
- //~ if(p != NULL) {
- //~
- //~ }
- }
- void writeFile(char data[]) {
- int i;
- char end;
- end = 10;
- FILE *w;
- w = fopen("instructions.txt", "wa");
- if(w != NULL) {
- for(i = 0; i < 32; i++) {
- fprintf(w, "%c", data[i]);
- }
- fprintf(w, "%c", end);
- }
- }
- void convertStringOfIntToStringOfBinary(char number[], char label[]) {
- int numberInt;
- numberInt = atoi(number);
- convertIntToBinary(numberInt, label);
- }
- void convertStringOfIntToStringOfBinary2(char number[], char jump[]) {
- }
- void convertStringOfIntToStringOfBinary5(char number[], char shift[]) {
- }
- void convertIntToBinary(int numberInt, char label[]) {
- }
Advertisement
Add Comment
Please, Sign In to add comment