/* create a .o file that references all the undefined symbols we want to resolve */ static char *create_undef_symbols_file( DLLSPEC *spec ) { char *as_file, *obj_file; int i; unsigned int j; FILE *f; as_file = get_temp_file_name( output_file_name, ".s" ); if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file ); fprintf( f, "\t.data\n" ); for (i = 0; i < spec->nb_entry_points; i++) { ORDDEF *odp = &spec->entry_points[i]; if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue; if (odp->flags & FLAG_FORWARD) continue; fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) ); } for (j = 0; j < extra_ld_symbols.count; j++) fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[j]) ); fclose( f ); obj_file = get_temp_file_name( output_file_name, ".o" ); assemble_file( as_file, obj_file ); return obj_file; }
int main(int argc, char *argv[]) { Options opts = parse_args(argc, argv); FILE *in_file; if (!(in_file = fopen(opts.in_filename, "r"))) { file_error("could not open file for reading", opts.in_filename); } if (opts.out_filename == NULL) { // write to stdout by default opts.out_filename = make_out_filename(opts.in_filename, "hack"); } FILE *out_file; if (!(out_file = fopen(opts.out_filename, "w"))) { file_error("could not open file for writing", opts.out_filename); } assemble_file(in_file, out_file); return 0; }
int main(int argc, char **argv) { check_usage(argc); const char *inputfn = argv[1]; size_t inputfnlen = strlen(inputfn); char *outputfn = malloc(inputfnlen + 2 + 1); if (outputfn == NULL) { error("Could not allocate space for filename.", NULL); return 1; } strncpy(outputfn, inputfn, inputfnlen); outputfn[inputfnlen] = '.'; outputfn[inputfnlen + 1] = 'o'; outputfn[inputfnlen + 2] = 0; FILE *input = open_file(inputfn, "r"); FILE *output = open_file(outputfn, "wb+"); assemble_file(input, output); fclose(input); fclose(output); }
// // Load an assembler file from the file system, compile it completely // into a sequential memory block, and run it on the machine in // a real time loop. // int run_assembler_file( void* menlo_cnc_registers_base_address, char *fileName ) { int ret; unsigned long status; PBLOCK_ARRAY binary = NULL; ret = assemble_file(fileName, &binary); if (ret != 0) { printf("assembler error %d %s, exiting\n", ret, strerror(ret)); return ret; } printf("assembled %d opcode blocks\n", block_array_get_array_size(binary)); printf("assembly success, exiting\n"); status = stream_instructions(menlo_cnc_registers_base_address, binary); ret = 0; if (menlo_cnc_registers_is_error(status)) { printf("Error 0x%lx returned from stream_instructions\n", status); ret = 1; } if (menlo_cnc_registers_is_underrun(status)) { printf("Underrun occurred during stream_instructions status 0x%lx\n", status); ret = 1; } return ret; }
/******************************************************************* * main */ int main(int argc, char **argv) { DLLSPEC *spec = alloc_dll_spec(); #ifdef SIGHUP signal( SIGHUP, exit_on_signal ); #endif signal( SIGTERM, exit_on_signal ); signal( SIGINT, exit_on_signal ); output_file = stdout; argv = parse_options( argc, argv, spec ); switch(exec_mode) { case MODE_DLL: spec->characteristics |= IMAGE_FILE_DLL; load_resources( argv, spec ); load_import_libs( argv ); if (!spec_file_name) fatal_error( "missing .spec file\n" ); if (!parse_input_file( spec )) break; switch (spec->type) { case SPEC_WIN16: if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] ); BuildSpec16File( output_file, spec ); break; case SPEC_WIN32: read_undef_symbols( spec, argv ); BuildSpec32File( output_file, spec ); break; default: assert(0); } break; case MODE_EXE: if (spec->type == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" ); if (!spec->file_name) fatal_error( "executable must be named via the -F option\n" ); load_resources( argv, spec ); load_import_libs( argv ); if (spec_file_name && !parse_input_file( spec )) break; read_undef_symbols( spec, argv ); BuildSpec32File( output_file, spec ); break; case MODE_DEF: if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] ); if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" ); if (!spec_file_name) fatal_error( "missing .spec file\n" ); if (!parse_input_file( spec )) break; BuildDef32File( output_file, spec ); break; case MODE_RELAY16: if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] ); BuildRelays16( output_file ); break; case MODE_RELAY32: if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] ); BuildRelays32( output_file ); break; default: usage(1); break; } if (nb_errors) exit(1); if (output_file_name) { fclose( output_file ); if (output_file_source_name) assemble_file( output_file_source_name, output_file_name ); output_file_name = NULL; } return 0; }
/******************************************************************* * main */ int main(int argc, char **argv) { DLLSPEC *spec = alloc_dll_spec(); #ifdef SIGHUP signal( SIGHUP, exit_on_signal ); #endif signal( SIGTERM, exit_on_signal ); signal( SIGINT, exit_on_signal ); output_file = stdout; argv = parse_options( argc, argv, spec ); switch(exec_mode) { case MODE_DLL: if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE) spec->characteristics |= IMAGE_FILE_DLL; /* fall through */ case MODE_EXE: load_resources( argv, spec ); load_import_libs( argv ); if (spec_file_name && !parse_input_file( spec )) break; if (fake_module) { if (spec->type == SPEC_WIN16) output_fake_module16( spec ); else output_fake_module( spec ); break; } read_undef_symbols( spec, argv ); switch (spec->type) { case SPEC_WIN16: output_spec16_file( spec ); break; case SPEC_WIN32: BuildSpec32File( spec ); break; default: assert(0); } break; case MODE_DEF: if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] ); if (!spec_file_name) fatal_error( "missing .spec file\n" ); if (!parse_input_file( spec )) break; output_def_file( spec, 1 ); break; case MODE_IMPLIB: if (!spec_file_name) fatal_error( "missing .spec file\n" ); if (!parse_input_file( spec )) break; output_import_lib( spec, argv ); break; case MODE_RESOURCES: load_resources( argv, spec ); output_res_o_file( spec ); break; default: usage(1); break; } if (nb_errors) exit(1); if (output_file_name) { if (fclose( output_file ) < 0) fatal_perror( "fclose" ); if (output_file_source_name) assemble_file( output_file_source_name, output_file_name ); output_file_name = NULL; } return 0; }
int main(void) { // INITIALIZING struct queue cmd_queue; q_init (&cmd_queue); uint8_t *mem = calloc (__MEMORY_SIZE, sizeof(uint8_t)); char *input = malloc (sizeof(char)*__INPUT_SIZE); char *cmd = malloc (sizeof(char)*__CMD_SIZE); char *filename = malloc (sizeof(char)*__FILENAME_SIZE); if (mem == NULL || input == NULL || filename == NULL || cmd == NULL) { puts("MEMORY INSUFFICIENT"); goto memory_clear; } if (!init_oplist (__OPCODE_FILENAME)) { puts("OPCODE LIST INITIALIZATION FAILED."); goto memory_clear; } // COMMAND PROCESSING while (true) { int i; struct q_elem *qe; uint8_t value; uint32_t start, end; DIR *dirp = NULL; struct dirent *dir = NULL; char check[2]; bool is_valid_cmd = false; char *tok = NULL; printf("%s", __SHELL_FORM); if (!get_chars(input, __INPUT_SIZE)) goto memory_clear; // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds", __CMD_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd)!=1) cmd[0] = '\0'; // Switching with commands switch(get_cmd_index(cmd)) { case CMD_HELP: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } puts(__HELP_FORM); is_valid_cmd = true; break; case CMD_DIR: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // open directory and read through all elem. i = 1; dirp = opendir("."); dir = readdir(dirp); for(; dir!=NULL; dir = readdir(dirp)) { struct stat st; if(stat((const char*) dir->d_name, &st)!=0) { puts("FILE NOT FOUND"); goto memory_clear; } // FIX: ignore . and .. if(_SAME_STR(dir->d_name, ".") || _SAME_STR(dir->d_name, "..")) continue; printf("%20s", dir->d_name); if(S_ISDIR(st.st_mode)) // is Directory? putchar('/'); else if( (st.st_mode & S_IXUSR) // is exe? || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH) ) putchar('*'); putchar('\t'); // print newline after 3 elements if((i++)%3==0) putchar('\n'); } if((i-1)%3!=0) putchar('\n'); is_valid_cmd = true; break; case CMD_QUIT: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } is_valid_cmd = true; goto memory_clear; case CMD_HISTORY: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } qe = q_begin (&cmd_queue); i = 1; // print every formatted history for (; qe!=q_end(&cmd_queue); qe=q_next(qe)) printf("%-4d %s\n", i++, q_entry(qe, struct cmd_elem, elem)->cmd); printf("%-4d %s\n", i, input); is_valid_cmd = true; break; case CMD_DUMP: switch(sscanf(input, "%s %x , %x", cmd, &start, &end)) { case 1: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } start = get_location (0, false); end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 2: if(sscanf(input, "%*s %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (start >= __MEMORY_SIZE) { puts("OUT OF MEMORY BOUNDS."); break; } end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (!(start<=end && end<__MEMORY_SIZE)) { puts("OUT OF MEMORY BOUNDS."); break; } hexdump (mem, start, end); // if end is too large, point to end and go 0 if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_EDIT: switch(sscanf(input, "%s %x , %hhx", cmd, &start, &value)) { case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, start, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_FILL: switch(sscanf(input, "%s %x , %x , %hhx", cmd, &start, &end, &value)) { case 4: if(sscanf(input, "%*s %*x , %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, end, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_RESET: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // equivalent to fill 0, __MEMORY_SIZE-1 hexfill (mem, __MEMORY_SIZE, 0, __MEMORY_SIZE - 1, 0); is_valid_cmd = true; break; case CMD_OPCODE: switch(sscanf(input, "%*s %s", cmd)) { case 1: if(sscanf(input, "%*s %*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } i = find_oplist (cmd); if (i != -1) printf("opcode is %02X\n", i); else { printf("%s: NO SUCH OPCODE\n", cmd); is_valid_cmd = false; } break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_OPCODELIST: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } print_oplist (); is_valid_cmd = true; break; case CMD_ASSEMBLE: // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds %%%ds %%1s", __CMD_SIZE - 1, __FILENAME_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd, filename, check)!=2) { puts("WRONG INSTRUCTION"); break; } if (!is_file((const char*)filename)) { puts("FILE NOT FOUND"); break; } is_valid_cmd = assemble_file (filename); break; case CMD_TYPE: // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds %%%ds %%1s", __CMD_SIZE - 1, __FILENAME_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd, filename, check)!=2) { puts("WRONG INSTRUCTION"); break; } if (!is_file((const char*)filename)) { puts("FILE NOT FOUND"); break; } else { print_file((const char*)filename); is_valid_cmd = true; } break; case CMD_SYMBOL: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } print_symbol_table (); is_valid_cmd = true; break; case CMD_PROGADDR: if(sscanf(input, "%*s %*x %1s", check) == 1 || sscanf(input, "%*s %x", &i) != 1) { puts("WRONG INSTRUCTION"); break; } if (i < 0 || i >= __MEMORY_SIZE - 1) { puts("INVALID PROGRAM ADDRESS"); break; } set_progaddr ((uint32_t) i); is_valid_cmd = true; break; case CMD_LOADER: init_loader (); tok = strtok (input, " "); while ( (tok = strtok (NULL, " ")) != NULL) { if (!is_file (tok)) { printf ("[%s]: INVALID FILE\n", tok); free_loader (); break; } if (!add_obj_loader (tok)) { printf ("[%s]: LOADER FAILED\n", tok); free_loader (); break; } } // if normally added if (tok == NULL) { // address __MEMORY_SIZE is reserved for boot if (get_proglen()+get_progaddr()>=__MEMORY_SIZE-1) { puts ("PROGRAM IS TOO BIG: LOADER FAILED"); free_loader (); break; } if (!run_loader (mem)) { puts ("LOADER FAILED"); free_loader (); break; } print_load_map (); } free_loader (); is_valid_cmd = true; break; case CMD_RUN: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (!init_run ()) { puts ("RUN FAILED"); free_run (); break; } run (mem); free_run (); is_valid_cmd = true; break; case CMD_BP: if(sscanf(input, "%*s %1s", check) != 1) { print_bp (); is_valid_cmd = true; break; } if(sscanf(input, "%*s %6s %1s", cmd, check) == 2 || sscanf(input, "%*s %6s", cmd) != 1) { puts("WRONG INSTRUCTION"); break; } if (_SAME_STR(cmd, "clear")) { puts ("\t[ok] clear all breakpoints"); free_bp (); is_valid_cmd = true; break; } if(sscanf(input, "%*s %*x %1s", check) == 1 || sscanf(input, "%*s %x", &start) != 1) { puts("WRONG INSTRUCTION"); break; } if (start >= __MEMORY_SIZE - 1) { puts ("INVALID BREAKPOINT ADDRESS"); break; } if (add_bp (start)) printf ("\t[ok] create breakpoint %x\n", start); is_valid_cmd = true; break; default: if(sscanf(input, "%1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } } if (is_valid_cmd) { // Saving commands struct cmd_elem *e = malloc(sizeof(struct cmd_elem)); if (e == NULL) { puts("MEMORY INSUFFICIENT."); goto memory_clear; } e->cmd = malloc(sizeof(char)*(strlen(input)+1)); if (e->cmd == NULL) { puts("MEMORY INSUFFICIENT."); goto memory_clear; } strcpy(e->cmd, input); q_insert (&cmd_queue, &(e->elem)); } } memory_clear: if (mem != NULL) free (mem); if (input != NULL) free (input); if (cmd != NULL) free (cmd); while (!q_empty(&cmd_queue)) { struct q_elem *e = q_delete(&cmd_queue); struct cmd_elem *ce = q_entry(e, struct cmd_elem, elem); if (ce->cmd != NULL) free(ce->cmd); free(ce); } free_oplist (); free_loader (); free_bp (); free_run (); return 0; }