200 likes | 421 Views
Assembly Language. Arithmetic and Logic Instructions. Arithmetic Instructions: (Addition). A. Addition : (Flags affected: A,C,O,P,S,Z) ADD AL,BL ; AL AL + BL , BL unchanged ADD CX,DI ; CX CX + DI , DI unchanged ADD AH,45H ; AH AH + 45H
E N D
Assembly Language Arithmetic and Logic Instructions
Arithmetic Instructions: (Addition) A. Addition: (Flags affected: A,C,O,P,S,Z) • ADD AL,BL ; AL AL + BL , BL unchanged • ADD CX,DI ; CX CX + DI , DI unchanged • ADD AH,45H ; AH AH + 45H • ADD [BX],AL ; [BX] [BX] + AL • ADD CX,[BX] ; CX CX + [BX] • ADD AL,CX ; INVALID B. Add with Carry: (Flags affected: A,C,O,P,S,Z) • ADC AH,BH ; AH AH + BH + Carry • ADC AX,CX ; AX AX + CX + Carry • ADC AL,[BX+SI] ; AL AL + [BX+SI] + Carry
Arithmetic Instructions: (Subtraction) C. Subtraction: (Flags affected: A,C,O,P,S,Z) • SUB AL,BL ; AL AL - BL ;BL unchanged • SUB CX,DI ; CX CX - DI ;DI unchanged • SUB AH,45H ; AH AH - 45H • SUB BL,ARRAY ; BL BL - [ARRAY] • SUB [BX],AL ; [BX] [BX] - AL • SUB AL,CX ; INVALID D. Subtract with Borrow: (Flags affected: A,C,O,P,S,Z) • SBB AH,BH ; AH AH - BH - Carry • SBB AX,CX ; AX AX - CX - Carry • SBB AL,[BX+SI] ; AL AL - [BX+SI] - Carry
Arithmetic Instructions: (Increment, Decrement) E. Increment: (Flags affected: A,O,P,S,Z) • INC AL ; AL AL + 1 • INC SP ; SP SP + 1 • INC COUNT1 ; [COUNT1] [COUNT1] + 1 • INC BYTE PTR[BX] ; [BX] [BX] + 1 • INC WORD PTR[BX] ; [BX] [BX] + 1 F. Decrement: (Flags affected: A,O,P,S,Z) • DEC AL ; AL AL - 1 • DEC SP ; SP SP - 1 • DEC COUNT1 ; [COUNT1] [COUNT1] - 1 • DEC BYTE PTR[BX] ; [BX] [BX] - 1 • DEC WORD PTR[BX] ; [BX] [BX] - 1
Examples • What will be the values of the carry, overflow, sign and zero flags after the execution of each of the following instructions: • MOV DX,0 • DEC DX • MOV AX,720H • SUB AX, 0E6H • MOV DX,0 • DEC DX
Arithmetic Instructions: (Multiplication) G. Multiplication: (Flags affected: C,O, (A,P,S,Z are undefined)) Unsigned multiplication: • MUL CL ; AX AL * CL • MUL CX ; DX,AX AX * CX • MUL BYTE PTR [BX] ; AX AL * [BX] • MUL WORD PTR [SI] ; DX,AX AX * [SI] Signed multiplication (2's complement): • IMUL BL ; AX AL * BL • IMUL BX ; DX,AX AX * BX • IMUL BYTE PTR [BX] ; AX AL * [BX] • IMUL WORD PTR [SI] ; DX,AX AX * [SI]
Arithmetic Instructions: (Division) H. Division: (Flags affected: A,C,O,P,S,Z (all undefined)) Unsigned Division: • DIV CL ; AL Quotient of AX/CL ; AH Remainder of AX/CL • DIV CX ; AX Quotient of DX,AX/CX ; DX Remainder of DX,AX/CX Signed Division: • IDIV CL ; AL Quotient of AX/CL ; AH Remainder of AX/CL • IDIV CX ; AX Quotient of DX,AX/CX ; DX Remainder of DX,AX/CX
Arithmetic Instructions: (BCD and ASCII Operations) I. BCD and ASCII Arithmetic: • DAA ; Decimal Adjust for Addition • DAS ; Decimal Adjust for Subtraction • AAA ; ASCII Adjust for Addition • AAS ; ASCII Adjust for Subtraction • AAM ; ASCII Adjust for Multiplication • AAD ; ASCII Adjust for Division
Logic Instructions:(AND, OR, XOR, NOT, NEG and TEST) Logic Instructions: • AND AL,BL ; AL AL AND BL • AND CL,33H ; CL CL AND 33H • AND AX,[DI] ; AX AX AND [DI] • OR AL,BL ; AL AL OR BL • OR AX,1234H ; AX AX OR 1234H • XOR AL,CL ; AL AL EX-OR CL • XOR BH,0FH ; BH BH EX-OR 0FH • NOT CH ; CH 1's complement of CH • NOT AX ; AX 1's complement of AX • NEG CH ; CH 2's complement of CH (ALWAYS SETS CF) • NEG BX ; BX 2's complement of BX • TEST AL,30H ;Perform AL AND30H and set the flags. AL is unchanged.
Program Control Instructions :(Jump and Call) Unconditional jump (JMP): • The JMP instruction specifies the address of the next instruction to be executed. There are three types of unconditional jump instructions: the SHORT, the NEAR, and the FAR. A SHORT jump is specified with only one byte which represents the displacement between the current instruction to the next instruction. The next instruction can be located at a distance from +127 to -128 memory locations away from the current instruction. A NEAR jump specifies the address of the next instruction within the current Code Segment. A FAR jump specifies the exact address of the next instruction by specifying the values of the CS and IP registers. • Examples: • JMP NEXT • JMP SHORT AGAIN • JMP NEAR AGAIN • JMP FAR AGAIN
Program Control Instructions :(Jump and Call) Conditional jump: • Conditional jumps are executed only if the specified conditions are true. Usually the condition specified by a conditional jump instruction is the state of a flag. A list of the conditional jump instructions that check the state of flags is given below: InstructionFlags testedAction JC C = 1 Jump if carry set JZ Z = 1 Jump if equal or zero JS S = 1 Jump on sign (Negative) JO O = 1 Jump on overflow JNC C = 0 Jump if not carry JNZ Z = 0 Jump if not equal or 0 JNS S = 0 Jump if not sign (Positive) JNO O = 0 Jump if not overflow
Program Control Instructions :(Jump and Call) Conditional jump using the Compare instruction: • Conditional jump instruction can be used after the compare ( CMP ) instruction. • Comparison of unsigned numbers is done using the Above or Below conditions. For example 81H is Above 7EH since 129>128. • Comparison of signed numbers is done using the Greater or Less conditions. For example 81H is Less than 7EH since -127<128. • The programmer can choose between the Above/Below or Greater/Less according to the application. If the values used in a program are always positive then these values are treated as unsigned numbers, and the Above /Below conditions are used, otherwise signed numbers are used and the Greater/Less conditions are used..
Program Control Instructions :(Jump and Call) A list of the conditional jump instructions used with the Compare instruction is given below: InstructionFlags testedAction JA C = 0 & Z = 0 Jump if above JAE C = 0 Jump if above or equal JB C = 1 Jump if below JBE C = 1 or Z = 1 Jump if below or equal JE or JZ Z = 1 Jump if equal or zero JG O = Z AND S Jump if greater JGE S = O Jump if greater or equal JL S = O Jump if less JLE Z = 1 or S = O Jump if less or equal JNE or JNZ Z = 0 Jump if not equal or 0
Program Control Instructions :(Jump and Call) Loops: • The LOOP instruction is a combination of the conditional jump and the decrement CX instructions. It will decrement the contents of CX and, if CX is not zero, jump to the label associated with the LOOP. If CX becomes zero, then the next sequential instruction is executed. • The LOOP instruction can also have conditional forms LOOPE (LOOPZ), and LOOPNE (LOOPNZ). • The conditional jump instruction JCXZ (Jump if CX = 0) can also be used. Procedures: • Procedures are implemented by using the PROC directive. The last instruction in a procedure must be the RET instruction. A procedure can be called by using the CALL instruction.