0% found this document useful (0 votes)
40 views106 pages

Chapter 2

Uploaded by

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

Chapter 2

Uploaded by

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

Chapter 2 Assemblers

1
Outlines

2.1 Basic Assembler Functions


2.2 Machine-dependent Assembler Features
2.3 Machine-independent Assembler Features
2.4 Assembler Design Options
2.5 Implementation Examples

2
Role of Assembler

Source Object
Assembler Linker
Program Code

Executable
Code

Loader

3
Introduction to Assemblers

•Fundamental functions
•translating mnemonic operation codes to their machine language
equivalents
•assigning machine addresses to symbolic labels

•Machine dependency
•different machine instruction formats and codes

4
Line Source statement

Figure 2.1 Example of a SIC assembler language program. 5


2.1 Basic Assembler Functions

•Example Program (Fig. 2.1)


•Purpose
•reads records from input device (device code F1)
•copies them to output device (code 05)
•at the end of the file, writes EOF on the output device, then
RSUB to the operating system

6
Assembler Directives

•Assembler directives
•Pseudo-Instructions
• Not translated into machine instructions
• Providing information to the assembler
•Basic assembler directives
START Specify name and sharing address for the program.
END Indicate the end of the source program and (optionally) specify the first
executable instruction in the program.
BYTE Generate character or hexadecimal constant, occupying as many bytes as
needed to represent the constant.
WORD Generate one-word integer constant.
RESB Reserve the indicated number if bytes fir a data area.
RESW Reserve the indicated number of words for a data area.

7
Example Program (Fig. 2.1)

•Data transfer (RD, WD)


•a buffer is used to store record
•buffering is necessary for different I/O rates
•the end of each record is marked with a null character (0016, 00H)
•the end of the file is indicated by a zero-length record
•Subroutines (JSUB, RSUB)
•RDREC, WRREC
•save link register first before nested jump

8
2.1.1 A Simple SIC Assembler

•Assembler’s functions
1. Convert mnemonic operation codes to their machine language
equivalents – e.g., translate STL to 14 (line 10)
2. Convert symbolic operands to their equivalent machine
addresses – e.g., translate RETADR to 1033 (line 10)
3. Build the machine instructions in the proper format
4. Convert the data constants to internal machine
representations – e.g., translate EOF to 454F46 (line 80)
5. Write the object program and the assembly listing

9
Line Loc Source statement Object code

Figure 2.2 Program from Fig. 2.1 with object code


10
2.1.1 A Simple SIC Assembler (cont’d)

•Forward reference: a reference to a label (RETADR) that


is defined later in the program
line loc Source statement Object code
Label Operator Operand
10 1000 FIRST STL RETADR 141033
……
95 1033 RETADR RESW 1
•Most assemblers make two passes over the source
program
•First pass: scan the source program for label definitions and
assign address
•Second pass: perform the actual translation (Convert symbolic
operands to their equivalent machine addresses)

11
•Assemblers write the generated object code onto some
output device
•Object programs
•Load into memory for execution later.
•Contain three types of records
• Head
• Text
• End

12
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 (2 columns per byte of object code)
•End
Col. 1 E
Col. 2~7 Address of first executable instruction (hex)
(END program name)
13
Fig. 2.3

Figure 2.3 Object program corresponding to Fig. 2.2

14
Two Pass Assembler

•Pass 1 (define symbols):


1. Assign addresses to all statements in the program
2. Save the values assigned to all labels for use in Pass 2
3. Perform some processing of assembler directives
•Pass 2 (assemble instructions and generate object
program):
1. Assembly instructions
2. Generate data values defined by BYTE, WORD
3. Perform processing of assembler directives not done in Pass 1
4. Write the object program and the assembly listing

15
Two Pass Assembler

•Read from input line


•LABEL, OPCODE, OPERAND

Source
program

Intermediate Object
Pass 1 Pass 2
file codes

OPTAB SYMTAB SYMTAB

16
2.1.2 Assembler Algorithm and Data Structure
•Simple assembler uses two major internal data structures
•Operation Code Table (OPTAB)
•Symbol Table (SYMTAB)

•OPTAB
•To look up mnemonic operation codes and translate them to their
machine language equivalents

•SYMTAB
•To store values (address) assigned to labels

•Location Counter (LOCCTR)


•The variable that is used to help assignment of address
17
OPTAB (operation code table)

•Content
•mnemonic, machine code (instruction format, length) etc.
•Characteristic
•static table
•Implementation
•hash table or other data structure (array), easy for search

18
SYMTAB (symbol table)

•Content
•label name, value, flag (to indicate error condition), other
information about data area or instruction labeled (type, length),
etc. COPY 1000
•Characteristic FIRST
CLOOP
1000
1003
•Dynamic table ENDFIL
EOF
1015
1024
• efficiency to insert and retrieval, delete rarely THREE 102D
ZERO 1030
RETADR 1033
•Implementation LENGTH
BUFFER
1036
1039
•hash table RDREC 2039

•hashing function to perform well with non-random keys

19
Algorithm for Pass 1 of Assembler

•It‘s possible for both passes of the assembler to read the


original source program as input
•certain information can or should be communicated between
passes
• pass 1 usually writes an intermediate file contains each source
statement, address, error indicators, etc.
•The intermediate file is used to pass 2 input

20
Pass 1:

21
Figure 2.4(a) Algorithm for Pass 1 of assembler

22
Pass 2:

23
Figure 2.4(b) Algorithm for Pass 2 of Assembler

24
Homework #1

•Apply the algorithm described in Fig 2.4 to assemble the


following SIC source program
SUM START 4000
FIRST LDX ZERO
LDA ZERO
LOOP ADD TABLE,X
TIX COUNT
JLT LOOP
STA TOTAL
RSUB
TABLE RESW 2000
COUNT RESW 1
ZERO WORD 0
TOTAL RESW 1
END FIRST

25
Assembler Design

•Machine Dependent Assembler Features


•instruction formats and addressing modes
•program relocation
•Machine Independent Assembler Features
•literals
•symbol-defining statements
•expressions
•program blocks
•control sections and program linking

26
2.2 Machine-dependent Assembler Features

•Sec. 2-2
•Instruction formats and addressing modes
•Program relocation

27
2.2.1 Instruction Format and Addressing Modes
•SIC/XE
•PC-relative or Base-relative addressing: op m (format 4)
•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)
•Example program
•Figure 2.5

28
Line Source statement

Figure 2.5 Example of a SIC/XE program 29


Translation

•Register translation
•register mnemonic (A, X, L, B, S, T, F, PC, SW) and their values
(0,1, 2, 3, 4, 5, 6, 8, 9)
•preloaded in SYMTAB

30
Address translation
•Most of the register-to-memory instructions are assembled
using either program-counter relative or base relative
addressing
•Assembler calculates a displacement to be assembled as part of the
object instructions
•The correct target address is displacement added to the contents of
program counter (PC) or base register (B)
•The displacement must be small enough to fit the 12-bit field in the
instruction, the displacement must be between
• 0 and 4095 (base relative mode)
• -2048 and +2047 (pc relative mode)
•If pc relative nor base relative mode addressing can not be used
(displacement is too large), then the 4-byte extended instruction format
(Format 4 20-bit address field) must be used

31
Line Loc Source statement Object code

Figure 2.6 Program from Fig. 2.5 with object code 32


Mode Indication Target address calculation
Direct x=0 TA=address
Indexed x=1 TA=address+(X)

(X): the contents of register X (Index register)

33
mode Bit n Bit i Target address
immediate addressing 0 1 Operand value
indirect addressing 1 0 The word at the location given the target address
simple address 0 0 The target address is taken as the location of the
1 1 operand

format 3 6 1 1 1 1 1 1 12
e =0 : format 3
op n i x b p e disp e =1 : format 4
format 4 6 1 1 1 1 1 1 20
op n i x b p e address

Mode Indication Target address calculation


Base relative b=1, p=0 TA=(B)+disp (0≤disp ≤4095)
PC relative b=0, p=1 TA=(PC)+disp (-2048≤disp ≤2047) 34
PC-Relative Addressing Modes
•PC-relative

10 0000 FIRST STL RETADR 17202D


op(6) n i xbp e disp(12)

(14)16 110010 (02D) 16


 displacement= RETADR - PC = 30-3 = 2D

40 0017 J CLOOP 3F2FEC


op(6) n i xbp e disp(12)
(3C)16 110010 (FEC) 16
 displacement= CLOOP-PC= 6 - 1A= -14= FEC
0006 0001A

35
Base-Relative Addressing Modes

•Base-relative
•base register is under the control of the programmer
12 0003 LDB #LENGTH
13 BASE LENGTH
160 104E STCH BUFFER, X 57C003
op(6) n i xbp e disp(12)
(54)16 111100 (003)16
1100 0000 0000 00112 = C00316

(54)16 1 1 1 0 1 0 0036-1051 = -101B16 =(-412310, EFE516)


• 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

36
Immediate Address Translation

•Immediate addressing
55 0020 LDA #3 010003
op(6) n i xbp e disp(12)
( 00 )16 010000 (003)16

133 103C +LDT #4096 75101000


op(6) n i xbp e disp(20)
(74)16 010001 (01000)16

37
Immediate Address Translation (Cont’d)

•Immediate addressing
12 0003 LDB #LENGTH 69202D
op(6) n i xbp e disp(12)
( 68)16 010010 (02D)16 PC relative
( 68)16 010010 (033)16 690033 immediate

•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
• Ex. Line 55 0020 LDA #3 010003

38
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
op(6) n i xbp e disp(12)
(3C)16 100010 (003)16

• TA= RETADR = 0030


• TA= (PC) + disp = 002D + 0003

39
2.2.2 Program Relocation

•Example Fig. 2.1


•Absolute program, starting address 1000
e.g. 55 101B LDA THREE 00102D
•Relocate the program to 2000
e.g. 55 101B LDA THREE 00202D
•Each Absolute address should be modified
•Example Fig. 2.5:
•Except for absolute address, the rest of the instructions need not
be modified
• not a memory address (immediate addressing)
• PC-relative, Base-relative
•The only parts of the program that require modification at load
time are those that specify direct addresses

40
Example

Figure 2.7 Example of program relocation

41
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 length of the address field to be modified, in half-bytes

42
Object Code

Figure 2.8 Object program corresponding to Fig 2.6

43
Homework #2

•Generate the object code, SYMTAB and object program


for each statement in the following SIC/XE program
SUM START 0
FIRST LDX #0
LDA #0
+LDB #TABLE2
BASE TABLE2
LOOP ADD TABLE,X
ADD TABLE2, X
TIX COUNT
JLT LOOP
+STA TOTAL
RSUB
COUNT RESW 1
TABLE RESW 2000
TABLE2 RESW 2000
TOTAL RESW 1
END FIRST

44
2.3 Machine-Independent Assembler Features
•Literals
•Symbol Defining Statement Expressions
•Program Blocks
•Control Sections and Program Linking

32

45
2.3.1 Literals

•Design idea
•Let programmers to be able to write the value of a constant
operand as a part of the instruction that uses it.
•This avoids having to define the constant elsewhere in the
program and make up a label for it.
•Example
e.g. 45 001A ENDFIL LDA =C’EOF’ 032010
93 LTORG
002D * =C’EOF’ 454F46
e.g. 215 1062 WLOOP TD =X’05’ E32011

46
Line Source statement

Figure 2.9 program demonstrating additional assembler features 47


Line Loc Source statement Object code

Figure 2.10 Program from Fig. 2.9 with object code 48


Literals vs. Immediate Operands

•Immediate Operands
•The operand value is assembled as part of the machine instruction
e.g. 55 0020 LDA #3 010003
•Literals
•The assembler generates the specified value as a constant at
some other memory location
e.g. 45 001A ENDFIL LDA =C’EOF’ 032010
•Compare (Fig. 2.6)
e.g.
45 001A ENDFIL LDA EOF 032010
80 002D EOF BYTE C 'EOF' 454F46

49
Literal - Implementation (1/3)

•Literal pools
•Normally literals are placed into a pool at the end of the program
• see Fig. 2.10 (END statement)
•In some cases, it is desirable to place literals into a pool at some
other location in the object program
• assembler directive LTORG
• reason: keep the literal operand close to the instruction

50
Literal - Implementation (2/3)

•Duplicate literals
e.g. 215 1062 WLOOP TD =X’05’
e.g. 230 106B WD =X’05’
•The assemblers should recognize duplicate literals and store only
one copy of the specified data value
•Comparison of the generated data value
• The benefits of using generated data value are usually not great enough to justify the
additional complexity in the assembler
•Comparison of the defining expression
• Same literal name with different value, e.g. LOCCTR=*

51
Literal - Implementation (3/3)
•LITTAB
•literal name, the operand value and length, the address assigned to the
operand
•Pass 1
•build LITTAB with literal name, operand value and length, leaving the
address unassigned
•when LTORG statement is encountered, assign an address to each literal
not yet assigned an address
•Pass 2
•search LITTAB for each literal operand encountered
•generate data values using BYTE or WORD statements
•generate modification record for literals that represent an address in the
program

52
2.3.2 Symbol-Defining Statements

•Labels on instructions or data areas


•the value of such a label is the address assigned to the statement
•The assembler directive used is EQU (for “equate”)
•Defining symbols
•symbol EQU value
•value can be: constant, other symbol, expression
•making the source program easier to understand
•no forward reference

53
2.3.2 Symbol-Defining Statements (cont’d)

•Example 1
MAXLEN EQU 4096
+LDT MAXLEN +LDT #4096

•Example 2
BASE EQU R1
COUNT EQU R2
INDEX EQU R3

54
ORG (origin)

•Indirectly assign values to symbols


•Reset the location counter to the specified value
•ORG value
•Value can be: constant, other symbol, expression
•No forward reference
•Example
SYMBOL: 6bytes SYMBOL VALUE FLAGS
STAB
VALUE: 1word (100 entries)
FLAGS: 2bytes
. . .
LDA VALUE, X .
.
.
.
.
.

55
ORG Example

•Using EQU statements


STAB RESB 1100
SYMBOL EQU STAB
VALUE EQU STAB+6
FLAG EQU STAB+9
•Using ORG statements
STAB RESB 1100
ORG STAB
SYMBOL RESB 6
VALUE RESW 1
FLAGS RESB 2
ORG STAB+1100

56
2.3.3 Expressions

•Expressions can be classified as absolute expressions or


relative expressions
•107 MAXLEN EQU BUFEND-BUFFER
•BUFEND and BUFFER both are relative terms, representing
addresses within the program
•However the expression BUFEND-BUFFER represents an
absolute value
•When relative terms are paired with opposite signs, the
dependency on the program starting address is canceled
out; the result is an absolute value

57
SYMTAB

•None of the relative terms may enter into a multiplication


or division operation
•Errors:
•BUFEND+BUFFER
•100-BUFFER
•3*BUFFER
•The type of an expression
•keep track of the types of all symbols defined in the program
Symbol Type Value
RETADR R 0030
BUFFER R 0036
BUFEND R 1036
MAXLEN A 1000
58
2.3.4 Program Blocks

•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 of the
unnamed (default) block
•If no USE statements are included, the entire program belongs to
this single block
•Example: Figure 2.11
•Each program block may actually contain several separate
segments of the source program

59
Line Source Statement

Figure 2.11 Example of a program with multiple program blocks 60


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

61
Line Loc/Block Source Statement Object code

Figure 2.12 Program from Fig.2.11 with object code 62


Figure 2.12

•Each source line is given a relative address assigned and a


block number
Block name Block number Address Length
(default) 0 0000 0066
CDATA 1 0066 000B
CBLKS 2 0071 1000

•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

63
Program Readability

•Program readability
•No extended format instructions on lines 15, 35, 65
•No needs 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
•see Fig. 2.13, Fig. 2.14

64
Figure 2.13
Figure 2.13Object
Objectprogram corresponding
program to Fig to
corresponding 2.12
Fig 2.11

65
Source program Object program Program
loaded in Relative
Line memory address

Figure 2.14 Program blocks from Fig 2.11 traced through the assembly and loading
processes

66
2.3.5 Control Sections and Program Linking
•Control Sections
•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
•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
•Fig. 2.15, 2.16

67
Line Source statement

Figure 2.15Illustration of control sections and program linking 68


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 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
69
Line Loc Source statement Object code

Figure 2.16 program from Fig. 2.15 with object code 70


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 R
•Col. 2-7 Name of external symbol referred to in this control section
•Col. 8-73 Name of other external reference symbols
71
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.10 Modification flag (+ or -)
•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

72
Figure 2.17 Object program corresponding to Fig. 2.15

73
External References in Expression

•Earlier definitions
•required all of the relative terms be paired in anexpression (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, the assembler cannot determine whether or not
the expression is legal at assembly time. This work will be
handled by a linking loader.

74
2.4 Assembler Design Options
•One-pass assemblers
•Multi-pass assemblers
•Two-pass assembler with overlay structure

54

75
Two-Pass Assembler with Overlay Structure
•For small memory
•pass 1 and pass 2 are never required at the same time
•three segments
• root: driver program and shared tables and subroutines
• pass 1
• pass 2
•tree structure
•overlay program

76
2.4.1 One-Pass Assemblers

•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

77
2.4.1 One-Pass Assemblers (cont’d)

•Main Problem
•forward references
• data items
• labels on instructions
•Two types of one-pass assembler
•load-and-go
• produces object code directly in memory for immediate execution
•the other
• produces usual kind of object code for later execution

78
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 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 actualaddress must be known at
assembly time, we can use an absolute program

79
Forward Reference in One-pass Assembler

•For any symbol that has not yet been defined


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

80
Load-and-go Assembler (cont’d)

•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
•Example
•Figure 2.18, 2.19

81
Line Loc Source statement Object code

Figure 2.18 Sample program of a one-pass assembler 82


Memory
address Contents Symbol Value

Figure 2.19(a) Object code in memory and symbol table entries for the program in Fig 2.18
after scanning line 40

83
Memory Symbol Value
address Contents

Figure 2.19(b) Object code in memory and symbol table entries for the program in Fig 2.18
after scanning line 160

84
Producing Object Code

•When external working-storage devices are not available


or too slow (for the intermediate file) between the two
passes
•Solution:
•When definition of a symbol is encountered, the assembler must
generate another Tex record with the correct operand address
•The loader is used to complete forward references that could not
be handled by the assembler
•The object program records must be kept in their original order
when they are presented to the loader
•Example: Figure 2.20
85
Figure 2.20 Object program from one-pass assemble for program in Fig 2.18

86
2.4.2 Multi-Pass Assemblers

•Restriction on EQU and ORG


•no forward reference, since symbols’ value can’t be defined
during the first pass
•Example
•Use link list to keep track of whose value depend on an undefined
symbol
•Figure 2.21

87
Figure 2.21 Example of multi-pass assemble operation

88
Figure 2.21 Example of multi-pass assemble operation (cont’d)

89
Figure 2.21 Example of multi-pass assemble operation (cont’d)

90
Figure 2.21 Example of multi-pass assemble operation (cont’d)

91
Figure 2.21 Example of multi-pass assemble operation (cont’d)

92
Figure 2.21 Example of multi-pass assemble operation (cont’d)

93
2.5 Implement Examples

•MASM Assembler
•SPARC Assembler
•AIX Assembler

94
2.5.1 MASM Assembler

•The programmer views memory as a collection of


segments
•CODE, DATA, CONST, and STACK
•Code segments are addressed using register CS, to indicate the
segment that contains the starting label specified in the END
statement
•Stack segments are addressed using register SS, to indicate the
last stack segment processed by the loader
•Data segment (including constant segments) are normally
addressing using DS, ES, FS, and GS.

95
2.5.1 MASM Assembler (cont’d)

•MASM assumes that all references to data segments use


register DS, this can be changed by ASSUME
•E.g.
ASSUME ES: DATASEG2
Tells the assembler to assume that register ES indicates the segment DATASEG2

•Registers DS, ES, FS, and GS must be loaded by the


program before they can be used to address data segment
•E.g.
MOV AX, DATASEG2
MOV ES, AX
Set ES to indicate the data segment DATASEG2

96
2.5.1 MASM Assembler (cont’d)

•Jump instructions are assembled in two type


•Near jump
• Jump to a target in the same code segment
• JMP SHORT TARGET
• 2 or 3 bytes (depending upon whether the jump address is within 128 bytes)
•Far jump
• Jump to a target in different code segment
• JMP FAR PTR TARGET
• 5 bytes
•Reference between segments that are assembled together are
automatically by the assembler, External reference between
separately assembled modules must be handled by the linker.

97
2.5.2 SPARC Assembler

•A SPARC assembler language program is divided into


units called sections,
•predefined sections as follows:
.TEXT Executable instructions
.DATA Initialized read / write data
.RODATA Read-only data
.BSS Uninitialized data areas

•References between different sections are resolved by the


linker, not by the assembler

98
2.5.2 SPARC Assembler (cont’d)

•The object file written by the SPARC assembler contains


translated versions of the segments of the program and a
list of relocation and linking operations that need to be
performed.
•Delay branch (Sec. 1.5.1)
CMP %L0, 10
BLE LOOP
ADD %L2, %L3, %L4

99
Delay Branch
•Original Instruction Sequence
LOOP: .
.
.
ADD %L2, %L3, %L4
CMP %L0, 10
BLE LOOP
NOP
•NOP (no-operation), to simplify debugging SPARC assembly
language programmer place NOP in delay slots

100
Delay Branch (cont’d)

•Another possible instruction sequence


LOOP: ADD %L2, %L3, %L4
.
.
CMP %L0, 10
BLE LOOP
NOP
•On the last execution of the loop, the ADD instruction should not
be executed

101
Delay Branch (cont’d)

•SPARC architecture a solution


•Conditional branch instruction like BLE can be annulled
LOOP: .
.
.
CMP %L0, 10
BLE, A LOOP
ADD %L2, %L3, %L

102
2.5.3 AIX Assembler

•PowerPC load and store a based register and a


displacement value to specify an address in memory
•Any general-propose registers (except GPR0) can be used as a
base register
.USINGLENGTH, 1
.USINGBUFFER, 4
•Use GPR1 and GPR4 as base register

•A base register table is used to remember which of the general-


purpose registers are currently available as base register, and what
base address they contain

103
2.5.3 AIX Assembler (cont’d)

•AIX assembler language allows the programmer to write


base registers and displacements, e.g.
L 2, 8(4)
•An operand address that is 8 bytes past the address contained in
GPR4
•An AIX assembler language program can be divided into
control sections using the .CSECT assembler directive.
•Each control section has an associated storage mapping
class that describes the kind of data it contains: PR
(executable instructions), RO (read-only data), RW
(read/write data), and BS (uninitialized read/write data)
104
2.5.3 AIX Assembler (cont’d)

•Dummy section: data items included in a dummy section


do not actually become part of the object program; they
serve only to define labels within the section.
•Common blocks: uninitialized blocks of storage that can
be shared between independently assembled programs
•.GLOBL makes a symbol available to the linker
•.EXTERN declares that a symbol is defined in another
source module

105
2.5.3 AIX Assembler (cont’d)

•Assembled control sections are placed into the object


program according to their storage mapping class
•Executable instructions, read-only data, and various kinds of
debugging tables are assigned to an object program .TEXT section.
•Read/write data and TOC entries are assigned to .DATA section.
•Uninitialized data is assigned to .RSS section

106

You might also like