LEC 4 Assembly
LEC 4 Assembly
2
Types of instructions (2)
3
Next ...
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
4
Arithmetic instructions (1)
The basic arithmetic instructions are addition, subtraction, multiplication and division
that includes variants. Several addressing modes are possible
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)
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
Examples
ADD Tab1, Tab2 ; not allowed
; will be replaced by:
MOV AX, Tab2
ADD Tab1, AX
5
Arithmetic instructions (2)
Increment: INC Destination ; Destination =Destination + 1
Examples:
INC AX ; AX = AX + 1 (increment of 16 bits)
INC AL ; AL = AL + 1 (increments of 8 bits)
INC [SI] ; the contents of the memory cell pointed by SI is incremented
6
Arithmetic instructions (3)
Decrement: DEC Destination ; Destination = Destination - 1
Examples:
DEC AX ; AX = AX - 1 (decrement of 16 bits)
DEC AL ; AL = AL - 1 (decrements of 8 bits)
DEC [SI] ; the contents of the memory cell pointed by SI is decremented
Note: You can’t decrement an immediate value
1 0 0 1 0 0 0 1 0 1 1 0 1 1 1 0
10
Next ...
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
11
Shift and Rotation instructions (1)
We discuss here the instructions that work on the binary representation:
• bit shifts
• bitwise logical operations
They are used to
• A decode bitwise data
• divide or multiply rapidly by a power of 2
All these operations change the state indicators
These operations shift to the left or to the right Cste-bits of the accumulator
They can operate on AX, BX or DX (16 bits) or AH, AL, BH,BL, DH or DL (8 bits)
In shifts, the bits that are moved are replaced by zeros
There are logical shifts (unsigned operations) and arithmetic shifts (signed operations)
In the shift instructions, the operand can be To shift the contents of a memory cell
a constant Cste (immediate) or the CL register: you must specify the size :
INST AX, 1 ; 1-bit shift AX INST byte [BX], 1 ; 1-bit shift of the
INST BL, 4 ; 4-bits shift BL contents of memory cell referenced by
INST BX, CL ; CL-bits shift BX BX
12
Shift and Rotation instructions (2)
SHL ; Shift Left instruction
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
Carry Flag
Example : 5 * 22 = 20
MOV DL,5 ; DL = 00000101b
SHL DL,2 ; DL = 00010100b = 20, Carry Flag = 0
13
Shift and Rotation instructions (3)
SHR ; Shift Right instruction
Performs a logical right shift on the destination operand
The last bit shifted out from the right becomes the Carry Flag
Shifting right n bits divides the operand by 2n (fast division)
0
CF
14
Shift and Rotation instructions (4)
SAR ; Shift Arithmetic Right
Performs a right arithmetic shift on the destination operand
Fills the newly created bit position with a copy of the sign bit
SAR preserves the number's sign
CF
Example
MOV DL,-80 ; DL = 10110000b
SAR DL,1 ; DL = 11011000b = -40, CF = 0
SAR DL,2 ; DL = 11110110b = -10, CF = 0
15
Shift and Rotation instructions (5)
ROL ; Rotate Left instruction
Rotates each bit to the left, according to the count operand
Highest bit is copied into the Carry Flag and into the Lowest Bit
No bits are lost
Example CF
MOV AL,11110000b
ROL AL,1 ; AL = 11100001b, CF = 1
MOV DL,3Fh ; DL = 00111111b Provide finer control
ROL DL,4 ; DL = 11110011b = F3h, CF = 1 over bits than HLL
17
Boolean Instructions (1)
AND destination, source ; Bitwise AND between each pair of matching bits
Following operand combinations are allowed
AND R, R We use the following abbreviations:
AND R, Adr INST: Instruction, Off: Offset address
R: any Register, Cste: given (Constant)
AND R, Cste SR: Segment Register disp: Displacement (Constant)
AND Cste, R OR: Offset Register Op: Operand
Adr: Address SO: Source Operand
AND Cste, Cste [Adr]: Memory content DO: Destination Operand
Operands can be 8 or 16 bits and they must be of the same size AND
AND instruction is often used to clear selected bits
00111011
AND0 0 0 0 1 1 1 1
cleared 00001011 unchanged
18
Boolean Instructions (2)
OR destination, source ; Bitwise OR between each pair of matching bits
Following operand combinations are allowed
OR R, R
OR R, Adr OR
OR R, Cste
OR Cste, R
OR Cste, Cste
Operands can be 8 or 16 bits and they must be of the same size
OR instruction is often used to set selected bits
00111011
OR 11110000
set 11111011 unchanged
19
Boolean Instructions (3)
Converting Characters to Uppercase Use the AND instruction to clear bit 5
20
Boolean Instructions (4)
XOR destination, source ; Bitwise XOR between each pair of matching bits
Following operand combinations are allowed
XOR R, R
XOR R, Adr
XOR R, Cste XOR
XOR Cste, R
XOR Cste, Cste
Operands can be 8 or 16 bits and they must be of the same size
XOR instruction is often used to invert selected bits
00111011
XOR 1 1 1 1 0 0 0 0
21
Boolean Instructions (5)
NOT destination ; Inverts all the bits in a destination operand
Result is called the 1's complement NOT 00111011
NOT R
NOT Adr