Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assemblers CSCI/CMPE 3334 David Egle.

Similar presentations


Presentation on theme: "Assemblers CSCI/CMPE 3334 David Egle."— Presentation transcript:

1 Assemblers CSCI/CMPE 3334 David Egle

2 Fundamental functions
Take a program written in assembly language and translate it to machine language (binary) What does this entail? Translate mnemonics to machine code Convert symbolic operands to addresses Convert constants into internal representation Process assembler directives Build machine language instruction Write the object and listing files

3 Machine dependencies The design and many features of assembler depend on underlying hardware instruction set instruction format registers memory management Some features do not depend on hardware Programmer conveniences Program format etc.

4 SIC assembly language General format
label – must begin in first column operation – may be mnemonic or directive operand – symbol, value, or hexadecimal address comment This is typical of many assembly languages. The specific formats may vary with respect to punctuation, etc.

5 SIC sample program (1/3) COPY START 1000 COPY FILE FROM INPUT TO OUTPUT FIRST STL RETADR SAVE RETURN ADDRESS CLOOP JSUB RDREC READ INPUT RECORD LDA LENGTH TEST FOR EOF (LENGTH = 0) COMP ZERO JEQ ENDFIL EXIT IF EOF FOUND JSUB WRREC WRITE OUTPUT RECORD J CLOOP LOOP ENDFIL LDA EOF INSERT END OF FILE MARKER STA BUFFER LDA THREE SET LENGTH = 3 STA LENGTH JSUB WRREC WRITE EOF LDL RETADR GET RETURN ADDRESS RSUB RETURN TO CALLER EOF BYTE C'EOF' THREE WORD 3 ZERO WORD 0 RETADR RESW 1 LENGTH RESW 1 LENGTH OF RECORD BUFFER RESB BYTE BUFFER AREA

6 SIC sample program (2/3) . . SUBROUTINE TO READ RECORD INTO BUFFER RDREC LDX ZERO CLEAR LOOP COUNTER LDA ZERO CLEAR A TO ZERO RLOOP TD INPUT TEST INPUT DEVICE JEQ RLOOP LOOP UNTIL READY RD INPUT READ CHARACTER INTO REGISTAR A COMP ZERO TEST FOR END OF RECORD (X'00') JEQ EXIT EXIT LOOP IF EOF STCH BUFFER,X STORE CHARCTER IN BUFFER TIX MAXLEN LOOP UNLESS MAX LENGTH JLT RLOOP HAS BEEN REACHED EXIT STX LENGTH SAVE RECORD LENGTH RSUB RETURN TO CALLER INPUT BYTE X'F1' CODE FOR INPUT DEVICE MAXLEN WORD 4096

7 SIC sample program (3/3) . . SUBROUTINE TO WRITE RECORD FROM BUFFER WRREC LDX ZERO CLEAR LOOP COUNTER WLOOP TD OUTPUT TEST OUTPUT DEVICE JEQ WLOOP LOOP UNTIL READY LDCH BUFFER,X GET CHARCTER FROM BUFFER WD OUTPUT WRITE CHARACTER TIX LENGTH LOOP UNTIL ALL CHARACTERS JLT WLOOP HAVE BEEN WRITTEN RSUB RETURN TO CALLER OUTPUT BYTE X'05' CODE FOR OUTPUT DEVICE END FIRST

8 SIC assembly language (cont’d)
Some specifics regarding SIC Directives (pseudo-ops) START END BYTE WORD RESB RESW Indexing uses “,X” format, as in STCH BUFFER,X Comment lines begin with period

9 End result – the object file
For ease in displaying the file, it is stored in ASCII with hexadecimal values for the code NOTE: This is not the usual way of storing the object file HCOPY__ A T E C A0C D T00101E150C C F T E E0205D30203FD8205D C205E38203F T C C0000F E DC20792C1036 T C000005 E001000

10 The process So, how do we get from the source FIRST STL RETADR
to the object code 141033 The STL part is easy – the code is 14 (hex) BUT, what about the operand, RETADR? What is its value? (It is called a forward reference) Some preliminary work must be done!

11 Handling forward references
We need to know the address of RETADR before we can complete the instruction Two possibilities Store the partial instruction until the address is determined (one pass assembly) Go through the code and determine ALL the addresses before starting the actual translation (two pass assembly) Both have their advantages and disadvantages We consider the two pass version first

12 Two pass assembler Pass one – BUILD SYMBOL TABLE
assign addresses to all statements save addresses assigned to labels in symbol table perform some processing of assembler directives Pass two – TRANSLATION convert mnemonics & symbols to machine code convert constants process remaining assembler directives write machine code to object file; also write listing file In both passes, try to detect any errors in syntax

13 Data structures The location counter (called LOCCTR in text) Tables
Initialized to address specified in START statement (which is a hexadecimal value) As each statement is examined, it’s length is added to the LOCCTR Tables Table of opcode values (OPTAB) Table of symbol values (SYMTAB) Files Intermediate file Object file Listing file

14 Opcode table Contains (for SIC) May also contain
mnemonic its equivalent machine language value May also contain instruction format required operands Usually organized as a closed (static) hash table fast retrieval with minimal searching

15 Symbol Table Contains (for SIC) May also contain
associated value (usually the address) May also contain type length scope information Also organized as a hash table but it is dynamic

16 Intermediate file Written in pass 1 and used in pass 2
Contains (at minimum) source statement address mnemonic or its value operand(s) errors in statement May contain pointers to OPTAB or SYMTAB

17 Object file Contains results of assembly
Format varies according to designer Usually read by a linker/loader program Several record types (this also can vary) header record linkage records code records end record

18 Listing file Contains results of assembly for viewing by the programmer (or others) Format varies but usually includes address of statement machine code equivalent source statement (including comments) errors associated with each statement

19 Algorithm for pass 1 (SIC)
read first input line and write to intermediate file if OPCODE = ‘START’ then { save #{OPERAND} as starting address initialize LOCCTR to starting address read next input line and write to intermediate file } else set LOCCTR=0 while OPCODE != ‘END’ do { if this is not a comment line then handle line 1 save (LOCCTR – starting address) as program length

20 Pass 1: handle line 1 If there is a symbol in the LABEL field then
{ search SYMTAB for LABEL if found then set error flag (duplicate label) else insert (LABEL, LOCCTR) into SYMTAB } search OPTAB for OPCODE if found then add 3 to LOCCTR else if OPCODE = ‘WORD’ then add 3 to LOCCTR else if OPCODE = ‘RESB’ then add #[OPERAND] to LOCCTR else if OPCODE = ‘RESW’ then add 3* #[OPERAND] to LOCCTR else if OPCODE = ‘BYTE’ then { find length of constant in bytes add length to LOCCTR } else set error flag (invalid operation code)

21 Algorithm for pass 2 (SIC)
read first input line from intermediate file if OPCODE = ‘START’ then { write listing line read next input line } write header record to object file initialize first text record while OPCODE != ‘END’ do { if this is not a comment line then handle line 2 write listing line write last text record to object file write end record to object file write last listing line

22 Pass 2: handle line 2 search OPTAB for OPCODE if found then
{ if there is a symbol in OPERAND field then { search SYMTAB for OPERAND store symbol value as operand address else store 0 as operand address set error flag (undefined symbol) } else if OPCODE = ‘BYTE’ or ‘WORD’ then convert constant to object code if object code will not fit into current text record then { write text record to object file initialize new text record } add object code to text record

23 Example (Exercise 2.1.2) Assemble the following: SUM START 4000
FIRST LDX ZERO INITIALIZE REGISTERS LDA ZERO LOOP ADD TABLE,X ADD THE ELEMENTS TIX COUNT ARE WE DONE? JLT LOOP IF NOT, LOOP STA TOTAL STORE THE TOTAL RSUB AND RETURN TABLE RESW FOR THE ARRAY COUNT RESW 1 NUMBER OF ELEMENTS ZERO WORD 0 CONSTANT TOTAL RESW 1 PLACE FOR TOTAL END FIRST


Download ppt "Assemblers CSCI/CMPE 3334 David Egle."

Similar presentations


Ads by Google