Professional Documents
Culture Documents
Assemblers
Assemblers
Assemblers
• Two functions:
– Mnemonic opcodeMachine Code
– Symbolic labels machine addresses (in memory)
• Some features:
– Depend on assembly language
– and machine language as well, may be very subtle.
• Other features:
– Machine-independent,
– Even are arbitrary decisions by the designers of the language
• Follows the steps for chapters.
An assembly language program as example
• Source Code
– Normally, when entering into subroutine/method/function/procedure, have STL to store the return
address in Linkage register into a variable, before return using RSUB, have LDL to load return address
(from the variable) into Linkage register, particularly when it calls other subroutine(s) within it to avoid
the return address in Linkage register to be over-written by the new return address.
– Even subroutine call JSUB and return RSUB are hardware-supported, but they are parameter-less.
Parameters/arguments of a method/function/procedure are software features, and need to be
specifically copied with/passed when you write assembly/machine language code yourself or by
compiler.
• Mnemonic instructions
• Symbolic labels
• Assembler directives
• Function
– Read from an INPUT device (code F1) and then copy to an OUTPUT device (code 05)
– Call two subroutines: RDREC and WRREC
– Called from OS by JSUB, so save return address and return by RSUB
– Support buffer size of 4096 and no error check.
A Simple SIC Assembler
• Translation of source code to object code needs:
1. Mnemonic opcodes to equivalent machine codes
1. E.g, STL to 14
2. Symbolic labels to equivalent machine address
1. E.g. RETADR to 1033
3. Build machine instructions in proper format.
4. Convert data constants into internal machine
representation, such as EOF to 454F46
5. Write the object program and the assembly listing.
Object Program
• Program from Fig2.1 with Object Code (Fig.2.2)
• Comments:
– Instructions and data are all in binary
– A same value can be an instruction or data, or address
– They are mixed together/interleave.
– Object code is written into a file with certain format and
then loaded into memory for execution later by a loader
• Object Program corresponding to Figure 2.2 (Fig2.3
)
– Three kinds of records: H—header, T: text, E: end.
Problem and solution
• Symbolic label and forward reference
– A reference to a label which is defined later.
– How to solve it?
• Two passes
– Scan label definitions and assign addresses
– Do actual translations
• Other functions:
– Process assembler directives such as START, END, BYTE, WORD, RESB,
RESW, …
– Write the object code onto output devices.
• Three kinds of records: Header, Text, and End (in hexadecimal)
• In the future, Modification record, Define record, Refer record,…
General Functions of an assembler
• Pass one
– Assign addresses to all statements in source code
– Save values (addresses) assigned to labels for use in pass two
– Process directives
• Pass two
– Translate instructions
– Convert labels to addresses
– Generate values defined by BYTE and WORD
– Process the directives not done in pass one
– Write object code to output device
Assembler algorithm and Data Structure
• Two major internal data structures
– OPTABLE:
• (mnemonic opcode, machine code, instruction format, length)
• Look up and validation in pass one, also increase LOCCTR by length
• Translate opcode to machine code (including format) in pass two
• Organized as a hash table.
– SYMTAB
• (label name, address, flags, other info such data type or length)
• Pass one: labels are entered along with their addresses (from LOCCTR).
• Pass two: labels are looked up to get addresses to insert in assembled instructions
• Organized as a hash table too.
• Location Counter Variable: LOCCTR
– Memory locations (addresses)
– Initialized as the address in START
– Increased by adding the #bytes of an instruction or data area
– When encountering a label, the current LOCCTR gives the address of the label.
Connection between two passes
• Pass one writes an intermediate file,
– Source instructions along with addresses, flags,
pointers to OPTAB and SYMTAB, and some results
from the first pass
A simple Two pass assembler
• Algorithm for pass 1 of an assembler (Fig2.4(a)
)
• Algorithm for Pass 2 of an assembler (Fig2.4(b)
)
• Very important for you to understand the
programs and to be able to implement an
assembler.
Machine-dependent Assembler Features