0% found this document useful (0 votes)
3 views43 pages

CH 4 Microprocessor and Assembly Language Programing

Chapter 4 covers various assembly language instructions, including data movement, arithmetic, and logic operations. It details specific instructions like MOV, XCHG, PUSH/POP, and various arithmetic operations such as ADD, SUB, and logical operations like AND, OR, and XOR. Additionally, it explains shift and rotate instructions that manipulate bits in operands.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views43 pages

CH 4 Microprocessor and Assembly Language Programing

Chapter 4 covers various assembly language instructions, including data movement, arithmetic, and logic operations. It details specific instructions like MOV, XCHG, PUSH/POP, and various arithmetic operations such as ADD, SUB, and logical operations like AND, OR, and XOR. Additionally, it explains shift and rotate instructions that manipulate bits in operands.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 4

INSTRUCTIONS

127 22/10/2022
5/24/2024
Contents
• 4.1. Data Movement Instructions

• 4.1.1 MOV Revisited

• 4.1.2 PUSH/POP

• 4.1.3 Load-Effective Address

• 4.1.4 String Data Transfers

• 4.2: Arithmetic and Logic instructions

• 4.2.1 Arithmetic Instructions

• 4.2.2 Basic Logic Instructions

• 4.2.3 Shift and Rotate

• 4.2.4 String Comparisons

5/24/2024
4.1. Data Movement Instructions
• These instructions are used to transfer data from source to
destination.
• Also called as Data Transfer Instructions
• The operand can be a constant, memory Location, register or
I/O port address.
 If the oprand is constant vakue we have to assign radex.
 Common radix characters:
 h – hexadecimal
 d – decimal
 b – binary
 O/q octal

5/24/2024
Cont ..
• x86 assembly language uses different types of instruction
operands.
 Syntax
 mnemonic [destination], [ source]
• The Source and destination can be one of the following
• Immediate—uses a numeric literal expression
• Register— uses a named register in the CPU
• Memory— references a memory location

5/24/2024
1. M O V Instruction
• The M OV instruction copies d a ta from a source operand
to a destination operand.
• Known as a d a ta transfer instruction, it is used in
virtually every program.
• Its basic format shows that the first operand is the
destination and the second operand is the source:

Sy n t a x
: MOV destination, source

5/24/2024
C o n t ..
• MOV is very flexible in its use of operands, as long as the following rules are
observed:
 Both operands must be the same size.
 Both operands cannot be memory operands.
 CS, EIP, and IP cannot be destination operands.
 An immediate value cannot be moved to a segment register
• The ff are correct
• MOV reg,reg
• MOV mem,reg
• MOV reg,mem
• MOV mem,imm
• MOV reg,imm

5/24/2024
Example

5/24/2024
2. X C H G Instruction
The XCHG (exchange data) instruction exchanges the contents of two
operands.
•It exchange the contents of the sourse and Destination.
There are three variants:
• X C H G reg,reg Example:
• X C H G reg,mem
xchg ax,bx ; exchange 16-bit regs
• X C H G mem,reg
xchg ah,al ; exchange 8-bit regs
xchg var1,bx ; exchange 16-bit mem op with B X
xchg eax,ebx ; exchange 32-bit regs
5/24/2024
3. POP/Push instructions
• PUSH D
• pushes D to the stack example :PUSH D X
• POP D
• pops the stack to D example: POP AS

Examples

5/24/2024
4. Load Effective Address(LEA)
• Computes the Effective address of the second operand( the source operand )
and stores it in the 1st operand (Destination Operand).
• The main deference b/n move and LDA is
• LEA means Load Effective Address
• M OV means Load Value

5/24/2024
4.1.4 String data Transfer
• String is s series of data byte or word available in memory at
consecutive locations.
• It is either referred as byte string or word string.
• Their memory is always allocated in a sequential order.
• Instructions used to manipulate strings are called string manipulation
instructions.

5/24/2024
Some instructions

5/24/2024
4.2 Arithmetic and Logic instructions
• 4.2.1 Arithmetic Instructions
• 4.2.2 Basic Logic Instructions
• 4.2.3 Shift and Rotate

5/24/2024
4.2.1 Arithmetic Instructions
• There are different instruction set. Let’s begin with
1. INC (increment),
2. DEC (decrement),
The syntax is
• INC reg/mem
• DEC reg/mem

5/24/2024
Cont.…
3 . A D D (add),
• The A D D instruction adds a source operand to a destination operand of the same size.
• The syntax is
A D D dest ,source
Example:
.CODE
MOV AL, 10H ;Sets AL to 10H
MOV BH, 75H ;Sets BX to 23H
ADD AL, BH ;Store the sum of AL and BX in AL
MOV CX, 0F21Ah ;Set CX to 0F21Ah
ADD [0154H], CX ;Store sum of CX data and data at memory address DS:0154 into the same
memory address
MOV AH, 9FH ;Sets AH to 9FH
ADD AH,BH ;Store sum of AH and BH into BH
RET ;stops the program
5/24/2024
5/24/2024
5/24/2024
5/24/2024
C o n t ..
[Link] (subtract)
• The SUB instruction subtracts a source operand from a destination
[Link] set of possible operands is the same as for the A D D
and MO V instructions.
• Flags The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are
changed according to the value that is placed in the destination operand
• The syntax is
SUB dest ,source

5/24/2024
5 . Mul and DIV Instruction
• MUL
• Its operand is 8-bit reg
• Used to multiply and divide unsigned byte by byte/word by
word.
• Example :

5/24/2024
6. N E G
• NEG D
• This operation performs 2’s compliment of D
• What is the output (value) stored in Ax? In the following example

• 250h in binary is 0000001001010000 so,


• it 2’s compliment will be 1111110110101111 +1
• 1111110110110000 = FDBDh
• therefore Ax will store FDBD

5/24/2024
7. CMP : Compare
• This instruction compares the source operand, which may be a register or
an
immediate data or a memory location, with a destination operand that may
be a register or a memory location
Eg.
• CMP BX, 0100H
CMP AX, 0100H
CMP [5000H], 0100H
CMP BX, [SI]
CMP BX, CX

5/24/2024
C o n t ..
• N OTE:In Addition and Multiplication if they produce results > 16 bits it will
be stored in D X register

5/24/2024
4.2.2 Basic Logic Instructions
• The processor instruction set provides the instructions
• AND,
• OR,
• XOR,
• NOT Boolean logic,
• which tests, sets, and clears the bits according to the need of the
program.

5/24/2024
I. The AND instruction
• performs a Boolean (bitwise) A N D operation between each pair of matching
bits in two operands and places the result in the destination operand:

A N D destination, source
• The following operand combinations are permitted:
AND reg,reg
AND reg,mem
AND reg,imm
AND mem,reg
AND mem,imm

5/24/2024
C o n t ..
• For example, suppose AL is initially set to 10101110 binary. After
ANDing it with 11110110, AL equals 10100110:

mov al,10101110b
and al,11110110b ; result in AL = 10100110

5/24/2024
Application of AND Instruction
 Converting Characters to Upper Case
o The AND instruction provides an easy way to translate a
letter from lowercase to uppercase.
o If we compare the ASCII codes of capital A and lowercase a,
it becomes clear that only bit 5 is different:
o If we AND any character with 11011111 binary, all bits are
unchanged except for bit 5, which is cleared.
o 0 1 1 0 0 0 0 1 = 61h ('a')
0 1 0 0 0 0 0 1 = 41h ('A')

5/24/2024
II. O R Instruction
• The O R instruction performs a Boolean OR operation between each pair of
matching bits in two operands and places the result in the destination
operand:
OR destination ,source
• The OR instruction uses the same operand combinations as the A N D
instruction:
OR reg,reg For example, if AL is initially equal to 11100011
OR reg,mem binary and then we OR it with 00000100, the
OR reg,imm result equals 11100111:
OR mem,reg
mov al,11100011b
OR mem,imm
and al,00000100b; result in AL = 11100111
5/24/2024
EXMPLES

5/24/2024
III. The X O R Instruction
• The X O R instruction implements the bitwise X O R operation.
• The X O R operation sets the resultant bit to 1, if and only if the bits from the
operands are different.
• If the bits from the operands are same (both 0 or both 1), the resultant bit is
cleared to 0.

5/24/2024
IV. NOT Instruction
• The complement of a set can be generated using the
NOT instruction, which reverses all bits.
• Example :

5/24/2024
4.3 Shift and rotate instructions
 There are two ways to shift an operand’s bits.
 The first,
 Logical shift, fills the newly created bit
position with zero.

5/24/2024
C o n t ..
 In the following illustration, a byte is logically shifted one
position to the right.
 In other words, each bit is moved to the next lowest bit position.
 Note that bit 7 is assigned 0:
 Example
o The following illustration shows a single logical right
shift on the binary value 11001111,
o producing 01100111.
o The lowest bit is shifted into the Carry flag:

5/24/2024
Cont ..
 Second an arithmetic shift.
 The newly created bit position is filled with a
copy of the original number’s sign bit:
 Example
o Binary 11001111, for example, h a s a 1 in the
sign bit.
o When shifted arithmetically 1 bit to the right,
it becomes 11100111:
5/24/2024
1 . S H L Instruction
o The S H L (shift left) instruction performs a
logical left shift on the destination operand,
filling the lowest bit with 0 .
o The highest bit is moved to the Carry flag, and the
bit that w a s in the Carry flag is discarded:
• Example
mov bl,8Fh ; BL = 10001111b
shl bl,1 ; CF = 1, BL = 00011110b

5/24/2024
Application of S H L
 Bitwise Multiplication: S H L can perform multiplication by powers
of 2.
o Shifting any operand left by n bits multiplies the operand by 2 n .
o For example, shifting the integer 5 left by 1 bit yields the product
of 5 x 2 1 = 10:
mov dl,5 dl= 00000101
shl dl,1 dl=00001010
o Exercise what will be the value of the Dl in decimal after SH l by 3
is performed
o mov dl,5 dl= 00000101
shl dl,3 dl=

5/24/2024
C o n t ..
 SHRInstruction
o The SHR(shift right) instruction performs a logical right shift on the destination
operand, replacing the highest bit with a 0.
o The lowest bit is copied into the Carry flag, and the bit that was previously
in the Carry flag is lost:

o Example In the following example, the 0 from the lowest bit


in ALis copied into the Carry flag, and the highest bit in ALis filled with a zero:

mov ; AL= 11010000b


al,0D0h shr ; AL= 01101000b, CF= 0
al,1 5/24/2024
Application of S H R
o Bitwise Division: Logically shifting an unsigned integer right by n bits
divides the operand by 2n.

 In the following statements, we divide 32 by 21, producing 16:
mov dl,32 ; dl=100000
shr dl,1 ; dl=010000 cf=0
 Exercise , write an assembly program that divide 32 by 23 using SHR:
mov dl,32 ; dl=100000
shr dl,3 ; dl=0000100 cf=0
 Write an assembly program that divide 32 by 24 using SHR

5/24/2024
R O L Instruction
• The ROL (rotate left) instruction shifts each bit to the left.
• The highest bit is copied into the Carry flag and the lowest bit position.

mov al,40h ;AL = 01000000b


rol al,1 ;AL = 10000000b,CF = 0
rol al,1 ;AL = 00000001b,CF = 1
rol al,1 ;AL = 00000010b,CF = 0
5/24/2024
C o n t ..
• Multiple Rotations:
• When using a rotation count greater than 1, the Carry flag contains the last bit
rotated out of the MSB position:
• mov al,00100000b
• rol al,3 ; CF = 1,AL = 00000001b
• Exchanging Groups of Bits:
• You can use ROL to exchange the upper (bits 4–7) and lower (bits 0–3) halves of a
byte.
• For example, 26h rotated four bits in either direction becomes 62h:
 mov al,26h
 rol al,4 ;AL = 62h

5/24/2024
R O R Instruction
• The ROR (rotate right) instruction shifts each bit to the right and copies the
lowest bit into the Carry flag and the highest bit position.

mov al,01h ;AL = 00000001b


ror al,1 ;AL = 10000000b,CF = 1
ror al,1 ;AL = 01000000b,CF = 0

5/24/2024
C o n t ..
• Multiple Rotations:
• When using a rotation count greater than 1, the Carry flag contains the last bit
rotated out of the LSB position:
• mov al,00000100b
• ror al,3 ;AL = 10000000b,CF = 1
• RCL and RCR Instructions
• The RCL (rotate carry left) instruction shifts each bit to the left, copies the Carry flag
to the LSB, and copies the MSB into the Carry flag

5/24/2024
C o n t ..
• RCR Instruction:
• The RCR (rotate carry right) instruction shifts each bit to the right, copies the
Carry flag into the MSB, and copies the LSB into the Carry flag:

5/24/2024

You might also like