Computer Organization & Assembly Languages Assembler

92
Computer Organization & Computer Organization & Assembly Languages Assembly Languages Pu-Jen Cheng Assembler Adapted from the slides prepared by Beck for the book, System Software: An Intro. to Systems Programming , 3rd Ed.

Transcript of Computer Organization & Assembly Languages Assembler

Page 1: Computer Organization & Assembly Languages Assembler

Computer Organization & Computer Organization & Assembly Languages Assembly Languages

Pu-Jen Cheng

Assembler

Adapted from the slides prepared by Beck for the book, System Software: An Intro. to Systems Programming , 3rd Ed.

Page 2: Computer Organization & Assembly Languages Assembler

Overview

SIC Machine ArchitectureSIC/XE Machine ArchitectureDesign and Implementation of Assembler

Source ObjectSource

ProgramAssembler

j

Code

Loader

Executable Code

Linker

SIC, SIC/XE Program

Page 3: Computer Organization & Assembly Languages Assembler

Overview

SIC Machine ArchitectureSIC/XE Machine ArchitectureDesign and Implementation of Assembler

Source ObjectSource

ProgramAssembler

j

Code

Loader

Executable Code

Linker

SIC, SIC/XE Program

Page 4: Computer Organization & Assembly Languages Assembler

The Simplified Instructional Computer (SIC)Memory

Memory consists of 8-bit bytesAny 3 consecutive bytes form a word (24 bits)Total of 32768 (215) bytes in the computer memory

SIC Machine Architecture

o o 3 768 ( ) by es e co pu e e o y

RegistersFive 24-bits registers

Page 5: Computer Organization & Assembly Languages Assembler

SIC Machine Architecture

Data FormatsIntegers are stored as 24-bit binary number2’s complement representation for negative valuesCharacters are stored using 8-bit ASCII codesNo floating-point hardware on the standard version of SIC

Instruction FormatsStandard version of SIC

8 1 15opcode x address

Mode Indication Target address calculation

Direct x=0 TA=address

Indexed x=1 TA=address+(X)

(X): the contents of register X

Page 6: Computer Organization & Assembly Languages Assembler

SIC Machine Architecture

Instruction SetLoad and store registers

LDA, LDX, STA, STX, etc.Integer arithmetic operations

ADD, SUB, MUL, DIVAll arithmetic operations involve register A and a word in memory, with the result being left in Athe result being left in A

COMPConditional jump instructions

JLT, JEQ, JGTSubroutine linkage

JSUB, RSUBI/O (transferring 1 byte at a time to/from the rightmost 8 bits of register A)

Test Device instruction (TD)Read Data (RD)Write Data (WD)

Page 7: Computer Organization & Assembly Languages Assembler

SIC Programming Example

LDA FIVESTA ALPHALDCH CHARZSTCH C1...

ALPHA RESW 1 one-word variableFIVE WORD 5 one-word constantCHARZ BYTE C’Z’ one-byte constantC1 RESB 1 one-byte variable

Page 8: Computer Organization & Assembly Languages Assembler

SIC Programming Example

LDX ZERO initialize index register to 0MOVECH LDCH STR1,X load char from STR1 to reg A

STCH STR2,XTIX ELEVEN add 1 to index, compare to 11JLT MOVECH loop if “less than”...

STR1 BYTE C’TEST STRING’STR2 RESB 11ZERO WORD 0ELEVEN WORD 11

Page 9: Computer Organization & Assembly Languages Assembler

SIC Programming ExampleLDA ZERO initialize index value to 0STA INDEX

ADDLP LDX INDEX load index value to reg XLDA ALPHA,X load word from ALPHA into reg AADD BETA,XSTA GAMMA,X store the result in a word in GAMMALDA INDEXADD THREE add 3 to index valueSTA INDEXCOMP K300 compare new index value to 300JLT ADDLP loop if less than 300......

INDEX RESW 1ALPHA RESW 100 array variables—100 words eachBETA RESW 100GAMMA RESW 100ZERO WORD 0 one-word constantsTHREE WORD 3K300 WORD 300

Page 10: Computer Organization & Assembly Languages Assembler

SIC Programming Example

INLOOP TD INDEV test input deviceJEQ INLOOP loop until device is ready (<)RD INDEV read one byte into register ASTCH DATA..

OUTLP TD OUTDEV test output deviceJEQ OUTLP loop until device is ready (<)LDCH DATAWD OUTDEV write one byte to output device..

INDEV BYTE X’F1’ input device numberOUTDEV BYTE X’05’ output device numberDATA RESB 1

Page 11: Computer Organization & Assembly Languages Assembler

Overview

SIC Machine ArchitectureSIC/XE Machine ArchitectureDesign and Implementation of Assembler

Source ObjectSource

ProgramAssembler

j

Code

Loader

Executable Code

Linker

SIC, SIC/XE Program

Page 12: Computer Organization & Assembly Languages Assembler

SIC/XE Machine ArchitectureAn XE version (upward compatible)Memory

Maximum 1 megabyte (220 bytes)Registers

Additional registers are provided by SIC/XEAdditional registers are provided by SIC/XESupport 48-bit floating-point data type

Page 13: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Instruction Formats8op

8 4 4op r1 r2

Format 1 (1 byte)

Format 2 (2 bytes)

Formats 1 and 2 are instructions that do not reference memory at all

6 1 1 1 1 1 1 12op n i x b p e dispFormat 3 (3 bytes)

6 1 1 1 1 1 1 20op n i x b p e addressFormat 4 (4 bytes)

Page 14: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Addressing modesBase relative (n=1, i=1, b=1, p=0)Program-counter relative (n=1, i=1, b=0, p=1)Direct (n=1, i=1, b=0, p=0)Immediate (n=0, i=1, x=0)( , , )Indirect (n=1, i=0, x=0)Indexing (both n & i = 0 or 1, x=1)Extended (e=1 for format 4, e=0 for format 3)

Page 15: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Base Relative Addressing Moden i x b p e

opcode 1 1 1 0 disp

n=1 i=1 b=1 p=0 TA=(B)+disp (0≤disp ≤4095)n 1, i 1, b 1, p 0, TA (B)+disp (0≤disp ≤4095)

Program-Counter Relative Addressing Mode

n i x b p e

opcode 1 1 0 1 disp

n=1, i=1, b=0, p=1, TA=(PC)+disp (-2048≤disp ≤2047)

Page 16: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Direct Addressing Mode

n i x b p e

opcode 1 1 0 0 disp

n=1 i=1 b=0 p=0 TA=disp (0≤disp ≤4095)n 1, i 1, b 0, p 0, TA disp (0≤disp ≤4095)

n i x b p e

opcode 1 1 1 0 0 disp

n=1, i=1, b=0, p=0, TA=(X)+disp (with index addressing mode)

Page 17: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Immediate Addressing Mode

n i x b p e

opcode 0 1 0 disp

n=0, i=1, x=0, operand=disp, , , p p

Indirect Addressing Mode

n i x b p e

opcode 1 0 0 disp

n=1, i=0, x=0, TA=(disp)

Page 18: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Simple Addressing Mode

n i x b p e

opcode 0 0 disp

i=0 n=0 TA=bpe+disp (SIC standard)i 0, n 0, TA bpe+disp (SIC standard)

n i x b p e

opcode 1 1 disp

i=1, n=1, TA=disp (SIC/XE standard)

Page 19: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Instruction Format

Page 20: Computer Organization & Assembly Languages Assembler

SIC/XE Machine Architecture

Instruction SetInstructions to load and store the new registers

LDB, STB, etc.Floating-point arithmetic operations

ADDF, SUBF, MULF, DIVFRegister move instructionRegister move instruction

RMORegister-to-register arithmetic operations

ADDR, SUBR, MULR, DIVRSupervisor call instruction

SVCInput and Output

There are I/O channels that can be used to perform input and output while the CPU is executing other instructions

Page 21: Computer Organization & Assembly Languages Assembler

SIC/XE Programming Example

LDA #5STA ALPHALDCH #90STCH C1

LDA FIVESTA ALPHALDCH CHARZSTCH C1

SIC version SIC/XE version

.

.

.ALPHA RESW 1C1 RESB 1

.

.

.ALPHA RESW 1FIVE WORD 5CHARZ BYTE C’Z’C1 RESB 1

Page 22: Computer Organization & Assembly Languages Assembler

SIC/XE Programming ExampleLDS INCRLDA ALPHAADDR S,A A = A + SSUB #1 A = A - 1STA BETALDA GAMMAADDR S,ASUB #1SUB #1STA DELTA......

ALPHA RESW 1 one-word variablesBETA RESW 1GAMMA RESW 1DELTA RESW 1INCR RESW 1

Page 23: Computer Organization & Assembly Languages Assembler

SIC/XE Programming Example

LDT #11 initialize register T to 11LDX #0 initialize index register to 0

MOVECH LDCH STR1,X load char from STR1 to reg ASTCH STR2,X store char into STR2TIXR T add 1 to index, compare to 11JLT MOVECH loop if “less than” 11...

STR1 BYTE C’TEST STRING’STR2 RESB 11

Page 24: Computer Organization & Assembly Languages Assembler

SIC/XE Programming ExampleLDS #3LDT #300LDX #0

ADDLP LDA ALPHA,X load from ALPHA to reg AADD BETA,XSTA GAMMA,X store in a word in GAMMAADDR S X add 3 to index valueADDR S,X add 3 to index valueCOMPR X,T compare to 300JLT ADDLP loop if less than 300......

ALPHA RESW 100 array variables—100 words eachBETA RESW 100GAMMA RESW 100

Page 25: Computer Organization & Assembly Languages Assembler

SIC/XE Programming Example

Page 26: Computer Organization & Assembly Languages Assembler

Overview

SIC Machine ArchitectureSIC/XE Machine ArchitectureDesign and Implementation of Assembler

Source ObjectSource

ProgramAssembler

j

Code

Loader

Executable Code

Linker

SIC, SIC/XE Program

Page 27: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 28: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 29: Computer Organization & Assembly Languages Assembler

Functions of a Basic Assembler

Mnemonic code (or instruction name) opcode.Symbolic operands (e.g., variable names) addresses.Choose the proper instruction format & addressing mode.Constants Numbers.Output to object files and listing files.

Page 30: Computer Organization & Assembly Languages Assembler

Assembler Directives

Pseudo-InstructionsNot translated into machine instructionsProviding information to the assembler

Basic assembler directivesSTARTSTARTENDBYTEWORDRESBRESW

Page 31: Computer Organization & Assembly Languages Assembler

Example Program with Object CodeLine Loc Source statement Object code

5 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 00102A45 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 4C000080 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 32: Computer Organization & Assembly Languages Assembler

110 . 115 . SUBROUTINE TO READ RECORD INTO BUFFER120 . 125 2039 RDREC LDX ZERO 041030130 203C LDA ZERO 001030135 203F RLOOP TD INPUT E0205D140 2042 JEQ RLOOP 30203D145 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 001000195 .200 . SUBROUTINE 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 33: Computer Organization & Assembly Languages Assembler

Symbolic Operands

Mnemonic code (or instruction name) opcode.STL 1033 opcode 14 10 33

U i bl i t d f dd

0001 0100 0 001 0000 0011 0011

Use variable names instead of memory addressesLabels (for jump instructions)SubroutinesConstants

COPY START 1000…LDA LEN……

LEN RESW 1forward references

Page 34: Computer Organization & Assembly Languages Assembler

Two Pass Assembler

Pass 1Assign addresses (LOC) to all statements in the programSave the values assigned to all labels for use in Pass 2Perform some processing of assembler directives

Pass 2Assemble instructionsGenerate data values defined by BYTE, WORDPerform processing of assembler directives not done in Pass 1Write the object program and the assembly listing

Page 35: Computer Organization & Assembly Languages Assembler

Two Pass Assembler

Pass 1 Pass 2 Intermediatefile

Object codes

Sourceprogram

file codes

OPTAB SYMTAB SYMTAB

Data Structures:Operation Code Table (OPTAB)Symbol Table (SYMTAB)Location Counter(LOCCTR)

Page 36: Computer Organization & Assembly Languages Assembler

Two Pass Assembler – Pass 1

Page 37: Computer Organization & Assembly Languages Assembler

Two Pass Assembler – Pass 2

Page 38: Computer Organization & Assembly Languages Assembler

OPTAB (operation code table)

ContentMnemonic, machine code (instruction format, length) etc.

CharacteristicStatic table

ImplementationArray or hash table, easy for search

Page 39: Computer Organization & Assembly Languages Assembler

SYMTAB (symbol table)

ContentLabel name, value, flag, (type, length) etc.

CharacteristicDynamic table (insert, delete, search)

COPY 1000FIRST 1000CLOOP 1003ENDFIL 1015EOF 1024THREE 102D

ImplementationHash table, non-random keys, hashing function

ZERO 1030RETADR 1033LENGTH 1036BUFFER 1039RDREC 2039

Page 40: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 41: Computer Organization & Assembly Languages Assembler

Object Program

HeaderCol. 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)

(END program_name)

Page 42: Computer Organization & Assembly Languages Assembler

Line Loc Source statement Object code

5 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 00102A45 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 4C000080 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 43: Computer Organization & Assembly Languages Assembler

Object Program Example

H COPY 001000 00107AT 001000 1E 141033 482039 001036 281030 301015 482061 ...T 00101E 15 0C1036 482061 081044 4C0000 454F46 000003 000000T 002039 1E 041030 001030 E0205D 30203F D8205D 281030 …T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064 …T 002073 07 382064 4C0000 05E 001000 starting address

Page 44: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 45: Computer Organization & Assembly Languages Assembler

An SIC/XE ExampleLine Loc Source statement Object code

5 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 4B10105D35 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 46: Computer Organization & Assembly Languages Assembler

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’ F1195 .200 . WRITE RECORD FROM BUFFER 205 .210 105D WRREC CLEAR X B410212 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 B850

...(omitted)

Page 47: Computer Organization & Assembly Languages Assembler

A Case of Object Code Generation

Line 10STL RETADR 17 20 2D

The mode bit p=1, meaning PC relative addressing mode.

OPCODE e Address6 bits 12 bits

n i x b p

0001 01 0 0000 0010 11011 1 0 0 1

17 2D20

Page 48: Computer Organization & Assembly Languages Assembler

Instruction Format and Addressing Mode

SIC/XEPC-relative or Base-relative addressing: op mIndirect addressing: op @mImmediate addressing: op #cExtended format: +op mExtended format: +op mIndex addressing: op m,xregister-to-register instructionslarger memory -> multi-programming (program allocation)

Page 49: Computer Organization & Assembly Languages Assembler

Translation

Register translationRegister name (A, X, L, B, S, T, F, PC, SW) and their values (0,1, 2, 3, 4, 5, 6, 8, 9)Preloaded in SYMTAB

Address translationAddress translationMost register-memory instructions use program counter relative or base relative addressingFormat 3: 12-bit address field

Base-relative: 0~4095PC-relative: -2048~2047

Format 4: 20-bit address field

Page 50: Computer Organization & Assembly Languages Assembler

PC-Relative Addressing Mode

PC-relative10 0000 FIRST STL RETADR 17202D

OPCODE e Addressn i x b p

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

Displacement= RETADR - PC = 30-3 = 2D40 0017 J CLOOP 3F2FEC

Displacement= CLOOP-PC= 6 - 1A= -14= FEC

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

Page 51: Computer Organization & Assembly Languages Assembler

Base-Relative Addressing Modes

Base-relativeBase register is under the control of the programmer12 LDB #LENGTH13 BASE LENGTH160 104E STCH BUFFER, X 57C003

Displacement= BUFFER - B = 0036 - 0033 = 3NOBASE is used to inform the assembler that the contents of the base registerno longer be relied upon for addressing

OPCODE e Addressn i x b p

0101 01 0 (003)161 1 1 1 0

Page 52: Computer Organization & Assembly Languages Assembler

Immediate Address Translation

Immediate addressing55 0020 LDA #3 010003

OPCODE e Addressn i x b p

0000 00 0 (003)0 1 0 0 0

133 103C +LDT #4096 75101000

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

Page 53: Computer Organization & Assembly Languages Assembler

Immediate Address TranslationImmediate addressing

12 0003 LDB #LENGTH 69202D

12 0003 LDB #LENGTH 690033

OPCODE e Addressn i x b p

0110 10 0 (02D)160 1 0 0 112 0003 LDB #LENGTH 690033

The immediate operand is the symbol LENGTHThe address of this symbol LENGTH is loaded into register BLENGTH=0033=PC+displacement=0006+02DIf immediate mode is specified, the target address becomes the operand

OPCODE e Addressn i x b p

0110 10 0 (033)160 1 0 0 0

Page 54: Computer Organization & Assembly Languages Assembler

Indirect Address Translation

Indirect addressingTarget addressing is computed as usual (PC-relative or BASE-relative)Only the n bit is set to 170 002A J @RETADR 3E2003

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

OPCODE e Addressn i x b p

0011 11 0 (003)161 0 0 0 1

Page 55: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 56: Computer Organization & Assembly Languages Assembler

Program Relocation

Page 57: Computer Organization & Assembly Languages Assembler

Examples of Program Relocation

Absolute program, starting address 10005 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

Page 58: Computer Organization & Assembly Languages Assembler

Problems of Program Relocation

Except for absolute address, the rest of the instructions need not be modified

not a memory address (immediate addressing)PC-relative, Base-relative

Th l t f th th t i difi tiThe only parts of the program that require modification at load time are those that specify direct addresses

Page 59: Computer Organization & Assembly Languages Assembler

Examples of Program Relocation

5 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

== 1000

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

Page 60: Computer Organization & Assembly Languages Assembler

How to Make Program Relocation Easier

Use program-counter (PC) relative addressesDid 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 BUFFERBUFFER.

Virtual memoryEvery program pretends that it has all of memory. Therefore, Text segment always starts at a fixed address; Stack segment always resides a some huge high address.

Page 61: Computer Organization & Assembly Languages Assembler

Relocatable Program

Modification recordCol 1 MCol 2-7 Starting location of the address field to be

modified, relative to the beginning of the program

Col 8 9 l th f th dd fi ld t b difi d i h lfCol 8-9 length of the address field to be modified, in half-bytes

Page 62: Computer Organization & Assembly Languages Assembler

Object Code with Modification Record

Page 63: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 64: Computer Organization & Assembly Languages Assembler

Program Blocks

Refer to segments of code that are rearranged within a single object program unitUSE [blockname]At the beginning, statements are assumed to be part

f th d (d f lt) bl kof the unnamed (default) blockIf no USE statements are included, the entire program belongs to this single blockEach program block may actually contain several separate segments of the source program

Page 65: Computer Organization & Assembly Languages Assembler
Page 66: Computer Organization & Assembly Languages Assembler

Program Blocks - Implementation

Pass 1Each program block has a separate location counterEach label is assigned an address that is relative to the start of the block that contains itAt the end of Pass 1, the latest value of the location ,counter for each block indicates the length of that blockThe assembler can then assign to each block a starting address in the object program

Pass 2The 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 67: Computer Organization & Assembly Languages Assembler

Program Blocks - Implementation

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

For absolute symbol, there is no block numberline 107

Example20 0006 0 LDA LENGTH 032060LENGTH = (Block 1) + 0003 = 0066 + 0003 = 0069LOCCTR = (Block 0) + 0009 = 0009

Page 68: Computer Organization & Assembly Languages Assembler

Object Code

Page 69: Computer Organization & Assembly Languages Assembler

Loading Program Blocks

Page 70: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 71: Computer Organization & Assembly Languages Assembler

Control Section

Are most often used for subroutines or other logical subdivisions of a programThe programmer can assemble, load, and manipulate each of these control sections separatelyI t ti i t l ti d t f tInstruction in one control section may need to refer to instructions or data located in another sectionBecause of this, there should be some means for linking control sections together

Page 72: Computer Organization & Assembly Languages Assembler
Page 73: Computer Organization & Assembly Languages Assembler
Page 74: Computer Organization & Assembly Languages Assembler

External Definition and References

External definitionEXTDEF name [, name]EXTDEF names symbols that are defined in this control section and may be used by other sections

External referenceEXTREF name [,name]EXTREF name [,name]EXTREF names symbols that are used in this control section and are defined elsewhere

Example15 0003 CLOOP +JSUB RDREC 4B100000160 0017 +STCH BUFFER,X 57900000190 0028 MAXLEN WORD BUFEND-BUFFER 000000

Page 75: Computer Organization & Assembly Languages Assembler

Implementation

The assembler must include information in the object program that will cause the loader to insert proper values where they are requiredDefine record

Col. 1 DCol. 2-7 Name of external symbol defined in this control sectionCol. 8-13 Relative address within this control section (hexadeccimal)Col.14-73 Repeat information in Col. 2-13 for other external symbols

Refer recordCol. 1 DCol. 2-7 Name of external symbol referred to in this control sectionCol. 8-73 Name of other external reference symbols

Page 76: Computer Organization & Assembly Languages Assembler

Modification RecordModification record

Col. 1 MCol. 2-7 Starting address of the field to be modified (hexiadecimal)Col. 8-9 Length of the field to be modified, in half-bytes (hexadeccimal)Col.11-16 External symbol whose value is to be added to or subtracted from the indicated fieldN l i i i ll l b l i i iNote: control section name is automatically an external symbol, i.e. it is available for use in Modification records.

Page 77: Computer Organization & Assembly Languages Assembler

Object Code

Page 78: Computer Organization & Assembly Languages Assembler

Functions of a Basic AssemblerObject FileAddress TranslationProgram Relocation

Design & Implementation of Assembler

Program BlockControl Section & Program LinkingOther Issues

One-pass AssemblerMulti-pass Assembler

Page 79: Computer Organization & Assembly Languages Assembler

One-Pass AssemblersMain problem

Forward referencesData items & labels on instructions

SolutionRequire all such areas be defined before they are referencedequ e suc e s be de ed be o e ey e e e e cedLabels on instructions: no good solution

Two types of one-pass assemblersLoad-and-go

Produces object code directly in memory for immediate execution

The otherProduces usual kind of object code for later execution

Page 80: Computer Organization & Assembly Languages Assembler

Load-and-go Assembler

CharacteristicsUseful for program development and testingAvoids the overhead of writing the object program out and reading it back Both one-pass and two-pass assemblers can be designed as p p gload-and-go. However one-pass also avoids the over head of an additional pass over the source programFor a load-and-go assembler, the actual address must be known at assembly time, we can use an absolute program

Page 81: Computer Organization & Assembly Languages Assembler

Load-and-go Assembler

Forward references handling1. Omit the address translation2. Insert the symbol into SYMTAB, and mark this symbol

undefined3. The address that refers to the undefined symbol is added to a3. 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 previous generated according to the forward reference list

Page 82: Computer Organization & Assembly Languages Assembler

Load-and-go Assembler

At the end of the programAny SYMTAB entries that are still marked with * indicate undefined symbolsSearch SYMTAB for the symbol named in the END statement and jump to this location to begin executionj p g

The actual starting address must be specified at assembly time

Page 83: Computer Organization & Assembly Languages Assembler
Page 84: Computer Organization & Assembly Languages Assembler

After Scanning Line 40

Page 85: Computer Organization & Assembly Languages Assembler

After Scanning Line 160

Page 86: Computer Organization & Assembly Languages Assembler

Object Code

Page 87: Computer Organization & Assembly Languages Assembler

Multi-Pass Assemblers

Restriction on EQUNo forward reference, since symbols’ value can’t be defined during the first pass

ExampleUse link list to keep track of hose al e depend on anUse link list to keep track of whose value depend on an undefined symbol

Page 88: Computer Organization & Assembly Languages Assembler

Example of Multi-pass Assembler

Page 89: Computer Organization & Assembly Languages Assembler

Example of Multi-pass Assembler

Page 90: Computer Organization & Assembly Languages Assembler

Example of Multi-pass Assembler

Page 91: Computer Organization & Assembly Languages Assembler

Example of Multi-pass Assembler

Page 92: Computer Organization & Assembly Languages Assembler

Example of Multi-pass Assembler