Assembly Language in 8086 Memory Architecture

1 Assembly Language Programming Microprocessors 2 Assembly Language Programming Assembly Language in 8086 Memory Architecture Spring 2011 3 Sho...
Author: Eileen Long
3 downloads 2 Views 151KB Size
1

Assembly Language Programming

Microprocessors

2

Assembly Language Programming

Assembly Language in 8086 Memory Architecture Spring 2011

3

Short Overview

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

4

Assembly Language Programs ⎯ 1

Dr. Martin Land

Assembly Language Programming

Microprocessors

Assembly language converted to machine language 89D8 01C8 2B873412 50 B441 CD21

BX CX [BX+1234] 41

MOV ADD SUB PUSH MOV INT

AX,BX AX,CX AX,[BX+1234] AX AH,41 21

Machine language (*.com) version of program Machine code = string of hexadecimal codes

Running this program requires CPU access to memory: 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

Assembly Language Programs ⎯ 2

Assembly language program List of 8086 instructions + data CPU executes — one by one — instructions in order of listing Example: MOV AX, ADD AX, SUB AX, PUSH AX MOV AH, INT 21

Microprocessors

Hadassah College

89D801C82B87341250B441CD21

Dr. Martin Land

Spring 2011

Hadassah College

Dr. Martin Land

5

Assembly Language Programming

Microprocessors

6

Assembly Language Programming

Assembly Language Programs ⎯ 3 OS loads program to 20-bit physical address OS loads registers

Stack Segment

SS register ← Stack base address / 10h Copy stack to memory from address SS:SP

IP register ← Code base address % 10h Copy code to memory from address CS:IP 89D801C82B87341250B441CD21

ES = DS register ← Data base address / 10h

Data Segment

Copy data to memory from address DS:EA Spring 2011

7

Assembly Language Programs ⎯ 4 Assign default values to general registers CX ← size of program (bytes) ← SS:SP

AX = BX = DX = SI = DI = BP ← 0 Higher Addresses

Code Segment

CS register ← Code base address / 10h

00 EF CD AB 21 CD 41 B4 50 12 34 87 2B C8 01 D8 89 12 34 56 78

Load initial values for program registers IP ← offset to first instruction in code segment SP ← offset to first location in stack segment Run program Fetch machine instructions from code segment (CS:IP) Access data and stack OS services (interventions) Loading program into memory

← CS:IP

Assigning initial values for registers in 8086 Adjusting pointers stored in program’s data segment

← DS:EA

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

8

AH BH CH DH

AL BL CL DL

Decoder and Control

AX BX CX ALU

DX

ALU

BP SP SI DI

Stack

ALU_OUT

Execution Unit (EU)

ALU_IN

Status Word System Bus

OFF

Instruction Pointer and Segment Registers

IP CS DS

SEG

Physical Address Unit (PAU)

PA

Address Bus Control (MAR)

SS ES

Logical Address = SEGMENT:OFFSET Physical Address = SEGMENT × 10h + OFFSET

Data Bus Control (MDR)

3 4 5 6

Instruction Queue

Bus Interface Unit (BIU)

Code

Decoder

20 bits Physical Address

Spring 2011

1 2

16 bits Control

Main Memory

Hadassah College

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Assembly Language Programs ⎯ 5

80186 Architecture General Registers

Microprocessors

Data

Data

00 EF CD AB 21 CD 41 B4 50 12 34 87 2B C8 01 D8 89 12 34 56 78

Dr. Martin Land

DOS program files Stored on disk or other long term storage Contains HEADER (only in *.exe files): Information for operating system LOAD MODULE (in *.exe, *.com, *.sys files): Data , Code, Stack part of program Loading and Running OS copies load module into memory OS initializes registers OS adjusts stored pointers (if file has header) OS points CS:IP at program start instruction Spring 2011

Hadassah College

Dr. Martin Land

9

Assembly Language Programming

Microprocessors

10

Assembly Language Programming

Assembly Language Programs ⎯ 6

Assembly Language Programs ⎯ 7

Data locations Transfer and ALU instructions refer (default) to data segment (DS) Execute SUB AX, [BX+1234] Calculate Effective Address (EA) BX+1234 Load operand from data segment address DS:BX+1234 Perform AX ← AX - [DS:BX+1234] Program must know where data is stored Code locations Instructions stored in memory in order of program list OS sets IP to first instruction in CS CPU updates IP after loading an instruction IP ← address of next instruction Program only needs to know CS:IP for branch instructions Spring 2011

11

Branch instructions A simple JUMP (skip over SUB and PUSH) looks like: MOV AX, BX ADD AX, CX JMP L1 SUB AX, [BX+1234] ; fall-through instruction PUSH AX L1: MOV AH, 41 ; target instruction INT 21 JMP assembly coded with pointer L1 to line MOV AH, 41h Pointer is address of target instruction JMP machine coded with displacement to line MOV AH, 41 Displacement is target address – fall-through address

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Assembly Language Programs ⎯ 8

Spring 2011

12

JMP instruction ASSEMBLY CODED with IP of target

Dr. Martin Land

Assembly Language Programming

Microprocessors

Stage

User Action

Detailed Actions

Top-down design

Plan

1. High level design 2. Organization into modules and functions

displacement = 4 + 1 = 5 Dr. Martin Land

1. Define local data structures 2. Write assembly language code for modules

Coding modules

Program

Assembly

Run Assembler

1. Convert assembly code to machine code 2. Resolve internal references

Linking

Run Linker

1. Resolve external address references for jump and call instructions 2. Build program header 3. Store executable program file on disk

Loading

Run Loader

1. Place program in memory 2. Set instruction pointer to program

JMP instruction MACHINE CODED with displacement

Hadassah College

Hadassah College

The Assembly Language Programming Process

Branch instructions Program listing with instruction lengths: bytes instruction 2 MOV AX, BX 2 ADD AX, CX 2 JMP L1 ; IP ← IP + 5 4 SUB AX, [BX+1234] ; fall-through 1 PUSH AX 2 L1: MOV AH, 41 ; target 2 INT 21

Spring 2011

Microprocessors

Spring 2011

Hadassah College

Dr. Martin Land

13

Assembly Language Programming

Microprocessors

14

Assembly Language Programming

Top-Down Design

Coding Program Modules Assign variables to memory locations VARIABLE NAME = SYMBOLIC LABEL for memory address List variables and addresses in scratchpad Used at assembly/compile time NOT part of program listing

Define requirements for program High Level Design Divide problem into modules Function — block of code to perform specific task 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

Graphical or other design technique Flowchart Pseudo-code Use cases UML etc

Coding

Spring 2011

15

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

16

Assembly by Hand ⎯ Coding Chart

Spring 2011

HEX Code

Label

Op-Code

Hadassah College

Operand

x ~ DS:0000 y ~ DS:0002 z ~ DS:0004

; scratchpad

MOV AX,[0000]

; AX ← x

ADD AX,[0002] MOV [0004],AX

; AX ← AX + y ; z ← AX

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Example of Assembly Coding Implement pseudocode example:

Enter instructions Write complete assembly program List op-code (mnemonic) and operands Write line labels when necessary Calculate Addresses Start with arbitrary start address Calculate HEX codes (machine instruction) for each line Branch instructions require knowing addresses

Address

Microprocessors

a = 3, b = -1, c = 0 while (a < 10){ b = a * 2 c = b + 7 a++ } end

Comments

Dr. Martin Land

Spring 2011

Hadassah College

Dr. Martin Land

17

Assembly Language Programming

Microprocessors

18

Assembly Language Programming

Assembly by Hand ⎯ Coding a = 3, b = -1, c = 0 while (a < 10){ b=a*2 c=b+7 a++ } Enter end address

HEX code

Machine Language Instruction Translate op-code and operands to HEX Code HEX Code format for each addressing mode Translation based on Intel op-code tables

op code, line labels, comments label

loop:

end:

op-code and operands MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

Spring 2011

19

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A end AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] loop

MOV WORD PTR [0000],0003

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

define a define b define c CMP a == 10 if a>= 10 then end AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to loop return to DOS

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

C70600000300

Spring 2011

20

Hand Assembly ⎯ Convert to Machine Code

HEX code C70600000300 C7060200FFFF C70604000000 833E00000A 7D__ A10000 D1E0 A30200 050700 A30400 FF060000 EB__ C3

label

loop:

end:

op-code and operands MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A end AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] loop

define a define b define c CMP a == 10 if a>10 then end AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to loop return to DOS

Hadassah College

Assembly Language Programming

Microprocessors

Count instruction length (bytes in each instruction) Next address = previous address + previous instruction length

address

HEX code

0000 0006 000C 0012

C70600000300 C7060200FFFF C70604000000

Branch codes incomplete: Target addresses unresolved (not calculated)

Spring 2011

Dr. Martin Land

Arbitrary start address Default IP = 0000 Relative to code segment base (CS)

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

Hadassah College

Address of Instruction in Memory

Enter hexadecimal code for each line address

Microprocessors

Dr. Martin Land

Spring 2011

label

op-code and operands MOV WORD [0000],3 MOV WORD [0002],FFFF MOV WORD [0004],0

comment ; define a ; define b ; define c

C7

06

02

00

FF

FF

0006

0007

0008

0009

000A

000B

Hadassah College

Dr. Martin Land

21

Assembly Language Programming

Microprocessors

22

Example of Assembly Coding ⎯ Unresolved address

HEX code

0000 0006 000C 0012 0017 0019 001C 001E 0021 0024 0027 002B 002D

C70600000300 C7060200FFFF C70604000000 833E00000A 7D__ A10000 D1E0 A30200 050700 A30400 FF060000 EB__ C3

label

loop:

end:

op-code and operands MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A end AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] loop

define a define b define c CMP a == 10 if a>=10 then 002D AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to 0012 return to DOS

Resolving branch references Line label = symbolic reference to instruction address Replace line labels in code with IP address of target line loop → 0012 end → 002D Spring 2011

23

HEX code

Dr. Martin Land

Assembly Language Programming

Microprocessors

0000 0006 000C 0012 0017 0019 001C 001E 0021 0024 0027 002B 002D

C70600000300 C7060200FFFF C70604000000 833E00000A 7D14 A10000 D1E0 A30200 050700 A30400 FF060000 EBE5 C3

loop:

end:

op-code and operands MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A 002D AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] 0012

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

HEX code

label

0000 0006 000C 0012 0017 0019 001C 001E 0021 0024 0027 002B 002D

C70600000300 C7060200FFFF C70604000000 833E00000A 7D__ A10000 D1E0 A30200 050700 A30400 FF060000 EB__ C3

op-code and operands

loop:

end:

MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A 002D AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] 0012

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

define a define b define c CMP a == 10 if a>=10 then 002D AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to 0012 return to DOS

002D – 0019 = 0014 → 14 (short) 0012 – 002D = FFE5 → E5 (short)

0017 JGE 002D: 002B JMP 0012:

Hadassah College

label

address

Branch machine coded with displacement displacement = (target address) – (fall-through address)

Example ⎯ Object Code with Resolved References address

Microprocessors

Example of Assembly Coding ⎯ Resolving Code

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

Assembly Language Programming

define a define b define c CMP a == 10 if a>=10 then 002D AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to 0012 return to DOS

Spring 2011

24

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Linking Program file File contents = Header + Load Module (data, code, stack) No header for *.com program (no linking)

Code section = string of executable machine instructions address

HEX code

0000 0006 000C 0012 0017 0019 001C 001E 0021 0024 0027 002B 002D

C70600000300 C7060200FFFF C70604000000 833E00000A 7D14 A10000 D1E0 A30200 050700 A30400 FF060000 EBE5 C3

label

loop:

end:

op-code and operands MOV MOV MOV CMP JGE MOV SHL MOV ADD MOV INC JMP RET

WORD [0000],3 WORD [0002],FFFF WORD [0004],0 WORD [0000],A 002D AX,[0000] AX,1 [0002],AX AX,7 [0004],AX WORD [0000] 0012

comment ; ; ; ; ; ; ; ; ; ; ; ; ;

define a define b define c CMP a == 10 if a>=10 then 002D AX ← a AX ← 2*AX b ← AX AX ← AX+7 c ← AX a ← a+1 back to 0012 return to DOS

link

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

String Processing Using Labels

Simple *.com Program 0100 0103 0106 0108 010B 010E 0110 0111 0110 0120

BE1101 B80000 0304 83C602 833C00 75F6 C3

MOV SI,0111 MOV AX,0 ADD AX,[SI] ADD SI,2 CMP WORD [SI],0 JNZ 0106 RET DW 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0

C3 01 00 02 00 03 00 04-00 05 00 06 00 07 00 08 00 09 00 0A 00 0B 00 0C-00 0D 00 0E 00 0F 00 00

AX = 0 → 1 → 3 → 6 → A → F → 15 → 1C → 24 → 2D → 37 → 42 → 4E → 5B → 69 → 78

Spring 2011

27

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

String Processing without Labels 0100 0102 0112 0122 0125 0129 012C 0130 0133 0134 0135 0136 0138 013A Spring 2011

0100 0102 0112 0122 0125 0129 012C 0130 0133 0134 0135 0136 0138 013A

S1: S2: L1:

L2:

JMP L1 db "Line of Text",0,0,0,0 db "New Line",0,0,0,0,0,0,0,0 MOV DI,0080 ; destination LEA SI,[S1] ; source_1 CALL L2 ; call copy LEA SI,[S2] ; source_2 CALL L2 ; call copy RET ; end of main routine LODSB ; AL ← [SI], SI++ STOSB ; [DI] ← AL, DI++ CMP AL,00 JNZ L2 RET

Spring 2011

28

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Arithmetic: Stored Data, Variables, and Pointers

JMP 0122 db "Line of Text",0,0,0,0 db "New Line",0,0,0,0,0,0,0,0 MOV DI,0080 ; destination LEA SI,[0102] ; source_1 CALL 0134 ; call copy LEA SI,[0112] ; source_2 CALL 0134 ; call copy RET ; end of main routine LODSB ; AL ← [SI], SI++ STOSB ; [DI] ← AL, DI++ CMP AL,00 JNZ 0134 RET Hadassah College

Microprocessors

Dr. Martin Land

0100 0104 0106 0108 010A 010D 0111 0112 0115 0117 011A 011C 011E 0120

Spring 2011

LEA JMP DW DW MOV LEA CWD MOV DIV ADD MOV LDS LEA ADD

SI,[0106] 010A 0000 0000 [SI+2],DS AX,[0132] BX,10 BX [SI+2],AX [SI],DX SI,[SI] BX,[SI] SI,4

0123 0124 0126 0129 012C 012F 0131 0132 0136 0146

Hadassah College

LODSW IMUL WORD [BX] MOV [BX+02],AX MOV [BX+04],DX CMP WORD [SI],0 JNZ 0123 RET DW 1122,0 DW 1,2,3,4,5,6,7,8 DW 9,a,b,c,d,e,f,0

Dr. Martin Land

29

Assembly Language Programming

Microprocessors

30

Assembly Language Programming

Assembly Programming — Matters of Style

Microprocessors

Program File Types DOS/Windows .com file = Load Module

Different methods are possible for: DOS *.exe and *.com programs Modular programming Segmentation Allocating memory for data Creating loops and control flow Defining function calls

Very simple programs Limited to one segment

CS = DS = SS = ES

No Program Header First instruction at IP = 0100 .exe file = DOS Header + Load Module Uses multiple segments and allows far branches OS provides value for DS Header contains initial values for CS:IP, SS:SP

Standard programming styles Assembler programming models

Header contains other OS parameters

Coding in native assembly language development environment

C language model

Unix/Linux (on PC)

Assembly coding according to C language requirements For combining assembly and C code into one program

ELF file = ELF Header + Load Module Use one only segment CS = DS = SS = ES Uses advanced 32-bit processor features

Spring 2011

31

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

32

Modular Programming (Main + Functions)

Spring 2011

Microprocessors

For programs compiled/assembled from separate source files Combines separate OBJECT CODE modules Resolves EXTERNAL REFERENCES between modules

prog.EXE

function.obj

localized *.exe file

Function Stack Function Code Function Data

Function Stack Function Code Function Data Main Stack Main Code Main Data Program Header

compile

main.OBJ

f1.ASM

assemble

f1.OBJ

f2.C

compile

f2.OBJ

main.obj

f_std.LIB

Main Stack Main Code Main Data

Hadassah College

Assembly Language Programming

Store executable program file on disk Required for DOS *.exe files and Linux executable files Place segments into standard order Create program header

main.C

load

Dr. Martin Land

Linking

Build program from separate source files (modules) Each source module edited in a separate file Compile or Assemble source files into separate object files Object code is machine code with symbolic external references Link object files together to create executable file Easier to design, read and understand programs Write most modules in high level language Write critical sections in assembly language Write, debug, and change modules independently link

Hadassah College

Dr. Martin Land

Spring 2011

unified *.exe file Unified Stack Function Code Main Code Unified Data Program Header Higher Addresses

Hadassah College

Dr. Martin Land

33

Assembly Language Programming

Microprocessors

34

Assembly Language Programming

Program Modules and Resolving References

Logical Offset — Paragraphs Logical address to physical address SEG:OFF → PA = SEG × 10h + OFF

Main module file — main.asm main:

Microprocessors

MOV AX,BX ADD AX,[0012] CALL function

Physical address to logical address PA → SEG:OFF = (PA / 10h):(PA % 10h)

; pass parameter in AX ; external reference ; resolved by linker

RET

Paragraph 10h bytes = smallest 8086 segment

Function module file — function.asm

Logical Offset N byte offset → (N / 10h):(N % 10h)

end:

function: up:

MOV CX,20 MUL CX LOOP up

; AX ← AX * CX ; CX ← CX – 1

N % 10h bytes

; if CX > 0 IP ← up ; internal reference ; resolved by assembler

Physical Offset = N bytes

Logical Offset = (N / 10h):(N % 10h)

RET Spring 2011

35

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

36

DOS Program in Main Memory

100h bytes inserted by DOS

User Program Program Segment Prefix (PSP)

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

The Program Segment Prefix (PSP)

Store

loaded from disk

N / 10h paragraphs

start of loaded user program start_segment:0000 start of user memory DS:0000

DOS System Kernel start of RAM 0000:0000

User memory Begins at DS:0000 — DOS always sets DS to point at PSP DS paragraphs = DS × 10h bytes from start of RAM

Offset 0000h 0005h 000Ah 000Eh 0012h 002Ch

Size 2 bytes 5 bytes 4 bytes 4 bytes 4 bytes 2 bytes

0080h

1 byte

0081h

127 bytes

Contents int 20h (old style program terminate command) Far call to DOS function handler Previous termination handler interrupt vector (int 22h) Previous contents of ctrl-C interrupt vector (int 23h) Previous critical error handler interrupt vector (int 24h) Segment address of the program's environment block Argument length Number of characters — including spaces — following program name Argument (command tail)

Command tail All characters (including spaces) after file name Example format a: /q command tail = " a: /q" command tail contains 6 characters command tail terminated by character 0x0d

Load User program (load module) Begins at start_segment:0000 = DS:0100 (PSP = 100h bytes) DS:0100 → DS × 10h + 0100 = (DS + 10) × 10h + 0000 start_segment = DS + 10 ⇒ DS = start_segment - 10

Disk Transfer Area (DTA) DOS functions use PSP:0080 — PSP:00FF as write buffer Must save Command Tail before overwriting

Spring 2011

Spring 2011

Hadassah College

Dr. Martin Land

Hadassah College

Dr. Martin Land

37

Assembly Language Programming

Microprocessors

38

Assembly Language Programming

Loading Program Arguments

39

more code

; stores command tail ; ; ; ;

SI nasm -f obj ex.asm NASM creates file ex.obj C:\nasm\programs\examples>link ex Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

5 1994

Run File [ex.exe]: List File [nul.map]: Libraries [.lib]: Definitions File [nul.def]:

L1:

lea bx,[di] mov ax,[si] mov [var3],ax ret section stack stack resb 64 stacktop: Spring 2011

59

LINK.EXE creates file ex.exe

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

60

*.exe Example ⎯ 3

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Linking of Named Sections

C:\nasm\programs\examples>debug ex.exe -r AX=0000 BX=0000 CX=0024 DX=0000 SP=0040 BP=0000 SI=0000 DI=0000 DS=0B76 ES=0B76 SS=0B89 CS=0B86 IP=000C NV UP EI PL NZ NA PO NC 0B86:000C B8860B MOV AX,0B86 -d ds:100 0B76:0100 41 42 43 44 45 46 47 48-34 12 00 00 B8 86 0B 8E ABCDEFGH4....... 0B76:0110 D8 BF 00 00 BE 08 00 E8-05 00 B8 00 4C CD 21 8D ............L.!. -u cs:0c l 20 0B86:000C B8860B MOV AX,0B86 ; data = DS + 10 = 0B76 + 10 0B86:000F 8ED8 MOV DS,AX 0B86:0011 BF0000 MOV DI,0000 ; var1 0B86:0014 BE0800 MOV SI,0008 ; var2 0B86:0017 E80500 CALL 001F 0B86:001A B8004C MOV AX,4C00 0B86:001D CD21 INT 21 0B86:001F 8D1D LEA BX,[DI] 0B86:0021 8B04 MOV AX,[SI] 0B86:0023 A30A00 MOV [000A],AX ; var3 0B86:0026 C3 RET 0B86:0027 E80A18 CALL 1834 0B86:002A EB18 JMP 0044

Spring 2011

Hadassah College

Dr. Martin Land

section data a1: dw 0x0011 section text ..start: mov ax,data ... call far Func mov ax,4c00h int 21h section data a2: dw 0x0033 section text Func: mov ax,a1 mov bx,a2 ret -u 0000 0002 0004 0007 000C 000F 0011 0014 0017 Spring 2011

1100 3300 B8740B 9A1100740B B8004C CD21 B80000 BB0200 C3

ADC [BX+SI],AX XOR AX,[BX+SI] MOV AX,0B74 CALL 0B74:0011 MOV AX,4C00 INT 21 MOV AX,0000 MOV BX,0002 RET

section data_1 a1: dw 0x0011 section text_1 ..start: mov ax,data_1 ... call far Func mov ax,4c00h int 21h section data_2 a2: dw 0x0033 section text_2 Func: mov ax,data_2 mov bx,a2 ret 0000 0002 0005 000A 000D 000F 0011 0014 0017 Hadassah College

1100 B8740B 9A0100750B B8004C CD21 3300 B8740B BB0F00 C3

ADC MOV CALL MOV INT XOR MOV MOV RET

[BX+SI],AX AX,0B74 0B75:0001 AX,4C00 21 AX,[BX+SI] AX,0B74 BX,000F

Dr. Martin Land

61

Assembly Language Programming

Microprocessors

62

Assembly Language Programming

Microprocessors

Far Call ⎯ 2

Far Call — 1 section data ; global data section ; declare initialized variables here stdout dw 0 str_1 db "hello world",0dh,0ah,0 section bss ; declare uninitialized variables here section text ..start: MOV AX, data section text MOV DS, AX print_scr: mov si, str_1 mov ah,02h call far print_scr next: lodsb MOV AX,4C00H cmp al,0 INT 21H jnz cout

retf mov dl,al int 21h jmp next section stack stack times 32 db 0 stacktop: C:\nasm\programs\examples>nasm -f obj ex5.asm cout:

C:\nasm\programs\examples>debug ex5.exe -r AX=0000 BX=0000 CX=0050 DX=0000 SP=0020 BP=0000 SI=0000 DI=0000 DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000 NV UP EI PL NZ NA PO NC 0B70:0000 B86F0B MOV AX,0B6F 0B70:0003 8ED8 MOV DS,AX 0B70:0005 BE0200 MOV SI,0002 0B70:0008 9A0200710B CALL 0B71:0002 → PA = 0B712 → 0B70:0012 0B70:000D B8004C MOV AX,4C00 0B70:0010 CD21 INT 21 0B70:0012 B402 MOV AH,02 0B70:0014 AC LODSB 0B70:0015 3C00 CMP AL,00 0B70:0017 7501 JNZ 001A 0B70:0019 CB RETF 0B70:001A 88C2 MOV DL,AL 0B70:001C CD21 INT 21 0B70:001E EBF4 JMP 0014 0B70:0020 0000 ADD [BX+SI],AL 0B70:0022 0000 ADD [BX+SI],AL 0B5F:0100 0B5F:0110

00 00 68 65 6C 6C 6F 20-77 6F 72 6C 64 0D 0A 00 B8 6F 0B 8E D8 BE 02 00-9A 02 00 71 0B B8 00 4C

..hello world... .o.........q...L

C:\nasm\programs\examples>link ex5 Spring 2011

63

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Spring 2011

64

Separate Modules — 1

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Separate Modules — 2

ex6a.asm extern print_scr

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

section data stdout dw 0 str_1 db "hello world",0dh,0ah,0 section bss section text ..start: MOV AX, data MOV DS, AX mov si, str_1 call far print_scr MOV AX,4C00H INT 21H

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

Spring 2011

Hadassah College

C:\nasm\programs\examples>link ex6a ex6b

ex6b.asm

Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

section text global print_scr print_scr: mov ah,02h next: lodsb cmp al,0 jnz cout retf cout: mov dl,al int 21h jmp next section stack stack resb 0020H stacktop: Dr. Martin Land

5 1994

Run File [ex6a.exe]: ex6.exe List File [nul.map]: Libraries [.lib]: Definitions File [nul.def]:

Spring 2011

Hadassah College

Dr. Martin Land

65

Assembly Language Programming

Microprocessors

66

Assembly Language Programming

Separate Modules — 3

ex6.exe File Contents

C:\nasm\programs\examples>debug ex6.exe -r AX=0000 BX=0000 CX=0030 DX=0000 SP=0020 BP=0000 SI=0000 DI=0000 DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000 NV UP EI PL NZ NA PO NC 0B70:0000 B86F0B MOV AX,0B6F ; DS ← data = start_seg = PSP + 10h 0B70:0003 8ED8 MOV DS,AX 0B70:0005 BE0200 MOV SI,0002 0B70:0008 9A0200710B CALL 0B71:0002 → PA = 0B712 → 0B70:0012 0B70:000D B8004C MOV AX,4C00 0B70:0010 CD21 INT 21 0B70:0012 B402 MOV AH,02 0B70:0014 AC LODSB 0B70:0015 3C00 CMP AL,00 0B70:0017 7501 JNZ 001A 0B70:0019 CB RETF 0B70:001A 88C2 MOV DL,AL 0B70:001C CD21 INT 21 0B70:001E EBF4 JMP 0014 -u 0B70:0020 0000 ADD [BX+SI],AL 0B70:0022 0000 ADD [BX+SI],AL 0B5F:0100 0B5F:0110

00 00 68 65 6C 6C 6F 20-77 6F 72 6C 64 0D 0A 00 B8 6F 0B 8E D8 BE 02 00-9A 02 00 71 0B B8 00 4C

Spring 2011

67

..hello world... .o.........q...L

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

C:\nasm\programs\examples>dump ex6.exe 0000 0010 0020 0030

4D 20 01 00

5A 00 00 00

50 00 0B 00

00 00 00 00

02 00 01 00

00 00 00 00

02 01 00 00

00 00 00 00

20 1E 00 00

01A0 01B0 01C0 01D0 01E0 01F0

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

0200

00 00 68 65 6C 6C 6F 20 77 6F 72 6C 64 0D 0A 00

Data

0210 0220

B8 00 00 8E D8 BE 02 00 9A 02 00 02 00 B8 00 4C CD 21 B4 02 AC 3C 00 75 01 CB 88 C2 CD 21 EB F4

Text ICS = 1h

0230 0240

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Stack ISS = 3h

Spring 2011

68

Spring 2011

B80000 8ED8 BE0200 9A02000200 B8004C CD21 B402 AC 3C00 7501 CB 88C2 CD21 EBF4 0000 0000

mov ax,0x0 ; start_seg = 0 mov ds,ax mov si,0x2 call 0x2:0x2 ; FnCS = 0x2 mov ax,0x4c00 int 0x21 mov ah,0x2 lodsb cmp al,0x0 jnz 0x1a retf mov dl,al int 0x21 jmp short 0x14 add [bx+si],al add [bx+si],al Hadassah College

00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00

FF 01 00 00

FF 00 00 00

03 01 00 00

00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

00 00 00 00 00 00

Header

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

*.exe Program Relocation ⎯ 1

ex6.exe Disassembly 00000010 00000013 00000015 00000018 0000001D 00000020 00000022 00000024 00000025 00000027 00000029 0000002A 0000002C 0000002E 00000030 00000032

Microprocessors

Dr. Martin Land

Offset registers set at load time SP ← value stored in header IP ← value stored in header Segment registers set at load time ES = DS ← start_segment – 10 (point at PSP) CS ← start_segment + ICS SS ← start_segment + ISS Relocation items: FAR BRANCH addresses adjusted Target for branch instruction stored in data, code, or stack section Example CALL Fn_CS:Fn_IP Stored value Fn_CS = paragraphs to Fn from start of load module Fn_CS ← start_segment + Fn_CS Adjusted Fn_CS = paragraphs to Fn from start of RAM Spring 2011

Hadassah College

Dr. Martin Land

69

Assembly Language Programming

Microprocessors

70

Assembly Language Programming

*.exe Program Relocation ⎯ 2

*.exe Program Header Offset

Stack

Stack

Contents 4D 5A h — signature for a valid *.exe file. ASCII code for M and Z Mark Zbikowski was a designer of DOS at Microsoft (Size of file including header) % (512) Int [(Size of file including header) / (512)] + 1 Number of relocation table items following header Size of header in 16 byte increments (paragraphs) Minimum number of 16 byte paragraphs required by program Maximum number of 16 byte paragraphs required ISS — paragraphs from start of load module to stack segment Starting SP

00h - 01h Far Code

IFn_CS = 0002 IP = 0002

Load Module Default Code Segment

Fn_CS = s_seg + IFn_CS = 0B6F + 0002 = 0B71

Far Code

02h - 03h 04h - 05h 06h - 07h 08h - 09h 0Ah - 0Bh 0Ch - 0Dh 0Eh - 0Fh 10h - 11h 12h - 13h 14h - 15h 16h - 17h 18h - 19h 1Ah - 1Bh

Default Code Segment

CALL 0002:0002 00022 bytes

00022 bytes

Default Data Segment

Default Data Segment s_seg = 0B6F

Header

DOS+PSP start of memory

Program File

Program in RAM

FAR segment addresses in *.exe file — relative to start of load module At load time, Relocation Items adjusted FAR SEG ← start segment + FAR SEG Spring 2011

71

0000 0010 0020 0030

4D 20 01 00

5A 00 00 00

50 00 0B 00

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

00 00 00 00

Offset 00h - 01h 02h - 03h

Value 4D 5A 50 00

04h - 05h

02 00

06h - 07h 08h - 09h 0Ah - 0Bh 0Ch - 0Dh 0Eh - 0Fh 10h - 11h 12h - 13h 14h - 15h 16h - 17h 18h - 19h

02 20 00 FF 03 20 00 00 01 1E

00 00 00 FF 00 00 00 00 00 00

02 00 01 00

00 00 00 00

02 01 00 00

00 00 00 00

20 1E 00 00

00 00 00 00

00 00 00 00

00 00 00 00

FF 00 00 00

Displacement (bytes) of first relocation vector in file Overlay number (0 for main resident part of program)

72

Hadassah College

Dr. Martin Land

Assembly Language Programming

Microprocessors

Program Header Example ⎯ Relocation 03 01 00 00

00 00 00 00

*MZ...... .......* *..{)J...........* *......U.......j.* *................*

Contents 4D 5A h Length of image mod 512 = 0050h Size of file in 512 byte pages (including remainder) = 0002h (1 × 200 + 50 = 0250 = 59210 bytes) Number of relocation table items following header = 0002 Size of the header in 16 byte increments (paragraphs) = 0020 Minimum number of 16 byte paragraphs required by program = 008B Maximum number of 16 byte paragraphs required ISS = 0003 SP = 0020 Word Checksum – minus (sum of all words in file modulo 10000h) IP = 0000 ICS = 0001 Displacement in bytes of first relocation vector in file = 001E

Relocation Vectors 0001:0001 —> 0010 + 0001 = 0011 Spring 2011

FF 01 00 00

Word Checksum - negative sum of words in file ignoring overflow Starting IP ICS — paragraphs from start of load module to code segment

Spring 2011

Program Header

Top of Header

Microprocessors

Segment registers C:\nasm\programs\examples>debug ex6.exe -r AX=0000 BX=0000 CX=0030 DX=0000 SP=0020 DS=0B5F ES=0B5F SS=0B72 CS=0B70 IP=0000

BP=0000 SI=0000 DI=0000 NV UP EI PL NZ NA PO NC

start_segment = 0B5F + 10 = 0B6F CS = 0B6F + 0001 = 0B70 SS = 0B6F + 0003 = 0B72

Relocation vectors Byte displacement in load module

0001:0001 —> 0010 + 0001 = 0011 0001:000B —> 0010 + 000B = 001B 001B

Load module in file ex6.exe

0010

B8 00 00 8E D8 BE 02 00 9A 02 00 02 00 B8 00 4C

Load module in memory

0BA6:0010

B8 6F 0B 8E D8 BE 02 00 9A 02 00 71 0B B8 00 4C

Relocation

0B6F + 0000 = 0B6F

0011

0B6F + 0002 = 0B71

0001:000B —> 0010 + 000B = 001B Hadassah College

Dr. Martin Land

Spring 2011

Hadassah College

Dr. Martin Land

Suggest Documents