The
aim of this assignment is to write a two pass assembler for an
extended SIMPLE instruction set. Then write and test programs in
SIMPLE assembly. A final part is to write an emulator for the
SIMPLE machine (replacing the one provided).
Tasks
1.Write a two pass assembler for the assembly language.
(65%) The assembler must,
§Read assembly language from a text file, assigning label
values and instruction opcodes. The format of the assembly language
is described
§Diagnose common assembly errors such as unknown
instruction, no such label, duplicate label.
§Produce an object file of the produced machine code. This
file should be a binary file. Code starts at address
zero.
§Produce a listing file. There is a choice of the format of
the listing file. It can either be a simple memory dump, or show
the bytes produced for each instruction, and that instruction's
mnemonic. The formats are shown later. Extra marks are
available for the latter type of listing file.
§You must write the assembler in ISO C89.
(`gcc -std=c89 -pedantic -W -Wall'
is a good way to check things.)
2.Test your assembler with the sample programs
listed
3.Test your assembler with additional programs and submit
evidence of this.
4.Write a bubble sort program in SIMPLE Assembler. The start
of this file is provided, you have to fill in the blanks. Up to
10%
5.Write an emulator for the SIMPLE machine. This should have
some, but not need all, of the functionality of
theemuprogram provided. Up to 25%
Here you can find a version of an emulator for this
architecture for PC Linux. Save the file and if
necessary, change its mode to executable.
This assembly language is for a machine with four
registers,
§Two registers, A & B, arranged as an
internal stack.
§A program counter, PC
§A stack pointer, SP
These registers are 32 bits in size. Instructions have
either no operands or a single operand. The operand is a signed 2's
complement value. The encoding uses the bottom 8 bits for opcode
and the upper 24 bits for operand.
As with most assembly languages, this is line based (one
statement per line). Comments begin with a `;' and anything on the
line after the `;' is ignored. Blank lines and lines containing
only a comment are permitted (and ignored). White space
(` ' and tabs) are permitted at the beginning of a
line (and ignored). Label definitions consist of the label name
followed by a `:', and an optional statement (there is not
necessarily a space between the `:' and the statement). A label use
is just the label name. For branch instructions label use should
calculate the branch displacement. For non-branch instructions, the
label value should be used directly. A valid label name is an
alphanumeric string beginning with a letter . An operand is either
a label or a number, the number can be
The following are all permitted lines
; a comment
; another comment
label1:
; a label on its own
ldc 5
; an instruction
label2: ldc 5 ; a label and an instruction
adc 5 ; an instruction
label3:ldc label3 ;look no space between label and
mnemonic
Each statement consists of a mnemonic (instruction name)
and an optional operand (number or label).
The Instructions
The instruction semantics do not show the incrementing of
the PC to the next instruction. This is implicitly performed by
each
instruction before the
actions of the instruction are done.
Mnemonic
Opcode
Operand
Formal Specification
Description
data
value
Reserve a memory location, initialized to the value
specified
ldc
0
value
B := A;
A := value;
Load accumulator with the value specified
adc
1
value
A := A + value;
Add the value specified to the accumulator
ldl
2
offset
B := A;
A := memory[SP + offset];
Load local
stl
3
offset
memory[SP + offset] := A;
A := B;
Store local
ldnl
4
offset
A := memory[A + offset];
Load non-local
stnl
5
offset
memory[A + offset] := B;
Store non-local
add
6
A := B + A;
Addition
sub
7
A := B - A;
Subtraction
shl
8
A := B <
Shift left
shr
9
A := B >> A;
Shift right
adj
10
value
SP := SP + value;
Adjust SP
a2sp
11
SP := A;
A := B
Transfer A to SP;
sp2a
12
B := A; A := SP;
Transfer SP to A
call
13
offset
B := A;
A := PC;
PC := PC + offset;
Call procedure
return
14
PC := A;
A := B;
Return from procedure
brz
15
offset
if A == 0 then
PC := PC + offset;
If accumulator is zero, branch to specified
offset
brlz
16
offset
if A
PC := PC + offset;
If accumulator is less than zero, branch to specified
offset
br
17
offset
PC := PC + offset;
Branch to specified offset
HALT
18
Stop the emulator. This is not a `real' instruction, but
needed to tell your emulator when to finish.
SET
value
Set the label on this line to the specified value (rather
than the PC). This is an optional extension, for which additional
marks are available.
The listing file is produced by the assembler and is a
human readable file showing what value is stored at each address.
The format is an address followed by zero or one 32 bit values (as
8 hex characters).
With the output, you can chose to show the human readable
mnemonic and operand, that each instruction corresponds to. You can
also show labels, by simply listing the
address followed by no data bytes.
Here are some acceptable example outputs,
00000000 00000111
00000001 00005AB4
00000002 00006500
00000003 00009D01
Or, with 4 locations per line,
00000000 00000111 00005AB4 00006500
00009D01
When showing labels and mnemonics, the output could be
(this is my preferred way of showing things)
00000000 00000111 br start
00000001 00005AB4 data 0x5ab4
00000002 start:
00000002 00006500 ldc 0x65
00000003 00009D01 adc 0x9d
This is a valid, but nonsense assembly file. Your assembler
should not issue any errors (it could
issue warnings though).
; test1.asm
label: ; an unused label
ldc 0
ldc -5
ldc +5
loop: br loop ; an infinite loop
br next ;offset should be zero
next:
ldc loop ;
load code address
ldc var1 ; forward ref
var1: data 0 ; a variable
And here it is as
text.
This example contains many errors. Your assembler should
spot them all (it need not copy the error message exactly, but
should issue something appropriate).
; test2.asm
; Test error handling
label:
label: ; duplicate label definition
br nonesuch ; no such label
ldc 08ge ; not a number
ldc ; missing operand
add 5 ; unexpected operand
ldc 5, 6; extra on end of line
0def: ; bogus label name
fibble; bogus mnemonic
0def ; bogus mnemonic
Which is here as
text.
If you implement the SET pseudo instruction, this program
should assemble
; test3.asm
; Test SET
val: SET 75
ldc val
adc val2
val2: SET 66
And that is
Here's an
implementation ofmemcpy
Here's a real file, (the one I will be testing with) you
should be able to assemble and then emulate it. See if you can
figure out what it's doing. The ldc
result is not a
mistake.
ldc 0x1000
a2sp
adj -1
ldc result
stl 0
ldc count
ldnl 0
call main
adj 1
HALT
;
main: adj -3
stl 1
stl 2
ldc
0 ; zero accumulator
stl 0
loop: adj -1
ldl 3
stl 0
ldl 1
call triangle
adj 1
ldl 3
stnl 0
ldl 3
adc 1
stl 3
ldl 0
adc 1
stl 0
ldl
0 ; reload it
ldl 2
sub
brlz loop