struct
) and how to initialize
structures at compile time.&
One of the taks required in the assembler is converting symbolic names that are used in an assembly language program to their underlying representation. For example the word "ADD" needs to be converted to the numeric code that the LC3 uses for the operation. Assemblers for other assembly lanuages will also have the name "ADD", but will likely have a different numeric code.
One way to convert words to values is to have a long sequence of statements like:
if (strcmp(name, "ADD") == 0)
code = OP_ADD;
else if (strcmp(name, ".FILL") == 0)
code = OP_FILL
...
An alternative this is to search a data structure. The advantage of this is
two fold. First the search code is written once and resused multiple times.
Secondly, the contents of the data structure could actually be read from a file,
thus changing the behavior of the program without changing the program itself.
This is exactly what happens in program like lex
which take a
textual description of a language and automatically produces a lexical analyzer
for it. If you ever take a compiler course (e.g. cs453), you will learn to use
these tools.
In this assignment there are three functions for converting names to values:
The fourth function analyzes a string and determines if it is a valid label.
cd
to itSave Target As..
for each
of the files.
make
./testUtil
. You will see a usage message which tells
you how to use the program.util_bin_search()
ADD
or .FILL
and determine the opcode associated
with that name.
You have learned how to write binary search in your previous courses. Build on
your knowlegde and complete this implementation on C. To test your code, use
the reg
option in testUtil. This will use you binary
search code using the provided code for the function util_get_reg()
.
cond_code_map[]
register_map[]
. Then
add code to intialize cond_code_map[]
. It should have eight
entries. To determine what numeric value goes with each "name", think of the
bits nzp
as a three bit binary number. Once you have
initialized cond_code_map[]
, initialize the variable
numCondCodes
following the pattern of the code for initializing
numRegisters
. Lastly, complete the routine
util_parse_cond()
following the pattern of the code for
util_get_reg()
. You may test your function using the
ccode
option of testUtil
.
util_get_opcode()
and initalizing the
variables numOps
and lc3_instruction_map[]
.
Test by using the option op
in testUtil
.util_is_valid_label()
.
Test by using the option label
in testUtil
.util.c
using the
checkin
program. Use the key UTIL. At the terminal
type:
~cs270/bin/checkin UTIL util.c