• 沒有找到結果。

Assembly Languages Assembly Languages

N/A
N/A
Protected

Academic year: 2022

Share "Assembly Languages Assembly Languages "

Copied!
92
0
0

加載中.... (立即查看全文)

全文

(1)

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.

(2)

Overview

„

SIC Machine Architecture

„

SIC/XE Machine Architecture

„

Design and Implementation of Assembler

Source Object

Source

Program Assembler j Code

Loader Executable

Code Linker SIC, SIC/XE Program

(3)

Overview

„

SIC Machine Architecture

„

SIC/XE Machine Architecture

„

Design and Implementation of Assembler

Source Object

Source

Program Assembler j Code

Loader Executable

Code Linker SIC, SIC/XE Program

(4)

„

The Simplified Instructional Computer (SIC)

„

Memory

¾ Memory consists of 8-bit bytes

¾ Any 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

„

Registers

¾ Five 24-bits registers

(5)

SIC Machine Architecture

„ Data Formats

¾ Integers are stored as 24-bit binary number

¾ 2’s complement representation for negative values

¾ Characters are stored using 8-bit ASCII codes

¾ No floating-point hardware on the standard version of SIC

„ Instruction Formats

¾ Standard version of SIC 8 1 15

opcode x address

Mode Indication Target address calculation

Direct x=0 TA=address

Indexed x=1 TA=address+(X)

(X): the contents of register X

(6)

SIC Machine Architecture

„ Instruction Set

¾ Load and store registers

„ LDA, LDX, STA, STX, etc.

¾ Integer arithmetic operations

„ ADD, SUB, MUL, DIV

„ All arithmetic operations involve register A and a word in memory, with the result being left in A

the result being left in A

¾ COMP

¾ Conditional jump instructions

„ JLT, JEQ, JGT

¾ Subroutine linkage

„ JSUB, RSUB

¾ I/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)

(7)

SIC Programming Example

LDA FIVE STA ALPHA LDCH CHARZ STCH C1

. . .

ALPHA RESW 1 one-word variable FIVE WORD 5 one-word constant CHARZ BYTE C’Z’ one-byte constant C1 RESB 1 one-byte variable

(8)

SIC Programming Example

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

STCH STR2,X

TIX ELEVEN add 1 to index, compare to 11 JLT MOVECH loop if “less than”

. . .

STR1 BYTE C’TEST STRING’

STR2 RESB 11 ZERO WORD 0 ELEVEN WORD 11

(9)

SIC Programming Example

LDA ZERO initialize index value to 0 STA INDEX

ADDLP LDX INDEX load index value to reg X

LDA ALPHA,X load word from ALPHA into reg A ADD BETA,X

STA GAMMA,X store the result in a word in GAMMA LDA INDEX

ADD THREE add 3 to index value STA INDEX

COMP K300 compare new index value to 300 JLT ADDLP loop if less than 300

...

...

INDEX RESW 1

ALPHA RESW 100 array variables—100 words each

BETA RESW 100

GAMMA RESW 100

ZERO WORD 0 one-word constants

THREE WORD 3

K300 WORD 300

(10)

SIC Programming Example

INLOOP TD INDEV test input device

JEQ INLOOP loop until device is ready (<) RD INDEV read one byte into register A STCH DATA

. .

OUTLP TD OUTDEV test output device

JEQ OUTLP loop until device is ready (<) LDCH DATA

WD OUTDEV write one byte to output device .

.

INDEV BYTE X’F1’ input device number OUTDEV BYTE X’05’ output device number DATA RESB 1

(11)

Overview

„

SIC Machine Architecture

„

SIC/XE Machine Architecture

„

Design and Implementation of Assembler

Source Object

Source

Program Assembler j Code

Loader Executable

Code Linker SIC, SIC/XE Program

(12)

SIC/XE Machine Architecture

„

An XE version (upward compatible)

„

Memory

¾ Maximum 1 megabyte (220 bytes)

„

Registers

¾ Additional registers are provided by SIC/XE

¾ Additional registers are provided by SIC/XE

„

Support 48-bit floating-point data type

(13)

SIC/XE Machine Architecture

„ Instruction Formats

8 op

8 4 4

op 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 12

op n i x b p e disp

Format 3 (3 bytes)

6 1 1 1 1 1 1 20

op n i x b p e address

Format 4 (4 bytes)

(14)

SIC/XE Machine Architecture

„

Addressing modes

¾ Base 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)

(15)

SIC/XE Machine Architecture

„ Base Relative Addressing Mode

n 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)

(16)

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)

(17)

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)

(18)

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)

(19)

SIC/XE Machine Architecture

„ Instruction Format

(20)

SIC/XE Machine Architecture

„

Instruction Set

¾ Instructions to load and store the new registers

„ LDB, STB, etc.

¾ Floating-point arithmetic operations

„ ADDF, SUBF, MULF, DIVF Register move instruction

¾ Register move instruction

„ RMO

¾ Register-to-register arithmetic operations

„ ADDR, SUBR, MULR, DIVR

¾ Supervisor call instruction

„ SVC

¾ Input and Output

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

(21)

SIC/XE Programming Example

LDA #5

STA ALPHA LDCH #90 STCH C1 LDA FIVE

STA ALPHA LDCH CHARZ STCH C1

SIC version SIC/XE version

. . .

ALPHA RESW 1

C1 RESB 1

. . .

ALPHA RESW 1 FIVE WORD 5

CHARZ BYTE C’Z’

C1 RESB 1

(22)

SIC/XE Programming Example

LDS INCR LDA ALPHA

ADDR S,A A = A + S SUB #1 A = A - 1 STA BETA

LDA GAMMA ADDR S,A SUB #1 SUB #1

STA DELTA ...

...

ALPHA RESW 1 one-word variables BETA RESW 1

GAMMA RESW 1 DELTA RESW 1 INCR RESW 1

(23)

SIC/XE Programming Example

LDT #11 initialize register T to 11 LDX #0 initialize index register to 0 MOVECH LDCH STR1,X load char from STR1 to reg A

STCH STR2,X store char into STR2

TIXR T add 1 to index, compare to 11 JLT MOVECH loop if “less than” 11

. . .

STR1 BYTE C’TEST STRING’

STR2 RESB 11

(24)

SIC/XE Programming Example

LDS #3 LDT #300 LDX #0

ADDLP LDA ALPHA,X load from ALPHA to reg A ADD BETA,X

STA GAMMA,X store in a word in GAMMA ADDR S X add 3 to index value

ADDR S,X add 3 to index value COMPR X,T compare to 300

JLT ADDLP loop if less than 300 ...

...

ALPHA RESW 100 array variables—100 words each BETA RESW 100

GAMMA RESW 100

(25)

SIC/XE Programming Example

(26)

Overview

„

SIC Machine Architecture

„

SIC/XE Machine Architecture

„

Design and Implementation of Assembler

Source Object

Source

Program Assembler j Code

Loader Executable

Code Linker SIC, SIC/XE Program

(27)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(28)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(29)

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.

(30)

Assembler Directives

„ Pseudo-Instructions

¾ Not translated into machine instructions

¾ Providing information to the assembler

„

Basic assembler directives

¾ START

¾ START

¾ END

¾ BYTE

¾ WORD

¾ RESB

¾ RESW

(31)

Example Program with Object Code

Line Loc Source statement Object code

5 1000 COPY START 1000

10 1000 FIRST STL RETADR 141033 15 1003 CLOOP JSUB RDREC 482039

20 1006 LDA LENGTH 001036

25 1009 COMP ZERO 281030

30 100C JEQ ENDFIL 301015

35 100F JSUB WRREC 482061

40 1012 J CLOOP 3C1003

45 1015 ENDFIL LDA EOF 00102A

45 1015 ENDFIL LDA EOF 00102A

50 1018 STA BUFFER 0C1039

55 101B LDA THREE 00102D

60 101E STA LENGTH 0C1036

65 1021 JSUB WRREC 482061

70 1024 LDL RETADR 081033

75 1027 RSUB 4C0000

80 102A EOF BYTE C’EOF’ 454F46

85 102D THREE WORD 3 000003

90 1030 ZERO WORD 0 000000

95 1033 RETADR RESW 1 100 1036 LENGTH RESW 1 105 1039 BUFFER RESB 4096

(32)

110 .

115 . SUBROUTINE TO READ RECORD INTO BUFFER

120 .

125 2039 RDREC LDX ZERO 041030

130 203C LDA ZERO 001030

135 203F RLOOP TD INPUT E0205D

140 2042 JEQ RLOOP 30203D

145 2045 RD INPUT D8205D

150 2048 COMP ZERO 281030

155 204B JEQ EXIT 302057

160 204E STCH BUFFER,X 549039

165 2051 TIX MAXLEN 2C205E

170 2054 JLT RLOOP 38203F

175 2057 EXIT STX LENGTH 101036

180 205A RSUB 4C0000

185 205D INPUT BYTE X’F1’ F1

190 205E MAXLEN WORD 4096 001000

195 .

200 . SUBROUTINE TO WRITE RECORD FROM BUFFER

205 .

210 2061 WRREC LDX ZERO 041030

215 2064 WLOOP TD OUTPUT E02079

220 2067 JEQ WLOOP 302064

225 206A LDCH BUFFER,X 509039

230 206D WD OUTPUT DC2079

235 2070 TIX LENGTH 2C1036

240 2073 JLT WLOOP 382064

245 2076 RSUB 4C0000

250 2079 OUTPUT BYTE X’05’ 05

255 END FIRST

(33)

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 addresses

¾ Labels (for jump instructions)

¾ Subroutines

¾ Constants COPY START 1000

LDA LEN

LEN RESW 1 forward references

(34)

Two Pass Assembler

„

Pass 1

¾ Assign addresses (LOC) 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

¾ 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

(35)

Two Pass Assembler

Pass 1 Intermediate Pass 2

file Object codes

Source program

file codes

OPTAB SYMTAB SYMTAB

Data Structures:

Operation Code Table (OPTAB) Symbol Table (SYMTAB)

Location Counter(LOCCTR)

(36)

Two Pass Assembler – Pass 1

(37)

Two Pass Assembler – Pass 2

(38)

OPTAB (operation code table)

„

Content

¾ Mnemonic, machine code (instruction format, length) etc.

„

Characteristic

¾ Static table

„

Implementation

¾ Array or hash table, easy for search

(39)

SYMTAB (symbol table)

„

Content

¾ Label name, value, flag, (type, length) etc.

„

Characteristic

¾ Dynamic table (insert, delete, search)

COPY 1000

FIRST 1000 CLOOP 1003 ENDFIL 1015

EOF 1024

THREE 102D

„

Implementation

¾ Hash table, non-random keys, hashing function

ZERO 1030

RETADR 1033 LENGTH 1036 BUFFER 1039 RDREC 2039

(40)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(41)

Object Program

„

Header

Col. 1 H

Col. 2~7 Program name

Col. 8~13 Starting address (hex)

Col. 14-19 Length of object program in bytes (hex)

„

Text

Col.1 T

Col.2~7 Starting address in this record (hex)

Col. 8~9 Length of object code in this record in bytes (hex) Col. 10~69 Object code (69-10+1)/6=10 instructions

„

End

Col.1 E

Col.2~7 Address of first executable instruction (hex) (END program_name)

(42)

Line Loc Source statement Object code

5 1000 COPY START 1000

10 1000 FIRST STL RETADR 141033 15 1003 CLOOP JSUB RDREC 482039

20 1006 LDA LENGTH 001036

25 1009 COMP ZERO 281030

30 100C JEQ ENDFIL 301015

35 100F JSUB WRREC 482061

40 1012 J CLOOP 3C1003

45 1015 ENDFIL LDA EOF 00102A

45 1015 ENDFIL LDA EOF 00102A

50 1018 STA BUFFER 0C1039

55 101B LDA THREE 00102D

60 101E STA LENGTH 0C1036

65 1021 JSUB WRREC 482061

70 1024 LDL RETADR 081033

75 1027 RSUB 4C0000

80 102A EOF BYTE C’EOF’ 454F46

85 102D THREE WORD 3 000003

90 1030 ZERO WORD 0 000000

95 1033 RETADR RESW 1 100 1036 LENGTH RESW 1 105 1039 BUFFER RESB 4096

(43)

Object Program Example

H COPY 001000 00107A

T 001000 1E 141033 482039 001036 281030 301015 482061 ...

T 00101E 15 0C1036 482061 081044 4C0000 454F46 000003 000000 T 002039 1E 041030 001030 E0205D 30203F D8205D 281030 …

T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064 T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064 … T 002073 07 382064 4C0000 05

E 001000 Åstarting address

(44)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(45)

An SIC/XE Example

Line Loc Source statement Object code

5 0000 COPY START 0

10 0000 FIRST STL RETADR 17202D

12 0003 LDB #LENGTH 69202D

13 BASE LENGTH

15 0006 CLOOP +JSUB RDREC 4B101036

20 000A LDA LENGTH 032026

25 000D COMP #0 290000

30 0010 JEQ ENDFIL 332007

35 0013 JSUB WRREC 4B10105D

35 0013 +JSUB WRREC 4B10105D

40 0017 J CLOOP 3F2FEC

45 001A ENDFIL LDA EOF 032010

50 001D STA BUFFER 0F2016

55 0020 LDA #3 010003

60 0023 STA LENGTH 0F200D

65 0026 +JSUB WRREC 4B10105D

70 002A J @RETADR 3E2003

80 002D EOF BYTE C’EOF’ 454F46 95 0030 RETADR RESW 1

100 0033 LENGTH RESW 1 105 0036 BUFFER RESB 4096

(46)

115 . READ RECORD INTO BUFFER

120 .

125 1036 RDREC CLEAR X B410

130 1038 CLEAR A B400

132 103A CLEAR S B440

133 103C +LDT #4096 75101000

135 1040 RLOOP TD INPUT E32019

140 1043 JEQ RLOOP 332FFA

145 1046 RD INPUT DB2013

150 1049 COMPR A,S A004

155 104B JEQ EXIT 332008

160 104E STCH BUFFER,X 57C003

165 1051 TIXR T B850

170 1053 JLT RLOOP 3B2FEA

175 1056 EXIT STX LENGTH 134000

180 1059 RSUB 4F0000

185 105C INPUT BYTE X’F1’ F1

195 .

200 . WRITE RECORD FROM BUFFER

205 .

210 105D WRREC CLEAR X B410

212 105F LDT LENGTH 774000

215 1062 WLOOP TD OUTPUT E32011

220 1065 JEQ WLOOP 332FFA

225 1068 LDCH BUFFER,X 53C003

230 106B WD OUTPUT DF2008

235 106E TIXR T B850

...(omitted)

(47)

A Case of Object Code Generation

„

Line 10

STL RETADR Æ 17 20 2D

„

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

OPCODE e Address

6 bits 12 bits

n i x b p

0001 01 1 1 0 0 1 0 0000 0010 1101

17 20 2D

(48)

Instruction Format and Addressing Mode

„

SIC/XE

¾ PC-relative or Base-relative addressing: op m

¾ Indirect addressing: op @m

¾ Immediate addressing: op #c

¾ Extended format: +op m

¾ Extended format: +op m

¾ Index addressing: op m,x

¾ register-to-register instructions

¾ larger memory -> multi-programming (program allocation)

(49)

Translation

„

Register translation

¾ Register 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 translation

„

Address translation

¾ Most register-memory instructions use program counter relative or base relative addressing

¾ Format 3: 12-bit address field

„ Base-relative: 0~4095

„ PC-relative: -2048~2047

¾ Format 4: 20-bit address field

(50)

PC-Relative Addressing Mode

„

PC-relative

¾ 10 0000 FIRST STL RETADR 17202D

OPCODE n i x b p e Address

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

„ Displacement= RETADR - PC = 30-3 = 2D

¾ 40 0017 J CLOOP 3F2FEC

„ Displacement= CLOOP-PC= 6 - 1A= -14= FEC 0001 01 1 1 0 0 1 0 (02D)16

OPCODE n i x b p e Address

0011 11 1 1 0 0 1 0 (FEC)16

(51)

Base-Relative Addressing Modes

„

Base-relative

¾ Base register is under the control of the programmer

¾ 12 LDB #LENGTH

¾ 13 BASE LENGTH

¾ 160 104E STCH BUFFER, X 57C003

„ Displacement= BUFFER - B = 0036 - 0033 = 3

¾ NOBASE is used to inform the assembler that the contents of the base register no longer be relied upon for addressing

OPCODE n i x b p e Address

0101 01 1 1 1 1 0 0 (003)16

(52)

Immediate Address Translation

„

Immediate addressing

¾ 55 0020 LDA #3 010003

OPCODE n i x b p e Address

0000 00 0 1 0 0 0 0 (003)

¾ 133 103C +LDT #4096 75101000

0000 00 0 1 0 0 0 0 (003)16

OPCODE n i x b p e Address

0111 01 0 1 0 0 0 1 (01000)16

(53)

Immediate Address Translation

„

Immediate addressing

¾ 12 0003 LDB #LENGTH 69202D

¾ 12 0003 LDB #LENGTH 690033

OPCODE n i x b p e Address

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

¾ 12 0003 LDB #LENGTH 690033

„ The immediate operand is the symbol LENGTH

„ The address of this symbol LENGTH is loaded into register B

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

„ If immediate mode is specified, the target address becomes the operand

OPCODE n i x b p e Address

0110 10 0 1 0 0 0 0 (033)16

(54)

Indirect Address Translation

„

Indirect addressing

¾ 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)+disp=002D+0003

OPCODE n i x b p e Address

0011 11 1 0 0 0 1 0 (003)16

(55)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(56)

Program Relocation

(57)

Examples of Program Relocation

„ Absolute program, starting address 1000

5 2000 1000 COPY START 1000

10 2000 1000 FIRST STL RETADR 141033 142033

15 2003 1003 CLOOP JSUB RDREC 482039 483039

20 2006 1006 LDA LENGTH 001036 002036

25 2009 1009 COMP ZERO 281030 282030

30 200C 100C JEQ ENDFIL 301015 302015

35 200F 100F JSUB WREC 482061 483061

40 2012 1012 J CLOOP 3C1003 3C2003

45 2015 1015 ENDFIL LDA EOF 00102A 00202A

50 2018 1018 STA BUFFER 0C1039 0C2039

55 201B 101B LDA THREE 00102D 00202D

60 201E 101E STA LENGTH 0C1036 0C2036

65 2021 1021 JSUB WREC 482061 483061

70 2024 1024 LDL RETADR 081033 082033

75 2027 1027 RSUB 4C0000 4C0000

80 202A 102A EOF BYTE C'EOF' 454E46 454E46

85 202D 102D THREE WORD 3 000003 000003

90 2030 1030 ZERO WORD 0 000000 000000

95 2033 1033 RETADR RESW 1

100 2036 1036 LENGTH RESW 1

105 2039 1039 BUFFER RESB 4096

(58)

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 ti

„

The only parts of the program that require modification

at load time are those that specify direct addresses

(59)

Examples of Program Relocation

5 1000 0000 COPY START 0

10 1000 0000 FIRST STL RETADR 17202D 17202D

12 1003 0003 LDB #LENGTH 69202D 69202D

13 BASE LENGTH

15 1006 0006 CLOOP +JSUB RDREC 4B101036 4B102036

20 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 4B10205D

40 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 4B10205D

70 102A 002A J @RETADR 3E2003 3E2003

80 102D 002D EOF BYTE C'EOF' 454F46 454F46

95 1030 0030 RETADR RESW 1

100 1036 0036 BUFFER RESB 4096

(60)

How to Make Program Relocation Easier

„

Use program-counter (PC) 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

BUFFER.

„

Virtual memory

¾ Every 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.

(61)

Relocatable Program

„

Modification record

¾ Col 1 M

¾ Col 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 lf

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

(62)

Object Code with Modification Record

(63)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(64)

Program Blocks

„

Refer to segments of code that are rearranged within a single object program unit

„

USE [blockname]

„

At the beginning, statements are assumed to be part f th d (d f lt) bl k

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

(65)
(66)

Program Blocks - Implementation

„

Pass 1

¾ Each program block has a separate location counter

¾ Each label is assigned an address that is relative to the start of the block that contains it

¾ At the end of Pass 1, the latest value of the location ,

counter for each block indicates the length of that block

¾ The assembler can then assign to each block a starting address in the object program

„

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

(67)

Program Blocks - Implementation

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

„ For absolute symbol, there is no block number

¾ line 107

„ Example

¾ 20 0006 0 LDA LENGTH 032060

¾ LENGTH = (Block 1) + 0003 = 0066 + 0003 = 0069

¾ LOCCTR = (Block 0) + 0009 = 0009

(68)

Object Code

(69)

Loading Program Blocks

(70)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(71)

Control Section

„

Are most often used for subroutines or other logical subdivisions of a program

„

The programmer can assemble, load, and manipulate each of these control sections separately

I t ti i t l ti d t f t

„

Instruction in one control section may need to refer to instructions or data located in another section

„

Because of this, there should be some means for linking

control sections together

(72)
(73)
(74)

External Definition and References

„ External definition

¾ EXTDEF name [, name]

¾ EXTDEF names symbols that are defined in this control section and may be used by other sections

„ External reference

¾ EXTREF name [,name]

¾ EXTREF name [,name]

¾ EXTREF names symbols that are used in this control section and are defined elsewhere

„ Example

¾ 15 0003 CLOOP +JSUB RDREC 4B100000

160 0017 +STCH BUFFER,X 57900000

190 0028 MAXLEN WORD BUFEND-BUFFER 000000

(75)

Implementation

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

„ Define record

¾ Col. 1 D

¾ Col. 2-7 Name of external symbol defined in this control section

¾ Col. 8-13 Relative address within this control section (hexadeccimal)

¾ Col.14-73 Repeat information in Col. 2-13 for other external symbols

„ Refer record

¾ Col. 1 D

¾ Col. 2-7 Name of external symbol referred to in this control section

¾ Col. 8-73 Name of other external reference symbols

(76)

Modification Record

„ Modification record

¾ Col. 1 M

¾ Col. 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 field

N l i i i ll l b l i i i

¾ Note: control section name is automatically an external symbol, i.e. it is available for use in Modification records.

(77)

Object Code

(78)

„

Functions of a Basic Assembler

„

Object File

„

Address Translation

„

Program Relocation

Design & Implementation of Assembler

„

Program Block

„

Control Section & Program Linking

„

Other Issues

¾ One-pass Assembler

¾ Multi-pass Assembler

(79)

One-Pass Assemblers

„

Main problem

¾ Forward references

„ Data items & labels on instructions

„

Solution

¾ Require all such areas be defined before they are referencedequ e suc e s be de ed be o e ey e e e e ced

¾ Labels on instructions: no good solution

„

Two types of one-pass assemblers

¾ Load-and-go

„ Produces object code directly in memory for immediate execution

¾ The other

„ Produces usual kind of object code for later execution

(80)

Load-and-go 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 p p g load-and-go.

¾ However one-pass also avoids the over head 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

(81)

Load-and-go Assembler

„

Forward references handling

1. Omit the address translation

2. Insert the symbol into SYMTAB, and mark this symbol undefined

3. The address that refers to the undefined symbol is added to a 3. The address that refers to the undefined symbol is added to a

list of forward references associated with the symbol table entry

4. When the definition for a symbol is encountered, the proper address for the symbol is then inserted into any instructions previous generated according to the forward reference list

(82)

Load-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 executionj p g

„

The actual starting address must be specified at

assembly time

(83)
(84)

After Scanning Line 40

(85)

After Scanning Line 160

(86)

Object Code

(87)

Multi-Pass Assemblers

„

Restriction on EQU

¾ No forward reference, since symbols’ value can’t be defined during the first pass

„

Example

Use link list to keep track of hose al e depend on an

¾ Use link list to keep track of whose value depend on an undefined symbol

(88)

Example of Multi-pass Assembler

(89)

Example of Multi-pass Assembler

(90)

Example of Multi-pass Assembler

(91)

Example of Multi-pass Assembler

(92)

Example of Multi-pass Assembler

參考文獻

Outline

相關文件

Programming languages can be used to create programs that control the behavior of a. machine and/or to express algorithms precisely.” -

• Uses a nested structure to accumulate path data as the simulation is running. • Uses a multiple branch structure to choose the

¾ Relocation, which modifies the object program so that it can be loaded at an address different from the location originally specified.. ¾ Linking, which combines two or

• The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with

 The class of languages decided by polynomial- time algorithms是the class of languages. accepted by

„ The calling program passes the address of the array, along with a count of the number of array

– Basic concept of computer systems and architecture – ARM architecture and assembly language.. – x86 architecture and

ReadInt - Reads 32-bit signed decimal integer from keyboard ReadKey – Reads character from keyboard input buffer.. ReadString - Reads string from standard input, terminated