Macros | Functions | Variables
lc3.c File Reference

provided utility functions for LC3 assembler (do not modify) More...

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include "lc3.h"
#include "util.h"

Functions

LC3_inst_tlc3_get_inst_info (opcode_t opcode)
 
inst_format_tlc3_get_inst_format (const char *name)
 
char * lc3_get_suffix (char *file_name)
 
bool lc3_file_has_suffix (const char *file_name, const char *suffix)
 
char * lc3_replace_suffix (char *file_name, char *new_suffix)
 
void lc3_set_obj_file_mode (const char *name)
 
int lc3_read_LC3_word (FILE *f)
 
void lc3_write_LC3_word (FILE *f, int val)
 
char lc3_escaped_char (char c)
 
bool lc3_get_address (sym_table_t *symTab, const char *addr, int *value)
 
void lc3_read_sym_table (FILE *sym_file, sym_table_t *symTab)
 
static void write_one_symbol (symbol_t *sym, void *data)
 
void lc3_write_sym_table (FILE *sym_file, sym_table_t *symTab)
 
const char * lc3_get_opcode_name (opcode_t op)
 
const char * lc3_get_operand_name (operand_t operand)
 
const char * lc3_get_format_name (format_t format)
 

Variables

static bool inHex
 
static LC3_inst_t lc3_instructions []
 

Detailed Description

This file implements the interface defined in lc3.h.

Author
Fritz Sieker

Function Documentation

char lc3_escaped_char ( char  c)

Convert an escape sequence to a single character

Parameters
cthe character after the escape character (the back-slash)
Returns
the ascaped value of the character
bool lc3_file_has_suffix ( const char *  file_name,
const char *  suffix 
)

Determine if file_name has the specified suffix

Parameters
file_name- file name to check
suffix- value to match
Returns
- true if suffix matches actual suffix of file_name
bool lc3_get_address ( sym_table_t symTab,
const char *  token,
int *  value 
)

Convert a string to an address. The string is assumed to be a symbol. If it is found in the symbol table, the address is returned via the pointer. Otherwise call lc3_get_int() to convert the "number" to an address.

Parameters
symTab- the file in which the symbol table is written
token- the string to be converted
value- pointer to where the address will be stored
Returns
true on success, false on failure
const char* lc3_get_format_name ( format_t  format)

Convert a format_t to a "name"

Parameters
format- the value to convert (e.g. FMT_RRI5)
Returns
- a human readable name (e.g. "reg,reg,imm5" )
inst_format_t* lc3_get_inst_format ( const char *  name)

Get the format for an instructon given its name. The ADD/AND instructions have two forms with the same name and this routine will return the one with reg,reg,imm5.

Parameters
namethe opcode as a string (e.g. "AND")
Returns
a pointer to the format or NULL
LC3_inst_t* lc3_get_inst_info ( opcode_t  opcode)

Get the information for an instruction, given its opcode This is simply an access into an array of values initialized with the information for each of the LC3's sixteen instructions and additional pseudo-ops.

Parameters
opcode- the opcode of the instruction/pseudo-op of interest
Returns
- a pointer to the syntax information for this instruction or NULL
const char* lc3_get_opcode_name ( opcode_t  op)

convert an opcode_t to a name

Parameters
op- the opcode to convert (e.g. OP_ADD)
Returns
- a human readable name (e.g. "ADD")
const char* lc3_get_operand_name ( operand_t  operand)

convert operand type to a "name"

char* lc3_get_suffix ( char *  file_name)

Get the suffix of a file name

Parameters
file_name- file name to check
Returns
- a pointer to the rightmost location of '.' in file_name or to the null byte at the end of file_name.
int lc3_read_LC3_word ( FILE *  f)

Read an LC3 word from a file (binary or hex - see lc3_set_obj_file_mode())

Parameters
f- the object file
Returns
the value, or -1 on EOF
void lc3_read_sym_table ( FILE *  sym_file,
sym_table_t symTab 
)

Read the symbol table file and populate the global variable lc3_sym_tab. No error checking is performed. It is assumbed the file is in the correct format.

Parameters
sym_filethe file containing the symbol table
symTabpointer to the symbol table
char* lc3_replace_suffix ( char *  file_name,
char *  new_suffix 
)

Create a new dynamically allocated string with the existing suffix (if any) replaced by a new suffix.

Parameters
file_name- original file name
new_suffix- replace existing suffix with this value
Returns
- the new string (must be free'd by caller)
void lc3_set_obj_file_mode ( const char *  name)

Set 'inHex' depending on the file suffix. lc3_read_LC3_word() and lc3_write_LC3_word() depend on 'inHex'

Parameters
name- the name of the file
void lc3_write_LC3_word ( FILE *  f,
int  value 
)

Write an LC3 word to a file (binary or hex - See lc3_set_obj_file_mode())

Parameters
f- the file to write to
value- the value to write
void lc3_write_sym_table ( FILE *  sym_file,
sym_table_t symTab 
)

Write the symbol table to a file from the global variable lc3_sym_tab

Parameters
sym_file- the file in which the symbol table is written
symTab- pointer to the symbol table
static void write_one_symbol ( symbol_t sym,
void *  data 
)
static

callback function for symbol_interate()

Variable Documentation

bool inHex
static

Global flag defining whether using .obj file or .hex file. If false, each LC3 instruction is encoded in 16 bits. If true, each instruction is encoded by 4 hex digits followed by a newline.

LC3_inst_t lc3_instructions[]
static

A compact representation of the LC3 ISA plus pseudo-ops