You are on page 1of 49

Chapter 3

Instruction Sets and Programming


In this chapter:
•Programming Overview
•Program Development Steps
•Addressing Modes
•8086 Instruction Sets and
Programming
3.1 Programming Overview
• System software
– Allows one to develop appln programs for MP-based systems
– Includes:
• Editors –create and change source programs
• Compilers –translate high-level source code to machine code
• Assemblers –translate assembly into binary or obj code

• Interpreters – execute one statement at a time


• Debuggers – interactive executing & debugging
• OS – performs resource mgt and human-machine interaction
3.1 Programming Overview
• Language levels – there are three:
– I ) MACHINE LANGUAGE:
• Sequence of binary codes
• E.g. 1011 1000 B8
1001 0100 94
0000 0001 01
This is binary code for the 8086 instruction
MOV AX, 0194H
This is very difficult to understand
To make it easier, HEX representation is used
3.1 Programming Overview
– ii) ASSEMBLY LANGUAGE
• Uses two-, three- or four-letter mnemonics to represent each instruction type
• Mnemonics – abbreviations for instruction opcodes
• Special program called assembler is required
• Assembler – allows operator to input code program in mnemonic form
• Standard form consists of four fields:

LABEL FIELD OP CODE FIELD OPERAND FIELD COMMENT FIELD

NEXT: ADD AL, 07H ;ADD CORRECTION


FACTOR
3.1 Programming Overview
LABEL FIELD OP CODE FIELD OPERAND FIELD COMMENT FIELD

NEXT: ADD AL, 07H ;ADD CORRECTION


FACTOR
– Label: represents an address which is not specifically known at the time the statement is
written
– Opcode: mnemonic for the instruction to be performed
– Operand: name for data items to be acted up on by an instruction
– Comment: reminds the function that an instruction or group of instructions performs in
the program
– To convert assembly to machine:
• Manual: e.g. MOV AX, 0194 ⬄ B8 94 01 ⬄ BINARY
• Use assembler
3.1 Programming Overview

– iii) HIGH-LEVEL LANGUAGEs


• Use more english like statements, which may represent many machine code
instruction's
• Interpreters or compilers – convert these into machine codes
Program development steps

• Defining the problem


– Think about the problem that the program has to solve
– Write down operations in general terms
– E.g. temperature control problem
• Read temperature from sensor
• Add correction factor of +7
• Save result in a memory location
Program development steps
• Representing program operations
– Algorithm – the formula or sequence of operations used to
solve a programming problem
– Two common ways of representing algorithm:
• Flow charts: graphic representation of different program operations
– Symbols:

INPUT
PROCESS DECISION OUTPUT START

CONNECTO
SUB R
ROUTINE TERMINATION
Program development steps
• Example flowchart START

READ VALUE
FROM SENSOR

ADD 7

STORE RESULT
IN MEMORY

WAIT 1 HOUR

24
SAMPLES?

STOP
Program development steps
• Pseudocodes
– English-like statements used to represent the commonly used
structures:
• SEQUENCE, IF-THEN-ELSE, WHILE-DO
– Early days approach: structured programming – breaking problem into
independent modules (or reverse)
• E.g IF Temperature is less than 70 degrees THEN
Turn on heater
ELSE
Turn off heater
Program development steps
• Reading assignment:
– Standard Program Structures
• SEQUENCE
• IF – THEN
• IF – THEN – ELSE
• WHILE – DO
• REPEAT UNTIL
• CASE
Addressing Modes
• Addressing modes:
– The different ways in which a processor can access data that it
operates on
– Indicated in the instruction in assembly languages
– E.g. Move instruction format:
MOV destination, source
Source – could be a number, register or memory location
Destination – can be register or memory (1 of 24 ways)
NB: both source and destination can’t be memory locations in an instruction
Addressing Modes

• The 8086 addressing modes can be viewed in five groups:


– Register and immediate
– Memory
– I/O
– Relative
– implied
Addressing Modes
• Immediate Addressing Mode
– 8- or 16-bit data can be specified as part of the instruction
• E.g MOV CX, 437BH
– Puts immediate hex no 437B in the 16-bit CX register

• Immediate: because the no. to be loaded into CX will be put in two


memory locations immediately following the code for the move
instruction
Addressing Modes
• Register addressing mode
– Where a register is the source of operand for the instruction
• E.g. MOV CX, AX
– Copies contents of AX register into 16-bit CX register

– Any 16-bit register can be moved to any 16-bit register


• BUT register sizes must be the same
– E.g. MOV BX, CH is ILLEGAL!
Addressing Modes
– Memory addressing modes
• Used to specify location of an operand in memory
• 8086 provides 17 different ways to access memory
• To access data, Effective Address (EA) is added to segment base address
• EU calculates EA for an operand from info specified in the instruction
• The way EU calculates EA form various memory addressing modes
Memory Addressing Modes
• Direct addressing mode
– The simplest memory addressing mode
– EA is 16-bit no written directly in instruction
– E.g 1 MOV BL, [437AH]
• Copy contents of mem loc at a displacement from DS base of 437AH into BL
• Direct – displacement of operand from segment base is specified directly in
the instruction
Memory Addressing Modes
– E.g. 2
• MOV BX, [437AH]
• Copies 16-bit word from memory to BX register
• Byte at displacement 437AH from DS is copied into BL
• Byte at displacement 437BH from DS is copied into BH
• 8086 automatically identifies # of bytes to copy from the instruction coding
• 8086 stores low byte in lower addr & high byte in higher addr
– Direct addressing can also specify destination
• E.g. MOV 437AH, BX
Memory Addressing Modes
• Register indirect addressing mode:
– EA is specified in either a pointer or index register
• Pointers: BX & BP
• Index: SI & DI
– The 20-bit phy addr is computed using DS and EA
• E.g. MOV [DI], BX
– Copies content of BX into mem at offset DI from current DS
• E.g. If [DS] = 5004H, [DI] = 0020H and [BX] = 2456H,
• Then after MOV [DI], BX content of BX (2456H) is moved to memory locations
50060H and 50061H
Memory Addressing Modes

• Based addressing mode


– EA is obtained by adding a displacement to contents of BX or BP
– DS and SS – segment registers for BX and BP resp.
• E.g. MOV AL, START[BX] or, MOV AL, [BX+START]
• EA is obtained by adding value of START and [BX]
– Based addressing mode provides convenient way to address structure
which may be stored at different places in memory:
Memory Addressing Modes
• Indexed addressing mode:
– EA is calculated by adding displacement and contents of SI or DI
– E.g MOV BH, START[SI]
• Moves contents of 20-bit address computed from displacement START, SI
and DS into BH
– Indexed addressing can be used to access a single table, where
displacement is starting address of table
Memory Addressing Modes
• Based and indexed addressing mode
– EA is computed by adding base register (BX or BP), and index register
(SI or DI), and displacement

– E.g. MOV ALPHA[SI][BX], CL


• If [BX] = 0200H, ALPHA = 08H, [SI] = 1000H, & [DS] = 3000H, then 8-bit
content of CL is moved to 20-bit physical address 31208H
Memory Addressing Modes
• String addressing mode
– Uses index registers, string instructions assume:
– SI points to first byte/word of source string/operand

– DI points to first byte/word of destination operand


– Contents of SI & DI automatically incremented

– Segment register for source is DS (can be overridden)


– Segment register for destination is ES (cannot be overridden)
I/O Addressing Modes
– Standard I/O uses port addressing modes
– For memory mapped I/O, mem address modes used
– Two types of port addressing:
• Direct: port number is immediate 8-bit operand
– This allows fixed access to ports o to 255
– E.g. OUT 05H, AL - outputs [AL] to 8-bit port 05H
• Indirect: port number is taken from DX
– Allows 64K 8-bit or 32K 16-bit ports
– E.g. IN AL, DX where [DX] = 5040H
– Inputs the 8-bit content of port 5040
Relative & Implied Addressing

• Relative addressing mode


– Operand is specified as signed 8-bit displacement relative to PC
• E.g. JNC START

• Implied addressing mode


– Instructions using this mode have no operands
– E.g. CLC - clears carry flag to zero
8086 Instruction Set
3.4 8086 Instruction Set
– Instruction set lists various instructions available to a processor
– 8086 has approximately 117 instructions
– 8086 contains no, one or two operand inst.s
– Divided into six major groups:
• Data Transfer Instructions
• Arithmetic Instructions
• Bit Manipulation Instructions
• String Instructions
• Program Execution Transfer Instructions
• Processor Control Instructions
3.4 8086 Instruction Set
– DATA TRANSFER INSTRUCTIONS
• General purpose byte/word transfer instructions:
• MOV:
– Syntax: MOV destination, source
– Description: copy byte or word from specified source to specified destination
– Source: register, memory location, immediate number
– Destination: register, memory location
– Examples:
» MOV CX, 037AH ;put immediate number 037AH in CX
» MOV BL, [437AH] ;copy byte in DS at offset 437AH to BL
» MOV AX, BX ;copy contents of register BX to AX
» MOV DL, [BX] ;copy byte from memory at [BX] to DL
» ;BX contains offset of byte in DS
» MOV DS, BX ;copy word from BX to DS register
8086 Instruction Set
– Syntax: PUSH source
– Description: copy specified word to top of stack. Decrements stack (SP) by
two.
– Source: general purpose register, segment register, memory.
• SS and SP registers must be initialized before this instruction can be used.
• Flags affected: none
• Examples:
– PUSH, BX ;decrement SP by two, copy BX to stack
– PUSH DS ;decrement SP by two, copy DS to stack
– PUSH AL ;ILLEGAL, must push a word.
Instruction Set
– Syntax: POP destination
– Description: copy word from top of stack to specified location/destination
– Destination: general purpose register, segment register or memory.
Increments SP automatically by two
– Examples:
• POP DX ;Copy a word from top of stack to DX
• POP DS ;Copy a word from top of stack to DS,
;increment SP by two
Instruction Set
– Single I/O port transfer instructions
• mnemonic: IN, syntax: IN ACC, port
• Description: copy a byte or word from port to AC
• Examples:
– IN AL, 0C8 ;input a byte from port 0C8H to AL
– IN AX, 34H ;input a word from port 34H to AX

• Mnemonic: OUT, syntax: OUT port, [AL]/[AX]


• Description: copy byte or word from [AL]/[AX]
• Examples:
– OUT 3BH ;copy contents of AL to port 3BH
– OUT 2CH ;copy contents of AX to port 2CH
Instruction Set
– Special address transfer instructions
• Mnemonic: LEA, syntax: LEA register, source
• Description: load effective address of operand into specified register.
• Example
– LEA CX, [BX], [DI] ;load CX with EA = [BX] + [DI]
• Other instructions:
– LDS, LES
Instruction Set
– Flag transfer instructions
• Mnemonic: LAHF
• Description: load AH with low byte of flag.
• Example
– LAHF
– PUSH AX
• Others
– SAHF, PUSHF, POPF
Instruction Set
• ARITHMETIC INSTRUCTIONS
– Addition
• Mnemonics: ADD, syntax: ADD destination, source
• Description: add byte to byte or word to word
• Source: immediate no, register, memory location
• Destination: register, memory location
• Flags affected: AF, CF, OF, PF, SF, ZF
• Examples:
– ADD AL, 74H ;add immed no 74 to AL, result in AL
– ADC CL, BL ;add contents of BL +carry to cont. of CL
– ADD DX, BX ;add cont of BX to cont of DX
– ADD DX, [SI] ;add word from M at offset SI in DS to DX
• others: ADC, INC, AAA, DAA
Instruction Set
– Subtraction
• Mnemonic: SUB, syntax SUB destination, source
• Description: subtract byte from byte, word from word
• Flags affected: AF, CF, OF, DF, SF
• Examples:
– SUB CX, BX ;subtract BX from CX, result in CX
– SUB AX, 3427H ;subtract immed no 3427H from AX
– SUB BX, [3427H] ;subtract word at displacement 3427H in
;DS and contents of CF from BX
• Others:
– DEC, NEG, CMP, AAS, DAS
Instruction Set
– Comparison
• Mnemonic: CMP, syntax: CMP destination, source
• Description: compares byte/word from the specified source with byte in
destination
• Source: immed no, reg, mem loc
• Destination: reg, mem loc
• Flags: AF, OF, SF, ZF, PF, CF – updated
• Examples:
– CMP BL, 01H ;compare immed no 01H with byte in BL
– CMP CX, BX ;compare word in BX with word in CX
Instruction Set
– Multiplication
• Mnemonic: MUL, syntax: MUL source
• Description: multiply unsigned bytes from some source
times unsigned byte in AL (or word,…) result in AX
• Flags affected:
– CF, OF
– AF, PF, SF, [ZF – undefined]
• Examples:
– MUL BH ;AL times BH, result in AX
– MUL CX ;AX times CX, result: high word in DX, low in AX
Instruction Set
– Division
• Mnemonic: DIV, syntax: DIV source
• Description: divide unsigned word by a byte (or dword by word)
• Word – must be in AX, divisor – can be in reg or M
• After division: AL = 8-bit result (quotient), AH = 8-bit remainder
• Examples:
– DIV BL ;divide word in AX by byte in BL; quotient in AL,
;remainder in AH
Instruction Set
• BIT MANIPULATION INSTRUCTIONS
– Logical instructions
• Mnemonic: NOT, syntax: NOT destination
• Description: invert each bit of operand (forms 1’s comp)
• Destination: register, mem loc
• Flags affected: none
• Examples:
– NOT BX ;complement contents of BX reg
– NOT BYTE PTR[BX] ;complement mem byte at offset [BX]
Instruction Set
• Mnemonic: AND, syntax: AND destination, source
• Description: AND corresponding bits of two operands
• Source: immed no, contents of a reg, contents of M loc
• Destination: register or mem loc
• Flags affected: CF & OF – zero after AND;
PF, ZF, SF – updated; AF – undefined
• Examples:
– AND CX, [SI] ;AND word in DS at offset [SI] with word in
;CX register, result in CX register
– AND BH, CL ;AND byte in CL with byte in BH, res in BH
– AND BX, 00FFH ;AND word in BX with immediate 00FFH
;masks upper byte, leaves lower unchanged
Instruction Set
• Mnemonic: OR, syntax: OR destination, source
• Description: OR corresponding bits of operands
• Source: immediate no, register, memory
• Destination: register, memory loc
• Flags affected: CF & OF = 0 after OR; PF, SF, ZF – updated; AF – undefined
• Examples:
– OR AH, CL ;CL ORed with AH, result in AH, CL no chg
– OR SI, BP ;BP ORed with SI, result in SI, BP no chg
– OR BL, 80H ;BL ORed with immed 80H (sets MSB of BL ;to 1)
• Others: XOR, TEST
Instruction Set
– Shift instructions
• Mnemonic: SAL/SHL,
• syntax: SAL/SHL destination, count
• Description: shift operand bits left, put 0 in LSB(s)
• Destination: byte or word in register or memory loc
• Count: no of shifts; if >1, CL reg is used to count
• Flags affected:
– CF – contains bit most recently shifted in from MSB
– OF=1 for count=1 if CF & current MSB are not same
– For multiple shifts – OF – undefined
– SF & ZF – updated to reflect condition of destination
– PF – has meaning only for operand in AL
– AF - undefined
Instruction Set
• Examples:
– SAL BX, 1 ;shift word in BX one bit left, LSB=0
– MOV CL, 02H ;load desired no of shifts in CL
– SAL BP, CL ;shift word in BP left CL bits
• Others:
– SAR/SHR
– Rotate
• Mnemonic: ROL/ROR,
• syntax: ROL/ROR destination, count
• Description: rotates all bits of byte/word left/right count
no of bits; LSB/MSB -> MSB/LSB, CF – updated
• Others : RCL/RCR
Instruction Set
• STRING INSTRUCTIONS
• REP – an instruction, repeat until CX=0
• E.g. REP CMPSB ;compare string bytes until CX=0 or ;string byte
not equal
• Others:
– MOVS/MOVSB/MOVSB
– COMPS/COMPSB/COMPSW
– INS/INSB/INSW
– OUTS/OUTSB/OUTSW
Instruction Set
• PROGRAM EXECUTION TRANSFER INSTRUCTIONS
– Used to tell 8086 to start fetching instructions from some new address
– Unconditional transfer instructions
• CALL – call a procedure (subprogram), save return addr in stk
– E.g. CALL BX ;intrasegment call, BX= offset of 1st instruction
• RET – return from procedure to calling program
• JMP – go to specified address to get next instruction
– E.g. JMP CONT ;fetch next inst from address at label CONT
Instruction Set
– Conditional transfer
• Often used after a compare instruction
• JA/JNBE – jump if above/jump if not below or equal
• JC – jump if CF = 1
• JNE/JNZ – jump if not equal/jump if not zero
– Iteration control
• LOOP – loop thru a sequence of instructions until CX=0
• LOOPE/LOOPZ – loop while ZF = 1 & CX ≠ 0
– Interrupt instructions
• INT – interrupt program execution, call service proc
• INTO – interrupt if OF = 1
• IRET – return from service proc to main
Instruction Set
• PROCESSOR CONTROL INSTRUCTIONS
– Flag set/clear instructions
• STC – set CF to 1
• CLC – clear CF to 0
• CLI – clear IF to 0 (disable INTR input)
– External HW sync instructions
• HLT – halt (do nothing) until interrupt or reset
• WAIT – wait until signal on TEST pin is low
• ESC – escape to external coprocessor such as 8087
Writing Programs for 8086
• Initialization instructions
– Various parts of system has to be initialized before writing a program
• Segment registers: loaded with upper 16-bits of addr
– No instruction to move a no directly into a segment reg
– => use two MOV instructions
• Stack: if used, load SS reg and SP with offset of TOS
• Peripheral devices: ports, timers, controllers, need instruction to send control
word to tell function
• Instructions to set or clear flags
Writing programs …
– Coding instructions – templates
– A large no of instructions, b/c of various ways of specifying source or
operand (e.g. MOV)
– So, a template is used for each instruction type to build up instruction codes
(machine codes)
– E.g. MOV coding template/format:

1 0 0 0 1 0 Low disp/direct High disp/direct


OPCODE D W MOD REG R/M addr low byte addr high byte

D – Direction: to/from, 0=to, 1=from


MOD+R/M = 5 bits – addressing
W – byte/word: 0=byte, 1=word
mode
REG – register select, e.g. 001=>CL
e.g. 11000=> reg mode

You might also like