0% found this document useful (0 votes)
18 views35 pages

5 - Introd. To Assem. Part 1

Uploaded by

hafsatamer03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views35 pages

5 - Introd. To Assem. Part 1

Uploaded by

hafsatamer03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 2 : Introduction to Assembly

Programming The Intel 8086 ISA


Instruction Set Architecture
Outline

 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 2


Types of Instructions
 Assignment Instruction (MOV):
 CPU  Main Memory (reading);
 CPU  Main Memory (writing);
 Register/Constant to Register
 Arithmetic and Logical Instructions :
 Perform arithmetic/logical operation.
 Data can be register or a value in a memory location.
 Comparison Instructions
 Compares contents of AX register to a data and updates the flags
 Branching instructions
 IP register points to the next instruction to be executed
 Branching instructions can change the value of IP, and thus alter the
order of instruction execution
 There are two types of branching:
 Unconditional branching: IP  address of a new instruction
 Conditional branching: If a condition is satisfied, then branch, otherwise go to
next instruction
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 3
Next

 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


4
YANBU
Arithmetic Instructions (ADD)
 The basic arithmetic instructions are addition, subtraction, multiplication
and division that includes variants.
 Several addressing modes are possible

ADD destination, source 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛+𝑠𝑜𝑢𝑟𝑐𝑒

Examples

ADD AH,[1100H]
;Adds the contents of the memory cell 1100H in AH (direct addressing)

ADD AH,[BX]
;Adds the contents of the cell pointed by BX in AH (based addressing)

ADD BYTE PTR [1200H],05H


; adds 05H to content of the memory offset (immediate addressing)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 5


Arithmetic Instructions (ADD)
 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

Example

ADD Tab1,Tab2 ;not allowed


;can be replaced by:

MOV AX, Tab2


ADD Tab1, AX

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 6


Arithmetic Instructions (SUB)
 Subtraction (SUB)

¿ destination , source
𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛− 𝑠𝑜𝑢𝑟𝑐𝑒

Examples

SUB AX, BX ; AX = AX - BX (Subtract 16-bit)


SUB AL, BH ; AL = AL - BH (Subtract 8-bit)
SUB AL, [SI] ; AL = AL - the contents of the memory cell pointed by SI
SUB [DI], AL
; the contents of the memory cell at DS:DI, is subtracted from AL

 Same restrictions like ADD instruction

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 7


Arithmetic Instructions (INC & DEC)
 Increment (INC)

𝐈𝐍𝐂𝐝𝐞𝐬𝐭𝐢𝐧𝐚𝐭𝐢𝐨𝐧 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛+1
Examples
INC AX ;AX = AX + 1 (increment of 16 bits)
INC AL ;AL = AL + 1 (increments of 8 bits)
INC [SI] ;increment contents of memory location pointed by DS:SI

 Decrement (DEC)

𝐃𝐄𝐂𝐝𝐞𝐬𝐭𝐢𝐧𝐚𝐭𝐢𝐨𝐧 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛−1
Examples

DEC AX ;AX = AX - 1 (increment of 16 bits)


DEC AL ;AL = AL - 1 (increments of 8 bits)
DEC [SI] ;decrement contents of memory location pointed by DS:SI

Note: You can’t increment/decrement an immediate value


Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 8
Arithmetic Instructions (NEG & CMP)
 Negation (NEG)

𝐍𝐄𝐆𝐝𝐞𝐬𝐭𝐢𝐧𝐚𝐭𝐢𝐨𝐧 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←0− 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛


Examples
NEG AX ;AX = 0 - AX
NEG AL ;AL = 0-AL
NEG [IF] ;IF  0 - IF

 Compare (CMP)

𝐂𝐌𝐏 𝐝𝐞𝐬𝐭𝐢𝐧𝐚𝐭𝐢𝐨𝐧 , 𝐬𝐨𝐮𝐫𝐜𝐞 𝐴𝐿𝑈 ←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛− 𝑠𝑜𝑢𝑟𝑐𝑒


 It subtracts the source from destination without storing result in destination
 But flags are updated by the result of subtraction (AF, CF, OF, PF, SF, ZF)
 Usually the compare instruction is followed by a conditional jump instruction
 For example after executing “CMP AX, 5” we will have
 ZF = 1 if , or
 ZF = 0 if
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 9
Arithmetic Instructions (CMP Example)
Data SEGMENT
Array DW 10,20,30,40,75,89,55,43,21,77
Sum DW ?
ENDS
Code SEGMENT
Begin:
MOV AX,Data
MOV DS,AX
MOV BX,0
MOV AX,0
L1: ADD AX,Array[BX]
ADD BX,2
CMP BX,20
JNZ L1
MOV Sum,AX
MOV AH,4Ch
INT 21h
ENDS
END Begin
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 10
Arithmetic Instructions (FLAGS)
How Carry and Overflow flags are affected after arithmetic operation?

Carry (C)

 Indicates that the last ALU operation produces a carry bit


 If the operation is subtraction; it indicates that a borrow is needed
1
2 1 1 2 2 2
89h C 10001001 89h B 10001001
+ -
92h 10010010 92h 10010010
1Bh 1 00011011 111 10 11 1
Carry flag = 1 Carry flag = 1

 It indicates an overflow condition for unsigned numbers


 Overflow for unsigned numbers occurs when the result is greater than
 For 8-bits; overflow occurs when the result
 8-bits unsigned number ranges from

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 11


Arithmetic Instructions (FLAGS)
Overflow(O)
 It indicates an overflow condition for signed numbers
 Signed number ranges from
 For 8-bits; signed number ranges from
 Overflow for signed numbers occurs when the result is
 Greater than 127 or
 Less than -128

70 01000110 -70 10111010


+ +
60 01111000 -60 11000100
130 10111110 -130 1 01111110
130>127 −130 <−128
Overflow flag = 1 Overflow flag = 1
 We can deduce that overflow for signed number occurs if the result is
 –ve when adding two positive numbers, or
 +ve when adding two negative numbers
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 12
Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=-1 C O S Z A P
ADD AL,1 ; AL=0
1 0 0 1 1 1

1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 FF (-1)
+ (0)
0 0 0 0 0 0 0 1 1 (1)

1 0 0 0 0 0 0 0 0 00
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 13
Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=-1 C O S Z A P
ADD AL,1 ; AL=0
SUB AL,1

0 0 0 0 0 0 0 0 00 (0)
-
0 0 0 0 0 0 0 1 01 (1)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 14


Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=-1 C O S Z A P
ADD AL,1 ; AL=0
; AL=-1
1 0 1 0 1 1
SUB AL,1

It is
subtraction ,
so invert the
actual carry
(borrow)
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 00 (0)
+ (-1)
1 1 1 1 1 1 1 1 FF (-1)

0 1 1 1 1 1 1 1 1 FF (-1)
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 15
Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=-1 C O S Z A P
ADD AL,1 ; AL=0
0 1 1 0 1 0
SUB AL,1
MOV AL,127 ; AL=7Fh
ADD AL,1 ; AL=80

0 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 7F (127)
+ (128)
0 0 0 0 0 0 0 1 01 (1)

0 1 0 0 0 0 0 0 0 80 (-128)
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 16
Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=FFh C O S Z A P
ADD AL,1 ; AL=0
SUB AL,1 ; AL=FFh
MOV AL,127 ; AL=7Fh
ADD AL,1 ; AL=80h
MOV AL,26h
SUB AL,95h

0 0 1 0 0 1 1 0 26 (38)
-
1 0 0 1 0 1 0 1 95 (-107)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 17


Arithmetic Instructions (affected flags)
 For each of the following instructions, show the values of the
destination operand and the six status flags:
MOV AL,0FFh ; AL=FFh C O S Z A P
ADD AL,1 ; AL=0
; AL=FFh
1 1 1 0 0 0
SUB AL,1
MOV AL,127 ; AL=7Fh
ADD AL,1 ; AL=80h
MOV AL,26h
SUB AL,95h ; AL=91h

0 1 1 0 1 1 1 0
0 0 1 0 0 1 1 0 26 (38)
+ (145)
0 1 1 0 1 0 1 1 6B (107)

0 1 0 0 1 0 0 0 1 91 (-111)
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 18
Arithmetic Instructions (Multiply)
1) BYTE Multiplication
M UL𝑆 𝑜𝑢𝑟𝑐𝑒8
𝐴𝑋 ← 𝐴𝐿× 𝑆 𝑜𝑢𝑟𝑐𝑒8
 Performs the multiplication of AL register by a 1-byte operand
 The 16 bits result is placed in AX

2) WORD Multiplication
M UL𝑆 𝑜𝑢𝑟𝑐𝑒16
DX : AX ← 𝐴𝑋 × 𝑆 𝑜𝑢𝑟𝑐𝑒16
 Performs the multiplication of AX register by a word (2-bytes) operand
 The 32 bits result is placed in DX:AX

 The operand cannot be immediate, it is register/memory location


 If the operand is a memory location; you must specify the size (byte or word)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 19


Arithmetic Instructions (Multiply Examples)
MUL BL ;AL x BL → AX
MUL CX ;AX x CX → DX:AX
MUL BYTE [BX] ;AL x (byte referenced by BX) → AX
MUL WORD [BX] ;AX x (word referenced by BX) → DX :AX

MOV AL,51 MOV AL ,43


MOV BL,32 MOV BYTE PTR [1200H],28
MUL BL MUL BYTE PTR [1200H]
; → AX = 51 × 32 ;AX = 43 × 28

MOV AX,4253 MOV AX,1234


MOV BX,1689 MOV WORD PTR [1200H],5678
MUL BX MUL WORD PTR [1200H]
; DX:AX = 4253 × 1689 ; DX:AX = 1234 × 5678
 The flags O and C are set to 1, if the higher part of the result is not zero
 The higher part is AH for 8-bit multiplication and DX for 16-bit multiplication

MUL 35 MUL AX,BX MUL AX,35

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 20


Arithmetic Instructions (Divide)
1) BYTE Division
𝐃𝐈𝐕 𝑆 𝑜𝑢𝑟𝑐𝑒8
𝐴𝐿←𝑞𝑢𝑜𝑡𝑖𝑒𝑛𝑡 ( 𝐴𝑋 /𝑆 𝑜𝑢𝑟𝑐𝑒8 ) 𝐴𝐻 ←𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 ( 𝐴𝑋 /𝑆 𝑜𝑢𝑟𝑐𝑒8 )
 Divide an unsigned word in AX by a byte
 After execution; quotient is stored in AL, and remainder in AH

2) WORD Division
𝐃𝐈𝐕 𝑆 𝑜𝑢𝑟𝑐𝑒16
𝐴𝑋 ←𝑞𝑢𝑜𝑡𝑖𝑒𝑛𝑡( 𝐷𝑋 : 𝐴𝑋 / 𝑆𝑜𝑢𝑟𝑐𝑒16) 𝐷𝑋 ←𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 (𝐷𝑋 : 𝐴𝑋 / 𝑆 𝑜𝑢𝑟𝑐𝑒16)
 Divide an unsigned double word in DX:AX by a word
 After execution; quotient is stored in AX and remainder in DX
 The operand can be a register/memory location
 If the operand is a memory location; you must specify the size
 Attempting to divide by 0 or the quotient is too large to fit in AL/AX, 8086
will automatically execute a type 0 interrupt
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 21
Arithmetic Instructions (Divide)
 To divide a byte by a byte, it is necessary to put the dividend byte in AL and 0’s in AH
 Similarly, to divide a word by a word, put the dividend in in AX and 0’s in DX

Examples
MOV CH,10
MOV AX,353
DIV CH
;AL = 35 and AH = 3

MOV CH,10
MOV AX,3500
DIV CH
;Error divide by 0  INT 0 is executed

MOV [2000],300
MOV DX,0
MOV AX,37600
DIV WORD PTR [2000]
;AX = 125 and DX = 100
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 22
Next

 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 23


Shift and Rotation Instructions
 We discuss here instructions that work on binary bits
 Bit shifts
 Bitwise logical operations (later )
SHIFT

 They are used to


 Process bitwise data
 Rapid divide or multiply by a power of 2
 All these operations affect flag register

 Shift instructions move binary data to the left/right by shifting them


within a register/memory location
 Bits that are moved are replaced by zeros
 There are logical shifts (unsigned) and arithmetic shifts (signed)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 24


Shift and Rotation Instructions (SHL)
SHIFT LEFT: SHL reg/memory, cst
 Shift left the operand by a number of bits given by cst
 Shifting left 1 bit multiplies a number by 2
 The last bit shifted out from the left becomes the Carry Flag
 Shifting left n bits multiplies the operand by 2n (fast multiplication)

0
CF
Example
MOV DL,5 ; DL = 00000101b
SHL DL,2 ; DL = 00010100b = 20, Carry Flag = 0

C 5

1 00000101 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 25
Shift and Rotation Instructions (SHL)
SHIFT LEFT: SHL reg/memory, cst
 Shift left the operand by a number of bits given by cst
 Shifting left 1 bit multiplies a number by 2
 The last bit shifted out from the left becomes the Carry Flag
 Shifting left n bits multiplies the operand by 2n (fast multiplication)

0
CF
Example
MOV DL,5 ; DL = 00000101b
SHL DL,2 ; DL = 00010100b = 20, Carry Flag = 0

C 10

0 00001010 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 26
Shift and Rotation Instructions (SHL)
SHIFT LEFT: SHL reg/memory, cst
 Shift left the operand by a number of bits given by cst
 Shifting left 1 bit multiplies a number by 2
 The last bit shifted out from the left becomes the Carry Flag
 Shifting left n bits multiplies the operand by 2n (fast multiplication)

0
CF
Example
MOV DL,5 ; DL = 00000101b
SHL DL,2 ; DL = 00010100b = 20, Carry Flag = 0

C 20

0 00010100 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 27
Shift and Rotation Instructions (SHR)
SHIFT RIGHT: SHR reg/memory, cst
 Shift right the operand by a number of bits given by cst
 Shifting right 1 bit divides a number by 2
 The LSB is shifted out to the carry flag
 Shifting right n bits divides the operand by 2n (fast division)

0
CF
Example
MOV DL,80 ; DL = 01010000b = 80
SHR DL,2 ; DL = 00010100b = 20, CF = 0
80 C

0 10100000 1
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 28
Shift and Rotation Instructions (SHR)
SHIFT RIGHT: SHR reg/memory, cst
 Shift right the operand by a number of bits given by cst
 Shifting right 1 bit divides a number by 2
 The LSB is shifted out to the carry flag
 Shifting right n bits divides the operand by 2n (fast division)

0
CF
Example
MOV DL,80 ; DL = 01010000b = 80
SHR DL,2 ; DL = 00010100b = 20, CF = 0
40 C

0 01010000 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 29
Shift and Rotation Instructions (SHR)
SHIFT RIGHT: SHR reg/memory, cst
 Shift right the operand by a number of bits given by cst
 Shifting right 1 bit divides a number by 2
 The LSB is shifted out to the carry flag
 Shifting right n bits divides the operand by 2n (fast division)

0
CF
Example
MOV DL,80 ; DL = 01010000b = 80
SHR DL,2 ; DL = 00010100b = 20, CF = 0
20 C

0 00101000 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 30
Shift and Rotation Instructions (SAR)
SHIFT Arithmetic RIGHT: SAR reg/memory, cst
 It is like Shift right operation but with the newly created bit position
(MSB) filled with a copy of the sign bit
 SAR preserves the sign bit
 Shifting arithmetic right n bits divides a signed operand by 2n (fast
signed division)

CF

Example
MOV DL,-80 ; DL = 10110000b
SAR DL,2 ; DL = 11011000b = -20, CF = 0
-80 C

10110000 1
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 31
Shift and Rotation Instructions (SAR)
SHIFT Arithmetic RIGHT: SAR reg/memory, cst
 It is like Shift right operation but with the newly created bit position
(MSB) filled with a copy of the sign bit
 SAR preserves the sign bit
 Shifting arithmetic right n bits divides a signed operand by 2n (fast
signed division)

CF

Example
MOV DL,-80 ; DL = 10110000b
SAR DL,2 ; DL = 11011000b = -20, CF = 0
-40 C

11011000 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 32
Shift and Rotation Instructions (SAR)
SHIFT Arithmetic RIGHT: SAR reg/memory, cst
 It is like Shift right operation but with the newly created bit position
(MSB) filled with a copy of the sign bit
 SAR preserves the sign bit
 Shifting arithmetic right n bits divides a signed operand by 2n (fast
signed division)

CF

Example
MOV DL,-80 ; DL = 10110000b
SAR DL,2 ; DL = 11011000b = -20, CF = 0
-20 C

11101100 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 33
Shift and Rotation Instructions (ROL)
ROTATE LEFT: ROL reg/memory, cst
 Rotates each bit to the left, according to the count operand
 Highest bit is copied into the Carry Flag and into the LSB
 No bits are lost

CF

Example

MOV AL,11110000b
ROL AL,1 ; AL = 11100001b, CF = 1
MOV DL,3Fh ; DL = 00111111b
ROL DL,4 ; DL = 11110011b = F3h, CF = 1

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 34


Shift and Rotation Instructions (ROR)
ROTATE RIGHT: ROR reg/memory, cst
 Rotates each bit to the right, according to the count operand
 LSB is copied into the Carry Flag and into the MSB
 No bits are lost

CF

Example

MOV AL,11110000b
ROR AL, 1 ; AL = 01111000b, CF = 0
MOV DL, 3Fh ; DL = 00111111b
ROR DL, 4 ; DL = F3h, CF = 1

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 35

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy