My Project
|
Defines the interface to assembler.c functions (do not modify) More...
Go to the source code of this file.
Data Structures | |
struct | line_info |
Macros | |
#define | LC3AS_VAR extern |
#define | ERR_OPEN_READ "could not open '%s' for reading." |
#define | ERR_OPEN_WRITE "could not open '%s' for writing." |
#define | ERR_LINE_TOO_LONG "source line too long (max is %d)" |
#define | ERR_NO_ORIG "no .ORIG directive found" |
#define | ERR_MULTIPLE_ORIG "multiple .ORIG found" |
#define | ERR_NO_END "no .END directive found" |
#define | ERR_ORIG_NOT_1ST "instruction(s) appear before .ORIG" |
#define | ERR_END_NOT_LAST "instruction(s) appear after .END - ignored" |
#define | ERR_EXPECTED_COMMA "expected comma, got '%s'" |
#define | ERR_EXPECTED_REG "expected register (R0-R7), got '%s'" |
#define | ERR_EXPECT_REG_IMM "expected register or immediate, got '%s'" |
#define | ERR_BAD_LABEL "label '%s' contains illegal characters" |
#define | ERR_MISSING_OP "expected LC3 op, got '%s'" |
#define | ERR_MISSING_OPERAND "too few operand(s)" |
#define | ERR_EXTRA_OPERAND "extra operand(s) '%s'" |
#define | ERR_DUPLICATE_LABEL "label '%s' previosly defined" |
#define | ERR_MISSING_LABEL "label '%s' never defined" |
#define | ERR_BAD_PCOFFSET "PCoffset to '%s' out of range" |
#define | ERR_BAD_IMM "immediate '%s' (bad format)" |
#define | ERR_IMM_TOO_BIG "immediate '%s' out of range" |
#define | ERR_EXPECTED_STR "expected quoted string, got '%s'" |
#define | ERR_BAD_STR "unterminated string '%s'" |
#define | ERR_EXPECTED_FILL "expected .FILL value, got '%s'" |
#define | ERR_BAD_CHAR_CONST "incorrect character constant \"%s\"" |
Typedefs | |
typedef struct line_info | line_info_t |
Functions | |
void | asm_error (const char *msg,...) |
line_info_t * | asm_init_line_info (line_info_t *info) |
void | asm_print_line_info (line_info_t *info) |
void | asm_init (void) |
line_info_t * | asm_pass_one (const char *asm_file_name, const char *sym_file_name) |
void | asm_pass_two (const char *obj_file_name, line_info_t *list) |
void | asm_term (line_info_t *list) |
Variables | |
LC3AS_VAR int | numErrors |
This file defines the interface to a C file assembler.c that you will complete. This is the main portion of an assembler for LC3.
This is a substantial assignment. One way to attack larger projects is to break the problem into pieces and code those pieces individually. You have already done this for the files util.c
and symbol.c
. Instead of writing large functions (e.g. asm_pass_one(), asm_pass_two()
), decompose them into a series of calls to shorter functions which do required subtasks. The decomposition may be continued until each low level function is easy to write correctly. If you find any one function getting "too" large, think about what the code does and decompose it into several smaller functions. In fact, if you can give the task a good symbolic name, you can postpone writing it until later. This is because the name implies "what" the function does. "How" it is done, will be determined when that function is coded. It is perfectly fine to have functions that are only called once from the code. The idea is that each of these functions peform a straight forward subtask. And it is perfectly fine to have functions that are only one or a few lines of code.
A good rule of thumb is that a function is too large if you can not see the entire function on a single page.
#define ERR_OPEN_READ "could not open '%s' for reading." |
Error messages passed to function asm_error()
#define LC3AS_VAR extern |
Handy way of defining global variable in a header file
typedef struct line_info line_info_t |
Typedef of structure type
void asm_error | ( | const char * | msg, |
... | |||
) |
A function to print error messages. This function takes a minimum of one parameter. It is exaclty like the printf()
function. The first parameter is a formatting string. This will be one of the values defined above that starts with ERR_
.The remaining parameters (if any) are the actual values to be printed (generally a token). The function prints the word ERROR:
and the the value of srcLineNum
along with the information provided by the parameters. It must be used for reporting all errors. After printing the error, the global variable numErrors
is incremented. This function relies on the static variable srcLineNum
in assembler.c
. Ideally, this function would throw an exception.
msg | - the formating string |
void asm_init | ( | void | ) |
Do whatever initialization is necessary for the assembler
line_info_t* asm_init_line_info | ( | line_info_t * | info | ) |
A function to initialize all the fields of the structure to default values. The fields info->lineNum
and info->address
are initialized from global values defined in assembler.c
.
info | - pointer to information about a source line. If the value is NULL , memory for the structure is dynamically allocated. |
line_info_t* asm_pass_one | ( | const char * | asm_file_name, |
const char * | sym_file_name | ||
) |
This function performs the processing required in the first pass. At a minimum, it must check the syntax of each instruction and create the symbol table. For this assignment, it will also create a linked list for use by the second pass. The flow of this function is:
fgets()
ADD
) on the line, then line_info_t
structure lc3_write_sym_tab()
. asm_file_name | - name of the file to assemble |
sym_file_name | - name of the symbol table file |
void asm_pass_two | ( | const char * | obj_file_name, |
line_info_t * | list | ||
) |
This function generates the object file. It is only called if no errors were found during asm_pass_one()
. The basic structure of this code is to loop over the data structure created in asm_pass_one()
, generate object code (16 bit LC3 instructions) and write it to the object file using lc3_write_LC3_eord()
. The function must also calculate PC offsets and verify that they are within range.
obj_file_name | - name of the object file for this source code |
list | - the linked list produced by asm_pass_one() |
void asm_print_line_info | ( | line_info_t * | info | ) |
A function to print the infomation extracted from a source line. This is used for debugging only.
info | - pointer to informaton about a source line |
void asm_term | ( | line_info_t * | list | ) |
Cleanup everything used by the assembler
list | - the list produced by asm_pass_one() |
LC3AS_VAR int numErrors |
A global variable defining the number of errors found