IDC Herzlya Shmon Schocken Assembler Shmon Schocken Sprng 2005 Elements of Computng Systems 1 Assembler (Ch. 6) Where we are at: Human Thought Abstract desgn Chapters 9, 12 abstract nterface H.L. Language & Operatng Sys. Compler Chapters 10-11 abstract nterface Vrtual Machne Software herarchy VM Translator Chapters 7-8 abstract nterface Assembly Language Assembler Chapter 6 abstract nterface Machne Language Computer Archtecture Chapters 4-5 Hardware herarchy abstract nterface Hardware Platform Gate Logc Chapters 1-3 abstract nterface Chps & Logc Gates Electrcal Engneerng Physcs Elements of Computng Systems 2 Assembler (Ch. 6)
Why care about assemblers? Because Assemblers employ some nfty trcks Assemblers are the frst rung up the software herarchy ladder An assembler s a compler for a smple language Wrtng an assembler s a good ntroducton for wrtng a compler. Elements of Computng Systems 3 Assembler (Ch. 6) Program translaton Translator The program translaton challenge Parse the source program, usng the syntax rules of the source language Re-express the program s semantcs usng the syntax rules of the target lang. Assembler = smple translator Translates each assembly command nto one or more machne nstructons Handles symbols (, sum, loop, end, ). Elements of Computng Systems 4 Assembler (Ch. 6)
Symbol resoluton In low level languages, symbols are normally used to represent: Varables Destnatons of goto commands Specal memory locatons The assembly process: Frst pass: construct a symbol table Second pass: translate the program, usng the symbol table for symbols resoluton. Elements of Computng Systems 5 Assembler (Ch. 6) Perspectve Smplfyng assumptons: Largest possble program s 1024 commands long Each command fts nto one memory locaton Each varable fts nto one memory locaton These assumptons can be relaxed rather easly, requrng more sophstcated assemblers. Elements of Computng Systems 6 Assembler (Ch. 6)
The Hack assembly language Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Assembly program = a stream of text lnes, each beng: An nstructon: A-nstructon or C-nstructon A symbol declaraton: (symbol) A comment / whte space: // comment. Elements of Computng Systems 7 Assembler (Ch. 6) A-nstructon Symbolc: @value // Where value s ether a non-negatve decmal number // or a symbol referrng to such number. value (v = 0 or 1) Bnary: 0 v v v v v v v v v v v v v v v Translaton to bnary: If value s a number: smple If value s a symbol: later. Elements of Computng Systems 8 Assembler (Ch. 6)
C-nstructon Symbolc: dest=comp;jump // Ether the dest or jump felds may be empty. // If dest s empty, the "=" s ommtted; // If jump s empty, the ";" s omtted. comp dest jump Bnary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 Translaton to bnary: smple! Elements of Computng Systems 9 Assembler (Ch. 6) The overall assembly logc Assembly program (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop For each symbolc command Parse nto the underlyng symbolc felds Replace each symbolc reference (f any) wth the correspondng memory address (a bnary number) For each feld, generate the correspondng bnary code Assemble the bnary codes nto a complete machne nstructon. Elements of Computng Systems 10 Assembler (Ch. 6)
Symbols handlng (n the Hack language) Program example // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Predefned symbols: (don t appear n ths example) Label symbols: The pseudo-command (Xxx) declares that the user-defned symbol Xxx should refer to the memory locaton holdng the next command n the program Varable symbols: Any symbol Xxx appearng n an assembly program that s not predefned and s not defned elsewhere usng the (Xxx) pseudo command s treated as a varable. Varables are mapped to consecutve memory locatons startng at RAM address 16. Elements of Computng Systems 11 Assembler (Ch. 6) Example Assembly code (Prog.asm) // Adds 1 +... + 100 M=1 // =1 M=0 // sum=0 (LOOP) Assembler @100 D=D-A // D=-100 D;JGT // f (-100)>0 goto END M=D+M // sum=sum+ M=M+1 // =+1 @LOOP 0;JMP // goto LOOP (END) 0;JMP // nfnte loop Bnary code (Prog.hack) (ths lne should be erased) 0000 0000 0001 0000 1110 1111 1100 1000 0000 0000 0001 0001 1110 1010 1000 1000 (ths lne should be erased) 0000 0000 0001 0000 1111 1100 0001 0000 0000 0000 0110 0100 1110 0100 1101 0000 0000 0000 0001 0010 1110 0011 0000 0001 0000 0000 0001 0000 1111 1100 0001 0000 0000 0000 0001 0001 1111 0000 1000 1000 0000 0000 0001 0000 1111 1101 1100 1000 0000 0000 0000 0100 1110 1010 1000 0111 (ths lne should be erased) 0000 0000 0001 0010 1110 1010 1000 0111 Elements of Computng Systems 12 Assembler (Ch. 6)
Proposed mplementaton An assembler program can be mplemented (n any language) based on the followng software modules: Parser: Unpacks each command nto ts underlyng felds Code: Translates each feld nto ts correspondng bnary value SymbolTable: Manages the symbol table Man: Intalzes fles and drves the show. Elements of Computng Systems 13 Assembler (Ch. 6) Parser module Elements of Computng Systems 14 Assembler (Ch. 6)
Parser module (cont.) Elements of Computng Systems 15 Assembler (Ch. 6) Code module Elements of Computng Systems 16 Assembler (Ch. 6)
Proposed mplementaton plan Stage I: Buld a basc assembler for programs wth no symbols Stage II: Extend the basc assembler wth symbol handlng capabltes Elements of Computng Systems 17 Assembler (Ch. 6) Symbol table Elements of Computng Systems 18 Assembler (Ch. 6)
Buldng the fnal assembler Intalzaton: create the symbol table and ntalze t wth the pre-defned symbols Frst pass: march through the program and buld the symbol table, wthout generatng any code Second pass: march agan through the program, and translate each lne: If the lne s a C-nstructon, smple If the lne s @Xxx where Xxx s a number, smple If the lne s @Xxx where Xxx s a symbol, look t up n the symbol table If the symbol s found, replace t wth ts numerc meanng and complete the command s translaton If the symbol s not found, then t must represent a new varable: add the par (Xxx,n) to the symbol table, where n s the next avalable RAM address, and complete the command s translaton. (The allocated RAM addresses are runnng, startng at address 16). Elements of Computng Systems 19 Assembler (Ch. 6) Perspectve Smple machne language, smple assembler Most assemblers are not stand-alone, but rather encapsulated n a translator of a hgher order Typcally, low-level C programmng (e.g. for real-tme systems) nvolves some assembly programmng (e.g. for optmzaton) Macro assemblers: // // R1 R1 = 1+2+...+100 sum=0 n=1 n=1 loop: f f n=101 goto goto end end sum=sum+n n=n+1 goto goto loop loop end: end: R1=sum Elements of Computng Systems 20 Assembler (Ch. 6)
Endnote I: Turng machne (1935) Alan Turng 1912-1954 Informal descrpton: A tape, dvded nto cells, each contanng a symbol A head that can move over the tape left and rght and read and wrte symbols A state regster that stores the machne s state An acton table (transton functon): If the current state s S, and the current symbol s s, then move the tape n postons rght/left, wrte a symbol s, and enter state S. Important conjecture: for any program runnng on any computer there s an equvalent TM that does the same thng. Elements of Computng Systems 21 Assembler (Ch. 6) The Haltng Problem Program = data: a TM program can be wrtten on the tape on another TM, becomng ts nput The haltng problem: a program H that, for any gven program p, prnts 1 f p halts on any nput, and 0 otherwse The haltng theorem: H does not exst Theoretcal sgnfcance: If H exsted, t would mply that we can prove theorems automatcally. Example: // // Goldbach Goldbach conjecture: conjecture: every every even even number number greater greater than than 2 2 s s the the sum sum of of two two prmes. prmes. Functon Functon goldbach() goldbach() = = 4 4 whle whle true true { { f f = = sum sum of of two two prmes prmes { { = = + + 2 2 else else { { prnt( the prnt( the conjecture conjecture s s false. false. Counter Counter example: example:,),) return return } } } } If H exsted, we could apply t to the goldbach() functon, thus provng or dsprove the Goldbach conjecture. Elements of Computng Systems 22 Assembler (Ch. 6)
Hstorcal perspectve Hlbert s challenge (1928): Can we devse a mechancal procedure (algorthm) whch could, n prncple, prove or dsprove any gven mathematcal proposton? Davd Hlbert 1862-1943 Alan Turng (1935): NO. Proof: uncomputablty of the haltng problem Kurt Godel (1931): NO. Proof: Incompleteness theorem (any system contanng the arthmetc of natural numbers s ether ncomplete or nconsstent) Alan Turng 1912-1954 Phlosophcal mplcatons. Kurt Godel 1906-1978 Elements of Computng Systems 23 Assembler (Ch. 6) Endnote II: The Engma Great book: Alan Turng: The Engma, by Andrew Hodges Elements of Computng Systems 24 Assembler (Ch. 6)