Sie sind auf Seite 1von 54

Chapter 3

Instruction Sets and


Programming
In this chapter:
Programming Overview
Addressing Modes
8086 Instruction Sets and
 Programming
What is computer instruction
Instructions:-set of characters which define an operation
together with one or more addresses, or no address and
which cause the computer to perform the operation on the
indicated quantities
A computer program:-a series of instructions or statements
prepared in a form acceptable to the computer the purpose
of which is to achieve a certain result.
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
…cont…

II. ASSEMBLY LANGUAGE


Uses two-, three- or four-letter mnemonics to represent each
instruction type.
Mnemonics – abbreviations for instruction opcodes. i.e. initials
or a shortened form of the English word(s) for respective
operations
Special program called assembler is required
Assembler – allows operator to input code program in
mnemonic form
Standard form consists of four fields:

LABEL OP CODE OPERAND COMMENT FIELD


FIELD FIELD FIELD
NEXT: ADD AL, 07H ;ADD CORRECTION
FACTOR
…cont…

 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
…cont…
III. HIGH-LEVEL LANGUAGEs
Use more english like statements, which may represent
many machine code isntructions
Interpreters or compilers – convert these into machine
codes
Compilers –translate high-level source code to machine
code
Interpreters – execute one statement at a time
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
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
– Tow common ways of representing algorithm:
• Flow charts: graphic representation of different
program operations
– Symbols:
INPUT
PROCESS DECISION OUTPUT START

CONNECTOR
SUB
ROUTINE TERMINATION
Program development steps

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
Introduction
The 8086 addresses a segmented memory. The complete
physical address for a main memory slot (which has 1byte
capacity), is 20-bits long, is generated using segment and
offset registers (which contain segment and offset
addresses), each 16-bits long.
To generate physical address, the content of a segment
register(segment address) is shifted left bit-wise four times
and to this result, content of an offset register(offset
address) is added, to produce a 20-bit physical address.
The segment address value is to be taken from an
appropriate segment register depending upon whether
code segment(CS), data segment(DS) or stack
segment(SS) are to be accessed, while the offset may
be the content of IP, BX, SI, DI,SP or an immediate
16-bit value, depending upon the addressing mode.
…cont…
Thus the addressing modes describe the types of
operands and the way they are accessed
for executing an instruction.
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
…cont…

The 8086 addressing modes can be viewed in five groups:


Register and immediate
Memory
I/O
Relative
implied
…cont…

1. 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
..cont…
2. 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!
…cont…
3. Memory addressing modes
Used to specify location of an operand in memory:
To access data, Effective Address (EA) is added to segment
base address
If not specified, CS is default segment for code and DS is
default segment for data, SS for stack, EX for destination data.
EU calculates EA for an operand from info specified in the
instruction
The way EU calculates EA form various memory addressing
modes
8086 provides 17 different ways to access memory
…cont…

3.1. Direct addressing mode


– The simplest memory addressing mode
displacement of operand from segment base (16 bit EA)
is specified directly in the instruction.
E.g. MOV BL, [437AH]
• Copy contents of memory location at a displacement from DS
base of 437AH into BL
…cont…
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 address & high byte in
higher address.
– Direct addressing can also specify destination
• E.g.3 MOV [437AH], BX
[Explain what does it mean]
…cont…

3.2. Register indirect addressing mode


In Register indirect addressing, name of the register which
holds the effective address (EA) will be specified in the
instruction.
EA is specified in either a pointer or index registers
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
3.3. Indexed addressing mode
 SI or DI register is used to hold an index value for memory data and a
signed 8-bit or unsigned 16-bit displacement will be specified in the
instruction.
 DS and ES are the default segments for index registers SI and DI
respectively.
 EA is calculated by adding displacement and contents of SI or DI
Eg: MOV AX, [SI]
Here, data is available at an offset address stored in SI in DS. The effective
address, in this case, is computed as 10H*DS+[SI].
E.g: MOV BH, START[SI] or MOV BH, [START+SI]
MOV BH, 20H[SI]
Moves contents of 20-bit address computed from displacement START (an
immediate value), SI and DS into BH.
START is a displacement, SI is start position
Indexed addressing can be used to access a single table, where displacement
is starting address of table
…cont…
3.4. Based addressing mode
Here, BX or BP is used to hold the base value for
effective address and a signed 8-bit or unsigned 16-bit
displacement will be specified in the instruction
EA is obtained by adding a contents of index registers
(SI or DI) 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]
• MOV AH,50
• 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
…cont…

7. 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][BP], CL
• If [BX] = 0200H, ALPHA = 08H, [SI] = 1000H, & [DS] =
3000H, then 8-bit content of CL is moved to 20-bit physical
address 31208H
…cont…

• 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
These addressing modes are used to access data from
standard I/O mapped devices or ports.
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
– 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
– Flag transfer instructions
• Mnemonic: LAHF
• Description: load AH with low byte of flag.
• Example
– LAHF
– PUSH AX
• Others
– SAHF, PUSHF, POPF
• 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
– 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
Examples
1. Write a program to add a data byte located at offset 0500H in 2000H segment to another
data byte available at 0600H in the same segment and store the result at 0700H in the same
segment.

2. Add the contents of the memory location 2000H:0500H to contents of 3000H:0600H and
store the result in 5000H:0700H.
Solution: Unlike the previous example programs, this program refers to the memory locations
in different segments, hence, while referring to each location, the data segment will have to
be newly initialized with the required value. Figure 5.13 shows the flow chart. The
instruction sequence for the above flow chart is given along with the comments.
…cont…
3. Move a byte string , 16-bytes long, from the offset 0200H to 0300H
in the segment 7000H.

4. Find out the largest number from an unordered array of sixteen 8-bit
numbers stored sequentially in the memory locations starting at
offset 0500H in the segment 2000H.

Das könnte Ihnen auch gefallen