You are on page 1of 21

AAiT

Lecture 05

Programming The 8086 In


Assembly Language
Continued

ECEg 4501 Microcomputers & Interfacing Daniel D. DECE


In This Lecture AAiT

 Basic Instructions

 Details of the basic instructions

ECEg - 4501 1
Basic Instructions AAiT

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
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
ECEg - 4501 2
1. Data Transfer Instructions
AAiT

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


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.

ECEg - 4501 3
Data Transfer Instructionscntd AAiT

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.
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

ECEg - 4501 4
Data Transfer Instructionscntd AAiT

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.
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
ECEg - 4501 5
Data Transfer Instructionscntd AAiT

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.

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.

ECEg - 4501 6
2. Arithmetic Instructions AAiT

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
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 doesnt affect the carry flag.

ECEg - 4501 7
2. Arithmetic Instructionscntd
AAiT

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
DEC CX subtract 1 from the content of the CX register

The NEG destination instruction can be used to form 2s


complement of the number in destination
NEG AL Replace number in AL with its 2s complement

The CMP source, dest is used to compare two numbers


CMP BH, CL compare a byte in BH with a byte in CL
ECEg - 4501 8
2. Arithmetic Instructionscntd AAiT

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


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.

ECEg - 4501 9
2. Arithmetic Instructionscntd AAiT

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/sourcequotient in AL, remainder in AH


Double/word: DX:AX/sourcequotient in AX, remainder in DX
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
ECEg - 4501 10
3. Bit-oriented Instructions AAiT

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.
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
ECEg - 4501 11
Bit-oriented Instructionscntd AAiT

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.
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


ECEg - 4501 12
4. String Instructions AAiT

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.
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]
ECEg - 4501 13
String Instructionscntd
AAiT

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.
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

ECEg - 4501 14
5. Program Execution Instructions
AAiT

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.

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


ECEg - 4501 15
... Program Execution Instructions
AAiT

JUMP instructions
JMP/JE/JZ/JC/JO/JA/JB/JG/JGE/JL/JLEaddress
Address: is the destination to jump to
Short jump: within the same segment; long jump: to a different seg.
e.g.

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

ECEg - 4501 16
... Program Execution Instructions
AAiT

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.
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.

ECEg - 4501 17
... Program Execution Instructions
AAiT

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

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 '$

ECEg - 4501 18
... Program Execution Instructions
AAiT

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.
mov dl, '0 ;Print the string hello world
test bl, 10000000b ; test first bit. msg db "hello world $
jz zero mov dx, offset msg
mov dl, '1 mov ah, 9
zero: int 21h ;print to console int 21h
shl bl, 1 ;wait for any key press
loop print mov ah, 1
; print binary suffix: int 21h
mov dl, b ret ;return to the operating system
int 21h

ECEg - 4501 19
6. Processor control Instructions AAiT

 flag set/clear instruction


STC/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.
 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

ECEg - 4501 20

You might also like