Unit – II Assembler

122
1 Unit – II Assembler MC9224 – System Software Department of Computer ponns ADHIPARASAKTHI ENGINEERING COLLEGE OmSakth i Melmaruvathur-603 319

description

OmSakthi. ADHIPARASAKTHI ENGINEERING COLLEGE. Melmaruvathur-603 319. Unit – II Assembler. Department of Computer Applications. ponns. MC9224 – System Software. OmSakthi. ADHIPARASAKTHI ENGINEERING COLLEGE. Melmaruvathur-603 319. Outline Basic Assembler Functions - PowerPoint PPT Presentation

Transcript of Unit – II Assembler

Page 1: Unit – II Assembler

1

Unit – IIAssembler

MC9224 – System Software

Department of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Page 2: Unit – II Assembler

2MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319Outline Basic Assembler Functions

A simpler SIC Assembler Assembler Algorithm and Data Structures

Machine-Dependent Assembler Features Instruction Formats and Addressing Modes Program Location

Machine-Independent Assembler Features Literals Symbol-Defining Statements Expressions Program Blocks Control Section and Program Linking

Assembler Design Options One-Pass Assembler Multi-Pass Assembler

Implementation Examples MASM Assembler

Page 3: Unit – II Assembler

3MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1. Basic Assembler FunctionsExample Program SIC Assembler Purpose

Reads records from input device (code F1) and store in BUFFER

Copies them to output device (code 05) At the end of the file, writes an extra EOF on the output

device, then RSUB to the operating system Data transfer (RD, WD)

End of each record is marked with a null character End of the file is indicated by a zero-length record

Subroutines (JSUB, RSUB) RDREC, WRREC Save link register first before nested jump

Page 4: Unit – II Assembler

4MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1. Basic Assembler Functions5 COPY START 1000 LOAD PROG AT LOC 100010 FIRST STL RETADR SAVE RETURN ADDRESS15 CLOOP JSUB RDREC READ INPUT RECORD20 LDA LENGTH TEST FOR EOF (LENGTH = 0)25 COMP ZERO30 JEQ ENDFIL EXIT IF EOF FOUND35 JSUB WRREC WRITE OUTPUT RECORD40 J CLOOP LOOP 45 ENDFIL LDA EOF INSERT END OF FILE MARKER50 STA BUFFER 55 LDA THREE SET LENGTH = 3 60 STA LENGTH65 JSUB WRREC WRITE EOF70 LDL RETADR GET RETURN ADDRESS 75 RSUB RETURN TO CALLER 80 EOF BYTE C’EOF’85 THREE WORD 390 ZERO WORD 095 RETADR RESW 1100 LENGTH RESW 1105 BUFFER RESB 4096 4096-BYTE BUFFER AREA

Page 5: Unit – II Assembler

5MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1. Basic Assembler Functions110 . 115 . SUBROUTINE TO READ RECORD INTO BUFFER120 . 125 RDREC LDX ZERO CLEAR LOOP COUNTER 130 LDA ZERO CLEAR A TO ZERO135 RLOOP TD INPUT TEST INPUT DEVICE140 JEQ RLOOP LOOP UNTIL READY 145 RD INPUT READ CHARACTER INTO A 150 COMP ZERO TEST FOR END OF RECORD155 JEQ EXIT EXIT LOOP IF EOR 160 STCH BUFFER,X STORE CHAR IN BUFFER165 TIX MAXLEN LOOP UNLESS MAX LENGTH 170 JLT RLOOP HAS BEEN REACHED 175 EXIT STX LENGTH SAVE RECORD LENGTH 180 RSUB RETURN TO CALLER 185 INPUT BYTE X’F1’ CODE FOR INPUT DEVICE190 MAXLEN WORD 4096

Page 6: Unit – II Assembler

6MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1. Basic Assembler Functions

195 . 200 . SUBROUTINE TO WRITE RECORD FROM BUFFER 205 . 210 WRREC LDX ZERO CLEAR LOOP COUNTER 215 WLOOP TD OUTPUT TEST OUTPUT DEVICE 220 JEQ WLOOP LOOP UNTIL READY 225 LDCH BUFFER,X GET CHAR FROM BUFFER230 WD OUTPUT WRITE CHARACTER235 TIX LENGTH LOOP UNTIL ALL CHAR240 JLT WLOOP HAVE BEEN WRITTEN 245 RSUB RETURN TO CALLER 250 OUTPUTBYTE X’05’ CODE FOR OUTPUT DEVICE 255 END FIRST

Page 7: Unit – II Assembler

7MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1. Basic Assembler Functions

Assembler Directives Pseudo-Instructions / Basic assembler

directives Not translated into machine instructions Providing information to the assembler

Basic assembler directives START – Specifies starting memory address of object

program END – Marks the end of the program BYTE – Generate Character / Hexadecimal constant – 1

byte WORD – Generate 1 word constant RESB – Reserve the indicated number of bytes for data

area RESW – Reserve the indicated number of words for data

area

Page 8: Unit – II Assembler

8MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler

Mnemonic code (or instruction name) opcode Symbolic operands (e.g., variable names)

Machine addresses Choose the proper instruction format and

addressing mode Constants Equivalent Internal Machine

representation Output to object files and listing files

Page 9: Unit – II Assembler

9MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Example Program &

Object Code Line Loc Source statement Object code5 1000 COPY START 100010 1000 FIRST STL RETADR 14103315 1003 CLOOP JSUB RDREC 48203920 1006 LDA LENGTH 00103625 1009 COMP ZERO 28103030 100C JEQ ENDFIL 30101535 100F JSUB WRREC 48206140 1012 J CLOOP 3C100345 1015 ENDFIL LDA EOF 00102A50 1018 STA BUFFER 0C103955 101B LDA THREE 00102D60 101E STA LENGTH 0C103665 1021 JSUB WRREC 48206170 1024 LDL RETADR 08103375 1027 RSUB 4C0000

Page 10: Unit – II Assembler

10MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Example Program &

Object Code Line Loc Source statement Object code

80 102A EOF BYTE C’EOF’ 454F4685 102D THREE WORD 3 00000390 1030 ZERO WORD 0 00000095 1033 RETADR RESW 1100 1036 LENGTH RESW 1105 1039 BUFFER RESB 4096

Page 11: Unit – II Assembler

11MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Example Program &

Object Code 110 . 115 . SUB TO READ RECORD INTO BUFFER120 . 125 2039 RDREC LDX ZERO 041030130 203C LDA ZERO 001030135 203F RLOOP TD INPUT E0205D140 2042 JEQ RLOOP 30203F145 2045 RD INPUT D8205D150 2048 COMP ZERO 281030155 204B JEQ EXIT 302057160 204E STCH BUFFER,X 549039165 2051 TIX MAXLEN 2C205E170 2054 JLT RLOOP 38203F175 2057 EXIT STX LENGTH 101036180 205A RSUB 4C0000185 205D INPUT BYTE X’F1’ F1190 205E MAXLEN WORD 4096 001000

Page 12: Unit – II Assembler

12MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Example Program &

Object Code 195 .200 . SUB TO WRITE RECORD FROM BUFFER 205 .210 2061 WRREC LDX ZERO 041030215 2064 WLOOP TD OUTPUT E02079220 2067 JEQ WLOOP 302064 225 206A LDCH BUFFER,X 509039230 206D WD OUTPUT DC2079235 2070 TIX LENGTH 2C1036240 2073 JLT WLOOP 382064 245 2076 RSUB 4C0000 250 2079 OUTPUT BYTE X’05’ 05 255 END FIRST

Page 13: Unit – II Assembler

13MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler Mnemonic code (or instruction name)

opcode Examples:

STL 1033 14 10 33

LDA 1036 00 10 36

0001 0100 0 001 0000 0011 0011

0000 0000 0 001 0000 0011 0110

Page 14: Unit – II Assembler

14MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Converting Symbol

to Numbers Isn’t it straightforward? Isn’t it simply the sequential processing of the source

program, one line at a time? Not so, if we have forward references

we don’t know the value of the symbol, because it is defined later in the code Need two pass AssemblerLoc Label Operator Operand

1000 FIRST STL RETADR

1003 CLOOP JSUB RDREC … … … …

…1012 J CLOOP

… … … ……

1033 RETADR RESW 1

Page 15: Unit – II Assembler

15MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Symbolic Operands

We are not likely to write memory addresses directly in our code Instead, we will define variable names

Other examples of symbolic operands: Labels (for jump instructions) Subroutines Constants

Page 16: Unit – II Assembler

16MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Two Pass Assembler Pass1

Scan the source program Identify the label definitions and assign addresses

Pass2 All other assembler operations

Load and Execution

Page 17: Unit – II Assembler

17MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Record Types

Object Program contains three Records Header

Col. 1 HCol. 2~7 Program nameCol. 8~13 Starting address (hex)Col. 14-19 Length of object program in bytes (hex)

Text Col.1 TCol.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 (69-10+1)/6=10 instructions

EndCol.1 ECol.2~7 Address of first executable instruction (hex)

Page 18: Unit – II Assembler

18MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler – Object Program

H^COPY ^001000^00107A

T^001000^1E^141033^482039^001036^281030^301015^482061^ 3C1003^00102A^0C1039^00102D

T^00101E^15^0C1036^482061^081044^4C0000^454F46^000003^000000

T^002039^1E^041030^001030^E0205D^30203F^D8205D^281030^302057^549039^2C205E^38203F

T^002057^1C^101036^4C0000^F1^001000^041030^E02079^302064^509039^DC2079^2C1036

T^002073^07^382064^4C0000 ^05

E^001000 starting address

Page 19: Unit – II Assembler

19MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.1. A Simple SIC Assembler

Pass 1 Assign addresses to all statements in the program Save the values assigned to all labels for use in Pass 2 Perform some processing of assembler directives

Pass 2 Assemble instructions by translating opcode and symbolic

operands Generate data values defined by BYTE, WORD Perform processing of assembler directives not done in

Pass 1 Write the object program and the assembly listing

Functions of a Two Pass Assembler

Page 20: Unit – II Assembler

20MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures

Internal Data Structures Two Major Data structures

Operation Code Table (OPTAB) Symbol Table (SYMTAB)

One variable - Location Counter (LOCCTR)

Pass 1 Pass 2 Intermediatefile

OPTAB SYMTAB SYMTAB

Sourceprogram

Object code

Page 21: Unit – II Assembler

21MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures OPTAB (Operation Code Table)

Content Mnemonic names, machine code (instruction format,

length for SIC/XE) etc. Characteristic

Static table (assembler itself written) Implementation

Array or hash table(mnemonic operation code as key), easy for search

Usage Pass 1 - look up and validate mnemonic operation codes in

the source program Pass 2 – translate opcodes into machine language

Page 22: Unit – II Assembler

22MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures SYMTAB (Symbol Table)

Content Label name, value, flag, (type, length for

data area ) etc. Characteristic

Dynamic table (insert, delete, search) Implementation

Hash table, non-random keys, hashing function

Usage Pass 1 - enter symbol names into

SYMTAB along with the assign address ( from LOCCTR)

Pass 2 – look up symbols, fetch address and insert in object code

COPY 1000FIRST 1000CLOOP 1003ENDFIL 1015EOF 1024THREE 102DZERO 1030RETADR 1033LENGTH 1036BUFFER 1039RDREC 2039

Page 23: Unit – II Assembler

23MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Intermediate File

Pass1 and pass2 use source program as input. Other information should be passed between

two passes. Pass 1 writes an intermediate file

Each source statement together with its assigned address

Error indicators Used as input for pass 2

Page 24: Unit – II Assembler

24MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Algorithm for Pass1 Assembler

Page 25: Unit – II Assembler

25MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Algorithm for Pass1 Assembler

Page 26: Unit – II Assembler

26MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Algorithm for Pass2 Assembler

Page 27: Unit – II Assembler

27MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Algorithm for Pass2 Assembler

Page 28: Unit – II Assembler

28MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.1.2. Assembler Algorithm and Data Structures Object Program Loading

1000 141033482039001036281030301015

100310061009100C

08103310244C00001027454F46102A000003102D0000001030

STL RETADRJSUB RDRECLDA LENGTHCOMP ZEROJEQ ENDFIL

LDL RETADRRSUB‘E’ ‘O’ ‘F’30

xxxxxx1033

1036 xxxxxx

BYTE C’EOF’WORD 3WORD 0RESW 1RESW 1

RETADRLENGTH

Page 29: Unit – II Assembler

29MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2. Machine-Dependent Assembler Features SIC/XE Assembler

We have learned the 2-pass assembler for SIC

What’s new for SIC/XE? More addressing modes Program relocation

Page 30: Unit – II Assembler

30MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

SIC/XE: PC-relative or base-relative addressing: op

m Indirect addressing: op

@m Immediate addressing: op #c Extended format:

+op m Index addressing: op

m,x Register-to-register instructions Larger memory multi-programming

(program allocation)

Page 31: Unit – II Assembler

31MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

Register translation – during pass 2 Register name (A, X, L, B, S, T, F, PC, SW) translated

to their ids (0,1, 2, 3, 4, 5, 6, 8, 9) Use separate table or may be preloaded in SYMTAB

Address translation Register-memory instructions: try PC-relative first,

then base-relative addressing Assembler makes its own decision User must specify extended format (format 4),

otherwise error will be generated by the assembler Format 3: 12-bit displacement

Base-relative: 0~4095 PC-relative: -2048~2047 Format 4: 20-bit address field

Page 32: Unit – II Assembler

32MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

Line Loc Source statement Object code5 0000 COPY START 010 0000 FIRST STL RETADR 17202D12 0003 LDB #LENGTH 69202D13 BASE LENGTH15 0006 CLOOP +JSUB RDREC 4B10103620 000A LDA LENGTH 03202625 000D COMP #0 29000030 0010 JEQ ENDFIL 33200735 0013 +JSUB WRREC 4B10105D40 0017 J CLOOP 3F2FEC45 001A ENDFIL LDA EOF 03201050 001D STA BUFFER 0F201655 0020 LDA #3 01000360 0023 STA LENGTH 0F200D65 0026 +JSUB WRREC 4B10105D70 002A J @RETADR 3E200380 002D EOF BYTE C’EOF’ 454F4695 0030 RETADR RESW 1100 0033 LENGTH RESW 1105 0036 BUFFER RESB 4096

Page 33: Unit – II Assembler

33MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

115 . READ RECORD INTO BUFFER120 . 125 1036 RDREC CLEAR X B410130 1038 CLEAR A B400132 103A CLEAR S B440133 103C +LDT #4096 75101000135 1040 RLOOP TD INPUT E32019140 1043 JEQ RLOOP 332FFA145 1046 RD INPUT DB2013150 1049 COMPR A,S A004155 104B JEQ EXIT 332008160 104E STCH BUFFER,X 57C003165 1051 TIXR T B850170 1053 JLT RLOOP 3B2FEA175 1056 EXIT STX LENGTH 134000180 1059 RSUB 4F0000185 105C INPUT BYTE X’F1’ F1

Page 34: Unit – II Assembler

34MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

195 .200 . WRITE RECORD FROM BUFFER 205 .210 105D WRREC CLEAR X B41012 105F LDT LENGTH 774000215 1062 WLOOP TD OUTPUT E32011220 1065 JEQ WLOOP 332FFA 225 1068 LDCH BUFFER,X 53C003230 106B WD OUTPUT DF2008235 106E TIXR T B850240 1070 JLT WLOOP 3B2FEF245 1073 RSUB 4F0000250 1076 OUTPUT BYTE X’05’ 05255 END FIRST

Page 35: Unit – II Assembler

35MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes

10 0000 FIRST STL RETADR 17202D

Displacement= RETADRPC = 00300003 = 02D

40 0017 J CLOOP 3F2FEC

Displacement= CLOOPPC= 0006001A= 14= FEC

OPCODE e Addressn i x b p

0001 01 0 (02D)161 1 0 0 1

OPCODE e Addressn i x b p

0011 11 0 (FEC)161 1 0 0 1

PC Relative Addressing

Page 36: Unit – II Assembler

36MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing ModesBase Relative Addressing Mode

BASE register and directive: 12 LDB #LENGTH13 BASE LENGTH Base register is under the control of programmer BASE directive tells assembler that LENGHTH is base

address; NOBASE releases the binding, until B value will be used as Base address

160 104E STCH BUFFER, X 57C003

Displacement= BUFFERB = 00360033 = 3 Compare lines 20 and 175 (PC vs Base addressing)

OPCODE e Addressn i x b p

0101 01 0 (003)161 1 1 1 0

Page 37: Unit – II Assembler

37MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing ModesBase Relative Addressing Mode

Why cannot we use PC-relative?

Assembler knows the PC value at execution time, it cannot be changeable

Base register is under the control of programmer, and set initially and changeable

Page 38: Unit – II Assembler

38MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

55 0020 LDA #3 010003

133 103C +LDT #4096 75101000

OPCODE e Addressn i x b p

0000 00 0 (003)160 1 0 0 0

OPCODE e Addressn i x b p

0111 01 1 (01000)160 1 0 0 0

2.2.1. Instruction Formats and Addressing ModesImmediate Address Translation

Page 39: Unit – II Assembler

39MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing Modes Immediate Address Translation

12 0003 LDB #LENGTH 69202D

12 0003 LDB #LENGTH 690033

The immediate operand is the value of the symbol LENGTH, which is the address assigned to LENGTH

LENGTH=0033=PC+displacement=0006+02D

OPCODE e Addressn i x b p

0110 10 0 (02D)160 1 0 0 1

OPCODE e Addressn i x b p

0110 10 0 (033)160 1 0 0 0

Page 40: Unit – II Assembler

40MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.1. Instruction Formats and Addressing ModesIndirect Address Translation

Target addressing is computed as usual (PC-relative or BASE-relative)

Only the n bit is set to 1

70 002A J @RETADR 3E2003

TA=RETADR=0030 TA=(PC)+displacement=002D+0003

OPCODE e Addressn i x b p

0011 11 0 (003)161 0 0 0 1

Page 41: Unit – II Assembler

41MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2.2. Program Relocation

Need for program relocation Example :

If program starts at 1000

55 101B LDA THREE 00102D If program starts at 2000

55 201B LDA THREE 00202D Some lines need not be modified (line 85,

value 3) Assembler does not know that where the

program will be loaded, but it can inform to loader via modification records.

Page 42: Unit – II Assembler

42MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Loaded at 0000 Loaded at 5000 Loaded at 7420

2.2.2. Program Relocation

Page 43: Unit – II Assembler

43MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Example Fig. 2.2 Absolute program, starting address 1000

5 2000 1000 COPY START 1000 10 2000 1000 FIRST STL RETADR 141033 14203315 2003 1003 CLOOP JSUB RDREC 482039 48303920 2006 1006 LDA LENGTH 001036 00203625 2009 1009 COMP ZERO 281030 28203030 200C 100C JEQ ENDFIL 301015 30201535 200F 100F JSUB WREC 482061 48306140 2012 1012 J CLOOP 3C1003 3C200345 2015 1015 ENDFIL LDA EOF 00102A 00202A50 2018 1018 STA BUFFER 0C1039 0C203955 201B 101B LDA THREE 00102D 00202D60 201E 101E STA LENGTH 0C1036 0C203665 2021 1021 JSUB WREC 482061 48306170 2024 1024 LDL RETADR 081033 08203375 2027 1027 RSUB 4C0000 4C000080 202A 102A EOF BYTE C'EOF' 454E46 454E4685 202D 102D THREE WORD 3 000003 00000390 2030 1030 ZERO WORD 0 000000 00000095 2033 1033 RETADR RESW 1100 2036 1036 LENGTH RESW 1105 2039 1039 BUFFER RESB 4096

==== 2000==== 2000

2.2.2. Program Relocation

Page 44: Unit – II Assembler

44MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319 Example Fig. 2.6:

Except for absolute address, rest of the instructions need not be modified not a memory address (immediate addressing) PC-relative, Base-relative

Parts requiring modification at load time are those with absolute addresses

== 10005 1000 0000 COPY START 010 1000 0000 FIRST STL RETADR 17202D 17202D 12 1003 0003 LDB #LENGTH 69202D 69202D 13 BASE LENGTH 15 1006 0006 CLOOP +JSUB RDREC 4B101036 4B10203620 100A 000A LDA LENGTH 032026 032026 25 100D 000D COMP #0 290000 290000 30 1010 0010 JEQ ENDFIL 332007 332007 35 1013 0013 +JSUB WRREC 4B10105D 4B10205D40 1017 0017 J CLOOP 3F2FEC 3F2FEC 45 101A 001A ENDFIL LDA EOF 032010 032010 50 101D 001D STA BUFFER 0F2016 0F2016 55 1020 0020 LDA #3 010003 010003 60 1023 0023 STA LENGTH 0F200D 0F200D 65 1026 0026 +JSUB WRREC 4B10105D 4B10205D70 102A 002A J @RETADR 3E2003 3E2003 80 102D 002D EOF BYTE C'EOF' 454F46 454F46 95 1030 0030 RETADR RESW 1100 1036 0036 BUFFER RESB 4096

2.2.2. Program Relocation

Page 45: Unit – II Assembler

45MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Use relative addresses Did you notice that we didn’t modify the addresses for

JEQ, JLT and J instructions? We didn’t modify the addresses for RETADR, LENGTH,

and BUFFER in Figure 2.6 either. The sample SIC/EX program is easier

Mostly PC or base relative Only extended format instructions have direct addresses

and require modification

Making Program Relocation Easier

2.2.2. Program Relocation

Page 46: Unit – II Assembler

46MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

An object program that contains information needed for address modification for loading

Modification record Col 1 M Col 2-7 Starting location of the address field to be modified, relative to the beginning of the program (count in

bytes) Col 8-9 length of the address field to be modified, in half-

bytes (address field to be modified may not occupy

an integral number of bytes, e.g. 20 bits)

Relocatable Program

2.2.2. Program Relocation

Page 47: Unit – II Assembler

47MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Modification records are added to the object files. (See pp.67 and Figure 2.8.)

Example:HCOPY 001000 001077T000000 1D 17202D…4B101036…T00001D ………M000007 05 Modification Record……E000000

Object File with M-Records

2.2.2. Program Relocation

Page 48: Unit – II Assembler

48MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Modification Record

0009 3 61 01 04 B2 D

0008000700060005

+JSUB RDREC

0004 2 06 92 D2 01 7

0003000200010000

LDB #LENGTH

STL RETADR

000C 2 62 00 3

000B000A LDA LENGTH

M000007 05

Address 0007

5 half-bytes

2.2.2. Program Relocation

Page 49: Unit – II Assembler

49MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Object Code

2.2.2. Program Relocation

Page 50: Unit – II Assembler

50MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

319

Design idea Let programmers write the value of a

constant operand as a part of the instruction that uses it

Avoids having to define the constant elsewhere in the program and make up a label for it

Example (Fig. 2.10) 045 001A ENDFIL LDA =C’EOF’

215 1062 WLOOP TD =X’05’

2.3. Machine-Independent Assembler Features - Literals

Page 51: Unit – II Assembler

51MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - LiteralsExample Program

5 0000 COPY START 010 0000 FIRST STL RETADR 17202D13 0003 LDB #LENGTH 69202D14 BASE LENGTH15 0006 CLOOP +JSUB RDREC 4B10103620 000A LDA LENGTH 03202625 000D COMP #0 29000030 0010 JEQ ENDFIL 33200735 0013 +JSUB WRREC 4B10105D40 0017 J CLOOP 3F2FEC45 001A ENDFIL LDA =C’EOF’ 03201050 001D STA BUFFER 0F201655 0020 LDA #3 01000360 0023 STA LENGTH 0F200D65 0026 +JSUB WRREC 4B10105D70 002A J @RETADR 3E200393 LTORG95 0030 RETADR RESW 1100 0033 LENGTH RESW 1105 0036 BUFFER RESB 4096106 1036 BUFEND EQU *107 1000 MAXLEN EQU BUFEND - BUFFER

Page 52: Unit – II Assembler

52MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - LiteralsExample Program

115 . READ RECORD INTO BUFFER120 . 125 1036 RDREC CLEAR X B410130 1038 CLEAR A B400132 103A CLEAR S B440133 103C +LDT #MAXLEN 75101000135 1040 RLOOP TD INPUT E32019140 1043 JEQ RLOOP 332FFA45 1046 RD INPUT DB2013150 1049 COMPR A,S A004155 104B JEQ EXIT 332008160 104E STCH BUFFER,X 57C003165 1051 TIXR T B850170 1053 JLT RLOOP 3B2FEA175 1056 EXIT STX LENGTH 134000180 1059 RSUB 4F0000185 105C INPUT BYTE X’F1’ F1

Page 53: Unit – II Assembler

53MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Example Program

195 .200 . WRITE RECORD FROM BUFFER 205 .210 105D WRREC CLEAR X B410212 105F LDT LENGTH 774000215 1062 WLOOP TD =X’05’ E32011220 1065 JEQ WLOOP 332FFA 225 1068 LDCH BUFFER,X 53C003230 106B WD =X’05’ DF2008235 106E TIXR T B850240 1070 JLT WLOOP 3B2FEF245 1073 RSUB 4F0000255 END FIRST

Page 54: Unit – II Assembler

54MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.2. Machine-Independent Assembler Features - Literals

Literal vs. Immediate Operands

Immediate operands Operand value is assembled as part of the machine

instruction55 0020 LDA #3

010003 Literals

The assembler generates the specified value as a constant at some other memory location

45 001A ENDFILLDA =C’EOF’ 032010 Compare (Fig. 2.6)

45 001A ENDFILLDA EOF 03201080 002D EOF BYTE C’EOF’ 454F46

Page 55: Unit – II Assembler

55MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Literal Implementation (1/4)

Literal pools All the literals used in the program grouped together

in to one or more literal pools Normally literals are placed into a pool at the end of

the program See Fig. 2.10 (after END statement)

In some cases, it is desirable to place literals into a pool at some other location in object program

Use assembler directive LTORG: create a literal pool that contains all of the literal operands used since the previous LTROG, and place it where LTORG was encountered

Reason: keep literal operand close to the instruction

Page 56: Unit – II Assembler

56MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Example Program with Literal Implementation5 0000 COPY START 0

10 0000 FIRST STL RETADR 17202D13 0003 LDB #LENGTH 69202D14 BASE LENGTH15 0006 CLOOP +JSUB RDREC 4B10103620 000A LDA LENGTH 03202625 000D COMP #0 29000030 0010 JEQ ENDFIL 33200735 0013 +JSUB WRREC 4B10105D40 0017 J CLOOP 3F2FEC45 001A ENDFIL LDA =C’EOF’ 03201050 001D STA BUFFER 0F201655 0020 LDA #3 01000360 0023 STA LENGTH 0F200D65 0026 +JSUB WRREC 4B10105D70 002A J @RETADR 3E200393 LTORG

002D * =C’EOF’ 454F4695 0030 RETADR RESW 1100 0033 LENGTH RESW 1105 0036 BUFFER RESB 4096106 1036 BUFEND EQU *107 1000 MAXLEN EQU BUFEND - BUFFER

Page 57: Unit – II Assembler

57MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

115 . READ RECORD INTO BUFFER120 . 125 1036 RDREC CLEAR X B410130 1038 CLEAR A B400132 103A CLEAR S B440133 103C +LDT #MAXLEN 75101000135 1040 RLOOP TD INPUT E32019140 1043 JEQ RLOOP 332FFA145 1046 RD INPUT DB2013150 1049 COMPR A,S A004155 104B JEQ EXIT 332008160 104E STCH BUFFER,X 57C003165 1051 TIXR T B850170 1053 JLT RLOOP 3B2FEA175 1056 EXIT STX LENGTH 134000180 1059 RSUB 4F0000185 105C INPUT BYTE X’F1’ F1

Example Program with Literal Implementation

Page 58: Unit – II Assembler

58MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Example Program with Literal Implementation

195 .200 . WRITE RECORD FROM BUFFER 205 .210 105D WRREC CLEAR X B410212 105F LDT LENGTH 774000215 1062 WLOOP TD =X’05’ E32011220 1065 JEQ WLOOP 332FFA 225 1068 LDCH BUFFER,X 53C003230 106B WD =X’05’ DF2008235 106E TIXR T B850240 1070 JLT WLOOP 3B2FEF245 1073 RSUB 4F0000255 END FIRST

1076 * =X’05’ 05

Page 59: Unit – II Assembler

59MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Duplicate literals215 1062 WLOOP TD =X’05’230 106B WD =X’05’

Assembler should recognize duplicate literals and store only one copy of specified data value

By comparing defining character string, e.g. =X’05’ By comparing the generated data value, e.g.

=C’EOF’ and =X’454F46’, but benefits are usually not great enough to justify the additional complexity in the assembler

Literal Implementation (2/4)

Page 60: Unit – II Assembler

60MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Literal Implementation (3/4) – Problem of duplicate literal recognition

'*' denotes a literal refer to the current value of location counter.

Some have same name, but different values. BASE * LDB =*

If the literal value represents an 'address' in the program, the assembler must also generate the appropriate 'Modification records'

Page 61: Unit – II Assembler

61MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3. Machine-Independent Assembler Features - Literals

Literal Implementation (4/4)

Use LITTAB: Literal name, operand value & length, operand address

Pass 1 Build LITTAB with literal name, operand value/length When LTORG is encountered, assign address to each literal

not yet assigned an address, update LOCCTR Pass 2

Search LITTAB for each literal encountered and generate the operand address

Data values of literals in literal pool are generated similarly as using BYTE or WORD statements

Generate modification record for literals that represent an address in the program

Page 62: Unit – II Assembler

62MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

Users can define labels on instructions or data areas

The value of a label is the address assigned to the statement

Users can also define symbols with valuessymbol EQU value value can be constants, other symbols, expressions Making source program easier to understand No forward reference

Page 63: Unit – II Assembler

63MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

Example 1: replacing symbolic names133 LDT #4096replaced as

MAXLEN EQU 4096133 +LDT #MAXLEN

Example 2: defining mnemonic names for registers

A EQU 0X EQU 1

RMO A,X can be replaced as

RMO 0,1

Page 64: Unit – II Assembler

64MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

Example 3: defining registers with symbolic namesv(SIC)

BASE EQU R1COUNT EQU R2INDEX EQU R3

To represent registers 1,2,3…

Page 65: Unit – II Assembler

65MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

Assembler directive ORG• Allow the assembler to reset the PC to values.

• ORG values• Use to indirectly assign values to symbols

• 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.

• If the previous value of LOCCTR can be automatically remembered, we can return to the normal use of LOCCTR by simply write ORG

Page 66: Unit – II Assembler

66MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

In the data structure Symbol :6 bytes Value :3 bytes Flags :2 bytes

If EQU statements are usedSTAB RESB 1100SYMBOL EQU STABVALUE EQU STAB+6FLAG EQU STAB+9

Symbol table structure to support ORG

Symbol

Value Flags

STAB

(100 entries)

: : :

Page 67: Unit – II Assembler

67MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

If ORG statements are usedSTAB RESB 1100

ORG STAB Set LOCCTR to STAB

SYMBOL RESB 6VALUE RESW 1 Size of

each fieldFLAGS RESB 2

ORG STAB +1100 Restore LOCCTR

VALUE field fetched by LDA VALUE,X

Page 68: Unit – II Assembler

68MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

All symbols used in the right –hand side must be defined previously in the program

ALPHA RESW 1BETA EQU ALPHA

BETA EQU ALPHAALPHA RESW 1

ALPHA EQU BETABETA EQU DELTADELTA RESW 1

Restriction with EQU

Valid

Invalid

Invalid

Page 69: Unit – II Assembler

69MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.2. Machine-Independent Assembler Features – Symbol Defining statement

All symbols used to specify the new location counter value must have been previously defined

ORG ALPHABYTE1 RESB 1BYTE2 RESB 2BYTE3 RESB 3

ORGALPHA RESB 1

Restriction with ORG

Invalid

Page 70: Unit – II Assembler

70MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.3. Machine-Independent Assembler Features – Expressions

Assemblers allow ‘the use of expressions as operand’• The assembler calculates the expressions and produce a

single operand address or value. • Operator +,-,*,/ • Division is usually defined to produce an integer result• Expressions may be constants, user-defined symbols or

special terms.• 106 1036 BUFFEND EQU *• Gives BUFFEND a value that is the address of the next byte

after the buffer area• Examples

• MAXLEN EQU BUFEND-BUFFER• STAB RESB (6+3+2)*MAXENTRIES

Page 71: Unit – II Assembler

71MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.3. Machine-Independent Assembler Features – Expressions

Values of terms can be• Absolute (independent of program location)

• Constants• Relative (to the beginning of the program)• Address labels

• * (value of LOCCTR)

Page 72: Unit – II Assembler

72MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.3. Machine-Independent Assembler Features – Expressions Expressions can be Absolute

Only absolute terms Relative terms in pairs with opposite signs for each

pair. Relative

All the relative terms except one can be paired as described in “absolute”.

The remaining unpaired relative term must have a positive sign.

No relative terms may enter into a multiplication or division operation

Expressions that do not meet the conditions of either “absolute” or “relative” should be flagged as errors.

Page 73: Unit – II Assembler

73MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.3. Machine-Independent Assembler Features – Expressions

Assembler needs to track type of an expression, to generate Modification records if needed

Need a ‘flag’ in the SYMTAB for indication

Absolute value BUFFEND – BUFFER

Illegal BUFFEND + BUFFER 100 – BUFFER 3* BUFFER

Handling relative terms in SYMTAB

Symbol Type Flag

RETADR R 0030

BUFFER R 0036

BUFEND R 1036

MAXLEN A 1000

Page 74: Unit – II Assembler

74MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks

Allow the generated machine instructions and data to appear in the object program in a different order Gather all code segments, data segments and stack

segments

Program blocks vs. Control sections Program blocks

Segments of code that are rearranged within a single object program unit

Control section Segments of code that are translated into independent

object program units

Page 75: Unit – II Assembler

75MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks Assembler directive : USE USE [blockname] At the beginning, statements are assumed to be part of the

unnamed (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 Assembler rearrange these segments to gather together the

pieces of each block and assign address Separate the program into blocks in a particular order Large buffer area is moved to the end of the object program Program readability is better if data areas are placed in the

source program close to the statements that reference them

Page 76: Unit – II Assembler

76MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks Three blocks are used default: executable instructions CDATA: all data areas that are less in

length CBLKS: all data areas that consists of

larger blocks of memory

Page 77: Unit – II Assembler

77MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program BlocksLine Source statement Object Code

5 0000 0 COPY START 010 0000 0 FIRST STL RETADR 17202D15 0003 0 CLOOP JSUB RDREC 4B202120 0006 0 LDA LENGTH 03202625 0009 0 COMP #0 29000030 000C 0 JEQ ENDFIL 33200635 000F 0 JSUB WRREC 4B203B40 0012 0 J CLOOP 3F2FEE45 0015 0 ENDFIL LDA =C’EOF’ 03205550 0018 0 STA BUFFER 0F205655 001B 0 LDA #3 01000360 001E 0 STA LENGTH 0F204865 0021 0 JSUB WRREC 4B202970 0024 0 J @RETADR 3E203F92 0000 1 USE CDATA95 0000 1 RETADR RESW 1100 0003 1 LENGTH RESW 1103 0000 2 USE CBLKS105 0000 2 BUFFER RESB 4096106 1000 2 BUFEND EQU *107 1000 MAXLEN EQU BUFEND - BUFFER

(Figure 2.11)

Page 78: Unit – II Assembler

78MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks115 . READ RECORD INTO BUFFER

120 . 123 0027 0 USE125 0027 0 RDREC CLEAR X B41030 0029 0 CLEAR A B400132 002B 0 CLEAR S B440133 002D 0 +LDT #MAXLEN 75101000135 0031 0 RLOOP TD INPUT E32038140 0034 0 JEQ RLOOP 332FFA145 0037 0 RD INPUT DB2032150 003A 0 COMPR A,S A004155 003C 0 JEQ EXIT 332008160 003F 0 STCH BUFFER,X 57A02F165 0042 0 TIXR T B850170 0044 0 JLT RLOOP 3B2FEA175 0047 0 EXIT STX LENGTH 13201F180 004A 0 RSUB 4F0000183 0006 1 USE CDATA185 0006 1 INPUT BYTE X’F1’ F1

Page 79: Unit – II Assembler

79MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks

195 .200 . WRITE RECORD FROM BUFFER 205 .208 004D 0 USE210 004D 0WRREC CLEAR X B410212 004F 0 LDT LENGTH 772017215 0052 0WLOOP TD =X’05’ E3201B220 0055 0 JEQ WLOOP 332FFA 225 0058 0 LDCH BUFFER,X 53A016230 005B 0 WD =X’05’ DF2012235 005E 0 TIXR T B850240 0060 0 JLT WLOOP 3B2FEF245 0063 0 RSUB 4F0000252 0007 1 USE CDATA253 LTORG

0007 1* =C’EOF’ 454F46000A 1* =X’05’ 05

255 END FIRST

Page 80: Unit – II Assembler

80MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks Pass 1 A separate location counter for each program block

Save and restore LOCCTR when switch between blocks At the beginning of a block, LOCCTR is set to 0.

Assign each label an address relative to the start of the block Store the block name or number in the SYMTAB along with the

assigned relative address of the label Indicate the block length as the latest value of LOCCTR for

each block at the end of Passssss1 Assign to each block a starting address in the object program

by concatenating the program blocks in a particular order Pass 2

The address of each symbol can be computed by adding the assigned block starting address and the relative address of the symbol to that block

Page 81: Unit – II Assembler

81MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program BlocksLine Loc/Block Source statement Object Code

5 0000 0 COPY START 010 0000 0 FIRST STL RETADR 17202D15 0003 0 CLOOP JSUB RDREC 4B202120 0006 0 LDA LENGTH 03202625 0009 0 COMP #0 29000030 000C 0 JEQ ENDFIL 33200635 000F 0 JSUB WRREC 4B203B40 0012 0 J CLOOP 3F2FEE45 0015 0 ENDFIL LDA =C’EOF’ 03205550 0018 0 STA BUFFER 0F205655 001B 0 LDA #3 01000360 001E 0 STA LENGTH 0F204865 0021 0 JSUB WRREC 4B202970 0024 0 J @RETADR 3E203F92 0000 1 USE CDATA95 0000 1 RETADR RESW 1100 0003 1 LENGTH RESW 1103 0000 2 USE CBLKS105 0000 2 BUFFER RESB 4096106 1000 2 BUFEND EQU *107 1000 MAXLEN EQU BUFEND - BUFFER

(Figure 2.12)

3 blocks:Default (0)CDATA (1)CBLKS (2)

Page 82: Unit – II Assembler

82MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks115 . READ RECORD INTO BUFFER

120 . 123 0027 0 USE125 0027 0 RDREC CLEAR X B41030 0029 0 CLEAR A B400132 002B 0 CLEAR S B440133 002D 0 +LDT #MAXLEN 75101000135 0031 0 RLOOP TD INPUT E32038140 0034 0 JEQ RLOOP 332FFA145 0037 0 RD INPUT DB2032150 003A 0 COMPR A,S A004155 003C 0 JEQ EXIT 332008160 003F 0 STCH BUFFER,X 57A02F165 0042 0 TIXR T B850170 0044 0 JLT RLOOP 3B2FEA175 0047 0 EXIT STX LENGTH 13201F180 004A 0 RSUB 4F0000183 0006 1 USE CDATA185 0006 1 INPUT BYTE X’F1’ F1

Page 83: Unit – II Assembler

83MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks

195 .200 . WRITE RECORD FROM BUFFER 205 .208 004D 0 USE210 004D 0WRREC CLEAR X B410212 004F 0 LDT LENGTH 772017215 0052 0WLOOP TD =X’05’ E3201B220 0055 0 JEQ WLOOP 332FFA 225 0058 0 LDCH BUFFER,X 53A016230 005B 0 WD =X’05’ DF2012235 005E 0 TIXR T B850240 0060 0 JLT WLOOP 3B2FEF245 0063 0 RSUB 4F0000252 0007 1 USE CDATA253 LTORG

0007 1* =C’EOF’ 454F46000A 1* =X’05’ 05

255 END FIRST

Page 84: Unit – II Assembler

84MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks At the end of pass1, assembler constructs a tab

Each source line is given a relative address and a block number

Absolute symbol has no block number (line 107)20 0006 0 LDA LENGTH 032060 LENGTH = (Block 1) + 0003 = 0066 + 0003 = 0069 LOCCTR = (Block 0) + 0009 = 0009 Displacement = 0069 – 0009 = 060

Block Name Block Number

Address Length

(default) 0 0000 066

CDATA 1 0066 000B

CBLKS 2 0071 1000

Page 85: Unit – II Assembler

85MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks Program readability

No extended format instructions (lines 15, 35, 65)

No need for base relative addressing (line 13, 14)

LTORG is used to make sure the literals are placed ahead of any large data areas (line 253)

Object code It is not necessary to physically rearrange

the generated code in the object program; loader can handle it

See Fig. 2.13, Fig. 2.14

Page 86: Unit – II Assembler

86MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks

CDATA

Page 87: Unit – II Assembler

87MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.4. Machine-Independent Assembler Features – Program Blocks

Page 88: Unit – II Assembler

88MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

Control sections Most often used for subroutines or other logical

subdivisions of a program Programmer can assemble, manipulate, and

load each of these control sections separately Instructions in one control section may need to

refer to instructions or data in another section Thus, there should be some means for linking

control sections together Fig. 2.15, 2.16: three control sections (COPY,

RDREC, WRREC)

Page 89: Unit – II Assembler

89MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

5 0000 COPY START 06 EXTDEF BUFFER,BUFEND,LENGTH7 EXTREF RDREC,WRREC10 0000 FIRST STL RETADR 17202715 0003 CLOOP +JSUB RDREC 4B10000020 0007 LDA LENGTH 03202325 000A COMP #0 29000030 000D JEQ ENDFIL 33200735 0010 +JSUB WRREC 4B10000040 0014 J CLOOP 3F2FEC45 0017 ENDFIL LDA =C’EOF’ 03201650 001A STA BUFFER 0F201655 001D LDA #3 01000360 0020 STA LENGTH 0F200A65 0023 +JSUB WRREC 4B10000070 0027 J @RETADR 3E200095 002A RETADR RESW 1100 002D LENGTH RESW 1103 LTORG

0030 * =C’EOF’ 454F46105 0033 BUFFER RESB 4096106 1033 BUFEND EQU *107 1000 MAXLEN EQU BUFEND - BUFFER

(Figure 2.16)

Page 90: Unit – II Assembler

90MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

109 0000 RDREC CSECT115 . READ RECORD INTO BUFFER120 . 122 EXTREF BUFFER,LENGTH,BUFEND125 0000 CLEAR X B410130 0002 CLEAR A B400132 0004 CLEAR S B440133 0006 LDT MAXLEN 77201F135 0009 RLOOP TD INPUT E3201B140 000C JEQ RLOOP 332FFA145 000F RD INPUT DB2015150 0012 COMPR A,S A004155 0014 JEQ EXIT 332009160 0017 +STCH BUFFER,X 57900000165 001B TIXR T B850170 001D JLT RLOOP 3B2FE9175 0020 EXIT +STX LENGTH 13100000180 0024 RSUB 4F0000185 0027 INPUT BYTE X’F1’ F1190 0028 MAXLEN WORD BUFEND – BUFFER 000000

Page 91: Unit – II Assembler

91MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

193 0000 WRREC CSECT195 .200 . WRITE RECORD FROM BUFFER 205 .207 EXTREF LENGTH,BUFFER210 0000 CLEAR X B410212 0002 +LDT LENGTH 77100000215 0006 WLOOP TD =X’05’ E32012220 0009 JEQ WLOOP 332FFA 225 000C +LDCH BUFFER,X 53900000230 0010 WD =X’05’ DF2008235 0013 TIXR T B850240 0015 JLT WLOOP 3B2FEF245 0018 RSUB 4F0000255 END FIRST

001B * =X’05’ 05

Page 92: Unit – II Assembler

92MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

External definitionEXTDEF name [, name] Declare symbols that are defined in this control section and

used by other sections External reference

EXTREF name [,name] Declare symbols that are used in this control section and are

defined elsewhere For EXTREF labels, assembler has no idea where the

corresponding control section will be loaded use 0 15 0003 CLOOP +JSUB RDREC 4B100000160 0017 +STCH BUFFER,X 57900000190 0028 MAXLEN WORD BUFEND-BUFFER

000000

External Definition and References

Page 93: Unit – II Assembler

93MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

Assembler must include information in object program that will cause loader to insert proper values where required

Define record Col. 1 D Col. 2-7Name of external symbol defined in this control

section Col. 8-13 Relative address within this control section (hex) Col.14-73 Repeat info in Col. 2-13 for other external symbols

Refer record Col. 1 R Col. 2-7Name of external symbol referred to in this section Col. 8-73 Name of other external reference symbols

Implementation

Page 94: Unit – II Assembler

94MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

Modification record Col. 1 M Col. 2-7Starting address of the field to be modified (hex) Col. 8-9Length of the field to be modified, in half-bytes (hex) Col.11-16 External symbol whose value is to be added to or

subtracted from the indicated field Note: control section name is automatically an external

symbol, i.e. it is available for use in Modification records. Example

Figure 2.17 M00000405+RDREC M00000705+COPY

Modification Record

Page 95: Unit – II Assembler

95MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

H COPY 000000 001033D BUFFER 00033 BUFEND 001033 LENGTH 00002DR RDREC WRRECT 000000 1D 172027 4B100000 032023 290000 332007

4B100000 ...T 00001D 0D 010003 0F200A 4B10000 3E2000T 000030 03 454F46M 000004 05+RDRECM 000011 05+WRRECM 000024 05+WRRECE 000000

Figure 2.17 (1/2)

Page 96: Unit – II Assembler

96MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

H RDREC 000000 00002BR BUFFER LENGTH BUFENDT 000000 1D B410 B400 B440 77201F E3201B 332FFA DB2015 ...T 00001D 0E 3B2FE9 13100000 4F0000 F1 000000M 000018 05+BUFFERM 000021 05+LENGTHM 000028 06+BUFENDM 000028 06-BUFFERE

H WRREC 000000 00001CR LENGTH BUFFERT 000000 1C B410 77100000 E32012 332FFA 53900000 DF2008 ...M 000003 05+LENGTHM 00000D 05+BUFFERE

Figure 2.17 (2/2)

190 MAXLEN WORD BUFEND - BUFFER

Page 97: Unit – II Assembler

97MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.3.5. Machine-Independent Assembler Features – Control section and Program Linking

Earlier definitions Required all of the relative terms be paired in an

expression (an absolute expression), or that all except one be paired (a relative expression)

New restriction Both terms in each pair must be relative within the same

control section Ex: BUFEND-BUFFER Ex: RDREC-COPY

In general, assembler cannot determine whether or not the expression is legal at assembly time. This work will be handled by a linking loader.

External Reference in Expressions

Page 98: Unit – II Assembler

98MC9224- System SoftwareDepartment of Computer

Applications

sps

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass Assembler

Main problem Forward references

Data items Labels on instructions

Solution Data items: require all such areas be

defined before they are referenced Labels on instructions: no good

solution

Page 99: Unit – II Assembler

99MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass Assembler

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

Page 100: Unit – II Assembler

100MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass Assembler

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

Load-and-Go Assembler

Page 101: Unit – II Assembler

101MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerLoad-and-Go Assembler

Assembler operations:1. Omit address translation for any undefined

symbol2. Insert the symbol into SYMTAB, mark it

undefined3. 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 reference list

Page 102: Unit – II Assembler

102MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerLoad-and-Go Assembler

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

Page 103: Unit – II Assembler

103MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig

2.18Line Loc Source statement Object code0 1000 COPY START 10001 1000 EOF BYTE C’EOF’ 454F462 1003 THREE WORD 3 0000033 1006 ZERO WORD 0 0000004 1009 RETADR RESW 15 100C LENGTH RESW 16 100F BUFFER RESB 409610 200F FIRST STL RETADR 14100915 2012 CLOOP JSUB RDREC 48203D20 2015 LDA LENGTH 00100C25 2018 COMP ZERO 28100630 201B JEQ ENDFIL 30202435 201E JSUB WRREC 48206240 2021 J CLOOP 3C201245 2024 ENDFIL LDA EOF 00100050 2027 STA BUFFER 0C100F55 202A LDA THREE 00100360 202D STA LENGTH 0C100C65 2030 JSUB WRREC 48206270 2033 LDL RETADR 08100975 2036 RSUB 4C0000

Page 104: Unit – II Assembler

104MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig 2.19(a) – Before

Line 40MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx . .2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx142010 100948-- --00100C 28100630 ----48--2020 --3C2012 . . Address unknown yet

Page 105: Unit – II Assembler

105MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig 2.19(a) - SYMTAB

LENGTH 100C

RDREC *

THREE 1003

ZERO 1006

WRREC *

EOF 1000

ENDFIL *

RETADR 1009

BUFFER 100F

CLOOP 2012

FIRST 200F

... ...

Symbol Value

2013

201F

201C

Page 106: Unit – II Assembler

106MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig 2.19(a) – After

Line 45MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx . .2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx142010 100948-- --00100C 28100630 202448--2020 --3C2012 001000 . .

Page 107: Unit – II Assembler

107MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig 2.19(a) - SYMTAB

LENGTH 100C

RDREC *

THREE 1003

ZERO 1006

WRREC *

EOF 1000

ENDFIL 2024

RETADR 1009

BUFFER 100F

CLOOP 2012

FIRST 200F

... ...

Symbol Value

2013

201F

Page 108: Unit – II Assembler

108MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig

2.18110 . 115 . SUB TO READ RECORD INTO BUFFER120 . 121 2039 INPUT BYTE X’F1’F1122 203A MAXLEN WORD 4096 001000124 .125 203D RDREC LDX ZERO 041006130 2040 LDA ZERO 001009135 2043 RLOOP TD INPUTE02039140 2046 JEQ RLOOP302043145 2049 RD INPUT D82039150 204C COMP ZERO 281006155 204F JEQ EXIT 30205B160 2052 STCH BUFFER,X 54900F165 2055 TIX MAXLEN 2C203A170 2058 JLT RLOOP382043175 205B EXIT STX LENGTH 10100C180 205E RSUB 4C0000

Page 109: Unit – II Assembler

109MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig 2.19 (b) – After

Line 160MemoryAddress Contents

1000 454F4600 00030000 00xxxxxx xxxxxxxx

1010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

.2000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx14

2010 10094820 3D00100C 28100630 202448--

2020 --3C2012 0010000C 100F0010 030C100C

2030 48----08 10094C00 00F10010 00041006

2040 001006E0 20393020 43DB2039 28100630

2050 ----5490 0F .

Page 110: Unit – II Assembler

110MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass Assembler

LENGTH 100CRDREC 203D

THREE 1003

ZERO 1006

WRREC *

EOF 1000

ENDFIL 2024

RETADR 1009

BUFFER 100F

CLOOP 2012

FIRST 200F

MAXLEN 203A

INPUT 2039

EXIT *

RLOOP 2043

... ...

201F

2031

2050

Program for one pass assembler – Fig 2.19 (b) – After Line 160

Page 111: Unit – II Assembler

111MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerProgram for one pass assembler – Fig

2.18195 .200 . SUB TO WRITE RECORD FROM BUFFER 205 .206 2061 OUTPUTBYTE X’05’ 05207 . 210 2062 WRREC LDX ZERO041006215 2065 WLOOP TD OUTPUT E02061220 2068 JEQ WLOOP 302065 225 206B LDCH BUFFER,X 50900F230 206E WD OUTPUT DC2061235 2071 TIX LENGTH 2C100C240 2074 JLT WLOOP 382065 245 2077 RSUB 4C0000 255 END FIRST

Page 112: Unit – II Assembler

112MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerType 2 Assembler

Will produce object code Assembler operations:

Forward references are entered into list as before Instruction referencing are written into object file as

a Text record, even with incorrect addresses When definition of a symbol is encountered,

assembler must generate another Text record with the correct operand address

Loader is used to insert correct addresses into instructions with forward references that could not be handled by the assembler

Object program records must be kept in original order when they are presented to the loader

Page 113: Unit – II Assembler

113MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerType 2 Assembler – Fig 2.20 (Partial)

H COPY 001000 00107AT 001000 09 454F46 000003 000000T 00200F 15 141009 480000 00100C 281006 300000 480000

3C2012T 00201C 02 2024T 002024 19 001000 0C100F 001003 0C100C 480000 081009

4C0000T 002013 02 203D…

E 00200F

Page 114: Unit – II Assembler

114MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4. 1 Assembler Design Options – One Pass AssemblerType 2 Assembler – Fig. 2.20

Page 115: Unit – II Assembler

115MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

Restriction on EQU and ORG No forward reference, since symbols’ value

can’t be defined during the first pass Example:ALPHA EQU BETABETAEQU DELTADELTA RESW 1 Assemblers with 2 passes cannot resolve

Page 116: Unit – II Assembler

116MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

Resolve forward references with as many passes as needed Portions that involve forward references in

symbol definition are saved during Pass 1 Additional passes through stored definitions Finally a normal Pass 2

Example implementation: Use link lists to keep track of whose value

depend on an undefined symbol

Page 117: Unit – II Assembler

117MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

HALFSZ &1 MAXLEN/2

MAXLEN *

1 symbol undefined

HALFSZ

Fig. 2.21 (a) – After Pass 1

Page 118: Unit – II Assembler

118MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HALFSZ &1 MAXLEN/2

MAXLEN &2 BUFEND-BUFFER

BUFFER *MAXLEN

MAXLEN

HALFSZ

Fig. 2.21 (c) – MAXLEN defined

Page 119: Unit – II Assembler

119MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HALFSZ &1 MAXLEN/2

MAXLEN &2 BUFEND-BUFFER

BUFFER *MAXLEN

MAXLEN

HALFSZ

Fig. 2.21 (c) – MAXLEN defined

Page 120: Unit – II Assembler

120MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

Fig. 2.21 (d) – PERVBT defined

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HLFSZ &1 MAXLEN/2

PREVBT &1 BUFFER-1

MAXLEN &2 BUFEND-BUFFER

BUFFER *

MAXLEN

PREVBT

HALFSZ

MAXLEN

Page 121: Unit – II Assembler

121MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

Fig. 2.21 (e) – After Line 4

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND *

HLFSZ &1 MAXLEN/2

PREVBT 1033

MAXLEN &1 BUFEND-BUFFER

BUFFER 1034

MAXLEN

Assume loc=1034

HALFSZ

Page 122: Unit – II Assembler

122MC9224- System SoftwareDepartment of Computer

Applications

ponns

ADHIPARASAKTHI ENGINEERING COLLEGEOmSakt

hi

Melmaruvathur-603

3192.4.2 Assembler Design Options – Multi Pass Assembler

Fig. 2.21 (f) – After Line 5

1 HALFSZ EQU MAXLEN/2

2 MAXLEN EQU BUFEND-BUFFER

3 PREVBT EQU BUFFER-1

...

4 BUFFER RESB 4096

5 BUFEND EQU *

BUFEND 2034

HLFSZ 800

PREVBT 1033

MAXLEN 1000

BUFFER 1034