Lecture5
-
Upload
misgina2005 -
Category
Documents
-
view
59 -
download
0
Transcript of Lecture5
Lecture 05
Programming The 8086 In
AAiT
Programming The 8086 In
Assembly Language…Continued
Daniel D.Daniel D.Daniel D.Daniel D. DECEECEgECEgECEgECEg –––– 4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing
� Basic Instructions
In This LectureAAiT
� Details of the basic instructions
1ECEg ECEg ECEg ECEg ---- 4501450145014501
Basic Instructions
1. Data transfer instructions
MOV, LEA, IN/OUT, PUSH/POP, PUSHF/POPF
2. Arithmetic instructions
ADD, INC, SUB, DEC, NEG, COMP, MUL/IMUL, DIV/IDIV
3. Bit manipulation instructions
logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR
AAiT
logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR
4. String instructions
REP/REPE/REPNE, LOADS, STOS
5. Program execution transfer instructions
JMP/JE/JG/JL/JGE/JLE/JC, CALL/RET, LOOP, INT/IRET
6. Processor control instructions
flags(set/clear), HLT, WAIT, NOP
2ECEg ECEg ECEg ECEg ---- 4501450145014501
1. Data Transfer Instructions
MOV destination, source
Destination: can be a register or a memory location.
Source: can be a register, a memory location, or an
immediate number.
The source and destination must both be of type byte, or they
AAiT
The source and destination must both be of type byte, or they
must both be of type word.
e.g.
MOV CX, 037AH Put the 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.
3ECEg ECEg ECEg ECEg ---- 4501450145014501
PUSH source, POP destination
Destination: can be a register or a memory location.
Source: can be a register or a memory location
The stack segment register and the stack pointer must be initialized
before these instructions can be used.
Data Transfer Instructions…cntdAAiT
before these instructions can be used.
e.g.
PUSH BX Decrement SP by 2, copy BX to stack
PUSH DS Decrement SP by 2, copy DS to stack
POP DX Copy a word from top of stack to DX, Increment SP by 2
PUSH AL Illegal, must push a word
POP CS Just Illegal
PUSHF/POPF can be used with flags
4ECEg ECEg ECEg ECEg ---- 4501450145014501
IN accumulator, port OUT port, accumulator
Port: 8 or 16-bit I/O port address. Can be fixed or variable
�For the fixed-port type, the 8-bit address of a port is specified
directly in the instruction.
e.g.
Data Transfer Instructions…cntd AAiT
e.g.
IN AX,34H Input a word from port 34H to AX
OUT 3BH,AL Copy the contents of AL to port 3BH
�For the variable port, the port address is loaded into the DX
register before the IN/OUT instruction.
e.g.
MOV DX, 0FF78H Initialize DX to point to port
IN AL, DX Input a byte from 8-bit port 0FF78H to AL
5ECEg ECEg ECEg ECEg ---- 4501450145014501
LEA register, source (load effective address)
This instruction determines the offset of the variable or memory
location named as the source and puts this offset in the indicated
16-bit register.
Data Transfer Instructions…cntdAAiT
e.g.
LEA BX, PRICES Load BX with offset of PRICES in DS
LEA BP, SS: STACK_TOP Load BP with offset of STACK_TOP in SS
Assume PRICES is an array of bytes in data segment. The instruction
LEA BX, PRICES will load the displacement of the first price in the list
directly into BX.
6ECEg ECEg ECEg ECEg ---- 4501450145014501
ADD destination, source /also ADC (add with carry)
e.g.
ADD AL, 74H Add immediate number 74H to contents of AL.
Result In AL.
ADD DX,[SI] Add word from memory at offset [SI] in DS to
2. Arithmetic InstructionsAAiT
ADD DX,[SI] Add word from memory at offset [SI] in DS to
contents of DX. Result in DX.
NB: Arithmetic instructions affect the flags.
INC destination can be used to add 1 to a number
e.g
INC BX, INC CX, …
INC doesn’t affect the carry flag.
7ECEg ECEg ECEg ECEg ---- 4501450145014501
SUB destination, source
e.g.
SUB CX, BX CX - BX. Result in CX
SUB AX,3427H Subtract immediate number 3427H from AX
�The decrement instruction DEC destination can be used to
subtract 1 from a number
2. Arithmetic Instructions…cntdAAiT
subtract 1 from a number
DEC CX subtract 1 from the content of the CX register
�The NEG destination instruction can be used to form 2’s
complement of the number in destination
NEG AL Replace number in AL with its 2’s complement
�The CMP source, dest is used to compare two numbers
CMP BH, CL compare a byte in BH with a byte in CL
8ECEg ECEg ECEg ECEg ---- 4501450145014501
MUL source IMUL source (signed)
•Source: can be memory or registers.
•one of the operands is in AL if source contains a byte, else in AX if
source holds a word.
AL x source = AH: AL (AX)……………byte x byte = word
2. Arithmetic Instructions…cntdAAiT
AL x source = AH: AL (AX)……………byte x byte = word
AX x source = DX: AX ………………..word x word = double word
e.g.
MUL BH AL times BH, result in AX
MUL CX AX times CX, result high word in DX, low word in AX
•IMUL can be used the same way for signed numbers taking care of
the sign bit.
9ECEg ECEg ECEg ECEg ---- 4501450145014501
DIV source IDIV source (signed)
• Source: contains the divisor. It can be in memory or registers.
• The dividend is in AX if word/byte or in DX:AX if double/word
• Word/byte: AX/source…quotient in AL, remainder in AH
• Double/word: DX:AX/source…quotient in AX, remainder in DX
e.g.
2. Arithmetic Instructions…cntdAAiT
e.g.
DIV BL Divide word in AX by byte in BL. Quotient in AL,
remainder in AH.
DIV CX Divide double word in DX and AX by word in CX.
Quotient in AX, remainder in DX.
•IMUL can be used the same way for signed numbers taking care of
the sign bit.
•CPU generates interrupts on divide by zero and quotient overflow
10ECEg ECEg ECEg ECEg ---- 4501450145014501
3. Bit-oriented Instructions
Logical (AND/TEST, OR, XOR, NOT)
AND/OR/XOR dest, source NOT dest
Do the indicated logic operation on the given source and
destination operands and keep the result in destination
e.g.
AAiT
e.g.
AND BH,CL AND byte in BH with byte in CL Result in BH
TEST AL, BH AND BH with AL, no result stored. Update PF, ZF
OR BL,80H BL ORed with immediate 80H. Set MSB of BL to a 1
XOR CL,BH Byte in BH Exclusive-ORed with byte in CL. Result in
CL. BH not chanced.
NOT BX Complement contents of BX register
Logical operators affect the zero flag and the parity flag
11ECEg ECEg ECEg ECEg ---- 4501450145014501
Bit-oriented Instructions…cntd
SHL dest, count SHR dest, count
Shift the binary number in dest. count times
CF MSB LSB 0 SHL ( *2)
0 MSB LSB CF SHR (/2)
e.g.
AAiT
e.g.
SHL BX, 1 Shift word in BX 1 bit position left, 0 in LSB
MOV CL, 02H Load desired number of shifts in CL
SHL BP, CL Shift word in BP left (CL) bit positions, 0's in 2 LSBs
SHR AL, 4 Shift AL four bit positions right and put 0's in
upper 4 bits.
• shift operators affect CF, ZF, OF, SF, and PF
12ECEg ECEg ECEg ECEg ---- 4501450145014501
4. String Instructions
MOVS {dest string, source string} LODS source / STOS dest
�MOVS: copies string in data segment to a string in extra segment
�LODS: copies a string in memory pointed by SI in to accumulator
�STOS: Copies a string in accumulator to memory at [DI]
Use the REP/REPE/REPNE prefixes for multiple operations
e.g.
CLD Clear direction flag, i.e. SI will be auto incremented.
AAiT
CLD Clear direction flag, i.e. SI will be auto incremented.
MOV SI, OFFSET SOURCE_STRING Point SI at start of string
LODS SOURCE_STRING Copy byte or word from string to AL or AX
MOV CX, 04H Load length of string into CX as counter
REP
MOVSB Decrement CX and copy string bytes until CX = 0
MOV DI, OFFGET TARGET_STRING Point DI at destination
STOS TARGET_STRING Store string starting @ [DI]
13ECEg ECEg ECEg ECEg ---- 4501450145014501
String Instructions…cntd
CMPS {dest string, source string}
The comparison is done by subtracting the byte or word pointed to
by DI from the byte or word pointed to by SI.
The AF, CF, OF, PF, SF, and ZF flags are affected by the comparison,
but neither of the operands is affected.
AAiT
but neither of the operands is affected.
e.g.
MOV SI, OFFSET FIRST-STRING Point SI at source string
MOV DI, OFFSET SECOND-STRING Point DI at destination string
CLD DF cleared, so SI and DI will auto-increment after compare
MOV CX,100 Put number of string elements in CX
REPE Repeat the comparison of string bytes until end
CMPSB of string or until compared bytes are not equal
14ECEg ECEg ECEg ECEg ---- 4501450145014501
5. Program Execution Instructions
CALL subroutine
There are two basic types of calls, near and far.
Near: is a call to a procedure, which is in the same segment
as the CALL instruction.
Far: a call to a procedure, which is in a different segment from the
one that contains the CALL instruction.
AAiT
CPU saves the return address in stack before branching to the
subroutine
e.g.
CALL BX near call to a subroutine whose address is in BX
CALL SAMPLE far call to a subroutine labeled SAMPLE
There should be the RET instruction at the end of a subroutine
15ECEg ECEg ECEg ECEg ---- 4501450145014501
... Program Execution Instructions
JUMP instructions
JMP/JE/JZ/JC/JO/JA/JB/JG/JGE/JL/JLE…address
Address: is the destination to jump to
Short jump: within the same segment; long jump: to a different seg.
e.g.
…
AAiT
…NEXT: CMP BX,DX ;Compare (BX-DX)
JGE DONE ;Jump to DONE if BX ≥ DX
SUB BX,AX ;Else subtract AX from BX
INC CX ;Increment counter
JMP NEXT ;Check again
DONE: MOV AX,CX ;Copy count to AX
IN AL, 8FH ;Read data from port 8FH
SUB AL,30H ;Subtract 30h from AX
JZ START_MACHINE ;Jump to label if result of subtraction was 0
16ECEg ECEg ECEg ECEg ---- 4501450145014501
... Program Execution Instructions
LOOP instructions
LOOP/LOOPE/LOOPNE label
LOOP: loop while CX ≠ 0 aBer auto decrement
LOOPE/LOOPZ: loop while CX ≠ 0 or ZF = 1
LOOPNE/LOOPNZ: loop while CX ≠ 0 or ZF = 0
e.g.
AAiT
e.g.
MOV BX, OFFSET ARRAY ;Point BX to start of array
DEC BX
MOV CX, 100 ;Put number of array elements in CX
NEXT: INC BX ;Point to next element in array
CMP [BX], FFH ;Compare array element with FFH
LOOPE NEXT ;Loop until an array element is equal to
;FFH or all elements of the array are checked.
17ECEg ECEg ECEg ECEg ---- 4501450145014501
... Program Execution Instructions
Interrupt instruction
INT type
Type: a number between 0 and 255 which identifies the interrupt.
• CPU pushes current program address and status in to stack before
executing the ISR.
• In DOS, INT 21 is used for basic I/O functions, like display, print…
AAiT
• In DOS, INT 21 is used for basic I/O functions, like display, print…
Example of DOS INT 21 ISRs are
o INT 21h / AH=1 read character from standard input, result is
stored in AL. If there is no character in the keyboard
buffer, the function waits until any key is pressed.
o INT 21h / AH=2 write character to standard output. DL = character to write
o INT 21h / AH=9 printout a string from DS:DX. String must be terminated by '$’
18ECEg ECEg ECEg ECEg ---- 4501450145014501
... Program Execution Instructions
…Interrupt instruction
e.g. (prints a number in binary on the console window)…..
mov al, 5 ; 05h, or 00000101b
mov bl, 10 ; 0Ah, or 00001010b
add bl, al ; 5 + 10 = 15 (decimal), 0Fh, or 00001111b
; print result in binary
mov cx, 8 ;for the 8 bits
print: mov ah, 2 ; print function.
AAiT
print: mov ah, 2 ; print function.
mov dl, '0‘
test bl, 10000000b ; test first bit.
jz zero
mov dl, '1‘
zero: int 21h ;print to console
shl bl, 1
loop print
; print binary suffix:
mov dl, ‘b’
int 21h
;Print the string “hello world”
msg db "hello world $“
mov dx, offset msg
mov ah, 9
int 21h
;wait for any key press
mov ah, 1
int 21h
ret ;return to the operating system
19ECEg ECEg ECEg ECEg ---- 4501450145014501
6. Processor control Instructions
� flag set/clear instructionSTC/CLC: set/clear the carry flag
STD/CLD: set/clear the direction flag, etc.
� HLT Stops the processor
To get the processor out of the halt state, apply an interrupt signal
on the INTR pin, an interrupt signal on the NMI pin, or a reset
signal on the RESET input.
AAiT
signal on the RESET input.
� WAIT puts the processor in idle state (no processing)
CPU stays in this idle state until the TEST input pin is made low or
an interrupt signal is received on the INTR or the NMI input pins.
� NOP fetch- decode- no execution
refer: ESC, LOCK instruction for multi-processor system
20ECEg ECEg ECEg ECEg ---- 4501450145014501