Sei sulla pagina 1di 18

1 Assembly Language Programming Microprocessors 2 Assembly Language Programming Microprocessors

Assembly Language
in Short Overview
8086 Memory
Architecture

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

3 Assembly Language Programming Microprocessors 4 Assembly Language Programming Microprocessors

Assembly Language Programs ⎯ 1 Assembly Language Programs ⎯ 2


Assembly language program Assembly language converted to machine language
List of 8086 instructions + data
89D8 MOV AX,BX
CPU executes — one by one — instructions in order of listing
01C8 ADD AX,CX
Example:
2B873412 SUB AX,[BX+1234]
MOV AX, BX
50 PUSH AX
ADD AX, CX
B441 MOV AH,41
SUB AX, [BX+1234]
CD21 INT 21
PUSH AX
MOV AH, 41
INT 21 Machine language (*.com) version of program
Machine code = string of hexadecimal codes
Running this program requires CPU access to memory: 89D801C82B87341250B441CD21
Code segment access ([CS:IP]) to fetch instructions
Data segment access ([DS:BX+1234]) to load operand
Stack segment access ([SS:SP]) to execute push

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
5 Assembly Language Programming Microprocessors 6 Assembly Language Programming Microprocessors

Assembly Language Programs ⎯ 3 Assembly Language Programs ⎯ 4


00 Assign default values to general registers
OS loads program to 20-bit physical address EF
Stack
OS loads registers Segment CD CX ← size of program (bytes)
AB ← SS:SP AX = BX = DX = SI = DI = BP ← 0
SS register ← Stack base address / 10h 21
Copy stack to memory from address SS:SP CD Load initial values for program registers
41
IP ← offset to first instruction in code segment

Higher Addresses
B4
50 SP ← offset to first location in stack segment
12
CS register ← Code base address / 10h
Code
34 Run program
Segment
87 Fetch machine instructions from code segment (CS:IP)
IP register ← Code base address % 10h 2B
Copy code to memory from address CS:IP C8 Access data and stack
01 OS services (interventions)
89D801C82B87341250B441CD21
D8
89 ← CS:IP Loading program into memory
12
ES = DS register ← Data base address / 10h Assigning initial values for registers in 8086
Data 34
Copy data to memory from address DS:EA Segment 56 Adjusting pointers stored in program’s data segment
78 ← DS:EA
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

7 Assembly Language Programming Microprocessors 8 Assembly Language Programming Microprocessors

80186 Architecture Assembly Language Programs ⎯ 5


General AH AL AX
Decoder
DOS program files
Registers and
Stored on disk or other long term storage
BH BL BX
00
CH CL CX Control
ALU EF
Contains
DH DL DX Stack
CD
ALU_OUT
BP ALU
Execution
SP ALU_IN Unit
AB
SI Status (EU) 21 HEADER (only in *.exe files):
DI Word CD
41 Information for operating system
System Bus B4
50 LOAD MODULE (in *.exe, *.com, *.sys files):
OFF
12
Physical Address
1
2
Data Data , Code, Stack part of program
Instruction Address Bus Bus 34
IP
3 Bus
Pointer Unit Control Control 87
CS PA
(MAR) 4 Interface
and DS SEG (PAU) (MDR)
Unit 2B
Segment 5
Registers
SS
6
(BIU)
Code
C8 Loading and Running
ES
Instruction Decoder
01
Queue D8 OS copies load module into memory
89
Logical Address = SEGMENT:OFFSET
20 bits 16 bits 12 OS initializes registers
Physical Address = SEGMENT × 10h + OFFSET
Physical Control Data Data
34 OS adjusts stored pointers (if file has header)
Address 56
Main Memory 78 OS points CS:IP at program start instruction

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
9 Assembly Language Programming Microprocessors 10 Assembly Language Programming Microprocessors

Assembly Language Programs ⎯ 6 Assembly Language Programs ⎯ 7


Data locations Branch instructions
Transfer and ALU instructions refer (default) to data segment (DS) A simple JUMP (skip over SUB and PUSH) looks like:
Execute SUB AX, [BX+1234] MOV AX, BX
Calculate Effective Address (EA) BX+1234 ADD AX, CX
Load operand from data segment address DS:BX+1234 JMP L1
Perform AX ← AX - [DS:BX+1234] SUB AX, [BX+1234] ; fall-through instruction
Program must know where data is stored PUSH AX
L1: MOV AH, 41 ; target instruction
Code locations INT 21
Instructions stored in memory in order of program list
JMP assembly coded with pointer L1 to line MOV AH, 41h
OS sets IP to first instruction in CS
Pointer is address of target instruction
CPU updates IP after loading an instruction
IP ← address of next instruction JMP machine coded with displacement to line MOV AH, 41
Program only needs to know CS:IP for branch instructions Displacement is target address – fall-through address

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

11 Assembly Language Programming Microprocessors 12 Assembly Language Programming Microprocessors

Assembly Language Programs ⎯ 8 The Assembly Language Programming Process


Branch instructions Stage User Action Detailed Actions
Program listing with instruction lengths:
Top-down 1. High level design
bytes instruction Plan
design 2. Organization into modules and functions
2 MOV AX, BX
1. Define local data structures
2 ADD AX, CX Coding
Program 2. Write assembly language code for
2 JMP L1 ; IP ← IP + 5 modules
modules
4 SUB AX, [BX+1234] ; fall-through
Run 1. Convert assembly code to machine code
1 PUSH AX Assembly
Assembler 2. Resolve internal references
2 L1: MOV AH, 41 ; target
2 INT 21 1. Resolve external address references for
Run jump and call instructions
JMP instruction ASSEMBLY CODED with IP of target Linking
Linker 2. Build program header
JMP instruction MACHINE CODED with displacement 3. Store executable program file on disk
displacement = 4 + 1 = 5 Run 1. Place program in memory
Loading
Loader 2. Set instruction pointer to program
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
13 Assembly Language Programming Microprocessors 14 Assembly Language Programming Microprocessors

Top-Down Design Coding Program Modules


Define requirements for program Assign variables to memory locations
VARIABLE NAME = SYMBOLIC LABEL for memory address
High Level Design
List variables and addresses in scratchpad
Divide problem into modules
Used at assembly/compile time
Function — block of code to perform specific task
NOT part of program listing
Module — source file containing one or more functions
Program — one or more modules
Code ALU operations as assembly instructions
Reference to variable uses reference by pointer
Low Level Design
Example: z = y + x
Code modules and their interfaces
Assignments x ~ DS:0000
Graphical or other design technique y ~ DS:0002 ; scratchpad
Flowchart z ~ DS:0004
Pseudo-code
Coding MOV AX,[0000] ; AX ← x
Use cases
UML ADD AX,[0002] ; AX ← AX + y
MOV [0004],AX ; z ← AX
etc
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

15 Assembly Language Programming Microprocessors 16 Assembly Language Programming Microprocessors

Assembly by Hand ⎯ Coding Chart Example of Assembly Coding


Enter instructions Implement pseudocode example:
Write complete assembly program
List op-code (mnemonic) and operands a = 3, b = -1, c = 0
Write line labels when necessary
Calculate Addresses
while (a < 10){
Start with arbitrary start address b = a * 2
Calculate HEX codes (machine instruction) for each line c = b + 7
Branch instructions require knowing addresses
a++
Address HEX Code Label Op-Code Operand Comments }
end

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
17 Assembly Language Programming Microprocessors 18 Assembly Language Programming Microprocessors

Assembly by Hand ⎯ Coding Machine Language Instruction


a = 3, b = -1, c = 0
while (a < 10){ Translate op-code and operands to HEX Code
b=a*2 HEX Code format for each addressing mode
c=b+7
a++ Translation based on Intel op-code tables
}
end Enter op code, line labels, comments
address HEX code label op-code and operands comment
MOV WORD PTR [0000],0003
MOV WORD [0000],3 ; define a
MOV WORD [0002],FFFF ; define b
MOV WORD [0004],0 ; define c
loop: CMP WORD [0000],A ; CMP a == 10
JGE end ; if a>= 10 then end
MOV AX,[0000] ; AX ← a
SHL AX,1 ; AX ← 2*AX
MOV [0002],AX ; b ← AX
ADD AX,7 ; AX ← AX+7 C70600000300
MOV [0004],AX ; c ← AX
INC WORD [0000] ; a ← a+1
JMP loop ; back to loop
end: RET ; return to DOS

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

19 Assembly Language Programming Microprocessors 20 Assembly Language Programming Microprocessors

Hand Assembly ⎯ Convert to Machine Code Address of Instruction in Memory


Enter hexadecimal code for each line Arbitrary start address
Default IP = 0000
address HEX code label op-code and operands comment Relative to code segment base (CS)
C70600000300 MOV WORD [0000],3 ; define a Count instruction length (bytes in each instruction)
C7060200FFFF
C70604000000
MOV
MOV
WORD [0002],FFFF
WORD [0004],0
;
;
define b
define c Next address = previous address + previous instruction length
833E00000A loop: CMP WORD [0000],A ; CMP a == 10
7D__ JGE end ; if a>10 then end
A10000 MOV AX,[0000] ; AX ← a
D1E0 SHL AX,1 ; AX ← 2*AX
address HEX code label op-code and operands comment
A30200 MOV [0002],AX ; b ← AX
050700 ADD AX,7 ; AX ← AX+7 0000 C70600000300 MOV WORD [0000],3 ; define a
A30400 MOV [0004],AX ; c ← AX 0006 C7060200FFFF MOV WORD [0002],FFFF ; define b
FF060000 INC WORD [0000] ; a ← a+1 000C C70604000000 MOV WORD [0004],0 ; define c
EB__ JMP loop ; back to loop 0012
C3 end: RET ; return to DOS

Branch codes incomplete:


Target addresses unresolved (not calculated) C7 06 02 00 FF FF
0006 0007 0008 0009 000A 000B

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
21 Assembly Language Programming Microprocessors 22 Assembly Language Programming Microprocessors

Example of Assembly Coding ⎯ Unresolved Example of Assembly Coding ⎯ Resolving Code


address HEX code label op-code and operands comment address HEX code label op-code and operands comment

0000 C70600000300 MOV WORD [0000],3 ; define a 0000 C70600000300 MOV WORD [0000],3 ; define a
0006 C7060200FFFF MOV WORD [0002],FFFF ; define b 0006 C7060200FFFF MOV WORD [0002],FFFF ; define b
000C C70604000000 MOV WORD [0004],0 ; define c 000C C70604000000 MOV WORD [0004],0 ; define c
0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10 0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10
0017 7D__ JGE end ; if a>=10 then 002D 0017 7D__ JGE 002D ; if a>=10 then 002D
0019 A10000 MOV AX,[0000] ; AX ← a 0019 A10000 MOV AX,[0000] ; AX ← a
001C D1E0 SHL AX,1 ; AX ← 2*AX 001C D1E0 SHL AX,1 ; AX ← 2*AX
001E A30200 MOV [0002],AX ; b ← AX 001E A30200 MOV [0002],AX ; b ← AX
0021 050700 ADD AX,7 ; AX ← AX+7 0021 050700 ADD AX,7 ; AX ← AX+7
0024 A30400 MOV [0004],AX ; c ← AX 0024 A30400 MOV [0004],AX ; c ← AX
0027 FF060000 INC WORD [0000] ; a ← a+1 0027 FF060000 INC WORD [0000] ; a ← a+1
002B EB__ JMP loop ; back to 0012 002B EB__ JMP 0012 ; back to 0012
002D C3 end: RET ; return to DOS 002D C3 end: RET ; return to DOS

Resolving branch references Branch machine coded with displacement


Line label = symbolic reference to instruction address displacement = (target address) – (fall-through address)
Replace line labels in code with IP address of target line
0017 JGE 002D: 002D – 0019 = 0014 → 14 (short)
loop → 0012
002B JMP 0012: 0012 – 002D = FFE5 → E5 (short)
end → 002D
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

23 Assembly Language Programming Microprocessors 24 Assembly Language Programming Microprocessors

Example ⎯ Object Code with Resolved References Linking


Program file
address HEX code label op-code and operands comment File contents = Header + Load Module (data, code, stack)
0000 C70600000300 MOV WORD [0000],3 ; define a No header for *.com program (no linking)
0006
000C
C7060200FFFF
C70604000000
MOV
MOV
WORD [0002],FFFF
WORD [0004],0
;
;
define b
define c
Code section = string of executable machine instructions
0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10 address HEX code label op-code and operands comment
0017 7D14 JGE 002D ; if a>=10 then 002D
0019 A10000 MOV AX,[0000] ; AX ← a 0000 C70600000300 MOV WORD [0000],3 ; define a
0006 C7060200FFFF MOV WORD [0002],FFFF ; define b
001C D1E0 SHL AX,1 ; AX ← 2*AX 000C C70604000000 MOV WORD [0004],0 ; define c link
001E A30200 MOV [0002],AX ; b ← AX 0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10
0021 050700 ADD AX,7 ; AX ← AX+7 0017 7D14 JGE 002D ; if a>=10 then 002D
0019 A10000 MOV AX,[0000] ; AX ← a
0024 A30400 MOV [0004],AX ; c ← AX 001C D1E0 SHL AX,1 ; AX ← 2*AX
0027 FF060000 INC WORD [0000] ; a ← a+1 001E A30200 MOV [0002],AX ; b ← AX
002B EBE5 JMP 0012 ; back to 0012 0021 050700 ADD AX,7 ; AX ← AX+7
002D C3 end: RET ; return to DOS 0024 A30400 MOV [0004],AX ; c ← AX
0027 FF060000 INC WORD [0000] ; a ← a+1
002B EBE5 JMP 0012 ; back to 0012
002D C3 end: RET ; return to DOS

Header000000000000000000000000000000
C70600000300C7060200FFFFC70604000000
833E00000A7D14A10000D1E0A30200050700
A30400FF060000EBE5C3
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
25 Assembly Language Programming Microprocessors 26 Assembly Language Programming Microprocessors

Simple *.com Program String Processing Using Labels


0100 BE1101 MOV SI,0111 0100 JMP L1
0103 B80000 MOV AX,0 0102 S1: db "Line of Text",0,0,0,0
0106 0304 ADD AX,[SI] 0112 S2: db "New Line",0,0,0,0,0,0,0,0
0108 83C602 ADD SI,2 0122 L1: MOV DI,0080 ; destination
010B 833C00 CMP WORD [SI],0 0125 LEA SI,[S1] ; source_1
010E 75F6 JNZ 0106 0129 CALL L2 ; call copy
0110 C3 RET 012C LEA SI,[S2] ; source_2
0111 DW 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0 0130 CALL L2 ; call copy
0133 RET ; end of main routine
0110 C3 01 00 02 00 03 00 04-00 05 00 06 00 07 00 08 0134 L2: LODSB ; AL ← [SI], SI++
0120 00 09 00 0A 00 0B 00 0C-00 0D 00 0E 00 0F 00 00 0135 STOSB ; [DI] ← AL, DI++
0136 CMP AL,00
AX = 0 → 1 → 3 → 6 → A → F → 15 → 1C → 0138 JNZ L2
24 → 2D → 37 → 42 → 4E → 5B → 69 → 78
013A RET

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

27 Assembly Language Programming Microprocessors 28 Assembly Language Programming Microprocessors

String Processing without Labels Arithmetic: Stored Data, Variables, and Pointers
0100 JMP 0122 0100 LEA SI,[0106] 0123 LODSW
0104 JMP 010A 0124 IMUL WORD [BX]
0102 db "Line of Text",0,0,0,0
0126 MOV [BX+02],AX
0112 db "New Line",0,0,0,0,0,0,0,0 0106 DW 0000
0129 MOV [BX+04],DX
0108 DW 0000
0122 MOV DI,0080 ; destination 012C CMP WORD [SI],0
010A MOV [SI+2],DS 012F JNZ 0123
0125 LEA SI,[0102] ; source_1
010D LEA AX,[0132] 0131 RET
0129 CALL 0134 ; call copy
0111 CWD 0132 DW 1122,0
012C LEA SI,[0112] ; source_2 0112 MOV BX,10 0136 DW 1,2,3,4,5,6,7,8
0130 CALL 0134 ; call copy 0115 DIV BX 0146 DW 9,a,b,c,d,e,f,0
0133 RET ; end of main routine 0117 ADD [SI+2],AX
0134 LODSB ; AL ← [SI], SI++ 011A MOV [SI],DX
0135 STOSB ; [DI] ← AL, DI++ 011C LDS SI,[SI]
0136 CMP AL,00 011E LEA BX,[SI]
0138 JNZ 0134 0120 ADD SI,4

013A RET

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
29 Assembly Language Programming Microprocessors 30 Assembly Language Programming Microprocessors

Assembly Programming — Matters of Style Program File Types


Different methods are possible for: DOS/Windows
DOS *.exe and *.com programs .com file = Load Module
Modular programming Very simple programs
Segmentation Limited to one segment CS = DS = SS = ES
Allocating memory for data No Program Header
First instruction at IP = 0100
Creating loops and control flow
.exe file = DOS Header + Load Module
Defining function calls
Uses multiple segments and allows far branches
Standard programming styles OS provides value for DS
Assembler programming models Header contains initial values for CS:IP, SS:SP
Coding in native assembly language development environment Header contains other OS parameters
C language model Unix/Linux (on PC)
Assembly coding according to C language requirements ELF file = ELF Header + Load Module
For combining assembly and C code into one program Use one only segment CS = DS = SS = ES
Uses advanced 32-bit processor features
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

31 Assembly Language Programming Microprocessors 32 Assembly Language Programming Microprocessors

Modular Programming (Main + Functions) Linking


Build program from separate source files (modules) Store executable program file on disk
Each source module edited in a separate file Required for DOS *.exe files and Linux executable files
Compile or Assemble source files into separate object files Place segments into standard order
Object code is machine code with symbolic external references
Create program header
Link object files together to create executable file
Easier to design, read and understand programs For programs compiled/assembled from separate source files
Write most modules in high level language Combines separate OBJECT CODE modules
Write critical sections in assembly language Resolves EXTERNAL REFERENCES between modules
Write, debug, and change modules independently
function.obj localized *.exe file unified *.exe file
main.C compile main.OBJ link prog.EXE Function Stack Function Stack
Function Code Unified Stack
Function Code
f1.ASM assemble f1.OBJ Function Data Function Data Function Code
Main Stack Main Code
f2.C compile f2.OBJ main.obj Main Code
Unified Data
Main Stack Main Data
load f_std.LIB Main Code Program Header Program Header
Main Data Higher Addresses
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
33 Assembly Language Programming Microprocessors 34 Assembly Language Programming Microprocessors

Program Modules and Resolving References Logical Offset — Paragraphs


Main module file — main.asm Logical address to physical address
SEG:OFF → PA = SEG × 10h + OFF
main: MOV AX,BX
Physical address to logical address
ADD AX,[0012] ; pass parameter in AX
CALL function ; external reference
PA → SEG:OFF = (PA / 10h):(PA % 10h)
; resolved by linker Paragraph
end: RET 10h bytes = smallest 8086 segment
Logical Offset
Function module file — function.asm N byte offset → (N / 10h):(N % 10h)
function: MOV CX,20
up: MUL CX ; AX ← AX * CX
N % 10h bytes
LOOP up ; CX ← CX – 1
; if CX > 0 IP ← up Physical Offset = N bytes N / 10h paragraphs
; internal reference
; resolved by assembler
Logical Offset = (N / 10h):(N % 10h)
RET
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

35 Assembly Language Programming Microprocessors 36 Assembly Language Programming Microprocessors

DOS Program in Main Memory The Program Segment Prefix (PSP)


Store
Offset Size Contents
0000h 2 bytes int 20h (old style program terminate command)
0005h 5 bytes Far call to DOS function handler
loaded 000Ah 4 bytes Previous termination handler interrupt vector (int 22h)
from User Program 000Eh 4 bytes Previous contents of ctrl-C interrupt vector (int 23h)
disk 0012h 4 bytes Previous critical error handler interrupt vector (int 24h)
start of loaded user program
start_segment:0000 002Ch 2 bytes Segment address of the program's environment block
100h bytes Program Segment Prefix Argument length
0080h 1 byte
inserted by DOS (PSP) Number of characters — including spaces — following program name
start of user memory 0081h 127 bytes Argument (command tail)
DS:0000
DOS System Kernel Command tail
start of RAM All characters (including spaces) after file name
0000:0000
User memory Example
format a: /q
Begins at DS:0000 — DOS always sets DS to point at PSP
command tail = " a: /q"
DS paragraphs = DS × 10h bytes from start of RAM command tail contains 6 characters
User program (load module)
Load command tail terminated by character 0x0d
Begins at start_segment:0000 = DS:0100 (PSP = 100h bytes)
Disk Transfer Area (DTA)
DS:0100 → DS × 10h + 0100 = (DS + 10) × 10h + 0000 DOS functions use PSP:0080 — PSP:00FF as write buffer
start_segment = DS + 10 ⇒ DS = start_segment - 10 Must save Command Tail before overwriting
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
37 Assembly Language Programming Microprocessors 38 Assembly Language Programming Microprocessors

Loading Program Arguments DOS *.com Program


org 0x100
section .data more more
argv: times 127 db 0 ; stores command tail code code

load module
section .text
DS:EA
mov si, 0x81 ; SI <-- pointer to command tail data data

Increasing
Addresses
JMP
mov di, argv ; DI <-- pointer to command tail buffer and and
NEAR
L1: lodsb ; al <-- character from command tail stack stack SS:SP
cmp al, 0x20 ; ignore initial spaces (0x20 characters)
jne L2s code code
jmp L1 start_segment
PSP
L2: lodsb ; get file name character
L2s: cmp al, 0x0d ; end on 0x0d character DOS DS = CS = SS = ES
je L3 = start_segment - 10
stosb ; copy character to file name buffer File on Disk File in RAM
jmp L2
; Load Module
L3: Code+Data+Stack stored in *.com program file on disk
; user program
Copied to RAM at start_segment
Load (after 100h bytes of PSP)
;
end: MOV AH,4Ch ; exit Single 64 KB segment
INT 21h ; call DOS CS = DS = SS = ES
Program codes starts at IP = 0100 (after 100h bytes of PSP)
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

39 Assembly Language Programming Microprocessors 40 Assembly Language Programming Microprocessors

Template for *.com Program *.com Example ⎯ 1


ORG 0x100 ex1.asm
section .data ORG 0x100
; define initiated data section .data
var1 dw 0x1234 ; var1 labels address of data
section .bss
section .text
; define uninitiated data mov ax, ds
section .text add ax, 10h
mov cx, ax
; code instructions mov bx, ax
mov ax,4C00h mov ax, [var1] ; NASM keeps track of variable
mov ah,4ch
int 21h int 21h
ret

C:\nasm\programs\examples>nasm ex1.asm

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
41 Assembly Language Programming Microprocessors 42 Assembly Language Programming Microprocessors

*.com Example ⎯ 2 Labels and Variables


C:\nasm\programs\examples>debug ex1 NASM associates a string with an address
-u
0B0E:0100 8CD8 MOV AX,DS LABEL = pointer (start address) to line of code
0B0E:0102 051000 ADD AX,0010
0B0E:0105 89C1 MOV CX,AX VARIABLE = pointer (start address) to data declaration
0B0E:0107 89C3 MOV BX,AX
0B0E:0109 A11401 MOV AX,[0114] ; NASM "knows" pointer
0B0E:010C B44C MOV AH,4C Label arithmetic
0B0E:010E CD21 INT 21
0B0E:0110 C3 RET
MOV AX,[var1 + 2]
0B0E:0111 0000 ADD [BX+SI],AL AX ← word after value of variable var1
0B0E:0113 0034 ADD [SI],DH ; value of var1 at 0114
0B0E:0115 1246EA ADC AL,[BP-16] Label arithmetic performed at assembly time
0B0E:0118 8956EC
0B0E:011B 8B34
MOV
MOV
[BP-14],DX
SI,[SI]
Assembler
0B0E:011D 00FD ADD CH,BH Keeps track of addresses
0B0E:011F 0A8B46EA OR CL,[BP+DI+EA46]
-r Performs arithmetic
AX=0000 BX=0000 CX=0016 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 Uses numerical results in instructions
DS=0B0E ES=0B0E SS=0B0E CS=0B0E IP=0100 NV UP EI PL NZ NA PO NC
0B0E:0100 8CD8 MOV AX,DS Example: var1 labels a variable defined at DS:0006
MOV AX,[var1 + 2] coded as MOV AX,[0008]

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

43 Assembly Language Programming Microprocessors 44 Assembly Language Programming Microprocessors

EQU Example — 1 EQU Example — 2


org 0x100
C:\nasm\programs\examples>debug ex7.com
section .data
-r
message1 db 'hello, world'
AX=0000 BX=0000 CX=0033 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
message2 db '123456789' DS=0B60 ES=0B60 SS=0B60 CS=0B60 IP=0100 NV UP EI PL NZ NA PO NC
here equ $ -u
; $ = &('9')+1 0B60:0100 B81001 MOV AX,0110 ; pointer message1
; here = numerical constant <— value &('9')+1 0B60:0103 BB2501 MOV BX,0125 ; value of here = &('9')+1
msglen2 equ here-message2 0B60:0106 B90900 MOV CX,0009 ; string length = 125 – 11C = 9
; message2 = address of '1' = &('1') 0B60:0109 B8004C MOV AX,4C00
; msglen2 = &('9')+1 – &('1') 0B60:010C CD21 INT 21
0B60:010E 0000 ADD [BX+SI],AL
; = length of string message2
message3 db 'goodbye, world'
-d 100
section .text 0B60:0100 B8 10 01 BB 25 01 B9 09-00 B8 00 4C CD 21 00 00 ....%......L.!..
mov ax, message1 0B60:0110 68 65 6C 6C 6F 2C 20 77-6F 72 6C 64 31 32 33 34 hello, world1234
mov bx, here 0B60:0120 35 36 37 38 39 67 6F 6F-64 62 79 65 2C 20 77 6F 56789goodbye, wo
mov cx, msglen2 0B60:0130 72 6C 64 01 00 EB 0D 31-C0 EB 09 83 7E 08 10 75 rld....1....~..u
mov ax,4C00H
INT 21H
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
45 Assembly Language Programming Microprocessors 46 Assembly Language Programming Microprocessors

DOS .exe Program — 1 DOS .exe Program — 2


Stack Stack SS Stack

load module

load module
SS:SP
Code N_stack Code CS Code ISS:SP SS:SP
Increasing

Increasing
Addresses

Addresses
CS:IP
stored part stored part stored part stored part
N_code ICS:IP CS:IP
of Data of Data of DS of Data
Data start_segment Data
Segment Segment
header PSP header PSP
start_segment
DS:0000
DOS DOS
File on Disk File in RAM File on Disk File in RAM
Load Module Parameters from header used to set CS and SS
Data+Code+Stack stored in *.exe program file on disk ICS (initial code segment) = paragraphs to code segment from start of load module
Load
Copied to RAM at logical address start_segment:0000 (after 100h bytes of PSP) Load
DOS sets CS ← start_segment + ICS
Header = paragraphs to code segment from start of RAM
Parameters used to determine program addresses (ISS, SP, ICS, IP, others) ISS (initial stack segment) = paragraphs to stack segment from start of load module
N_code (physical byte offset) → ICS:IP (logical offset) DOS sets SS ← start_segment + ISS
N_stack (physical byte offset) → ISS:SP (logical offset) = paragraphs to stack segment from start of RAM
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

47 Assembly Language Programming Microprocessors 48 Assembly Language Programming Microprocessors

Simplest Segmentation Model Overlapping Segment Model — 1


Define non-overlapping segments for data, code, stack Each program section immediately follows previous section
Example Physical address = start address + size of sections
DS = 1000, CS = 2000, SS = 3000
Data in addresses DS:0000 — DS:FFFF → 10000 — 1FFFF
stack
Code in addresses CS:0000 — CS:FFFF → 20000 — 2FFFF
0300h bytes
Stack in addresses SS:0000 — SS:FFFF → 30000 — 3FFFF
10000h + 0108h + 0208h = 10310h
code
Very inefficient 0208h bytes
Full segment = 64 KB stack
10000h + 0108h = 10108h
Small program in 64 KB segment wastes space SS
data
Example 0108h bytes
code
DS = 1000 physical load address = 10000h
CS = 2000 CS start_segment = 10000h / 10h = 1000h
PSP
Data segment = 32 bytes
Addresses 10000 to 1001F data
DS
Wastes memory from 10020 to 1FFFF
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
49 Assembly Language Programming Microprocessors 50 Assembly Language Programming Microprocessors

Overlapping Segment Model — 2 Linked *.exe File


Physical addresses → logical addresses Assemble: main.asm → main.obj
Assemble: function.asm → function.obj
stack Link: main.obj + function.obj → program.exe
0300h bytes
Stack Logical Address SS:SP
10310h SS = int(10310 / 10h) = 1031
Offset (bytes) to Section
SP = 10310 % 10h = 0000 Section Size (bytes)
code from Start of Main
0208h bytes Code Logical Address CS:IP Stack b5 b1 + b2 + b3 + b4
10108h CS = int(10108 / 10h) = 1010
IP = 10108 % 10h = 0008 Padding b4 = 10h – (b1 + b2 + b3) % 10h
data Function Code b3 b1 + b2
0108h bytes
physical load address = 10000h Main Code b2 b1

PSP
start_segment = 1000h Main Data b1 0
100h bytes Header 200h
PSP Logical Address DS:0000
DS = int((10000h – 100h)/10h)
= 0FF0
Logical Address of data = DS:0100
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

51 Assembly Language Programming Microprocessors 52 Assembly Language Programming Microprocessors

Overlapping Segment Example — on Disk Overlapping Segment Example — RAM


DOS program in *.exe file on disk DOS program in main memory (RAM)
Physical load address PAstart determined by DOS
Section Contents Size Segment Pointer s_seg = start_segment = PAstart / 10h
ISS = (b1 + b2 + b3 + b4)/10
Stack Stored Stack Values b5 Section Contents Size in bytes Physical Address
SP = 0
SS = s_seg + ISS
Padding Zeros b4 = 10 – (b1 + b2 + b3) % 10 Stack Stack Values b5
SP = 0
function: MOV CX,20
Function up: MUL CX Fn_CS = (b1 + b2)/10 Padding Zeros b4
b3
Code LOOP up Fn_IP = (b1 + b2) % 10 function: MOV CX,20
RET Function up: MUL CX Fn_CS ← s_seg + Fn_CS
b3
main: MOV AX,BX Code LOOP up Fn_IP
Main ADD AX,[0012] ICS = b1 / 10 RET
b2
Code CALL Fn_CS:Fn_IP IP = b1 % 10 main: MOV AX,BX
end: RET
Main ADD AX,[0012]
b2
CS = s_seg + ICS
Data Stored Data b1 Code CALL Fn_CS:Fn_IP IP
end: RET
Stores
Data Stored Data b1 s_seg = PAstart / 10h
Header Program Header b0 ICS,IP,ISS,SP
Pointer for Fn_CS PSP DOS Data 100h DS = s_seg – 10h

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
53 Assembly Language Programming Microprocessors 54 Assembly Language Programming Microprocessors

Numerical Example — Linked *.exe Template for *.exe Program


Size section data
Section Contents Segment Pointer
(bytes) ; declare initialized data here
Stored Stack ISS = (0362 + E)/10 = 0037 section bss
Stack Values
1000
SP = 0000 ; declare uninitialized data here
10 – (0108 + 0052 + 0208)] % 10 section text
Padding Zeros 000E
= 10 – (0362 % 10) = E ..start: ; starting IP at next line
function: MOV CX,20 Fn_CS = (0108 + 0052)/10
MOV AX, data ; points DS at data segment
Function up: MUL CX
0208 = (015A)/10 = 0015
MOV DS, AX
Code LOOP up
RET Fn_IP = (015A) % 10 = 000A
;
main: MOV AX,BX
; put user code in this area
Main ADD AX,[0012]
0052
ICS = 0108 / 10 = 0010 ;
Code end:
CALL Fn_CS:Fn_IP
RET
IP = 0108 % 10 = 0008 MOV AX,4C00H
INT 21H
Data Stored Data 0108 s_seg = 1000 (load address 10000h)
section stack stack ; declares stack segment named stack
Prefix DOS Data — Prefix only in memory ; name is required for linker
DS ← 1000 – 10 = 0FF0 resb 2000h ; default 8 KB stack
At stacktop: ; points SP ay top of stack
Program Header CS ← 1000 + 0010 = 1010
Header 200 load Fn_CS ← 1000 + 0015 = 1015
(ICS,IP,ISS,SP)
time SS ← 1000 + 0037 = 1037

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

55 Assembly Language Programming Microprocessors 56 Assembly Language Programming Microprocessors

Section Declarations Default Code


Defines and separates named sections With Default Code Without Default Code
Usage depends on code type SS:SP
Unix, Linux, and DOS *.com files stacktop
SP STACK
Standard section names are required
SS:0000 SS:SP = SS:0000
.data, .bss, .text
Linked file begins with .text section (code)
Data placed after end of code
CODE
DOS *.exe files
Any section names are allowed ..start:
Sets IP = b % 10h
Section names have no special meaning CS:IP
CS:IP = CS:0000
NASM changes .data → data DATA CS = int(b / 10h)
Linked file contains named sections in order of their definition MOV AX,data (b bytes)
MOV DS, AX
Sections from separate sources DS stored section data
PSP
Combined if names are identical DS
Not combined if names are different
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
57 Assembly Language Programming Microprocessors 58 Assembly Language Programming Microprocessors

*.exe Example ⎯ 1 *.exe Example ⎯ 2


ex.asm
section data
var1 db "ABCDEFGH"
var2 dw 0x1234 C:\nasm\programs\examples>nasm -f obj ex.asm
var3 dw 0x0000
section text NASM creates file ex.obj
..start:
mov ax, data C:\nasm\programs\examples>link ex
mov DS, ax
Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994
mov di,var1 Copyright (C) Microsoft Corp 1984-1993. All rights reserved.
mov si,var2
call L1 Run File [ex.exe]:
mov ax,4c00h List File [nul.map]:
int 21h Libraries [.lib]:
Definitions File [nul.def]:
L1: lea bx,[di]
mov ax,[si] LINK.EXE creates file ex.exe
mov [var3],ax
ret
section stack stack
resb 64
stacktop:
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

59 Assembly Language Programming Microprocessors 60 Assembly Language Programming Microprocessors

*.exe Example ⎯ 3 Linking of Named Sections


C:\nasm\programs\examples>debug ex.exe section data section data_1
-r a1: dw 0x0011 a1: dw 0x0011
AX=0000 BX=0000 CX=0024 DX=0000 SP=0040 BP=0000 SI=0000 DI=0000 section text section text_1
DS=0B76 ES=0B76 SS=0B89 CS=0B86 IP=000C NV UP EI PL NZ NA PO NC ..start: ..start:
0B86:000C B8860B MOV AX,0B86 mov ax,data mov ax,data_1
-d ds:100 ... ...
0B76:0100 41 42 43 44 45 46 47 48-34 12 00 00 B8 86 0B 8E ABCDEFGH4....... call far Func call far Func
0B76:0110 D8 BF 00 00 BE 08 00 E8-05 00 B8 00 4C CD 21 8D ............L.!. mov ax,4c00h mov ax,4c00h
-u cs:0c l 20 int 21h int 21h
0B86:000C B8860B MOV AX,0B86 ; data = DS + 10 = 0B76 + 10 section data section data_2
0B86:000F 8ED8 MOV DS,AX a2: dw 0x0033 a2: dw 0x0033
0B86:0011 BF0000 MOV DI,0000 ; var1 section text section text_2
0B86:0014 BE0800 MOV SI,0008 ; var2 Func: mov ax,a1 Func: mov ax,data_2
0B86:0017 E80500 CALL 001F mov bx,a2 mov bx,a2
0B86:001A B8004C MOV AX,4C00 ret ret
0B86:001D CD21 INT 21
0B86:001F 8D1D LEA BX,[DI] -u
0000 1100 ADC [BX+SI],AX
0B86:0021 8B04 MOV AX,[SI] 0000 1100 ADC [BX+SI],AX
0002 B8740B MOV AX,0B74
0B86:0023 A30A00 MOV [000A],AX ; var3 0002 3300 XOR AX,[BX+SI]
0004 B8740B MOV AX,0B74 0005 9A0100750B CALL 0B75:0001
0B86:0026 C3 RET 000A B8004C MOV AX,4C00
0B86:0027 E80A18 CALL 1834 0007 9A1100740B CALL 0B74:0011
000D CD21 INT 21
0B86:002A EB18 JMP 0044 000C B8004C MOV AX,4C00
000F 3300 XOR AX,[BX+SI]
000F CD21 INT 21
0011 B80000 MOV AX,0000 0011 B8740B MOV AX,0B74
0014 BB0200 MOV BX,0002
0014 BB0F00 MOV BX,000F
0017 C3 RET
0017 C3 RET

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
61 Assembly Language Programming Microprocessors 62 Assembly Language Programming Microprocessors

Far Call — 1 Far Call ⎯ 2


section data
C:\nasm\programs\examples>debug ex5.exe
; global data section -r
; declare initialized variables here AX=0000 BX=0000 CX=0050 DX=0000 SP=0020 BP=0000 SI=0000 DI=0000
stdout dw 0 DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000 NV UP EI PL NZ NA PO NC
str_1 db "hello world",0dh,0ah,0 -
section bss 0B70:0000 B86F0B MOV AX,0B6F
; declare uninitialized variables here 0B70:0003 8ED8 MOV DS,AX
section text 0B70:0005 BE0200 MOV SI,0002
..start: 0B70:0008 9A0200710B CALL 0B71:0002 → PA = 0B712 → 0B70:0012
MOV AX, data section text 0B70:000D B8004C MOV AX,4C00
0B70:0010 CD21 INT 21
MOV DS, AX print_scr: 0B70:0012 B402 MOV AH,02
mov si, str_1 mov ah,02h 0B70:0014 AC LODSB
call far print_scr next: lodsb 0B70:0015 3C00 CMP AL,00
MOV AX,4C00H cmp al,0 0B70:0017 7501 JNZ 001A
INT 21H jnz cout 0B70:0019 CB RETF
0B70:001A 88C2 MOV DL,AL
retf 0B70:001C CD21 INT 21
mov dl,alcout: 0B70:001E EBF4 JMP 0014
int 21h 0B70:0020 0000 ADD [BX+SI],AL
jmp next 0B70:0022 0000 ADD [BX+SI],AL
section stack stack
0B5F:0100 00 00 68 65 6C 6C 6F 20-77 6F 72 6C 64 0D 0A 00 ..hello world...
times 32 db 0
0B5F:0110 B8 6F 0B 8E D8 BE 02 00-9A 02 00 71 0B B8 00 4C .o.........q...L
C:\nasm\programs\examples>nasm -f obj ex5.asm stacktop:
C:\nasm\programs\examples>link ex5
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

63 Assembly Language Programming Microprocessors 64 Assembly Language Programming Microprocessors

Separate Modules — 1 Separate Modules — 2


ex6a.asm

extern print_scr C:\nasm\programs\examples>nasm -f obj ex6a.asm

section data C:\nasm\programs\examples>nasm -f obj ex6b.asm


stdout dw 0
C:\nasm\programs\examples>link ex6a ex6b
str_1 db "hello world",0dh,0ah,0 ex6b.asm
section bss Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994
section text section text Copyright (C) Microsoft Corp 1984-1993. All rights reserved.
..start:
MOV AX, data global print_scr Run File [ex6a.exe]: ex6.exe
MOV DS, AX List File [nul.map]:
mov si, str_1 print_scr: Libraries [.lib]:
call far print_scr mov ah,02h Definitions File [nul.def]:
MOV AX,4C00H next: lodsb
INT 21H cmp al,0
jnz cout
retf
cout: mov dl,al
int 21h
jmp next
section stack stack
resb 0020H
stacktop:

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
65 Assembly Language Programming Microprocessors 66 Assembly Language Programming Microprocessors

Separate Modules — 3 ex6.exe File Contents


C:\nasm\programs\examples>debug ex6.exe C:\nasm\programs\examples>dump ex6.exe
-r
AX=0000 BX=0000 CX=0030 DX=0000 SP=0020 BP=0000 SI=0000 DI=0000 0000 4D 5A 50 00 02 00 02 00 20 00 00 00 FF FF 03 00
DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000 NV UP EI PL NZ NA PO NC
- 0010 20 00 00 00 00 00 01 00 1E 00 00 00 01 00 01 00
0B70:0000 B86F0B MOV AX,0B6F ; DS ← data = start_seg = PSP + 10h 0020 01 00 0B 00 01 00 00 00 00 00 00 00 00 00 00 00
0B70:0003 8ED8 MOV DS,AX 0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:0005 BE0200 MOV SI,0002 ...
0B70:0008 9A0200710B CALL 0B71:0002 → PA = 0B712 → 0B70:0012 01A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Header
0B70:000D B8004C MOV AX,4C00
0B70:0010 CD21 INT 21 01B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:0012 B402 MOV AH,02 01C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:0014 AC LODSB 01D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:0015 3C00 CMP AL,00 01E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:0017 7501 JNZ 001A
0B70:0019 CB RETF 01F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0B70:001A 88C2 MOV DL,AL
0B70:001C CD21 INT 21
0200 00 00 68 65 6C 6C 6F 20 77 6F 72 6C 64 0D 0A 00 Data
0B70:001E EBF4 JMP 0014
-u 0210 B8 00 00 8E D8 BE 02 00 9A 02 00 02 00 B8 00 4C Text
0B70:0020 0000 ADD [BX+SI],AL 0220 CD 21 B4 02 AC 3C 00 75 01 CB 88 C2 CD 21 EB F4 ICS = 1h
0B70:0022 0000 ADD [BX+SI],AL
0230 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Stack
0B5F:0100 00 00 68 65 6C 6C 6F 20-77 6F 72 6C 64 0D 0A 00 ..hello world...
0B5F:0110 B8 6F 0B 8E D8 BE 02 00-9A 02 00 71 0B B8 00 4C .o.........q...L 0240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ISS = 3h
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

67 Assembly Language Programming Microprocessors 68 Assembly Language Programming Microprocessors

ex6.exe Disassembly *.exe Program Relocation ⎯ 1


00000010 B80000 mov ax,0x0 ; start_seg = 0 Offset registers set at load time
00000013 8ED8 mov ds,ax SP ← value stored in header
00000015 BE0200 mov si,0x2 IP ← value stored in header
00000018 9A02000200 call 0x2:0x2 ; FnCS = 0x2
Segment registers set at load time
0000001D B8004C mov ax,0x4c00
ES = DS ← start_segment – 10 (point at PSP)
00000020 CD21 int 0x21
CS ← start_segment + ICS
00000022 B402 mov ah,0x2
00000024 AC lodsb SS ← start_segment + ISS
00000025 3C00 cmp al,0x0 Relocation items: FAR BRANCH addresses adjusted
00000027 7501 jnz 0x1a Target for branch instruction stored in data, code, or stack section
00000029 CB retf Example
0000002A 88C2 mov dl,al CALL Fn_CS:Fn_IP
0000002C CD21 int 0x21 Stored value Fn_CS = paragraphs to Fn from start of load module
0000002E EBF4 jmp short 0x14 Fn_CS ← start_segment + Fn_CS
00000030 0000 add [bx+si],al Adjusted Fn_CS = paragraphs to Fn from start of RAM
00000032 0000 add [bx+si],al
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
69 Assembly Language Programming Microprocessors 70 Assembly Language Programming Microprocessors

*.exe Program Relocation ⎯ 2 *.exe Program Header


Offset Contents
Stack Stack
4D 5A h — signature for a valid *.exe file.
00h - 01h ASCII code for M and Z
Far IFn_CS = 0002 Far Mark Zbikowski was a designer of DOS at Microsoft
Code Fn_CS = s_seg + IFn_CS Code
Load
IP = 0002
= 0B6F + 0002
02h - 03h (Size of file including header) % (512)
Module = 0B71 04h - 05h Int [(Size of file including header) / (512)] + 1
Default CALL 0002:0002 Default 06h - 07h Number of relocation table items following header
Code Code 08h - 09h Size of header in 16 byte increments (paragraphs)
Segment Segment
00022 00022 0Ah - 0Bh Minimum number of 16 byte paragraphs required by program
bytes bytes
0Ch - 0Dh Maximum number of 16 byte paragraphs required
Default Default
Data Data 0Eh - 0Fh ISS — paragraphs from start of load module to stack segment
Segment Segment 10h - 11h Starting SP
s_seg = 0B6F 12h - 13h Word Checksum - negative sum of words in file ignoring overflow
Header DOS+PSP
start of 14h - 15h Starting IP
memory ICS — paragraphs from start of load module to code segment
Program File Program in RAM 16h - 17h
FAR segment addresses in *.exe file — relative to start of load module 18h - 19h Displacement (bytes) of first relocation vector in file
1Ah - 1Bh Overlay number (0 for main resident part of program)
At load time, Relocation Items adjusted
FAR SEG ← start segment + FAR SEG
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

71 Assembly Language Programming Microprocessors 72 Assembly Language Programming Microprocessors

Program Header Program Header Example ⎯ Relocation


Top of Header
0000 4D 5A 50 00 02 00 02 00 20 00 00 00 FF FF 03 00 *MZ...... .......* Segment registers
0010 20 00 00 00 00 00 01 00 1E 00 00 00 01 00 01 00 *..{)J...........* C:\nasm\programs\examples>debug ex6.exe
0020 01 00 0B 00 01 00 00 00 00 00 00 00 00 00 00 00 *......U.......j.*
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 *................* -r
AX=0000 BX=0000 CX=0030 DX=0000 SP=0020 BP=0000 SI=0000 DI=0000
Offset Value Contents DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000 NV UP EI PL NZ NA PO NC
00h - 01h 4D 5A 4D 5A h
02h - 03h 50 00 Length of image mod 512 = 0050h start_segment = 0B5F + 10 = 0B6F
Size of file in 512 byte pages (including remainder) = 0002h CS = 0B6F + 0001 = 0B70 SS = 0B6F + 0003 = 0B72
04h - 05h 02 00
(1 × 200 + 50 = 0250 = 59210 bytes)
06h - 07h 02 00 Number of relocation table items following header = 0002 Relocation vectors
08h - 09h 20 00 Size of the header in 16 byte increments (paragraphs) = 0020
Byte displacement 0001:0001 —> 0010 + 0001 = 0011
0Ah - 0Bh 00 00 Minimum number of 16 byte paragraphs required by program = 008B
in load module 0001:000B —> 0010 + 000B = 001B
0Ch - 0Dh FF FF Maximum number of 16 byte paragraphs required
001B
0Eh - 0Fh 03 00 ISS = 0003
Load module 0011
10h - 11h 20 00 SP = 0020
in file 0010 B8 00 00 8E D8 BE 02 00 9A 02 00 02 00 B8 00 4C
12h - 13h 00 00 Word Checksum – minus (sum of all words in file modulo 10000h)
ex6.exe
14h - 15h 00 00 IP = 0000
16h - 17h 01 00 ICS = 0001
Load module
18h - 19h 1E 00 Displacement in bytes of first relocation vector in file = 001E 0BA6:0010 B8 6F 0B 8E D8 BE 02 00 9A 02 00 71 0B B8 00 4C
in memory
Relocation Vectors Relocation 0B6F + 0000 = 0B6F 0B6F + 0002 = 0B71
0001:0001 —> 0010 + 0001 = 0011 0001:000B —> 0010 + 000B = 001B

Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land

Potrebbero piacerti anche