The final assignment (#14) of the C course (20465) at The Open University.
This project is based on the two-pass assembler model.
Note: the computer model for this project and the given assembly language are imaginary.
- Ben Arviv
- Gal Israel
Use makefile to compile the project like this:
> make
After preparing assembly files with an .as
extension, open terminal and pass file names as arguments (without the file extensions) as following:
As for the files x.as, y.as, hello.as we will run:
> assembler x y hello
The assembler will generate output files with the same filenames and the following extensions:
.am
- Macro expansion file.ob
- Object file.ent
- Entries file.ext
- Externals file
An example of input and output files can be found under the 'check-files' folder.
-
CPU
-
RAM (including a stack), with the size of 256 words.
-
A word's size in memory is 10 bits.
-
Uses signed 2's complement arithmetic for integers (with no support for real numbers).
The CPU has 9 registers: 8 general registers (named r0,r1,...,r7) and a register called PSW (program status word).
The size of each register is a word (10 bits).
Every software instruction is encoded into a few words in memory (max is 5 words). The first word is of the following structure:
9 8 7 6 | 5 4 | 3 2 | 1 0 |
---|---|---|---|
Opcode | Source operand addressing method | Destination operand addressing method | E, R, A |
Encoding of each instruction word is done using an imaginary base32 as defined here:
0: !
1: @
2: #
3: $
4: %
5: ^
6: &
7: *
8: <
9: >
10: a
11: b
12: c
13: d
14: e
15: f
16: g
17: h
18: i
19: j
20: k
21: l
22: m
23: n
24: o
25: p
26: q
27: r
28: s
29: t
30: u
31: v
so that each word can be encoded as 2 digits in this base (with leading ! as needed).
The commands allowed in bits 6-9 are:
Opcode (decimal) | Command Name |
---|---|
0 | mov |
1 | cmp |
2 | add |
3 | sub |
4 | not |
5 | clr |
6 | lea |
7 | inc |
8 | dec |
9 | jmp |
10 | bne |
11 | get |
12 | prn |
13 | jsr |
14 | rts |
15 | hlt |
A directive line of the following structure:
-
An optional preceding label. e.g.
PLACE1:
. -
A directive:
.data
,.string
,.struct
,.entry
or.extern
. -
Operands according to the type of the directive.
This directive allocates memory in the data image for storing received integers later in memory (also increases the data counter and updates the symbol table). The parameters of
.data
are valid integers (separated by a comma).
e.g.LABEL1: .data +7, -56, 4, 9
.This direcive receives a string as an operand and stores it in the data image. It stores all characters by their order in the string, encoded by their ASCII values. e.g.
STRING1: .string "abcdef"
is a valid directive.This directive allocates memory for a structure consists of 2 fields: the first is an integer and the second is a string. e.g.
STRUCT4: .struct 4, "abc"
is a vaild directive.This directive outputs a received name of a label to the symbol table, so that later it will be recognized by other assembly files (and they would be able to use it). e.g.
; file1.as .entry HELLO HELLO: add #1, r1
This directive receives a name of a label as a parameter and declares the label as being external (defined in another file) and that the current file shall use it.
This way, the directive.extern HELLO
infile2.as
will match the.entry
directive in the previous example.