ICS 312 Set 9 Logic Shift Instructions Logic

  • Slides: 20
Download presentation
ICS 312 Set 9 Logic & Shift Instructions

ICS 312 Set 9 Logic & Shift Instructions

Logic & Shift Instructions Logic and Shift Instructions can be used to change the

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.

Logic Instructions The Logic (Boolean) Instructions are performed on the operands as "bitwise" operators

Logic Instructions 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) Operation AND OR Description Result in flags are destination register is 1 when both input bits are 1 and set. destination register is 1 when either input bit is 1 and 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.

AND Instruction (1) n n Bitwise AND Operation --- performed bit by bit on

AND Instruction (1) n n 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

AND Instruction (2) n n How used: to CLEAR specific bits in a register

AND Instruction (2) n n 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 = 10101101 b and BL = 00001010 b AND AL, BL = 00001000 b Could also use AND AL, 00001010 b

OR Instruction (1) n n Bitwise OR Operation --- performed bit by bit on

OR Instruction (1) n n 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

OR Instruction (2) n n How used: to SET bits in a register or

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

XOR Instruction (1) n n Bitwise XOR Operation (Exclusive OR) --- performed bit by

XOR Instruction (1) n n 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

XOR Instruction (2) n n How used: to REVERSE (or toggle) bits in a

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

NOT Instruction n n Bitwise NOT Operation --- creates 1's complement of original value

NOT Instruction n n Bitwise NOT Operation --- creates 1's complement of original value Syntax: NOT 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 = 10101111 b NOT AL = 01010000 b

NEG Instruction (1) n n Creates 2's complement of original value (i. e. ,

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

TEST Instruction (1) n n Sets the flags according to the result of an

TEST Instruction (1) n n 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

TEST Instruction (2) n n How used: to TEST bits in a register or

TEST Instruction (2) n n 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, 00000100 b JNZ PLACE Example: Determine whether a character in DL is uppercase or lowercase: TEST DL, 00100000 b 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, 00000001 b JZ EVEN

CMP Instruction (1) n n Sets the flags according to the result of an

CMP Instruction (1) n n 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

Shift Instructions n Shift instructions provide a mechanism for moving bits to the left

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

SHL (1) n Shift left. Note SHL and SAL are synonyms n Instruction Format:

SHL (1) n Shift left. Note SHL and SAL are synonyms n 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.

SHR n Shift Right n Same instruction formats as SHL. n n Example: AL

SHR n Shift Right n Same instruction formats as SHL. n n Example: AL = 10101111 b SHR AL, 1 ; AL = 01010111 b or: SHR AL, 5 ; AL = 00000101 b 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.

Binary and HEX I/O n n n Convert "binary" keyboard input of a sequence

Binary and HEX I/O n n n 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 30 h. Code Example: MOV BX, 0 ; clear BX MOV AH, 1 ; input char INT 21 H ; char in AL WHILE_: CMP AL, 0 DH JE END_WHILE SUB AL, 30 H ; convert to binary SHL BX, 1 ; shift bits left OR BL, AL ; insert new bit INT 21 H ; read another character JMP WHILE_ END_WHILE:

Convert HEX input to internal binary form MOV BX, 0 MOV INT AH, 1

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

Textbook Reading (Jones): Sections 9. 1 -9. 3 Bit Operations HOMEWORK. Do exercises 9.

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.