Assembler Features and Design Options
Assembler Features and Design Options
MODULE III
• Assembler Features and Design Options:
o Machine Dependent Assembler Features
▪ Instruction Format and Addressing Modes
▪ Program Relocation
o Machine Independent Assembler Features
▪ Literals
▪ Symbol Defining Statements
▪ Expressions
▪ Program Blocks
▪ Control Sections and Program Linking
o Assembler Design Options
▪ One Pass Assembler
▪ Multi Pass Assembler
o Implementation Example-MASM Assembler
▪ SIC/XE
• Memory size = 220 bytes
• This supports four different types of instruction types
o 1 byte instruction
o 2 byte instruction
o 3 byte instruction
o 4 byte instruction
• Instructions can be:
o Instructions involving register to register
o Instructions with one operand in memory, the other in
Accumulator
o Extended instruction format
1
Module III System Software(CST 305)
2
Module III System Software(CST 305)
o Eg:
0006 CLOOP- - - -
- - - - - - -
- - - - - - -
0017 J CLOOP
▪ Translate J CLOOP
▪ Hex code for J is 3C => 0011 1100
• This is 8 bit wide. Delete the rightmost 2 bits and
place the remaining part in op fields.
▪ TA = address of CLOOP (from SYMTAB)= (0006)16
▪ After fetching this instruction (PC) = (001A) 16
▪ disp = TA – (PC) = (0006)16 - (001A) 16= (FFEC) 16
▪ Take the rightmost 12 bit disp (FEC)
3
Module III System Software(CST 305)
▪ Translate J @RETADR
5
Module III System Software(CST 305)
o Program Relocation
▪ Absolute Program
• The program must be loaded at the address that specified at
assembly time.
6
Module III System Software(CST 305)
7
Module III System Software(CST 305)
8
Module III System Software(CST 305)
o Literals
▪ Programmers can be able to write the value of a constant operand as a
part of the instruction. Such an operand is called literals.
▪ A literal is defined with a prefix =
▪ Eg: LDA =X’05’
9
Module III System Software(CST 305)
o Eg:
• Immediate Operand
o In immediate mode the operand value is assembled as part of the
instruction itself.
o Eg:
10
Module III System Software(CST 305)
▪ Literal Pools
• All the literal operands used in a program are gathered together into
one or more literal pools.
• There are two ways to place the literals in the program
o Can place the literals at the end of the program (After END
statement).
o Can place the literals at some other location in the object
program.
▪ Reason: keep the literal operand close to the instruction
▪ An assembler directive LTORG is used.
▪ Whenever the LTORG is encountered, it creates a literal pool
that contains all the literal operands used since the beginning
of the program or since the previous LTORG.
▪ It is better to place the literals close to the instructions.
• If the literal operand would be placed too far away from
the instruction referencing, we cannot use PC-relative
addressing or Base-relative addressing to generate Object
Program. Here we are forced to choose extended
instruction format. To avoid this we can use LTORG in
different places in the program.
▪ Implementation of Literals
• During Pass-1:
o The literal encountered is searched in the literal table.
o If the literal already exists, no action is taken.
11
Module III System Software(CST 305)
o If it is not present, the literal name, operand value and length are
added to the LITTAB.
o When encounters a LTORG statement or the end of the program
▪ The assembler makes a scan of the LITTAB and assigns an
address for each literal not yet assigned an address.
▪ Update the location counter value.
• During Pass-2:
o Search LITTAB for each literal operand encountered
o Literal values placed at correct locations in the object program.
o If the literal value represents an address in the program, the
assembler must also generate the appropriate Modification
Record.
▪ Allow literals that refer to the current value of the location counter.
• ‘*’ denotes a literal refer to the current value of program counter
• Eg: LDB =*
▪ Duplicate literals
• The same literal used more than once in the program
• e.g. WLOOP TD =X’05’
WD =X’05’
• The assemblers should recognize duplicate literals and store only
one copy of the specified data value
▪ ORG Statement
• ORG is an Assembler directive
• Allow the assembler to reset the PC to values
• Syntax: ORG value
• When ORG is encountered, the assembler resets its LOCCTR to the
specified value
• ORG will affect the values of all labels defined until the next ORG
• We can return to the normal use of LOCCTR by simply write ORG
• ORG is used to control assignment storage in the object program.
• No forward reference is allowed
o Expressions
▪ The assemblers allow the expressions as operand
▪ The assembler evaluates the expressions and produces a single operand
address or value
13
Module III System Software(CST 305)
▪ Expressions consist of
• Operator: +,-,*,/
• Constants
• User-defined symbols
• Special terms: *, the current value of LOCCTR
• Examples
MAXLEN EQU BUFEND-BUFFER
STAB RESB (6+3+2)*MAXENTRIES
BUFEND EQU *
The current value of location counter is assigned to
BUFEND.
▪ Values of terms can be classified as absolute or relative.
• Absolute terms
o Independent of program location
o Eg: Constants
MAXLEN EQU 1000
• Relative terms
o Defined relative to the beginning of the program
o Eg:
▪ Labels on instructions
▪ References to location counter: *
▪ Expressions can be either absolute or relative
• Absolute Expression
o Expression contains only absolute terms
MAXLEN EQU 1000+5
o Relative terms in pairs with opposite signs for each pair
MAXLEN EQU BUFEND-BUFFER
▪ BUFEND and BUFFER both are relative terms, representing
addresses within the program. The expression BUFEND-
BUFFER represents an absolute value.
▪ When relative terms are paired with opposite signs, the
dependency on the program starting address is canceled out.
The result is an absolute value.
▪ No relative term may enter into a multiplication or division
operation.
• Relative Expression
o Contains an odd number of relative terms, with one more positive
term than negative term.
STAB EQU OPTAB + (BUFEND – BUFFER)
14
Module III System Software(CST 305)
• With this information the assembler can easily determine the type of
each expression used as an operand and generate Modification
Record in the object program for relative values.
o Program blocks
▪ The source programs logically contained subroutines, data area etc.
▪ Within the object program the generated machine instructions and data
appeared in the same order as they were written in the source program.
▪ Program blocks allow the generated machine instructions and data to
appear in a different order while they are loading in memory.
• Separating blocks for storing code, data, stack, and larger data block
▪ Assembler directive: USE
• Syntax: USE [blockname]
• USE indicates which portion of the source program belongs to the
various blocks.
• At the beginning, statements are assumed to be part of the default
block
• If no USE statements are included, the entire program belongs to
this single block
• Each program block may actually contain several separate segments
of the source program
15
Module III System Software(CST 305)
16
Module III System Software(CST 305)
▪ Pass 1
• A separate location counter for each program block
o At the beginning of a block, LOCCTR is set to 0.
o Save and restore LOCCTR when switching between blocks
• Assign each label an address relative to the start of the block that
contains it.
• Store the block name (or number) in the SYMTAB along with the
assigned relative address of the label
• At the end of Pass1 the latest value of LOCCTR for each block
indicates the length of that block.
• At the end of Pass1 the assembler constructs a block table that
contains the block name, block number, starting addresses and
length of all blocks.
▪ Pass 2
• Calculate the address for each symbol relative to the start of the
object program by adding the location of the symbol relative to the
start of its block, to the assigned block starting address.
• The loader loads the default block in the memory from location 0000
17
Module III System Software(CST 305)
Pass 1 Algorithm
Begin
block number = 0
LOCCTR[i] = 0 for all i
Read the first input line
If OPCODE = ‘START’ then
{ Write line into intermediate file
Read next input line
}
While OPCODE != ‘END’ do
{ If OPCODE = ‘USE’ then
{
If there is no operand name then block name = Default
Else block name = OPERAND name
If there is no entry for block name in block table then
Insert (block name, block no++) in to block table
i = bock number for block name
if there is not a comment line then
{ If there is a symbol in the LABEL field then
{
Search SYMTAB for LABEL
If found then Set error flag
Else Insert (LABEL, LOCCTR[i], block number) into
SYMTAB
}
18
Module III System Software(CST 305)
19
Module III System Software(CST 305)
Pass 2 Algorithm
20
Module III System Software(CST 305)
21
Module III System Software(CST 305)
▪ The assembler must include information in the object program that will
cause the loader to insert proper values where they are required. Define
Record; Refer Record and Modification Record are used for this
purpose.
22
Module III System Software(CST 305)
23
Module III System Software(CST 305)
}
Else
Insert (LABEL, LOCCTR) into SYMTAB
}
Search OPTAB for OPCODE
If found then
{
Search SYMTAB for OPERAND address
If found then
{
If symbol value != null then
OPERAND address = symbol value
Else
Insert a node at the end of the linked list with address as
LOCCTR+1
}
Else
{ Insert (symbol name, null) into SYMTAB
Create a linked list with address as LOCCTR+1
}
Generate object code and load it in memory location LOCCTR
LOCCTR = LOCCTR +3
}
Else if OPCODE = ‘WORD’ then
{
Object code = #OPERAND
load this object code in memory location LOCCTR
LOCCTR = LOCCTR +3
}
Else if OPCODE = ‘RESW’ then
LOCCTR = LOCCTR +3x#OPERAND
Else if OPCODE = ‘RESB’ then
LOCCTR = LOCCTR + #OPERAND
Else if OPCODE = ‘BYTE’ then
{
Convert constant to object code and load it in memory location
LOCCTR
LOCCTR = LOCCTR +length of the constant
}
Else
Set error flag
25
Module III System Software(CST 305)
}
Read the next input line
}
If there are still SYMTAB entries indicated undefined symbols
Reports the error
Else
Jump to the location specified in END statement.
End
{
If symbol value as null then
{
Symbol value = LOCCTR
Generate separate Text record with corresponding
operand address of each entry in the linked list
Delete the linked list
}
}
Else
Insert (LABEL, LOCCTR) into SYMTAB
}
Search OPTAB for OPCODE
If found then
{
Search SYMTAB for OPERAND address
If found then
{
If symbol value != null then
OPERAND address = symbol value
Else
Insert a node at the end of the linked list with address as
LOCCTR+1
}
27
Module III System Software(CST 305)
Else
{ Insert (symbol name, null) into SYMTAB
Create a linked list with address as LOCCTR+1
}
Generate object code
LOCCTR = LOCCTR +3
}
Else if OPCODE = ‘WORD’ then
{
LOCCTR = LOCCTR +3
Object code = #OPERAND
}
Else if OPCODE = ‘RESW’ then
LOCCTR = LOCCTR +3x#OPERAND
Else if OPCODE = ‘RESB’ then
LOCCTR = LOCCTR + #OPERAND
Else if OPCODE = ‘BYTE’ then
{
LOCCTR = LOCCTR +length of the constant
Convert constant to object code
}
Else
Set error flag
If object code will not fit into the current text record then
{
Write Text Record into object program
Initialize new Text Record
}
Add object code to Text Record
}
Read the next input line
}
Write last Text Record to object program
Write End Record to object program
End
28
Module III System Software(CST 305)
o Multipass Assembler
▪ The symbols used on the RHS of EQU should be defined previously in
the program.
▪ Eg:
29
Module III System Software(CST 305)
PTR TARGET
o If the jump address is within 128 bytes, the programmer can
specify a shorter(2 bytes) near bytes by writing JMP
SHORT TARGET
▪ Length of the assembled instruction is depends on its operand
• Eg: operands of ADD instruction can be
o Registers
o Memory locations: May take varying amount of space, depending
upon the location of the operand.
o Immediate operands: May occupy from 1 to 4 bytes in the
instruction
▪ Pass 1 of an x86 assembler is more complex than Pass 1 of SIC
assembler
• During Pass 1 of x86
o Analyze the operands of each instruction
o Looking at the operation code table
▪ It contains information on which addressing modes are valid
for each operand.
▪ Segments in a MASM source program can be written in more than one
part.
• All the parts are gathered together by the assembly process.
▪ References between segments are handled by the assembler.
• Use the directive PUBLIC. It has the same function as EXTDEF in
SIC/XE.
▪ External references between separately assembled modules must be
handled by the linker.
• Use the directive EXTRN. It has the same function as EXTREF in
SIC/XE.
▪ The object program from MASM may be in several different formats
• Allow easy and efficient execution of the program in a variety of
operating environments.
▪ MASM produces an instruction timing that shows the number of clock
cycles required to execute each instruction
31
Module III System Software(CST 305)
32