Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- int EOF_flag = 0;
- char* get_input(FILE *file)
- {
- char *input = NULL;
- size_t buf_size = 0;
- EOF_flag = getline(&input, &buf_size, file);
- return input;
- }
- int main(int argc, char** argv) {
- short opcode;
- int argcode;
- FILE *input;
- FILE *output;
- char *line = NULL;
- char *commandName;
- char *commandArg;
- size_t bufSize = 0;
- const int tabSize = 13;
- char *commands[] = {"JNS", "LOAD", "STORE", "ADD", "SUBT", "INPUT", "OUTPUT", "HALT", "SKIPCOND", "JUMP", "CLEAR", "ADDI", "JUMPI"};
- input = fopen(argv[1], "r");
- if(input == NULL) {
- printf("Błąd podczas otwierania pliku źródłowego\n");
- exit(EXIT_FAILURE);
- }
- output = fopen(argv[2], "w");
- if(output == NULL) {
- printf("Błąd podczas tworzenia pliku docelowego\n");
- exit(EXIT_FAILURE);
- }
- while(getline(&line, &bufSize, input) != -1) {
- char *temp;
- int p = 0;
- commandName = strtok(line, " ");
- commandArg = strtok(NULL, " \n");
- for(int i = 0; i < tabSize; i++) {
- if(strcmp(commandName, commands[i]) == 0) {
- opcode = i;
- if(commandArg != NULL) {
- argcode = strtol(commandArg, NULL, 16);
- short opcode_number = opcode << 12;
- short arg_number = (short) argcode;
- short result = opcode_number + arg_number;
- fwrite(&result, sizeof result, 1, output);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement