• No results found

ROTATE INSTRUCTIONS

In document Lyla b das3.pdf (Page 41-46)

TYPICAL APPLICATIONS OF LOGICAL INSTRUCTIONS Now ,let us use these logical instructions in some simple applications

2) SHR –shift right logical Usage:SHR dest,count

3.5.2 ROTATE INSTRUCTIONS

Rotate instructions have the same format as the shift instructions .There are two types of rotate – rotate through carry or without taking the carry bit as a part of the rotate act.

ROL – Rotate Left Usage: ROL dest, count

This instruction rotates the bits in the destination to the left "count" times with all data pushed out at the left re-entering on the right. The Carry Flag will contain the value of the last bit rotated out.

Modifies Flags: CF OF

Examples

ROL SI, CL ;rotate left the word in SI ,by the count in CL ROL BYTE PTR [DI][BX],1 ;rotate left the byte pointed by EA=[DI+BX] once ROR - Rotate Right

Usage: ROR dest,count

This instruction rotates the bits in the destination to the right ‘count’ times with all data pushed out at the right side re-entering on the left. The Carry Flag will contain the value of the last bit rotated out.

Modifies Flags: CF OF Examples

ROR AL,1 ;rotate right once the byte in AL

ROR DX,CL ;rotate right the word in DX the count in CL Let us examine what Example 3.25 will do.

Example 3-25

.MODEL TINY .CODE

.STARTUP MOV BX,5634H MOV CL,8

MOV AL,0C6H MOV CL,4 ROR AL,CL .EXIT END

In the first instance ,there is a 16 bit register BX which contains the value 5634H..By using ROL ,and rotating 8 times the value in BX changes to 3456H. .Similarly with AL=C6H , using ROR with a count of 4 ,Al will have the new value of 6C H. Thus we can switch exchange nibbles or bytes using the rotate instructions with the appropriate count in CL . Note that ,for this application ,it does not matter if ROL or ROR is used.

It is easy to check that Example 3-24 can be re-written replacing the SHR instruction with either the ROL or ROR instruction.

RCL - Rotate Through Carry Left Usage: RCL dest, count

This instructions causes the left most bit (LSB) to enter the carry flag ,and the CF enters through the left end(MSB) Thus ,due to one shift operation ,the MSB enters the CF ,and the CF gets into the LSB position.

Modifies Flags: CF OF

Examples

RCL BL,CL ; rotate left through carry , BL ,by the count in CL RCL CX,1 ; rotate left through carry the word in CX once

RCR - Rotate Through Carry Right Usage: RCR dest, count

This instructions causes the right most bit (MSB) to enter the carry flag ,and the CF enters through the left end(MSB) Thus ,due to one shift operation ,the LSB enters the CF ,and the CF gets into the MSB position.

Modifies Flags: CF OF Examples

RCR BYTE PTR [SI] ,1 ;rotate right through carry ,the byte pointed by SI,once RCR WORD PTR [DI][BX],CL ;rotate right through carry the word pointed by EA=SI+BX ,by the count specified in CL Example 3.26

Find the values in the destination for each line of this program segment.

STC

MOV AX,5485H RCR AL,1 MOV CL,03 RCL AX,CL MOV CL,05 ROR AX,CL ROL AX,1 Solution:

Instructions Result after execution

STC CF=1

MOV AX,5485H AX=0101 0100 1000 0101 RCR AL,1 AL=1100 0010

MOV AL,03 CL=03

RCL AX,CL AX=1010 0110 0001 0101 MOV CL,05 CL=5

ROR AX,CL AX=1010 1101 0011 0000 ROL AX,1 AX=0101 1010 0110 0001

Key Points of this chapter

 BIOS and DOS function calls facilitate the use of input and output devices in programming, making it interactive and more fun.

 The instruction set of 8086 can be divided into groups based on functionality.

 MOV is the most frequently used instruction and it is part of the data transfer group.

 LEA is an instruction that copies the offset of a memory address to a 16 bit register. It can be used to make the register be a pointer to an array of data items in memory.

 PUSH and POP can be used only with the stack segment

 The SS register contains the upper 16 bits of the lowest (base) address in the stack segment. SP contains the offset (with respect to the base address ) of the highest address in the stack.

 Branch instructions cause the program sequence to change.

 JUMP is an important branch instruction which can be far or near ,and unconditional or conditional.

 Jumps using the direct mode of addressing are re-locatable and ‘relative’.

 LOOP is a jump instruction which causes branching as well as decrementing a count in CX. A LOOP instruction can be conditional ,as well.

 There are 20 arithmetic instructions for 8086 ,of which a few specifically cater to signed numbers, a few to BCD numbers ,and a few to ASCII numbers.

 The ADC(Add with carry) and SBB (Subtract with borrow) instructions find use in multibyte additions and subtractions.

 The INC and DEC instructions do not affect the carry flag.

 The CMP (Compare) instruction subtracts the source from the destination and sets/resets flags ,but neither the source nor the destination is altered.

 MUL is the mnemonic for unsigned multiplication .For a byte by byte multiplication ,one operand is implied to be in AL and the product in AX. For a word by word multiplication , one operand is to be in AX and the product will be in DX-AX.

 DIV is the mnemonic for unsigned division. For a word by byte division ,the dividend is to be in AX .After division ,the quotient will in AL ,and the remainder in AH .For a double word by word division ,the dividend is to be in DX-AX .Then the quotient will be in AX and the remainder in DX.

 If the quotient is too large to fit in the register assigned for it , a ‘divide by zero’

error will be generated.

 The logical instructions of 8086 are AND,OR,XOR,NOT and TEST.

 The TEST instruction does logical ANDing ,but only the flags are affected.

 Shifting of a data can be done left or right ,and also by as many positions as required.

 Arithmetic shifting is meaningful for signed numbers , while logical shifting pertains to unsigned arithmetic.

 Rotation can be done left or right and can be direct or through the carry flag.

QUIZ

1. Distinguish between the top-down and bottoms-up approach in programming.

2. What is the use of DOS function calls in assembly programming?

3. How many operands do each of the following instructions have?

a. ADD ii) ADC iii) INC

4. Which conditional flag is not affected by the DEC instruction?

5. If AX=5600H and BX=0C07H ,find the contents of CX and DX after the following three sets of instructions.

i) PUSH AX ii) PUSH AX iii) PUSH BX PUSH BX PUSH BX PUSH AX

POP CX POP DX POP CX POP DX POP CX POP DX

6. Why is the stack pointer called the TOP OF STACK?

7. Distinguish between a near and a far jump.

8. Specify two ways of specifying an unconditional jump in the indirect mode of addressing.

9. Why does the far jump have five bytes of instruction length?

10. Name a conditional jump instruction that does not test a conditional flag.

11. What are the conditions under which a LOOPZ instruction exits looping?

12. What does the instruction STC do?

13. Can the ADD instruction use CS as a destination?

14. The instruction INC [SI] seems ambiguous .Why? How can the ambiguity be corrected?

15. What does the instruction CMP AX,BX do?

16. What is the role of OF and CF in multiplication?

17. What happens if the DIV instruction is used for a dividend of 1938H and divisor of 05?

18. What is meant by masking? Give an example of how it is used?

19. How can multiplication and division be achieved by shift operations?

20. What is the difference between the instructions RCL and ROL?

EXERCISE

1) Write a program to print on two separate lines ,the messages ‘Hi, folks’

and ‘I am Samantha’.

2) Enter a character through the keyboard without echoing it on the video monitor.

Compare it with a number stored in memory and display the message ‘EQUAL’

or ‘NOT EQUAL’ as the case may be.

3) Display each character of the word PRADO on a separate line.

4) Enter ‘N’ characters through the keyboard without echo. Save it in memory as an array .Then display it as a character string.

5) Indicate what is wrong with each of these instructions.

i. MOV CX,DL

ii. ADD DATA1,0978H

iii. MOV BYTE PTR [SI][BX],DX iv. MOV 045FH,AX

v. MOV [DX],AL vi. MOV [SI][CX].AX vii. MOV DS,3453H

6) Write a program that adds two quad words and stores the result in memory.

7) Write a program that subtracts two double words and stores the result in memory.

8) Find the status of the CF and ZF flags after the execution of the following sets of instructions.

i) MOV AX,9078H CMP AX,0C089H ii) XOR AL,AL iii) MOV AL,29H

CMP AL,0

9) Find the biggest number in an array of i) bytes ii) words. Display the biggest number in each case ,as a decimal number.

10) There are 10 unsigned bytes stored in memory .Arrange these bytes in i) ascending and ii) descending order.

11) Find the average of 20 bytes stored in memory .Display the average as a decimal number.

12) Add the sum of the first 20 natural numbers .Display the sum.

13) Write a program that counts the number of 1s in a binary number.

14) Write a program to add two N x N matrices.

15) Find the number of times a particular character is present in a character string stored in memory.

In document Lyla b das3.pdf (Page 41-46)

Related documents