Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.

Similar presentations


Presentation on theme: "ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The."— Presentation transcript:

1 ICS312 Set 9 Logic & Shift Instructions

2 Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The ability to manipulate bit values is generally absent in high-level languages (except for C and C++), and is an important aspect of assembly language programming. The logic instructions AND, OR, XOR, TEST all set the carry flag unconditionally off, and set the ZF (zero flag) and SF (sign flag) according to whether the result is zero, negative or positive.

3 Logic Instructions OperationDescription AND Result in destination register is 1 when both input bits are 1 and flags are set. OR Result in destination register is 1 when either input bit is 1 and flags are set. XOR Result in destination register is 1 when the input bits are different(Exclusive OR) and flags are set. NOT Result in destination register is the reverse (1's complement) of the input and flags are set. TEST Sets the flags based on the result of an implied AND operation, but does not change the contents of the destination register. The Logic (Boolean) Instructions are performed on the operands as "bitwise" operators --- i.e., the instructions are applied to each bit. Table 1. Boolean Instructions (summarized)

4 AND Instruction (1) Bitwise AND Operation --- performed bit by bit on contents of operands Syntax: AND destination, source Result per bit: = 1 if both operand bits are 1 = 0 of either operand bit is 0

5 AND Instruction (2) How used: to CLEAR specific bits in a register or memory value Example: to clear all bits in AL except bits 1 & 3: Assume AL = 10101101b and BL = 00001010b AND AL, BL = 00001000b Could also use AND AL, 00001010b

6 OR Instruction (1) Bitwise OR Operation --- performed bit by bit on contents of operands Syntax: OR destination, source Result per bit: = 1 if either operand bit is 1 = 0 of both operand bits are 0

7 OR Instruction (2) How used: to SET bits in a register or memory value Example: To set bit 1 of AL. Assume AL = 10100000b Then OR AL, 00000010b = 10100010b

8 XOR Instruction (1) Bitwise XOR Operation (Exclusive OR) --- performed bit by bit on contents of operands Syntax: XOR destination, source Result per bit: = 1 if operand bits are different (i.e., one operand bit is 0 and the other is 1) = 0 of both operand bits are 0 or both are 1

9 XOR Instruction (2) How used: to REVERSE (or toggle) bits in a register or memory value Example: To reverse bits 0 and 2 of DL Assume DL = 00100100b Then XOR DL, 00000101b = 00100001b

10 NOT Instruction Bitwise NOT Operation --- creates 1's complement of original value Syntax: NOT destination destination can be register or memory Result per bit: = 1 if operand bit is 0 = 0 if operand bit is 1 Does not alter the SF or ZF How used: to reverse the bits (form the 1's complement) of a value in a register or memory value Example: AL = 10101111b NOT AL = 01010000b

11 NEG Instruction (1) Creates 2's complement of original value (i.e., forms the negative of a given value). Syntax: NEG destination destination can be register or memory Example: AL = 10101111b NEG AL = 01010001b

12 TEST Instruction (1) Sets the flags according to the result of an implied AND operation, but does not change the contents of the destination operand. Syntax: TEST destination, source Result: destination operand is unchanged, but flags are set for result of AND operation

13 TEST Instruction (2) How used: to TEST bits in a register or memory value Example: To jump to PLACE if bit 2 of AL is a 1. TEST AL, 00000100b JNZ PLACE Example: Determine whether a character in DL is uppercase or lowercase: TEST DL, 00100000b JZ UPPER This uses the fact that all upper case characters have a zero in bit 5, while the lower case characters have a one in bit 5. Example: Determine whether a number in BH is even or odd. TEST BH, 00000001b JZ EVEN

14 CMP Instruction (1) Sets the flags according to the result of an implied SUB operation, but does not change the contents of the destination operand. Syntax: CMP destination, source Result: destination operand is unchanged, but flags are set for result of SUB operation How used: to compare register or memory values and make decisions in programs

15 Shift Instructions Shift instructions provide a mechanism for moving bits to the left or right in a operand.

16 SHL (1) Shift left. Note SHL and SAL are synonyms Instruction Format: SHL destination, immed Operands: destination may be 8, 16, or 32-bits (reg or memory) immed is an immediate 8-bit value Used to multiply by powers of 2.

17 SHR Shift Right Same instruction formats as SHL. Example: AL = 10101111b SHR AL, 1 ; AL = 01010111b or: SHR AL, 5 ; AL = 00000101b Application. Division by 2. Each shift to the right is the same as dividing by a factor of 2. SHR is a much faster way to divide than the division instruction.

18 Binary and HEX I/O Convert "binary" keyboard input of a sequence of zeros and ones into the corresponding binary number. Discussion: values input from keyboard are encoded in ASCII form. To convert '0' or '1' to numeric values requires subtracting 30h. Code Example: MOV BX,0 ; clear BX MOV AH, 1 ; input char INT 21H ; char in AL WHILE_: CMP AL, 0DH JE END_WHILE SUB AL, 30H ; convert to binary SHL BX, 1 ; shift bits left OR BL, AL ; insert new bit INT 21H ; read another character JMP WHILE_ END_WHILE:

19 Convert HEX input to internal binary form MOV BX, 0 MOV AH, 1 INT 21H WHILE_: CMP AL, 0DH ; CR? JE END_WHILE ; exit loop CMP AL, 39H ; digit? JG LETTER ; no - skip to letter SUB AL, 30H ; convert digit to binary JMP SHIFT LETTER: ; convert hex char to binary SUB AL, 37H ; A IS 41H. 41H-37H = 10 SHIFT: SHL BX,4 ; shift 4 bits OR BL, AL ; insert 4 bits. (High order 4 bits of AL are 0) INT 21H ; read next char JMP WHILE_ ; repeat loop END_WHILE: ; loop finished

20 Textbook Reading (Jones): Sections 9.1-9.3 Bit Operations HOMEWORK. Do exercises 9.2, nos. 1 & 2 on p. 233-234. Omit questions invoving ROL, ROR.


Download ppt "ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The."

Similar presentations


Ads by Google