1 / 24

Assembly Language

Assembly Language. Shift Rotate Instructions. Shift Instructions. Shifting means to move bits right and left inside an operand. There are two basic ways to shift bits in a number. The first, called logical shift , fills the newly created bit position with zero.

morelock
Download Presentation

Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly Language Shift Rotate Instructions

  2. Shift Instructions • Shifting means to move bits right and left inside an operand. • There are two basic ways to shift bits in a number. The first, called logical shift, fills the newly created bit position with zero. • For example, a single logical right shift on the binary value 11001111, it becomes 01100111. • The other type of shift is called an arithmetic shift. The newly created bit position is filled with a copy of the original’s sign bit. • For example, binary value 11001111 has a 1 as sign bit. When shifted one bit to the right, it becomes 11100111. Boolean Instructions

  3. Shift Instructions • SHL Instruction • The SHL instruction performs a logical left shift on the destination operand, filling the lowest bit with zero. The highest bit is moved into the Carry Flag. • Syntax: • SHL destination, count • Example: • MOV bl, 8Fh ; 10001111 • SHL bl, 1 ; 00011110 , CF=1 Boolean Instructions

  4. Shift Instructions • SHL Instruction • Since shifting an integer value to the left one position is equivalent to multiplying that value by two, you can also use the shift left instruction for multiplication by powers of two: • shl ax, 1 ;Equivalent to AX*2 • shl ax, 2 ;Equivalent to AX*4 • shl ax, 3 ;Equivalent to AX*8 • shl ax, 4 ;Equivalent to AX*16 • shl ax, 5 ;Equivlaent to AX*32 • etc. Boolean Instructions

  5. Shift Instructions • SHL Instruction • Example: • Example of multiplication: • MOV dl, 5 • SHL dl, 1 • Before: 00000101 =5 • After : 00001010 =10 Boolean Instructions

  6. Shift Instructions • SHR Instruction • SHR (shift right) instruction performs a logical right shift on the destination operand, replacing the highest bit with a 0. • The lowest bit is copied into the Carry Flag. • SHR syntax: • SHR destination, count • Example: • MOV al, D0h ; AL=11010000 • SHR al, 1 ; AL=01101000 CF=0 Boolean Instructions

  7. Shift Instructions • SHR Instruction • Since shifting an unsigned integer value to the right one position is equivalent to dividing that value by two, you can also use the shift right instruction for division by powers of two: • shr ax, 1 ;Equivalent to AX/2 • shr ax, 2 ;Equivalent to AX/4 • shr ax, 3 ;Equivalent to AX/8 • shr ax, 4 ;Equivalent to AX/16 • shr ax, 5 ;Equivlaent to AX/32 Boolean Instructions

  8. Shift Instructions • SHR Instruction • Example: • MOV dl, 0Ah • SHR dl, 1 Before: 00001010 = 10 After : 00000101 = 5 • Remember that division by two using SHR only works for unsigned operands. For signed numbers, use SAR. Boolean Instructions

  9. Shift Instructions • Shifting means to move bits right and left inside an operand. • SAL instruction • Stands for Shift Arithmetic Left. • This instruction move each bit in the destination operand one bit position to the left according to the number of times specified by the count operand. • Syntax • SAL destination, count @ SAL destination • Destination is the value to shift and count specifies the number of bit positions to shift. • Zeros are shifted into the low-order bit Boolean Instructions

  10. Shift Instructions • Example: • MOV al, F0h ;11110000h • SAL al, 1 ;11100000h • The high order bit will be shifted into the carry flag. Boolean Instructions

  11. Shift Instructions • SAL instruction sets the flags as follows: • If the shift count is zero, it will not affect any flags. • The carry flag contains the last bit shifted out of the H.O bit of the operand. • Zero flag will be one if the shift produces a zero result. • The sign flag will contain the H.O bit of the result. • The parity flag will contain one if there is an even number of one bits in the L.O byte of the result. • Overflow flag will contain one if the two H.O bits were different prior to a single bit shift. The overflow flag is undefined if the shift count is not one. Boolean Instructions

  12. Shift Instructions • SAR instruction • The SAR instruction shifts all the bits in the destination operand to the right one bit, replicating the H.O bit. • Syntax: • SAR destination, count • The L.O bit will be shifted into the carry flag. • It’s main purpose is to perform a signed division by some power of two. Each shift to the right divides the value by two. Boolean Instructions

  13. Shift Instructions • SAR instruction • Example: • MOV al, F0h ; AL= 11110000h (-16) • SAR al, 1 ; AL = 11111000h (-8) • The above example shows how SAR duplicates the sign bit. AL is negative before and after it is shifted to the right. Boolean Instructions

  14. Rotate Instructions • The rotate instructions shift the bits around, just like the shift instructions, except the bits shifted out of the operand by the rotate instructions recirculate through the operand. They include rcl (rotate through carry left), rcr (rotate through carry right), rol (rotate left), and ror (rotate right). Boolean Instructions

  15. Rotate Instructions • ROL instruction • ROL (rotate left) instruction shifts each bit to the left. • The H.O bit is copied both into the Carry Flag and into the L.O bit. • Syntax: • ROL destination, count • Bit rotation does not lose any bits. A bit that is rotated off one end of a number appears again at the other end. Boolean Instructions

  16. Rotate Instructions • Example: • MOV al, 40h ; 01000000 • ROL al, 1 ; 10000000, CF=0 • ROL al, 1 ; 00000001, CF=1 • ROL al, 1 ; 00000010, CF=0 Boolean Instructions

  17. Rotate Instructions • The ROL instruction sets the flag bits as follows: • The carry flag contains the last bit shifted out of the H.O. bit of the operand. • If the shift count is one, ROL sets the overflow flag if the sign changes as a result of the rotate. If the count is not one, the overflow flag is undefined. • The ROL instruction does not modify the zero, sign, parity, or auxiliary carry flags. Boolean Instructions

  18. Rotate Instructions • ROR instruction • The ROR (rotate right) instruction shifts each bit to the right. • The L.O bit is copied into the Carry flag and into the H.O bit at the same time. • Syntax: • ROR destination, count Boolean Instructions

  19. Rotate Instructions • ROR instruction • Example: • MOV al, 01h ; AL= 00000001 • ROR al, 1 ; AL =10000000 CF=1 • RO al, 1 ; AL = 01000000 CF=0 Boolean Instructions

  20. Rotate Instructions • The ROR instruction sets the flag bits as follows: • The carry flag contains the last bit shifted out of the L.O. bit of the operand. • If the shift count is one, then ROR sets the overflow flag if the sign changes (meaning the values of the H.O. bit and carry flag were not the same before the execution of the instruction). However, if the count is not one, the value of the overflow flag is undefined. • The ROR instruction does not affect the zero, sign, parity, or auxiliary carry flags. Boolean Instructions

  21. Rotate Instructions The RCL instruction • RCL (rotate carry left) instruction shifts each bit to the left. • It copies the Carry flag to the LSB and copies the MSB into the Carry flag. • Syntax: • RCL destination, count Boolean Instructions

  22. Rotate Instructions The RCL instruction Example: CLC ; CF=0 MOV bl, 88h ; BL= 10001000 RCL bl, 1 ; BL= 00010000 CF=1 RCL bl, 1 ; BL= 00100001 CF=0 Boolean Instructions

  23. Rotate Instructions The RCR instruction • The RCR (rotate carry right) instruction shifts each bit to the right • It copies the Carry flag into the MSB, and copies the LSB into the carry flag. • Syntax: • RCR destination, count Boolean Instructions

  24. Rotate Instructions The RCR instruction Example: STC ; CF=1 MOV ah, 10h ; AH= 00010000 CF=1 RCR ah, 1 ; AH= 10001000 CF=0 Boolean Instructions

More Related