5 - Introd. To Assem. Part 1
5 - Introd. To Assem. Part 1
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
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)
Example
¿ destination , source
𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛− 𝑠𝑜𝑢𝑟𝑐𝑒
Examples
𝐈𝐍𝐂𝐝𝐞𝐬𝐭𝐢𝐧𝐚𝐭𝐢𝐨𝐧 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛←𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛+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
Compare (CMP)
Carry (C)
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)
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)
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
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
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
CF
Example
MOV AL,11110000b
ROR AL, 1 ; AL = 01111000b, CF = 0
MOV DL, 3Fh ; DL = 00111111b
ROR DL, 4 ; DL = F3h, CF = 1