You are on page 1of 26

8086 uses segmented memory.

A memory address on the 8086 consists of two numbers the segment and the offset. This combination of segment and offset is referred to as a logical address. The segment number refers to a 64 KB block of memory and the offset is an index into the memory segment. e.g. the address AB10:1024 corresponds to the byte stored in segment 0xAB10 at offset 0x1024.

How to access the Memory Addressing Modes


Direct or Offset Addressing mov ax,[number] Register Indirect mov ax, [bx] Register Indirect + Offset mov ax,[bx+number] ;base + offset mov ax,[si+number] ;index + offset Base + Index mov ax,[bx+si] Base + Index + Offset mov ax, [bx+si+number]

Summary of Addressing Modes to Access the Memory


mov ax, [number] mov ax, [bx] mov ax, [si] mov ax, [bx+ number] mov ax, [si+ number] mov ax, [bx+si] mov ax, [bx+si+ number] ; (o) - Offset ; (b) - Base ; (i) - Index ;b+o ;i+o ;b+i ;b+i+o

Segment Override Prefix


Instruction mov ax,[cs:bx] mov ax,[es:bx] mov ax,[ss:bx] mov ax,[bx] Opcode 2E9B07 268B07 368B07 8B07

Summary of Addressing Modes to Access the Memory


Offset Addressing ; Default segment = ds mov ax, [0x1234] mov ah, [0x1234] mov byte[0x1234], 10 mov word[0x1234], 10

; word move ; byte move

Summary of Addressing Modes to Access the Memory


Base Addressing
; Default segment = ds mov ax, [bx] mov byte [bx], 10 mov word [bx], 10 ; Default segment = ss mov ax, [bp] mov byte [bp], 10 mov word [bp], 10

Summary of Addressing Modes to Access the Memory


Index Addressing ; Default segment = ds mov ax, [si] mov byte [di], 10 mov word [si], 10

Summary of Addressing Modes to Access the Memory


Base + Offset Addressing ; Default segment = ds mov ax, [bx+0x0100] mov byte [bx+0x100], 10 mov word [bx+0x100], 10 ; Default segment = ss mov byte [bp+0x10], 10 mov word [bp+0x100], 10

Summary of Addressing Modes to Access the Memory


Index + Offset Addressing ; Default segment = ds mov ax, [si+0x0100] mov byte [di+0x100], 10 mov word [di+0x100], 10 mov byte [si+0x10], 10 mov word [si+0x0100], 10

Summary of Addressing Modes to Access the Memory


Base + Index Addressing ; Default segment = ds mov ax, [bx+si] mov byte [bx+si], 10 mov word [bx+di], 10 ; Default segment = ss mov byte [bp+si], 10 mov word [bp+di], 10

Summary of Addressing Modes to Access the Memory


Base + Index + Offset Addressing ; Default segment = ds mov ax, [bx+si+0x0100] mov byte [bx+si+0x0100], 10 mov word [bx+di+0x0100], 10 ; Default segment = ss mov byte [bp+si+0x0100], 10 mov word [bp+di+0x0100], 10

Summary of Addressing Modes to Access the Memory

General Form [base + index + offset]

Illegal Addressing
mov al, [bl] ; Address cannot be 8-bit ; has to be 16-bit ; Registers cannot be ; subtracted ; Two bases cannot be ; used in one addressing ; Two indices cannot be ; used in one addressing

mov

ax, [bx - si]

mov

ax, [bx + bp]

mov

ax, [si + di]

Physical Address Calculation


[cs:bx + si+ 0x0700]

BX = 0x0100 SI = 0x0200 CS = 0x1000

Effective Address = EA = Base + Index + Offset EA = 0x0100 + 0x0200 + 0x0700 = 0x0A00 Physical Address = Segment * 0x10 + EA = 0x1000 * 0x10 + 0x0A00 = 0x10A00

Physical Address Calculation


[bx+0x7000]

BX = 0x9100 DS = 0x1500

Effective Address = An effective address is any operand to an instruction which references memory. EA = Base + Index + Offset = 0x9100 + 0x0000 + 0x07000 = 0x10100 ; 17-bits ! = 0x0100 ; Segment wrap ; around

Physical Address Calculation


[bx + 0x7000] Effective Address = EA = 0x0100 BX = 0x9100 DS = 0x1500 ; Segment wrap around

Physical Address = Segment * 0x10+EA = 0x1500 * 0x10+0x100 = 0x15100

Physical Address Calculation


[bx+0x0100] BX = 0x0100 DS = 0xFFF0

Effective Address = EA = Base + Index + Offset = 0x0100 + 0x0000 + 0x0100 = 0x0200 Physical Address = Segment*0x10+EA = 0xFFF0*0x10+0x0200 = 0x100100 ; 21-bits ! = 0x00100 ; memory wrap ; around

CMP
 Subtracts the source (src) from destination (dest)  Does not change contents of src or dest.  Affects AF,CF,OF,PF,SF and ZF.  The relation is of destination to source.

Conditional Jumps

jz Jnz

;jump if zero ;jump if not zero

[ZF=1] [ZF=0]

Example cmp ax, bx jz label1

Conditional Jumps
je Jne ;jump if equal [same as jz] ;jump if not equal [same as jnz]

Conditional Jumps
jc Jnc ;jump if carry [CF=1] ;jump if not carry [CF=0]

Example add ax, bx jc label1 sub ax, bx jnc label1

Conditional Jumps
ja jb ;jump if above ;jump if below unsigned integers [ZF = 0 and CF=0] [CF=1]

jl jg

;jump if less ;jump if greater signed integers

[SF <> OF=0] [ZF = 0 and SF=OF]

Conditional Jumps
jae ;jump if above or equal jbe ;jump if below or equal jge ;jump if greater or equal jle ;jump if less or equal jno ;Jump if not overflow jns ; Jump if not sign

Renamed Conditional Jumps


JNBE JNB JNAE JNA JZ JNLE JNL JNGE JNG JNZ JPO JPE JA JAE JB JBE JE JG JGE JL JLE JNE JNP JP

Conditional Jumps
jcxz ;jump if the cx register is zero [cx=0]

[org 0x0100] jmp start data: swap: start: loop1: dw 60, 55, 45, 50, 40, 35, 25, 30, 10, 0 db 0 mov bx, 0 mov byte [swap], 0 mov ax, [data+bx] cmp ax, [data+bx+2] jbe noswap mov dx, [data+bx+2] mov [data+bx+2], ax mov [data+bx], dx mov byte [swap], 1 add bx, 2 cmp bx, 18 jne loop1 cmp byte [swap], 1 je start ; initialize array index to zero ; rest swap flag to no swaps ; load number in ax ; compare with next number ; no swap if already in order ; load second element in dx ; store first number in second ; store second number in first ; flag that a swap has been done ; advance bx to next index ; are we at last index ; if not compare next two ; check if a swap has been done ; if yes make another pass

noswap:

mov ax, 0x4c00 int 0x21

You might also like