程序代写代做代考 assembler C assembly CL20 – Building an Assembler

CL20 – Building an Assembler
1

HACK Assembly – Manual Assembly – Quick Refresh
Eg Add ME andMC andputresultsin MCD FfmHo m MCD
1 open filehandlesforinput asm and FILE’ffhaffbeftisniyymputtle
3 Pausetheline
d IfCinstruction i jumpf
O
g D DtM Tooo
dest op
lll l 000010 010 001
b td deal huljlump
o2 Jumpis 000
11Nowwe willneed
0
Magnesian
partitions Case2 each
of
parkhim from Clust bit formal accordingto th
theGML
thevalues of andthenantetheE Inst 1UT

des
op
mp
Writing an Assembler
3

Dealing with Variables and Special Symbols
11swapcontentsofMemlocations 0 and1
11 n y temp n n
SymbolTable
stash thevalueof MCO
y yetemp
in sometempIoe
H Ff
eggs
M D M withME temp 16
mp
11exchange Rl
DM a RO
M DM 651content into MG Hunte
temp
DM RI
4
MD
Too
predesignated

Dealing with Variables and Special Symbols – Flowchart
tonne t.IE
noiammIIsEFnH l
Hack ASM with variables and special symbols
Ino
w.intn
F
yes
D
7
q
5
Yes
TBINK.TT pains
a

Dealing with LABELS (Branches)
nyelse 2 Ney
Hlfn ythenz Lineof
Value RO O
ROM
Sym
i im
on IiIi
A GTR 10 GTR 10
5 DJGT 6y
END 12
7DM 8n
99
D bt.im
ntyVNolM
needed fnTR to z
fl MID END
a 13 0Jmp
12 END
6

I 0JMP
Dealing with LABELS (Branches) – Flowchart
1 1i
a en tRO O
Iago iz
inthis caseoneofthem wouldbe a to
eIa y
Hi
s2
7
ED iK
i
i
thereis nocollision
boshE FisentifspsomsshibFeblinat
Natee.ca
entries two differentsymbols
of havethe same inthe st may
Yue
LABELpointing a

Comments on Implementation
8

Parsing + Translating
Symbolic syntax: dest = comp ; jump
Binary syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 C Instruction
// Assume that current command is
// D = M+1; JGT
String c=parser.comp(); // “M+1”
String d=parser.dest(); // “D”
String j=parser.jump(); // “JGT”
String cc = Code.comp(c); // “1110111”
String dd = Code.dest(d); // “010”
String jj = Code.jump(j); // “001”
String out = “111” + cc + dd + jj;
Trr concatenation
A Instruction
// Assume that current command is @x
String num=parser.var(); //“x”
String addr_data = Code.num(num); //hash table lookup String out = “0” + addr_data;
// Assume that current command is @
String num=parser.num(); //“
String addr_data = num;
String out = “0” + addr_data;
Credit: www.nand2tetris.org 9

Using the Symbol Table
􏰀Create a new empty table
􏰀Add all the pre-defined symbols to the table
􏰀While reading the input, add labels and new variables to the table
􏰁 Labels: when you see a “(xxx)” command, add the symbol xxx and the address of the next machine language command
• Comment 1: this requires maintaining this running address
• Comment 2: this may need to be done in a first pass
􏰁 Variables: when you see an “@xxx” command, where xxx is not a number and not already in the table, add the symbol xxx and the next free address for variable allocation
Symbol
R0
..
..
sum
Address
0
..
..
12
Credit: www.nand2tetris.org 10

Overall assembler logic
􏰀Initialization 􏰁 Of Parser
also initializethe tablewith predefined symbol
􏰁 Of Symbol Table e
􏰀First Pass: Read all commands, only paying attention to labels
and updating the symbol table
􏰀Second Pass:
􏰁 Get the next Assembly Language Command and parse it
􏰁 For A-commands: Translate symbols to binary addresses
􏰁 For C-commands: get code for each part and put them together 􏰁 Output the resulting machine language command
Note that
can
symbolsin thefirstpassoftheassembly
you
code
11