Sie sind auf Seite 1von 17

Assembly and Linking Process

Thorne : Chapter 14
(Irvine Edition IV : Section 3.3) ( )
How does the Assembly Work
Theassembler isaprogramthat readsyour ASM filelineby line The assembler is a program that reads your .ASM file line-by-line.
The assembler uses a two-passapproach
A pass =Processes all statements in .ASM file sequentially from
t t t fi i h start to finish
SYSC3006 1
How does the Assembly Work
1st Pass: for each statement:
1 Checksyntax 1. Check syntax
2. Allocate any memory needed for image
For memory declaration (DB, DW)
hi i ll hb f d For each instruction, allocate enough bytes for opcode +
operand
For directives (pseudo opcodes): ?
3. For every label definition, assign a value to the label and
record this (label,value) association in the Symbol Table
(Throne, p. 342)
If there were anysyntax errors in 1
st
pass, write errorsto
.LST file and stop, else ....
SYSC3006 2
How does the Assembly Work
2nd Pass: Build the binary image
F hi t ti l t th i t ti di For each instruction, completethe instruction encoding.
Errors may result when calculating offsets
e.g. trying to jump too far for a conditional jump (target g y g j p j p( g
out of range)
If so, write results to .LST file
if it lt t OBJ fil if no errors write results to .OBJ file
SYSC3006 3
1
st
Pass in Detail
Intention: When the resulting program is loaded into memory,
then the binary image associated with each statement (either
instruction or memory declaration) will be loaded at the offset(s)
of the allocated location(s) physical segment
containing code
Assembled: (.OBJ )
Offset Contents

0103 B231
.ASM source
.code
. . .
dl 1
Run-Time Memory:
Offset Contents

0103 B231 0103 B2 31
0105
mov dl, 1
. . .
0103 B2 31
0105
bl l d
SYSC3006 4
assembler loader
1
st
Pass in Detail : Memory Allocation
Ineachsegment ( data code stack) allocationstartsat In each segment (.data, .code, .stack), allocation starts at
offset 0000H
A location counter ($) is used to track address of next byte ( ) y
to allocate
As bytes are allocated, adjust $ value
SYSC3006 5
1
st
Pass in Detail : Memor Allocation 1
st
Pass in Detail : Memory Allocation
For each memory declaration, the assembler allocates the
number of bytesdeclared(DB or DW) number of bytes declared (DB or DW)
Example : Suppose $ =0006H andnext line of program is: dw
All t 2b t f d t t t Allocate 2 bytes for dw statement:
use address 0006H for low byte
use address 0007H for high byte
Little Endian
After allocation: $ =0008H (next byteto allocate)
Example : If next lineof programis: db Hi Mom Example : If next line of program is: db Hi Mom
Allocate 6 bytes for db statement, using address 0008H
for H, , 000DH for m
After allocation: $=000EH
SYSC3006 6
After allocation: $ =000EH
1
st
Pass in Detail : Memory Allocation
For each instruction, the assembler allocates the number of
bytes needed to encode operation and operands specified
Example: Suppose$ = 0006H whenthenext lineis: Example: Suppose $ 0006H when the next line is :
MOV CL, BL
This instruction requires 2 bytes to encode (8A CB)
Af ll i $ 0008 After allocation: $ = 0008H
Example : If the next line of program is:
MOV BX, 1
instruction
encoding
details later
This instruction requires 3bytes to encode (BB 01 00)
1 byte: opcode, BX dest, imm as src
2bytes: immsrcvalue
Little Endian
2 bytes: imm src value
After allocation: $ = 000BH
SYSC3006 7
1st Pass in Detail : Memory Allocation y
Whenever a label definition is encountered (either variable or
codelabel) code label)
The value of label is assigned the current value of $
In other words, the value of label is the address of the next
b t t b ll t dt th byteto be allocated to the program
The (label, $-value) pair is stored in a Symbol Tablefor
recall during 2nd pass
Symbol Table associates labels with values
artifact of assembler
NOTE : Do not confuse $ with IP !!!!!
artifact of processor
SYSC3006 8
artifact of processor
2nd Pass in Detail : Memory Allocation 2nd Pass in Detail : Memory Allocation
The assembly now builds image of bytes to be loaded at
addresses
For memory declarations, fill in the initial value
If none is given, use default g ,
For instructions, complete the encodingfor opcodes and
operands
Will dependontheaddressingmodes Will depend on the addressing modes.
For all label references, get the value of the label from
the symbol table.
For relativeoffsets calculatetheoffset =label value $ For relative offsets, calculate the offset =label value - $
Write results to .LST file (in readable format for human use)
Write results to .OBJ file (in binary format for machine use)
SYSC3006 9
Example : Assembly Process ( LST file)
DataSegment
Example : Assembly Process (.LST file)
.8086
Data Segment
.model small
Offset Contents
.stack 100H
0000 .data
0000 000A Ten dw 10
offset =0 at start of segment
0002 0064 Hundred dw 100
0004 03E8 Thousand dw 1000
0006 2710 TenThousand dw 10000
littleendian?
0006 2710 TenThousand dw 10000
0008 80BE Value dw 80BEH
little endian?
NO! in .LST
What are the values of these labels?
SYSC3006 10
Example : Assembly Process (.LST file) Code Segment
0000 .code
new segment, offset =0
unknown value (loader directive!)
0000 main proc
0000 B8 ---- R mov ax, @data
0003 8E D8 mov ds, ax
0005 B5 10 mov ch, 16
0007 8B 1E 0008 R mov bx Value
immediate =16
0007 8B 1E 0008 R mov bx, Value
000B Outloop:
direct =offset into data segment
What is the value of
O tL ?
000B D1 E3 shl bx, 1
000D 72 04 jc Got1
OutLoop?
How many bytes have
been allocated to
Outloop?
What istheoffset of thetarget of thisj mp?
SYSC3006 11
p
What is the offset of the target of this jump?
Example : Assembly Process ( LST file)
CodeSegment
Example : Assembly Process (.LST file)
000F Got0:
Code Segment
000F B2 30 mov dl, '0'
0011 EB 02 jmp Dump
target of previousjc
0013 Got1:
0013 B2 31 mov dl, '1'
0015 Dump:
target of previous jc
0015 Dump:
0015 B4 02 mov ah, 2
0017 CD 21 int 21H
[ ETC . . . . ]
end main
SYSC3006 12
How does the Linker Work ?
If we look inside the go.bat script that you use in the lab, we see
two commands (other than error-checking)
tasm/zi file.asm tasm/zi file.asm
tlink/v file.obj
It assumes the program is contained within one file.
In practice, there will often be more than one file
Using libraries that are supplied by a vendor
Using good software practices to organize your larger
programs into modules.
In these cases, we must replace the simple go.bat script
For each .asm file : tasm /zi filen.asm
Then: tlink/vfile1obj file2obj
SYSC3006 13
Then : tlink /v file1.obj file2.obj .
Assemble 1-by-1(independently)
How does the Linker Work ?
It is the role of the linker to resolve referencesto labels among
different assembler files different assembler files
file1.asm
file2.asm
.data
x DW 1
.data
y DW ?
.code
.code
MOV AX, x
MOV y AX
But how does the assembler know whether a label is inside the
givenfile(local) or insideanother filethat will beidentifiedat
MOV y, AX
SYSC3006 14
given file (local) or inside another file that will be identified at
link time (external) ?
How does the Linker work ? How does the Linker work ?
When the assembler finds a reference to a name that is not in the
current source file, it cannot calculate the names effective
address. Use the EXTRN directive to identify names that exist
outside the current source file. This tells the assembler that the
names address will be filled in by the linker.
Whenthelabel isdefined: PUBLIC name When the label is defined : PUBLIC name
When the label is referenced :
EXTRN name:type
where type ={BYTE, WORD, NEAR, FAR, PROC} among
others
SYSC3006 15
others
A Linking Example A Linking Example
file1.asm
.data
file2.asm
.data
xDW 1
PUBLIC x
EXTRN x:WORD
y DW ?
.code
Later : External labelsbecomeespeciallyimportant whenwe
.code MOV AX, x
MOV y, AX
Later : External labels become especially important when we
use subroutines.
In this case, the external declaration is
EXTRN PROC
SYSC3006 16
EXTRN name:PROC
A Linking Example
(Subroutine) (Subroutine)
.LST of a caller program
.LST of a called program
0000 .data
0000 05 x DB 5
=0003 y EQU 3
0000 .data
EXTRN x:BYTE ; pass-by-ref
EXTRN y:ABS ; pass-by-value
p g
PUBLIC x ; pass-by-ref
PUBLIC y ; pass-by-value
0000 .code
0000 .code
PUBLIC disp
0000 disp PROC NEAR
0000 B8 ---- R MOV AX, @data
EXTRN disp:PROC
0000 main PROC
0000 E8 0000 E CALL disp
0003 8E D8 MOV DS, AX
0005 B4 02 MOV AH, 2
0007 80 06 0000 E 30 ADD x,30h
000C 8A16 0000E MOV DL, x
0003 main ENDP
END main
000C 8A 16 0000 E MOV DL, x
0010 CD 21 INT 21h
0012 B2 30 E MOV DL, y+30h
0014 CD 21 INT 21h
SYSC3006 17
R: Relocatable
E: External

Das könnte Ihnen auch gefallen