Example #1
0
int main(int argc, char *argv[])
{
    unsigned char test_data[16] = {0x38,0xC2}; // xor eax, eax
    int len = 2;

    if (translate_inst(ARCH_X86, test_data, len) >= 0)
    {
        return 0;
    }

    return -1;
}
Example #2
0
/* Reads an intermediate file and translates it into machine code. You may assume:
    1. The input file contains no comments
    2. The input file contains no labels
    3. The input file contains at maximum one instruction per line
    4. All instructions have at maximum MAX_ARGS arguments
    5. The symbol table has been filled out already

   If an error is reached, DO NOT EXIT the function. Keep translating the rest of
   the document, and at the end, return -1. Return 0 if no errors were encountered. */
int pass_two(FILE *input, FILE* output, SymbolTable* symtbl, SymbolTable* reltbl) {
    // Since we pass this buffer to strtok(), the chars here will GET CLOBBERED.
    char buf[BUF_SIZE];
    // Store input line number / byte offset below. When should each be incremented?
    uint32_t byte_offset = 0;
    int line_number = 1;
    // First, read the next line into a buffer.
    fgets(buf, BUF_SIZE, input);
    // Next, use strtok() to scan for next character. If there's nothing,
    // go to the next line.
    strtok(buf, "\n");
    while (strcmp(buf, "") == 0) {
	line_number++;
	strtok(buf, "\n");
    }
    // Parse for instruction arguments. You should use strtok() to tokenize
    // the rest of the line. Extra arguments should be filtered out in pass_one(),
    // so you don't need to worry about that here.
    int errored = 0;
    while (buf != NULL) {
	char* args[MAX_ARGS];
	int num_args = 0;
	*args = strtok(buf, " ,\n");
	num_args = sizeof(args)/sizeof(args[0]);
    // Use translate_inst() to translate the instruction and write to output file.
    // If an error occurs, the instruction will not be written and you should call
    // raise_inst_error(). 
	if (strcmp(*args, "") == 0) {
	    line_number++;
	    int boolean = translate_inst(output, args[0], args, num_args, byte_offset, symtbl, reltbl);
	    if (!boolean) {
		raise_inst_error(line_number, args[0], args, num_args);
		errored++;
	    } else {
		byte_offset += 4;
	    }
	}
    // Repeat until no more characters are left, and the return the correct return val
	line_number++;
	strtok(buf, "\n");
    }
    if (errored != 0) { //my own: errors... in the output function?
	return -1;
    } else { //my own:if there are no errors
	return 0;
    }
}
Example #3
0
int pass_two(FILE *input, FILE* output, SymbolTable* symtbl, SymbolTable* reltbl) {
    /* YOUR CODE HERE */

    // Since we pass this buffer to strtok(), the chars here will GET CLOBBERED.
    char buf[BUF_SIZE];
    // Store input line number / byte offset below. When should each be incremented?

    // First, read the next line into a buffer.

    // Next, use strtok() to scan for next character. If there's nothing,
    // go to the next line.

    // Parse for instruction arguments. You should use strtok() to tokenize
    // the rest of the line. Extra arguments should be filtered out in pass_one(),
    // so you don't need to worry about that here.
  
    int err = 0;
    // Use translate_inst() to translate the instruction and write to output file.
    // If an error occurs, the instruction will not be written and you should call
    // raise_inst_error(). 

    // Repeat until no more characters are left, and the return the correct return val

    uint32_t line_no = 0;
    uint32_t addr = 0;
   
    while(fgets(buf, BUF_SIZE, input) != NULL) {

	 line_no++;
	 
	 if(strlen(buf) == 0) continue;

	 char* args[MAX_ARGS];
	 int num_args = 0;
	 char instr[BUF_SIZE];   // instruction string
	 instr[0] = '\0';      

	 char * pch;
	 pch = strtok (buf, IGNORE_CHARS);
	 
	 if(pch != NULL) {

	      strcpy(instr, pch);   // first is instruction string

	      pch = strtok (NULL, IGNORE_CHARS);
	      
	      while (pch != NULL) { // following is arguments
		   args[num_args++] = strdup(pch);
		   pch = strtok (NULL, IGNORE_CHARS);
	      }
	      
	      #if 0
	      printf("instr: %s\n", instr);

	      printf("num_args: %d\n", num_args);

	      printf("args:\n");
	      
	      for(int i = 0; i < num_args; i++)
		   printf("\t %s", args[i]);

	      printf("addr: %d\n", addr);
	      #endif
	      
	      if(translate_inst(output, instr, args,  num_args, addr,
				symtbl, reltbl) == -1)  {
		   raise_inst_error(line_no, instr, args, num_args);
		   err = -1;
	      }
	      else {
		   addr += 4;
	      }
	 }
	 else {
	      raise_inst_error(line_no, instr, args, num_args);
	      err = -1;
	 }

	 // free memeory in args
	 
	 for(int i = 0; i < num_args; i++)
	      free(args[i]);
	
    }
    
    return err;
}