Module 2 - Assemblers & Macro Processor

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Module 2

ASSEMBLERS AND MACRO PROCESSORS


Syllabus
Content Duration Self Study
Assemblers: Elements of Assembly Language 2 Lecture 2 hours
Programming

Basic Assembler functions , Design of the Assembler, 1 Lecture 2 hours


Types of Assembler

Two pass assembler – IBM 360/370, Format of 2 Lecture 3 hours


databases, Algorithm

Single pass Assembler for Intel x86. 1 Lecture 2 hours


Macro Processors: Macros, Basic Functions of 2 Lecture 2 hours
Macro Processor, Features of Macro Facility

Design of Two pass Macro Processor, Format of 2 Lecture 4 hours


Databases and Algorithm.
Assembler:

Assembler : An assembler is a program that accepts as input an assembly language program &
produces its machine language equivalent along with information for the loader.

ALP m/c language &


Other information for the loaders
Databases

• Functions of Assembler:

- Convert mnemonic operation codes to their machine language equivalents


- Convert symbolic operands to their equivalent machine addresses

- Decide the proper instruction format


- Convert the data constants to internal machine representations

- Write the object program and the assembly listing


Features of assembly language programming:

1) Mnemonic opcode specification:


Instead of using the binary opcodes mnemonics can be used.
E.g. L- load
A- Add
ST- Store
BR- Branch

2) Symbolic Operand Specification:


Instead of referring to instruction or data by its address, symbol can be used.
(Symbolic name can be associated with data or instructions).
It is function of assembler to replace each symbol by its address.

3) Storage Area specification:


Declaration of data & storage areas.
Assembly language can be used to specify some part of memory can be used for
storage.
Statements in ALP:

1. Imperative statement:
Understood by m/c & executed by m/c.
e.g. All the instructions.
1. Declarative statements:
DC statement: Declare Constant
Label Opcode Operand
FOUR DC F ‘4’

2. Assembler Directive statement:


These are neither executable nor declarative.
They direct the assembler to perform the specific task.
e.g. a) START statement:
-These statement indicates the start of assembly language program.
b) END statement: END of ALP.
c) BYTE, WORD, RESB, RESW
Design of Assembler: For IBM 360/370 Machine

1) Registers:-

-There are 16 general purpose register of 32 bit each.

- There are 4 floating point register of 64 bit each.

- There is 1 program status word of 64 bits.

2) Memory:-
Unit Of Memory Bytes Bits
Byte 1 8
-Basic unit Bytes.
Half Word 2 16
Full Word 4 32
Double Word 8 64
Instruction Formats:

a) RR Format:-
In this format the first & second operands present in the registers.
Format:
Opcode R1 (OP1) R2 (OP2)
0 7 8 11 12 15
e.g AR 3,4
3) RX format:
-In this format first operand is in the register & second operand is external (
present in the memory location).
-The address of second operand is given by-
C (B2) + C(X2) + D2
Contents of Base Reg. + Contents of Index Reg. + Displacement

Format:
Opcode R1 X1 B2 D2
0 7 8 11 12 15 16 19 20 31
e.g A 1, 90(2,15)
i.e. C(15) + C(2) +90
Assembler Directives :
USING & DROP
• USING: It is an assembler directive statement which indicates that the given register is available to be used as base
register along with its value.

Format:

USING value, Reg. no.

-Evaluate the operand

-Make the entries in the BT

• DROP statement: It is a assembler directive statement which indicate that the given register is unavailable to be used
as base register.

Format:

DROP Register no.

DS: Define Storage

DC: Declare Constant


Forward Reference Problem in IBM 360/370
Processor

a) Definition of forward reference:

• The rules of ALP state that the symbol should be defined somewhere in
the program. So that there might be a case in which a reference is made to
a symbol prior to its definition. Such a reference is called forward
reference.

b) What is FRP?

•The function of the assembler is to replace each symbol by its m/c address
& if we refer to that symbol before it is defined , its address is not known by
the assembler. Such a problem is called FRP.
Example of FRP
- Forward reference problem (For IBM 360) is solved by
making two passes over the assembly code.
Pass1:-
Purpose- Define Symbols & Literals.
1) Keep track of LC.
2) Determine length of m/c instructions.

Solution 3) Remember the value of symbols until pass2.


4) Process some Pseudo-ops e.g EQU, DS, DC.
to FRP 5) Remember Literals.

Pass2:
Purpose- Generate Object Program.
1) Lookup values of symbols.
2) Generate instructions.
3) Generate data.
4) Process some Pseudo-ops e.g. USING, DROP.
Format of Databases: 1. MOT
MOT is a fixed length table i.e. the contents of these tables are not altered during the
assembly process.

Mnemonic Binary Op- Instruction Instruction Not used in


Opcode code Length Format this design
(4 Bytes (1 Byte ( 2 Bits binary) (3 bits binary) (3 bits)
character) Hexadecimal)
“Abbb” 5A 10 001
“AHbb” 4A 10 001
“ALbb” 5E 10 001
“ALRb” 1E 01 000
b- Represents the char blanks.
Codes:-
Instruction Length Instruction Format
01= 1 Half word=2 Bytes 000 = RR
10= 2 Half word=4 Bytes 001 = RX
11= 3 Half word=6 Bytes 010 = RS
011= SI
100= SS
- MOT is a predef ined table.
- In PASS1 , MOT is consulted to obtain the instruction length.(to Update LC)
- In PASS2, MOT is consulted to obtain:
a) Binary Opcode (to generate instruction)
b) Instruction length ( to update LC)
c) Instruction Format (to assemble the instruction).
Format of Databases:
POT (Pseudo Opcode Table):-
POT is a fixed length table i.e. the contents of these table are not altered during the
assembly process.

Pseudo Opcode Address of routine to process pseudo-


(5 Bytes charater) opcode.
(3 bytes= 24 bit addrss)
“DROPb” P1 DROP
“ENDbb” P1 END
“EQUbb” P1 EQU
“START” P1 START
“USING” P1 USING

- The table will actually contain the physical addresses.


- POT is a predefined table.
- In PASS1 , POT is consulted to process some pseudo opcodes like-DS,DC,EQU
- In PASS2, POT is consulted to process some pseudo opcodes like
DS,DC,USING,DROP
ST & LT
C) Symbol table (ST):

Symbol Value Length Relocation


(8 Bytes (4 Bytes ( 1 Byte (R/A)
charaters) Hexadecimal) Hexadecimal) (1 Byte character)
“PRG1bbb” 0000 01 R
“FOURbbbb” 000C 04 R

- ST is used to keep a track on the symbol defined in the program.


- In pass1- whenever the symbol is defined an entry is made in the ST.
- In pass2- Symbol table is used to generate the address of the symbol.

D) Literal Table (LT):


Literal Value Length Relocation
(R/A)

= F ‘5’ 28 04 R
-
- LT is used to keep a track on the Literals encountered in the program.
- In pass1- whenever the literals are encountered an entry is made in the LT.
- In pass2- Literal table is used to generate the address of the literal.
Base Table (BT):

E) Base Table (BT):

Register Availability Contents of Base register


(1 Byte Character) (3 bytes= 24 bit address hexadecimal)

1 ‘N’ -
2 ‘N’ -
. -
.
.
15 ‘N’ 00

- Code availability-
- Y- Register specified in USING pseudo-opcode.
- N--Register never specified in USING pseudo-opcode.
- BT is used to keep a track on the Register availability.
- In pass1- BT is not used.
- In pass2- In pass2, BT is consulted to find which register can be used as base registers
along with their contents.
Location Counter (LC)

F) Location Counter (LC):

- LC is used to assign addresses to each instruction & address to the symbol defined in the
program.

- LC is updated only in two cases:-

a) If it is an instruction then it is updated by instruction length.

b) If it is a data representation (DS, DC) then it is updated by length of data field.


Example on two pass assembler
Databases
Example
Pass1 Databases
The various databases maintained are as follows.
a) Original source cards:
It contains the original program.
The a) Location counter (LC):

purpose of It is used to assign addresses to the instruction & addresses to the


symbols defined in the program.
pass1 is to c)Mnemonic Opcode Table (MOT):
define the MOT is consulted to obtain the instruction length ( to Update LC).

symbols & d)Pseudo Opcode Table (POT):


POT is consulted to process some pseudo opcodes like DS, DC, EQU.
literals. e) Symbol Table (ST):
Whenever a symbol is defined entry is made in ST.
f) Literal Table (LT):
Whenever literal are encountered entry is made in LT.
g) Copy File:
It is prepared to be used by pass2.
The various databases maintained are as follows.
a) Copy File:
It is prepared by pass1.
a) Location counter (LC):
It is used to assign addresses to the instruction & addresses to the
symbols defined in the program.
c)Mnemonic Opcode Table (MOT):
MOT is consulted to obtain the binary opcode, instruction length & instruction
Pass 2 Databases format.
d)Pseudo Opcode Table (POT):
POT is consulted to process some Pseudo opcodes like DS, DC, USING, DROP.
e)Symbol Table (ST):
The purpose of pass2 It is used to generate the address for the symbols.
is to generate the f) Literal Table (LT):
instruction & data. It is used to generate the address of the literals.
g)Base Table (BT):
It is consulted to find the registers which are available to be used as base
registers.
h) Instruction workspace:
It is used to hold the instruction while its various parts are getting assembled.
i) PUNCH workspace:
It is used to punch the assembled instruction onto the cards.
j) PRINT workspace:
It is used to generate a printed assembly listing.
k) Assembled object cards:
Which contain the object program in a format required by the loader.
PASS 1: DEFINE SYMBOLS
Pass1 The purpose of the first pass is to assign a location to each instruction and data defining
(Algorithm) pseudo-instruction
1)Initially, the Location Counter (LC) is set to the first location in the program (relative
address 0).
2)Read the source statement.
3)Examine the opcode field to check weather it is psedo-op or machine code.
If it is machine-op then MOT is searched to find a match for the source
statement's op-code field. From MOT entry determine the length field L = Length.
4) The operand field is scanned for the presence of a literal. If a new literal is found, it is
entered into the Literal Table (LT) for later processing.
5) The label field of the source statement is then examined for the presence of a symbol. If
there is a label, the symbol is saved in the Symbol Table (ST) along with the current value of
the location counter.
6) The current value of the location counter is incremented by the length of the. The above
sequence is then repeated for the next instruction.
7) Pseudo-ops:
a) USING and DROP do neither. The assembler need only save the USING and DROP
cards for pass 2.
b) In the case of the EQU pseudo-op during pass 1, we are concerned only with defining the
symbol in the label field. This requires evaluating the expression in the operand field.
c) The DS and DC pseudo-ops can affect both the location counter and the definition of
symbols in pass 1.
d) When the END pseudo-op is encountered, pass 1 is terminated.
PASS 1 : Flowchart
PASS 2 PASS 2: GENERATE CODE
Algorithm 1) Read copy file generated by pass 1.

2) As in pass 1, the operation code field is examined to determine if it is a pseudo-op; if it is not, the table of
machine op-codes (MOT) is searched to find a-match for the card's op-code field. The matching MOT entry
specifies the length, binary op-code, and the format type of the instruction. The operand fields of the different
instruction format types require somewhat different processing.

3) For the RR-format instructions, each of the two register specification fields is evaluated. This evaluation
may be very simple, as in:

AR 2, 3.

4) For RX-·format instructions, the register and index fields are evaluated and processed in the same way as
the register specifications for RR-format instructions.

 5) Print the EQU card as part of the printed listing.

6) The USING and DROP pseudo-ops, which were largely ignored in pass 1, require additional processing in
pass 2. The operand fields of the pseudo-ops are evaluated; then the corresponding Base Table entry is either
marked as available, if USING, or unavailable, if DROP. The base table is used extensively in pass 2 to
compute the base and displacement fields for machine instructions with storage operands.

7) The DS and DC pseudo-ops are processed essentially as in pass 1. In pass 2, however, actual code must be
generated for the DC pseudo-op.

8) The END pseudo-op indicates the end of the source program and terminates the assembly. Various
"housekeeping" tasks must now be performed. For example, code must be generated for any literals remaining
in the Literal Table.
PASS 2
FLOWCHART
FAQ
Macro-Processor
• Macros: Single line abbreviation for group of instructions
• Macro Definition:
The template to be followed for defining a MACRO is as follows:

MACRO Start of Definition

Macro Name

Macro Body

MEND END of Macro definition


Example

A 1, DATA Add contents of DATA to register 1 The above requirement can be achieved by using macro facility as follows.
Source Expanded source
A 2, DATA Add contents of DATA to register 2
MACRO
A 3, DATA Add contents of DATA to register 3
INCR
.
A 1,DATA
.
A 2,DATA
.
A 3,DATA

MEND
A 1, DATA Add contents of DATA to register 1
.
A 2, DATA Add contents of DATA to register 2
.
A 3,DATA Add contents of DATA to register 3 .
A 1, DATA
. INCR A 2, DATA

DATA DC F '5' A 3, DATA


.
.
the above program the sequence
. A 1, DATA
INCR A 2, DATA
A 1, DATA . . A 3, DATA
A 2, DATA
A 3, DATA .
DATA DC F'5'
occurs twice.
Macro Processor
• Macro processor is a program which is responsible for processing the macro.
 There are four basic tasks/ functions that any macro instruction processor must perform.
• Recognize macro definitions : A macro instruction processor must recognize macro definitions
identified by the MACRO and MEND pseudo-ops.
• Save the macro definition :The processor must store the macro instruction definitions, which it
will need for expanding macro calls.
• Recognize macro calls : The processor must recognize macro calls that appear as operation
mnemonics.
• Expand macro calls : Replace macro call by its definition.

Source
Macro Expanded Compiler or obj
Code
(with macro)
Processor Code Assembler
Features of Macroprocessor

1. Macro Instruction Arguments (Parameter Passing)


2. Conditional Macro Expansion
3. Macro Calls Within Macros (Nested Call)
1. Macro Instruction Arguments : Example 1

• Providing arguments, or parameters in macro definition and macro calls or also


called macro dummy arguments.
A 1, DATA1 Add contents of DATA1 to register 1
A 2, DATA1 Add contents of DATA1 to register 2
A 3, DATA1 Add contents of DATA1 to register 3
.
.
.

A 1, DATA2 Add contents of DATA2 to register 1


A 2, DATA2 Add contents of DATA2 to register 2
A 3,DATA2 Add contents of DATA2 to register 3

.
.
.
DATA DC F '5'
DATA DC F '10’

- In this case the instruction sequences are very similar but not identical. The first
sequence performs an operation using DATAl as operand; the second, using DATA2.
Example 1
Example 2: Supply more than one argument in
a macro call.

Source Expanded source

A 1, DATA1 Add contents of DATA1 to register 1 MACRO


INCR & ARG1, & ARG2, &ARG3
A 2, DATA2 Add contents of DATA2 to register 2
A 3, DATA3 Add contents of DATA3 to register 3 A 1, &ARG1
A 2, &ARG2
.
A 3, &ARG3
.
MEND
.
.
.
A 1, DATA3 Add contents of DATA3 to register 1 .
A 1, DATA1
A 2, DATA2 Add contents of DATA2 to register 2
INCR DATA1, DATA2, DATA3 A 2, DATA2
A 3, DATA1 Add contents of DATA1 to register 3 A 3, DATA3
.

. .
. A 1, DATA3
.
INCR DATA3, DATA2, DATA1 A 2, DATA2
. . . A 3, DATA1
DATA DC F '5'
DATA DC F '10’ .

DATA DC F '15’
DATA1 DC F'5'
DATA2 DC F’10’
DATA DC F’15’
2. Conditional Macro Expansion:
AIF is a conditional branch pseudo-o ; it performs an arithmetic test and branches only if the tested condition is
true.
AGO is an unconditional branch pseudo-op or 'go to' statement.

Example 4:

A 1, DATA1 Add contents of DATA1 to register 1 MACRO


LOOP1 A 2, DATA2 Add contents of DATA2 to register 2 &ARG0 VARY &COUNT, &ARG l,&ARG2,&ARG3
A 3, DATA3 Add contents of DATA3 to register 3 &ARG0 A l, & ARG l
. AIF (&COUNT EQ 1) .FINI Test if & COUNT = 1
. A 2, &ARG2
. AIF (&COUNT EQ 2) .FINI Test if & COUNT = 2
A 3,&ARG3
LOOP2 A 1, DATA3 Add contents of DATA3 to register 1 .FINI MEND
A 2, DATA2 Add contents of DATA2 to register 2 .
. . Expanded Source
. .
. LOOP1 VARY 3, DATA 1,DATA2, DATA3 LOOP1 A 1,DATAl
LOOP2 A 3,DATA1 Add contents of DATA1 to register 3 . A 2, DATA2
. A 3, DATA3
. .
.
. LOOP2 VARY 2, DATA3,DATA2 LOOP2 A 1,DATA3

DATA DC F '5' A 2,DATA2


DATA DC F '10’
DATA DC F '15’ .
LOOP3 VARY 1,DATA1 { LOOP3 A 1,DATA1

In this example, the operands, labels, and the number of instructions generated change in each
sequence. This program could be written as follows: DATAl DC F'5'

. DATA2 DC F'10'

. DATA3 DC F'15'

.
3. Macro Calls Within Macros
Macro definition within macros
Design of Two Pass Macro processor
• The macro processor algorithm will two passes
over the input text, searching for the macro
definition & then for macro calls Argument List Array:

• Format of Databases: Index 8 bytes per entry

1) Argument List Array: The Argument List Array


(ALA) is used during both pass 1 and pass 2. 0 "bbbbbbbb" (all blank)
1 "DATA3bbb"

• During pass 1, in order to simplify later argument 2 "DATA2bbb"


replacement during macro expansion, dummy 3 "DATA1bbb"
arguments in the macro definition are replaced
with positional indicators when the definition is
stored.

• During pass 2 it is necessary to substitute macro


call arguments for the index markers stored in the
macro definition.
2) Macro Definition Table:

 The Macro Definition Table


Macro Definition Table
(MDT) is a table of text lines.

 Every line of each macro 8O bytes per entry


Index Card
definition, except the . .
MACRO line, is stored in the . .
15 INCR &ARG1, &ARG2,&ARG3
MDT.
16 A 1, #1
17 A 2, #2
 The MEND is kept to indicate
18 A 3,#3
the end of the definition; and 19 MEND
the macro name line is . .
. .
retained to facilitate keyword
. .
argument replacement.
The Macro Name Table (MNT) :

 MNT serves a function very


similar to that of the assembler's
Machine-Op Table (MOT) and
8- Bytes 4 -Bytes
Pseudo-Op Table (POT).
Index Macro Name a pointer (index) to the entry
 Each MNT entry consists of a
in the MDT
character string (the macro
. . .
name) and a pointer (index) to . . .
the entry in the MDT that 3 “INCRbbbb” 15
corresponds to the beginning of . . .

the macro definition. . . .


Data bases required for Pass1 & Pass2
Macro processor:
• Pass 1 data bases:
Pass 2 data bases:

1. The input macro source deck 1. The copy of the input macro source deck
2. The output macro source deck copy for use by pass 2 2. The output expanded source deck to be used as
3. The Macro Definition Table (MDT), used to store the input to the assembler
body of the macro definitions 3. The Macro Definition Table (MDT), created by
4. The Macro Name Table (MNT), used to store the pass 1
names of defined macros
4. The Macro Name Table (MNT), created by pass 1
5. The Macro Definition Table Counter (MDTC), used
5. The Macro Definition Table Pointer (MDTP),
to indicate the next available entry in the MDT
used to indicate the next line of text to be used
6. The Macro Name Table Counter (MNTC), used to during macro expansion
indicate the next available entry in the MNT
6. The Argument List Array (ALA), used to
7. The Argument List Array (ALA), used to substitute
substitute macro call arguments for the index
index markers for dummy arguments before storing a
markers in the stored macro definition
macro definition
Algorithm of Pass1 & Pass2
Macro processor:
PASS I-MACRO DEFINITION: PASS2-MACRO CALLS AND EXPANSION:
The algorithm for pass 2 tests the operation mnemonic of each
The algorithm for passl tests each input line. input line to see if it is a name in the MNT. When a call is
found:-
If-it is a MACRO pseudo-op:
1. The entire macro definition that follows is 1)The macro processor sets a pointer, the Macro Definition
saved in the next available locations in the Table Pointer (MDTP) , to the corresponding macro definition
Macro Definition Table (MDT).
stored in the MDT. The initial value of the MDTP is obtained
from the "MDT index" field of the MNT entry.
2. The first line of the definition is the macro
name line. The name is entered into the Macro
Name Table (MNT), along with a pointer to 2)The macro expander prepares the Argument List
the first location of the MDT entry of the Array(ALA) consisting of a table of dummy argument indices
definition. and corresponding arguments to the call.

3. When the END pseudo-op is encountered, all


of the macro definitions have been processed 3)Reading proceeds from the MDT; as each successive line is
so control transfers to pass 2 in order to read, the values from the argument list are substituted for
process macro calls. dummy argument indices in the macro definition.

4)Reading of the MEND line in the MDT terminates


expansion of the macro, and scanning continues from the input
file.

5)When the END pseudo-op is encountered, the expanded


source deck is transferred to the assembler for further
processing.
Flowchart of Pass1 & Pass2 Macro processor:
Pass 1: processing macro definitions
Pass 2 : Processing macro calls and expansion
Differentiate Between Macros and Procedure /
Function
MACROS PROCEDURE / Subroutine

1 The corresponding machine code is 1 The Corresponding m/c code is writte


written every time a macro is called in a only once in memory
program.
2 Program takes up more memory space. 2 Program takes up comparatively less
memory space.

3 No transfer of program counter. 3 Transferring of program counter is


required.

4 No overhead of using stack for 4 Overhead of using stack for transferr


transferring control. control.

5 Execution is fast 5 Execution is comparatively slow.

6 Assembly time is more. 6 Assembly time is comparatively less

7 More advantageous to the programs when 7 More advantageous to the programs


repeated group of instruction is too short. when repeated group of instructions i
quite large.
Thank You

You might also like