Professional Documents
Culture Documents
Arithmetic and Logic Instructions: Chapter 4
Arithmetic and Logic Instructions: Chapter 4
Arithmetic and Logic Instructions
Objectives
• Arithmetic and Logic Instructions and Programs
• arithmetic operations with 8 & 16 bit unsigned numbers
• Code the Assembly language logic instructions (AND, OR, XOR, CMP & logical
shift instructions)
• Code bitwise rotation instructions (ROR, ROL, RCR & RCL)
Overview of EMU886
[Single Step] button executes instructions
one by one stopping after each instruction.
• Open Emu8086,
• Load Example (Hello World ) from file menu
• Rune line by line the code and observe the
output
• Which register change at every
instruction execution
• Which flags are get affected by the code
• Now, Change the text color to Blue and run
UNSIGNED ADDITION AND SUBTRACTION
• The ADD instruction is:
• When "LOOP xxxx" is executed, CX decrements automatically, and if CX is not 0, the processor will
jump to target address xxxx. If CX is 0, the next instruction (below "LOOP xxxx") is executed.
…UNSIGNED ADDITION AND SUBTRACTION
• The SUB instruction is:
–1. Take the 2's complement of the subtrahend.(source operand)
–2. Add it to the minuend. (destination operand)
–3. Invert the carry.
Example
…UNSIGNED ADDITION AND SUBTRACTION
• SBB is used for multibyte (multiword) numbers.
It will take care of the borrow of the lower operand.
• If the carry flag is 0, SBB works like SUB.
• If the carry flag is 1, SBB subtracts 1 from the result.
*The PTR (pointer) data specifier directive is used to specify the size of the operand.
Exercise
• Write a code that adds two unsigned DD numbers and subtract a third DD from the result.
(USE Emu8086)
• How can you tell the sign of the result?
Num1= 19963CEH
Num2= 4FA23B8H
Num3= A58DE22H
•What would happen if
you replaced the lines:
with
UNSIGNED MULTIPLICATION & DIVISION
…UNSIGNED MULTIPLICATION & DIVISION
byte × byte
word × word
…UNSIGNED MULTIPLICATION & DIVISION
byte/byte
• the numerator must be in the AL register and AH must be set to zero
• Various addressing modes of the denominator
Exercise (Homework)
1. Write an assembly code that multiply 1DH by 13H and divide the
result by 10H. Read the result from the appropriate register.
2. Modify the above code to multiply 112DH by 213H and divide the
result by 10H. Read the result from the appropriate register.
1, 2,
.DATA .DATA
DATA1 DB 1DH DATA1 DW 112DH
DATA2 DB 13H DATA2 DW 213H
DATA3 DB 10H DATA3 DW 10H
RESULT1 DB 2 DUP (?) RESULT1 DW 2 DUP (?)
QOUNT DB 1 DUP (?) QOUNT DW 1 DUP (?)
REM DB 1 DUP (?) REM DW 1 DUP (?)
.CODE .CODE
MAIN PROC FAR MAIN PROC FAR
MOV AX, @DATA MOV AX, @DATA
MOV DS. AX MOV DS. AX
MOV AL, OFFSET DATA1 MOV AX, DATA1
MOV BL, OFFSET DATA2 MOV BX, DATA2 or ignore
MUL DATA2 or= MUL BL MUL DATA2 or= MUL BX
MOV RESULT1, AX MOV RESULT1, AX
MOV RESULT1+2, DX
MOV AL, RESULT1
SUB AH, AH MOV AX, RESULT1
MOV BH, DATA3
DIV BH MOV DX, RESULT1+2
MOV QOUNT, AL SUB DX, DX
MOV REM, AH MOV BH, DATA3 or ignore
MAIN ENDP
END MAIN
LOGIC INSTRUCTIONS
AND
• AND will automatically change the CF & OF to zero.
– PF, ZF, and SF are set according to the result.
• The rest of the flags are either undecided or unaffected. c
• AND can mask certain bits of the operand, and also to test
for a zero operand:
Identify Flags value using Emu8086: SF, ZF, PF, CF, OF
LOGIC INSTRUCTIONS
OR
•Flags are set the same as for the AND instruction.
–CF & OF will be reset to zero.
•SF, ZF, and PF will be set according to the result.
•All other flags are not affected.
Example code: uses CMP to find the highest byte
in a series of 5 bytes defined in the data segment
ROTATE INSTRUCTIONS
• ROR, ROL and RCR, RCL are designed specifically to
perform a bitwise rotation of an operand.
– They allow a program to rotate an operand right or left.
• Similar to shift instructions, if the number of times an
operand is to be rotated is more than 1, this is indicated
by CL.
– The operand can be in a register or memory.
• There are two types of rotations.
– Simple rotation of the bits of the operand
– Rotation through the carry.
ROTATE INSTRUCTIONS
ROR rotate right
– As each bit exits LSB, a copy is given to the carry flag.
• In ROR the LSB is moved to the MSB, & copied to CF.
– Register CL is number of times to be rotated.
ROTATE INSTRUCTIONS
ROR rotate right
–Every bit that leaves the MSB is copied to the carry flag.
•In ROL the MSB is moved to the LSB and is also copied to CF
– Register CL is number of times to be rotated.
ROTATE INSTRUCTIONS
RCR right through carry
– The LSB is moved to CF and CF is moved to the MSB.
• CF acts as if it is part of the operand.
– register CL holds the number of rotations.
ROTATE INSTRUCTIONS
RCL left through carry
– The MSB is moved to CF and CF is moved to the LSB.
• CF acts as if it is part of the operand.