0% found this document useful (0 votes)
16 views37 pages

SS Mod 2

Uploaded by

amayavk118
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

SS Mod 2

Uploaded by

amayavk118
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Role of Assembler

Source Object
Assembler Linker
Program Code

Executable
Code

Loader

2
Assemblers
• Fundamental functions
– Translating mnemonic operation codes to their
machine language equivalents.
– Assigning machine addresses to symbolic labels.

• Machine dependency
– Different machine instruction formats and codes
3
Example Program (Fig. 2.1)
– A main routine reads records from input device (code
F1) and copies them to output device (code 05).
– Main routine calls subroutine RDREC to read a record
into the buffer and WRREC to write the record from
the buffer to the output device.
– Buffering is needed as the I/O rate of two devices may
be different(eg: disk and printer).
– End of a record is marked with a null character(00).
– When the eof is detected, program writes EOF on
output device and terminates by executing RSUB
instruction.
4
SIC Assembly Program (Fig. 2.1)
Line numbers Mnemonic opcode
(for reference)
comments
Address labels
operands

5
SIC Assembly Program (Fig. 2.1)
Indicate comment lines

Index addressing
6
SIC Assembly Program (Fig. 2.1)

7
Example Program (Fig. 2.1)
• Data transfer (RD, WD)
– a buffer is used to store record
– buffering is necessary for different I/O rates
– the end of each record is marked with a null character
(0016)
– the end of the file is indicated by a zero-length record
• Subroutines (JSUB, RSUB)
– RDREC, WRREC
– save link register first before nested jump

8
Assembler Directives
• Pseudo-Instructions
– Not translated into machine instructions.
– Providing information to the assembler
• Basic assembler directives
START – Specify name and starting address for the program.
END
– BYTE
– WORD
– RESB
– RESW
Assembler Tasks
• The translation of source program to object code
requires to accomplish the following functions:
– Convert mnemonic operation codes to their machine
language equivalents (e.g. translate STL to 14 - Line 10)
– Convert symbolic operands to their equivalent
machine addresses (e.g. translate RETADR to 1033 -
Line 10)
– Build machine instructions in the proper format
– Convert the data constants specified in the source
program into their internal machine representations
(e.g. translate EOF to 454F46) - Line 80
– Write object program and the assembly listing

10
Fig. 2.2 (1) Program with Object code

11
Fig. 2.2 (2) Program with Object code

12
Fig. 2.2 (3) Program with Object code

13
Forward Reference
• A reference to a label (RETADR) that is defined
later in the program
• Solution
– Two passes
• First pass: does little more than scan the source
program for label definition and assign addresses.
• Second pass: performs most of the actual instruction
translation.

14
Difficulties: Forward Reference
• Forward reference: reference to a label that is
defined later in the program.

Loc Label Operator Operand

1000 FIRST STL RETADR


1003 CLOOP JSUB RDREC
… … … … …
1012 J CLOOP
… … … … …
1033 RETADR RESW 1

15
• Assembler must:
– Translate instructions of the source program.
– Process assembler directives.
– Write the generated object code onto some
output device.
• Object program contain three types of
records:
– Header Record
– Text Record
– End record
Object Program
• Header
Col. 1 H
Col. 2-7 Program name
Col. 8-13 Starting address (hex)
Col. 14-19 Length of object program in bytes (hex)
• Text
Col.1 T
Col.2-7 Starting address in this record (hex)
Col. 8-9 Length of object code in this record in bytes (hex)
Col. 10-69 Object code.
• End
Col.1 E
Col.2-7 Address of first executable instruction (hex)
(END program_name)

17
Fig. 2.3 (Object Program)

1033-2038: Storage reserved by the loader

18
Two Pass SIC Assembler
• Pass 1 (define symbols)
– Assign addresses to all statements in the program
– Save the addresses assigned to all labels for use in Pass 2
– Perform processing of assembler directives(processing that affects
address assignment.Eg: determining length of data area defined by
BYTE,RESW etc.)

Pass 2 (assemble instructions and generate object program)


– Assemble instructions (translate opcode and look up addresses)
– Generate data values defined by BYTE, WORD etc.
– Perform processing of assembler directives not done during Pass 1
– Write the object program and the assembly listing.

19
Assembler Data Structures
• Operation Code Table (OPTAB)
• Symbol Table (SYMTAB)
• Location Counter (LOCCTR)

OPTAB

Pass 1
Intermediate Object
file Program
Source
Pass 2
LOCCTR SYMTAB

20
Two major Data structures:
• Operation Code Table(OPTAB)
• Used to look up mnemonic opcodes and translate
them to their machine language equivalents.
• Symbol Table(SYMTAB)
– Used to store values(addresses) assigned to labels.
Location Counter (LOCCTR)
• A variable used to help in the assignment of addresses, i.e.,
LOCCTR gives the address of the associated label.
• LOCCTR is initialized to the beginning address specified in the
START statement.
• After each source statement is processed during pass 1, the
length of assembled instruction or data area to be generated
is added to LOCCTR.
• When we reach a label in the source program, the current
value of the LOCCTR gives the address associate with that
label.

22
Operation Code Table (OPTAB)
• Contents:
– Mnemonic operation codes (as the keys)
– Machine language equivalents
– Instruction format and length
– Note: SIC/XE has instructions of different lengths
• During pass 1:
– Look up an validate operation codes in the source program.
– Find the instruction length to increase LOCCTR.
• During pass 2:
– Translate the operation codes to their machine language equivalents.
– Determine the instruction format to use in assembly process.
• Implementation: usually organised as a static hash table with
mnemonic opcode as key. (entries are not normally added to
or deleted from it)
– Hash table organization is particularly appropriate – fast
retrieval with minimum searching.
23
COPY 1000
SYMTAB FIRST
CLOOP
1000
1003
• Contents: ENDFIL 1015
EOF 1024
– Label name THREE 102D
– Label address(value) ZERO 1030
RETADR 1033
– Flags (to indicate error conditions) LENGTH 1036
– Data type or length(data area) BUFFER 1039
RDREC 2039
• During pass 1:
– Store label name and assigned address (from LOCCTR) in
SYMTAB.
• During pass 2:
– Symbols used as operands are looked up in SYMTAB, to
obtain addresses to be inserted in the assembled
instructions.
• Implementation:
– a dynamic hash table for efficient insertion and retrieval.
– Should perform well with non-random keys (LOOP1,
LOOP2). 24
• Both passes of the assembler can read the original
source program as input.
• Certain info. can/should be communicated between
2 passes.(LOCCTR value, error flag stmts, etc.)
• Pass 1 writes an intermediate file(source statement
together with assigned addresses, error indicators
etc.)
• This intermediate file is used as input to Pass 2.
Two Pass SIC Assembler
• Read from input line
– LABEL, OPCODE, OPERAND

Source
program

Intermediate Object
Pass 1 Pass 2
file codes

OPTAB SYMTAB SYMTAB

26
Program with Object code

27
Program with Object code

28
Program with Object code

29
Pseudo code Pass 1

30
Pseudo code Pass 1

31
Pseudo code Pass 2

32
Pseudo code Pass 2

33
One-Pass Assemblers (2/2)
• Two types of one-pass assembler
– Type 1: Load-and-go
• Produces object code directly in memory for immediate
execution
– Type 2:
• Produces usual kind of object code for later execution

34
Load-and-go Assembler (1/3)
• Characteristics
– Useful for program development and testing
– Avoids the overhead of writing the object program
out and reading it back
– Both one-pass and two-pass assemblers can be
designed as load-and-go
– However one-pass also avoids the overhead of an
additional pass over the source program
– For a load-and-go assembler, the actual address
must be known at assembly time, we can use an
absolute program 35
Load-and-go Assembler (2/3)
• Assembler operations:
1. Omit address translation for any undefined
symbol
2. Insert the symbol into SYMTAB, mark it undefined
3. The address that refers to the undefined symbol is
added to a list of forward references associated
with the symbol table entry
4. When the definition for a symbol is encountered,
the proper address for the symbol is then inserted
into any instructions previously generated
according to the forward
36 reference list
Load-and-go Assembler (3/3)
• At the end of the program
– Any SYMTAB entries that are still marked with *
indicate undefined symbols
– Search SYMTAB for the symbol named in the END
statement and jump to this location to begin
execution
• The actual starting address must be specified
at assembly time

37

You might also like