0% found this document useful (0 votes)
13 views

Lecture #11, Microprocessor-ALP-Data-Arithmetic

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture #11, Microprocessor-ALP-Data-Arithmetic

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

EEE-3103: Microprocessor and Interfacing

Lecture #: Introduction to Assembly Language

Dr. Sharnali Islam


Department of Electrical and Electronic Engineering
University of Dhaka
[email protected]
Operand Types
❖ Immediate
✓ Constant integer (8, 16, or 32 bits)
✓ Constant value is stored within the instruction
❖ Register
✓ Name of a register is specified
✓ Register number is encoded within the instruction
❖ Memory
✓ Reference to a location in memory
✓ Memory address is encoded within the instruction, or
✓ Register holds the address of a memory location
Source: Basic Instructions and Addressing Modes
Instruction Set of 8086
✓ There are 117 basic instructions.

✓ Instruction set can be divided into the following groups:

(i) Data transfer instructions (vii) Compare instructions


(ii) Arithmetic instructions (viii) Jump instructions
(iii) Logic instructions (ix) Subroutines and subroutine handling
(iv) Shift instructions instructions
(v) Rotate instructions (x) Loop and loop handling instructions
(vi) Flag control instructions (xi) Strings and string handling instructions
Data Transfer Instructions

✓ MOV

✓ XCHG

✓ XLAT

✓ LEA

✓ LDS

✓ LES
MOV Instruction
❖ Move source operand to destination Rules
MOV destination, source • Both operands must be of same size

Example: • No memory-to-memory moves


• Destination cannot be CS or IP
MOV AX, WORD1
• No immediate to segment moves

This reads “Move WORD1 to AX”. The contents of register AX are replaced by the contents
of memory location WORD1. The contents of WORD1 are unchanged.

Before After

0006 0008

AX AX

0008 0008

WORD1 WORD1
XCHG Instruction
❖ Exchanges the values of two operands
Rules
XCHG destination, source • Operands must be of the same size
Example: • At least one operand must be a register
XCHG AH, BL • No immediate operands are permitted

This instruction swaps the contents of AH and BL, so that AH contains what was previously
in BL and BL contains what was originally in AH.
Before After

It’s like doing 3 move operations- 1A 00 05 00


i) from destination to temporary register, then AH AL AH AL
ii) from source to destination, then
iii) from temporary register to source. 00 05 00 1A
No register needs to be reserved for temporary storage. BH BL BH BL
XLAT Instruction
❖ Used as translate data of AL register from look up table.
❖ After execution,
Rules
AL [AL+BX] • No operand
Procedure: • BX should be loaded with the starting address
of the translation table.
Step-01: First, you’ll have to make a look up table
• AL must contain an index into the table
with memory pointer BX. • Index value starts at zero.
Step-02: Then execute XLAT • The instruction reads the byte at this index in
the translation table and stores this value in
AL
• The index value in AL is lost.
• Translation table can have at most 256 entries
(due to AL).
XLAT Instruction for Seven-Segment Display
Equivalent Codes of Seven Segment Display
DP G F E D C B A DATA INPUT
0 0 1 1 1 1 1 1 3FH 0
0 0 0 0 0 1 1 0 06H 1
0 1 0 1 1 0 1 1 5BH 2
0 1 0 0 1 1 1 1 4FH 3
0 1 1 0 0 1 1 0 66H 4
0 1 1 0 1 1 0 1 6DH 5
0 1 1 1 1 1 0 1 7DH 6
0 0 0 0 0 1 1 1 07H 7
0 1 1 1 1 1 1 1 7FH 8
0 1 1 0 1 1 1 1 6FH 9
XLAT Instruction for Seven-Segment Display

DATA SEGMENT

; 7-segment codes for digits 0-9 (common cathode)

; The order represents the segments for 0-9 in hexadecimal.

; For example, '0' is 0x3F (00111111 for segments a-g).

HEX_TABLE DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH

DIGIT DB 5 ; Digit to display (example: 5)

SEGMENT_CODE DB ? ; To store the 7-segment code

DATA ENDS

DP g f e d c b a DATA INPUT
0 1 1 0 1 1 0 1 6DH 5
XLAT Instruction for Seven-Segment Display
CODE SEGMENT
ASSUME DS:DATA, CS:CODE DATA INPUT
START: 3FH 0 BX
06H 1
MOV AX, DATA ; Load data segment address into AX
5BH 2
MOV DS, AX ; Initialize DS register 4FH 3
MOV BX, OFFSET HEX_TABLE ; Load the offset of HEX_TABLE into BX 66H 4
6DH 5 BX + AL
MOV AL, DIGIT ; Load the digit (index) into AL
7DH 6
XLAT ; Translate digit to 7-segment code 07H 7
MOV SEGMENT_CODE, AL ; Store the result in SEGMENT_CODE 7FH 8
MOV AH, 4CH ; DOS interrupt to exit 6FH 9

INT 21H
CODE ENDS
END START
LEA Instruction
❖ LEA = Load Effective Address
LEA destination, source

destination = offset address of source

❖ The LEA instruction can be used to get the offset address of a variable
Example:
LEA BX, MSG

This instruction puts the offset address of the variable MSG into BX.
LDS & LES Instruction
❖ LDS/LES instruction copies a word from two memory locations into a specified register.
❖ It then copies a word from the next two memory locations into DS/ES register.
LDS destination, source
Example:
LDS BX, [4326]
✓ BL ← [4326]
✓ BH ← [4327]
✓ DS ← [4328, 4329]

Before After

0000 2A 0D AE07

DS [4327] [4326] DS

1A 05 AE 07 2A 0D

BH BL [4329] [4328] BH BL
Arithmetic Instructions

✓ INC/DEC

✓ ADD/SUB

✓ MUL/IMUL

✓ DIV/IDIV

✓ ADC/SBB
INC/DEC Instruction
✓ destination = destination + 1 ✓destination = destination – 1
INC destination DEC destination
Example: Example:
INC WORD1 DEC BYTE1

Adds 1 to the contents of WORD1 Subtracts 1 to the from variable BYTE1


Before After

0002 0003

WORD1 WORD1

Before After

FFFE FFFD

BYTE1 BYTE1
NEG Instruction
✓ destination = 2's complement of destination
NEG destination
Hexadecimal 0002
Example:
Binary 0000 0000 0000 0010
NEG BX 1111 1111 1111 1101
+1
Negates the contents of BX Binary 1111 1111 1111 1110
Hexadecimal F F F E

Before After

0002 FFFE

BX BX
ADD/SUB Instruction
✓destination = destination + source ✓destination = destination – source
ADD destination, source SUB destination, source

Rules

✓ Destination can be a register or a memory location

✓ Source can be a register, memory location, or a constant

✓ Destination and source must be of the same size

✓ Memory-to-memory arithmetic is not allowed


ADD/SUB Instruction
✓ destination = destination + source ✓destination = destination - source
ADD destination, source SUB destination, source

Example: Example:
ADD WORD1, AX SUB AX, DX

Before After

01BC 01BC

AX AX

0523 06DF

WORD1 WORD1
ADD/SUB Instruction
✓ destination = destination + source ✓destination = destination - source
ADD destination, source SUB destination, source

Example: Example:
ADD WORD1, AX SUB AX, DX

Before After

0000 FFFF

AX AX

0001 0001

DX DX
MUL/IMUL Instruction
❖ The MUL instruction is used for unsigned multiplication
❖ The IMUL instruction is used for signed multiplication r/m8
Multiplier
(Byte)
❖ Multiplies 8-, 16-, or 32-bit operand by AL, AX, or EAX
MUL multiplier r/m16 Multiplier
(Word)
❖ The instruction formats are:
MUL r/m8 ; AX = AL * r/m8 r/m32 Multiplier
(Double-
MUL r/m16 ; DX:AX = AX * r/m16 Word)

MUL r/m32 ; EDX:EAX = EAX * r/m32


MUL/IMUL Instruction
Example:
MOV AL, 10 ; store decimal 10 into AL register as multiplicand
MOV DL, 25 ; store decimal 25 into DL register as multiplier
MUL DL ; multiply 25 with 10 and result will be stored in AX

Before After

000A 00FA

AX AX

19 19

DL DL
MUL/IMUL Instruction
Example:
MOV AL, 0BEH ; AL=-66
MOV DL, 0FFH ; DL=-1
IMUL DL ; multiply 0BEH with 0FFH and result will be stored in AX

Before After

00 BE 00 42

AH AL AH AL

00 FF 00 FF

DH DL DH DL
DIV/IDIV Instruction
❖ The DIV instruction is used for unsigned division
❖ The IDIV instruction is used for signed multiplication r/m8
Divisor
(Byte)
❖ A single operand (divisor) is supplied
r/m16 Divisor
 Divisor is an 8-bit, 16-bit, or 32-bit register or memory
(Word)
 Dividend is implicit and is either AX, DX:AX, or EDX:EAX
Divisor
❖ The instruction formats are: r/m32
(Double-
DIV r/m8 Word)

DIV r/m16
DIV r/m32
DIV/IDIV Instruction
Example:
Divide AX = 8003h by CX = 100h

MOV DX,0 ; clear dividend, high


MOV AX,8003h ; dividend, low
MOV CX,100h ; divisor
DIV CX ; AX = 0080h, DX = 3 (Remainder)

00 00 00 03

DH DL DH DL

01 00 01 00

CH CL CH CL

80 03 00 80

AH AL AH AL
DIV/IDIV Instruction
Question:
❖ What will be the hexadecimal values of DX and AX after the following instructions execute?

mov dx,0087h
mov ax,6023h
mov bx,100h
div bx

Solution: DX = 0023h, AX = 8760h

Source: Integer Arithmetic


Signed Integer Division
❖ Signed integers must be sign-extended before division
➢ Fill high byte, word, or double-word with a copy of the sign bit

❖ CBW, CWD, and CDQ instructions


➢ Provide important sign-extension operations before division
➢ CBW: Convert Byte to Word, sign-extends AL into AH
➢ CWD: Convert Word to Double, sign-extends AX into DX
➢ CDQ: Convert Double to Quad, sign-extends EAX into EDX

• Example:
MOV AX, 0FE9Bh ; AX = -357
cwd ; DX:AX = FFFFFF9Bh
IDIV Instruction
Example:
Divide DX:AX (-48) by BX (-5)

mov ax,-48
cwd ; sign-extend AX into DX
mov bx,-5
idiv bx ; AX = 9, DX = -3

FF FF FF FD

DH DL DH DL

FF D0 00 09

AH AL AH AL

FF FB FF FB

BH BL BH BL
ADC/SBB Instruction
✓ ADC Instruction: Addition with Carry
ADC destination, source
destination = destination + source + CF
✓ SBB Instruction: Subtract with Borrow
SBB destination, source
destination = destination - source – CF
✓ Destination can be a register or a memory location
✓ Source can be a register, memory location, or a constant
✓ Destination and source must be of the same size
✓ Memory-to-memory arithmetic is not allowed
Source: Integer Arithmetic
ADC/SBB Instruction
Example:

MOV AX, 0FFFFH ; Load the value 0xFFFF into AX (16-bit register)
MOV BX, AX ; Copy the value of AX into BX
ADD AX, 1 ; Add 1 to AX, resulting in an overflow
ADC BX, 1 ; Add 1 to BX, considering the carry flag from the previous addition

Final Result:
AX = 0000H
BX = 0001H

Source: Integer Arithmetic

You might also like