Week3 - WordPress.com

advertisement
Arithmetic and Logical Instructions
Addition (ADD)
All addressing modes for addition are allowed except; memory to memory and segment registers
ADD AL,BL
;AL = AL + BL
ADD DX,CX
ADD BX,254FH
ADD [BX],AL
ADD [SI+2], BX
ADD BX,TEMP[DI]
ADD BYTE PTR [DI],3
Increment Addition (INC)
Adds 1 to a register or a memory location.
INC BL
INC SP
INC WORD PTR [BX]
INC DATA1
Addition-with-Carry (ADC)
ADC AL,AH
;AL = AL + AH + Carry
ADC DH,[BX]
Example:
ADD AX,CX
ADC BX,DX
Subtraction (SUB)
All addressing modes for addition are allowed except; memory to memory and segment registers
SUB AL,BL
;AL = AL - AH
SUB AX,SP
SUB DX,254FH
SUB [DI],CH
SUB [SI+2], BX
SUB BX,TEMP[SI]
SUB BYTE PTR [DI],9
Decrement Subtraction (DEC)
Subtract 1 from a register or contents of a memory location.
DEC BH
DEC CX
DEC WORD PTR [SI]
DEC NUMB
Subtract-with-Borrow (SBB)
SBB AH,AL
; AH = AH - AL - Carry
Example:
SUB AX,DI
SBB BX,SI
Comparison (CMP)
It is a subtraction that changes only the flag bits; destination operand never changes. Use to check
Or compare the contents or values in the register or memory location. It followed by a conditional
jump instructions (like; JA, JB, JAE, JBE).
CMP CL,BL
; CL - BL
CMP AX,SP
CMP AX,2000H
CMP DI,TEMP[BX]
Example:
CMP
JAE
……
……
……
NEXT: ……
……
……
AL,10H
NEXT
.…,….
.…,….
.…,….
.…,….
.…,….
.…,….
Multiplication (MUL or IMUL):
i) 8-bit Multiplication: (Byte x Byte)
• Multiplicand is always in AL register
• Multiplier can be any 8-bit register or a memory location
• µP always places product or result in AX register
MUL CL
IMUL BH
Example:
MOV BL, 5
MOV BL, 5
MOV AL, 10
MOV AL, 10
MUL BL
IMUL BL
MOV MRES1, AX
MOV MRES2, AX
ii) 16-bit Multiplication: (Word x Word)
• Multiplicand is always in AX register
• Multiplier can be any 16-bit register or a memory location
• µP always places product or result Higher Byte in DX and Lower Byte in AX register
MUL CX
IMUL BX
Division (DIV or IDIV)
Division can results in two types of errors
 Divide by ZERO
 Divide Overflow (3000 / 2 results 1500 a larger number, which can not be stored to a register)
i) 8-bit Division: (WORD / Byte)
• Dividend is always in AX register
• Multiplier can be any 8-bit register or a memory location
• µP always places result of division quotient in AL and remainder in AH register after division
DIV CL
MOV AX,0042H
IDIV BL
MOV BL,02H
DIV BYTE PTR [BX]
DIV BL
MOV ANSQ,AL
MOV ANSR,AH
ii) 16-bit Division: (DWORD / WORD)
• Dividend is always in DX -AX registers
• Multiplier can be any 16-bit register or a memory location
• µP always places result of division quotient in AX and remainder in DX register after division
Logic Instructions:
AND instruction perform logical multiplication (dot-operation). It clears (0) the bit (s) of binary
numbers. The task of clearing a bit is called masking.
AND AL, CL
AND CX,DX
AND BL,33H
AND AX,[DI]
AND Array[SI],DH
Example
MOV BX,3135H
AND BX,0F0FH
OR instruction perform logical addition (+ operation). It sets (1) the bit (s) of binary
numbers.
OR
AL, CL
OR
CX,DX
OR
BL,33H
OR
AX,[DI]
OR
Array[SI],DH
Example
MOV BX,0507H
OR
BX,3030H
Exclusive OR It inverts the bit (s) of binary numbers.
XOR CH, DL
XOR SI,BX
XOR CH,55H
XOR DX,[SI]
XOR DEAL[DI+7],AH
Example
OR
CX,0600H
;set bits 9 and 10
AND CX,0FFFCH ;clear bits 0 and 1
XOR CX,1000H
;invert bit 12
TEST Instruction performs AND operation but doesn't change the destination operand. It only
effects the condition of flag register (Z-flag). It tests a bit or multiple bits.
TEST AH,4
TEST CX,BX
Example:
TEST AL,1
JNZ RIGHT
TEST AL,128
JNZ LEFT
;TEST rightmost bit
;if set
;TEST leftmost bit
;if set
NOT and NEG NOT performs logical inversion (1’s compliment) and NEG performs arithmetic
sign inversion (2’s complement).
NOT CH
NEG CH
NOT TEMP
NEG BX
SHIFT position or move numbers to the left or right within a register or memory location. Also
Performs multiplication by powers of 2+𝑛 (left shift) and division by powers of 2−𝑛 (right shift).
It has four instructions (Two logical and two arithmetic shifts).
SHL
DX,14
Rotate position binary data by rotating the information in register or memory location.
It has four instructions (Rotate with carry and without carry).
ROL
RCL
SI,14
BL,6
Assembly Language Programming
.MODEL SMALL
.STACK 64
.DATA
DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
.CODE
MAIN PROC FAR
MOV
MOV
MOV
BYTES
AX,@DATA ; this is the program entry point
DS,AX
; load the data segment address
AX,0000H ; assign value to DS
MOV AH,DATA1 ; move the first operand
MOV BL,DATA2 ; move the second operand
ADD AH,BL
; Add the operands
MOV SUM,AH ; store the results in location SUM
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
; set-up to return to OS
; this the program exit point
Directives (pseudo-instructions):
ORG (origin) it indicates the beginning of offset address.
DUP (duplicate) is used to duplicate a given number of characters.
EQU (equate) is used to define a constant without occupying a memory location.
COUNT
EQU 25
;
COUNTER1 DB
COUNT
;direct addressing mode
MOV
CX,COUNT
LEA (Load Effective Address)
IN and OUT instructions performs I/O operations
IN
AL, p8
IN
AX,p8
IN
AH,DX
OUT DX,AL
OUT p8, AL
OUT p16,AX
Machine Language:
• is the native binary code that the microprocessor and uses as its instructions to control its
operation.
• 8086 are 16-bit mode instructions
OPCODE: It selects the operation (addition, subtraction, move, and so on) performed by the µP.
(D) bit:
(W) bit:
D=1 data flow to the register REG field from R/M field.
D=0 data flow to the register R/M field field from REG.
W=1 data size is word or doubleword
W=0 data size is always a byte
MOD Field: it selects the addressing mode for the selected instruction and weather the
displacement is present.
[address-size override prefix (67H) for the selection of 16-bit instruction if instruction mode 32-bit]
[register-size override prefix (66H) for the selection of 16-bit register if register mode is 32-bit]
If MOD=11, register addressing mode uses R/M field to specify a register instead of memory location. MOV AL, BL
If MOD=00,01 or 10, R/M field to selects one of the data memory addressing mode.
MOV AL, [DI] MOV AL, [DI±2]
MOV AL, [DI±1000H]
Register Field: Below table shows register assignment for REG filed and the R/M field (MOD=11)
otherwise
Example (MOD=11): OPCODE for MOV is 100010
8BECH
1000 1011 1110 1100B
Example (MOD=00):
Instruction 8A15H
MOV DL,[DI]
MOV BP,SP
Assignment # 3
 String Data Transfer
 Miscellaneous Data Transfer Instructions
 String Comparisons (SCAS, CMPS)
Download