This assignment aims to give you some experience with C programming and to help you gain
better understanding of the addressing mode of LC-3.
Important Notes
? There are subtle differences between various C compilers. We will use the GNU compiler gcc
on login.cs.auckland.ac.nz for marking. Therefore, you MUST ensure that your submissions
compile and run on login.cs.auckland.ac.nz. Submissions that fail to compile or run on
login.cs.auckland.ac.nz will attract NO marks.
? Markers will compile your program using command “gcc –o name name.c” where name.c is
the name of the source code of your program, e.g. part1.c. That is, the markers will NOT use
any compiler switches to supress the warning messages.
? Markers will use machine code that is different from the examples given in the
specifications when testing your programs.
? The outputs of your programs will be checked by a program. Thus, your program’s outputs
MUST be in the same format as shown in the example given in each part. You must make
sure that the registers appear in the same order as shown in the example and there are no
extra lines.
In this assignment, you are required to write C programs to implement a LC-3 simulator. That is,
the programs will execute the binary code generated by LC-3 assembler.
Part 1 (28 marks)
LC3Edit is used to write LC-3 assembly programs. After a program is written, we use the LC-3
assembler (i.e. the “Translate ? Assemble” function in LC3Edit) to convert the assembly
program into binary executable. The binary executable being generated by LC3Edit is named
“file.obj” where “file” is the name of the assembly program (excluding the “.asm” suffix). In this
specification, a “word” refers to a word in LC-3. That is, a word consists of two bytes. The
structure of the “file.obj” is as below:
? The first word (i.e. the first two bytes) is the starting address of the program.
? The subsequent words correspond to the instructions in the assembly program and the
memory locations reserved for the program using various LC-3 directives.
? In LC-3, data are stored in Big-endian format (refer to
https://en.wikipedia.org/wiki/Endianness to learn more about Big-endian format). For
2
example, if byte 0x12 in word 0x1234 is stored at address 0x3000, byte 0x34 is stored at
address 0x3001. This means, when you read a sequence of bytes from the executable of
an LC-3 assembly program from a file, the most significant bit of each word is read first.
In this part of the assignment, you are required to write a C program to display each word in the
“.obj” file of a program in hexadecimal form.
? Name the C program as “part1.c”.
? The name of the “.obj” file (the name of the file INCLUDES the “.obj” suffix) must be
given as a command line argument.
? In the output, each line shows the contents of one word.
? The value of each word must have a “0x” prefix.
? The letter digits “a” to “f” must be shown as lowercase letters.
Here is an example of the execution of the program. In this example, the LC-3 assembly program
is as below. The name of the executable of the assembly program is “p1.obj” (markers will
probably use a file with a different name and different contents).
.ORIG X4500
LD R0, A
LEA R1, B
LDI R2, C
AND R3, R0, R1
AND R3, R1, #0
NOT R4, R3
ADD R4, R4, #1
BRp F
ADD R3, R3, #1
F HALT
A .FILL X560A
B .FILL X4507
C .FILL X4501
.END
The execution of the program is shown below. The command line argument is marked in red.
$ ./part1 p1.obj
0x4500
0x2009
0xe209
0xa409
0x5601
0x5660
0x98ff
0x1921
0x0201
0x16e1
0xf025
0x560a
0x4507
3
0x4501
Part 2 (46 marks)
In this part, you are required to write a C program to implement a LC-3 simulator that is capable
of executing instruction “LD”.
? Name the C program as “part2.c”.
? The name of the “.obj” file (the name of the file INCLUDES the “.obj” suffix) must be
given as a command line argument.
? The state of the simulator consists of the contents of the 8 general purpose registers (i.e.
R0 to R7), the value of the program counter (i.e. PC), the contents of the instruction
register (i.e. IR), and the value of the condition code (i.e. CC).
? The values in R0 to R7, PC and IR should be shown as hexadecimal value. The value of CC
is either N, Z or P.
? Before the simulator starts executing a program, it should first display the initial state of
the LC-3 machine. In the initial state, R0 to R7 and IR should all be 0; PC should be the
starting address of the program to be executed; and CC should be set to Z.
? When displaying the value of R0 to R7, PC, IR and CC, a tab character (denoted as “\t” in