Last active
December 27, 2015 06:18
-
-
Save hjpotter92/7280121 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ADD 111 | |
SUB 112 | |
MOV 113 | |
XOR 114 | |
CMP 115 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdlib.h> | |
# include <string.h> | |
# include <stdio.h> | |
# include <time.h> | |
struct stud { | |
char command[10]; | |
char var1[50]; | |
char var2[50]; | |
int com, first, second; | |
}; | |
typedef struct stud node; | |
char *ToUpper( char *str ) { | |
char *newstr, *p; | |
p = newstr = strdup(str); | |
while( *p = toupper(*p) ) p++; | |
return newstr; | |
} | |
int check_constant( char var[] ) { | |
int i = -1; | |
while( var[++i] != '\0' ) { | |
if( var[i] <= '9' && var[i] >= '0' ) continue; | |
else return 0; | |
} | |
return 1; | |
} | |
node check_reg( node head ) { | |
FILE *fp = fopen( "reg", "r" ); | |
if( fp == NULL ) exit( EXIT_FAILURE ); | |
int flag1 = 0, flag2 = 0, x; | |
char temp_reg[10]; | |
if( check_constant(head.var1) == 1 ) { | |
printf( "error: improper operand type\n" ); | |
exit( EXIT_FAILURE ); | |
} | |
while( fscanf(fp, "%[^ ] %d", temp_reg, &x) != EOF ) { | |
if( temp_reg[0] == '\n' ) memmove( temp_reg, temp_reg + 1, strlen(temp_reg) ); | |
if( strcasecmp(temp_reg, head.var1) == 0 ) { | |
head.first = x; | |
flag1 = 1; | |
} | |
if( strcasecmp(temp_reg, head.var2) == 0 ) { | |
head.second = x; | |
flag2 = 1; | |
} | |
if( flag1 == 1 && flag2 == 1 ) break; | |
} | |
srand (time(NULL)); | |
if( flag1 == 0 ) { | |
head.first = rand() % 9000 + 200; | |
} | |
if( flag2 == 0 ) { | |
head.second = rand() % 18000 + 400; | |
} | |
if( flag1 && flag2 && head.first == head.second ) { | |
printf( "error: different registers expected\n" ); | |
exit( EXIT_FAILURE ); | |
} | |
fclose(fp); | |
return head; | |
} | |
node check_command( node head ) { | |
FILE *fp = fopen( "instruction", "r" ); | |
if( fp == NULL ) exit( EXIT_FAILURE ); | |
char temp_command[10]; | |
int flag = 0, x; | |
while( fscanf(fp, "%[^ ] %d", temp_command, &x) != EOF ) { | |
if( temp_command[0] == '\n' ) memmove( temp_command, temp_command + 1, strlen(temp_command) ); | |
if( strcasecmp(temp_command, head.command) == 0 ) { | |
head.com = x; | |
flag = 1; | |
break; | |
} | |
} | |
if( flag == 0 ) { | |
printf("error: command does not exist\n"); | |
exit( EXIT_FAILURE ); | |
} | |
fclose(fp); | |
return head; | |
} | |
node read( node head, char *str ) { | |
int i = 0; | |
while( *str == ' ' ) str++; | |
while( *str != ' ' ) { | |
head.command[i++] = *str; | |
str++; | |
} | |
head.command[i] = '\0'; | |
while( *str == ' ' ) str++; | |
i = 0; | |
while( *str != ' ' && *str != ',' ) { | |
head.var1[i++] = *str; | |
str++; | |
} | |
head.var1[i] = '\0'; | |
while( *str == ' ' ) str++; | |
if( *str == ',' ) str++; | |
else { | |
printf( "error: expected punctuation ','\n" ); | |
exit( EXIT_FAILURE ); | |
} | |
while( *str == ' ' ) str++; | |
i = 0; | |
while(*str != ' ' && *str != '\0') { | |
head.var2[i++] = *str; | |
str++; | |
} | |
head.var2[i] = '\0'; | |
return head; | |
} | |
int main() { | |
node head; | |
int reg_res, command_res; | |
char *str = (char *) malloc( 100 * sizeof(char) ); | |
FILE *fp = fopen( "code", "w" ); | |
if( fp == NULL ) exit( EXIT_FAILURE ); | |
printf("enter instruction:\n"); | |
scanf( "%[^\n]s", str ); | |
head = read( head, str ); | |
head = check_command( head ); | |
head = check_reg( head ); | |
fprintf( fp, "Command: %s %s, %s\nbyte-code: %08x\t%04x %04x\n", ToUpper(head.command), ToUpper(head.var1), ToUpper(head.var2), head.com, head.first, head.second ); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AH 11 | |
AL 12 | |
AX 13 | |
BH 14 | |
BL 15 | |
BX 16 | |
CH 17 | |
CL 18 | |
CX 19 | |
DH 20 | |
DL 21 | |
DX 22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Labwork is done :D