BITWISE OPERATIONS 353156 Microprocessor Asst Prof Dr Choopan
BITWISE OPERATIONS 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat
Objectives To understand � Bitwise Logical Operations OR AND XOR � Shift Operations Shift Left Shift Right Rotate
Bitwise Operations (1) Up until now, we’ve done : � Arithmetic (ADD, SUB, RSB, . . etc. . ) � Some of data movement (MOV) All of these instructions view content of register as a single quantity But for bitwise operations, we will see the content of register as 32 bits rather than as a single 32 -bit number
Bitwise Operations (2) Since register are composed of 32 bits, we may want to access individual bits rather than the whole. Introduction two new classes of instructions/operations : � Logical Instruction � Shift Operations
Logical Operations Operator Names: AND � BIC � ORR � EOR � Operands Destination : Register � Operand 1 : Register � Operand 2 : Register, Shifted Register, Immediate � Example AND a 1, v 2 � AND a 1, v 1, #0 x 40 �
Logical AND Operator AND (truth table) A B A AND B 0 0 1 1 1 AND : bit-by-bit operation leaves a 1 in the result only if both bits of the operands are 1
Example : Logical AND Operator Assume that register A and B are 8 -bit register Let A stores value 0011 1011 Let B stores value 1001 0010 Find A AND B 0 1 0 0 1 1 0 0 0 0 1 0 AND
Exercise 1: Logical AND Operator What is the value store in register R 0 after the execution of program is done (1) AREA ex 1_1, CODE, READONLY ENTRY (2) AREA ex 1_2, CODE, READONLY ENTRY start MOV a 2, #0 x 42 MOV a 2, #0 x. BC 000000 MOV a 3, #0 x. FC AND END a 1, a 2, a 3 a 1, a 2, #0 x. FF
Logical BIC (AND NOT) Operator BIC (BIt Clean) : bit-by-bit operation leaves a 1 in the result only if bit of the first operand is 1 and the second operands 0 Example 0 0 1 1 BIC 0 1 0
Exercise 2: Logical BIC Operator What is the value store in register R 0 after the execution of program is done (1) AREA ex 2_1, CODE, READONLY ENTRY start (2) AREA ex 2_2, CODE, READONLY ENTRY start MOV a 2, #0 x 42 MOV a 2, #0 x. BC 000000 MOV a 3, #0 x. FC BIC END a 1, a 2, a 3 a 1, a 2, #0 x. FF
MASK AND and BIC normally use to create a mask Example : Given R 0 stores 0 x 12345678 � if we want to keep only the value of the last 2 bytes and we want to clear the first 2 bytes to 0 (0 x 00005678) R 1 stores 0 x. FFFF AND R 0, R 1 ; short written constant value � if we want to keep only the value of the first 2 bytes and we want to clear the last 2 bytes to 0 (0 x 12340000) R 1 stores 0 x. FFFF 0000 AND R 0, R 1 ; long written constant value R 2 stores 0 x. FFFF BIC R 0, R 2 ; better ?
Logical OR Operator OR (truth table) A B A OR B 0 0 1 1 1 0 1 1 OR : bit-by-bit operation leaves a 1 in the result if either bit of the operands is 1
Example : Logical OR Operator Assume that register A and B are 8 -bit register Let A = 0011 1011, and B = 1001 0010 Find A OR B 0 0 1 1 1 0 1 1 OR 1 0 0 1 0 1 1 Example : ARM Instruction � ORR R 0, #0 x 20
Exercise 3 : Logical OR Operator What is the value store in register R 0 after the execution of program is done AREA ex 3_1, CODE, READONLY ENTRY start MOV a 2, #0 x 34 MOV a 3, #0 x 86 ORR a 1, a 2, a 3 END
Logical XOR Operator XOR (truth table) A B A XOR B 0 0 1 1 1 0 XOR : bit-by-bit operation leaves a 1 in the result if bit of the operands are different
Example : Logical XOR Operator Assume that register A and B are 8 -bit register Let A = 0011 1011, and B = 1001 0010 Find A XOR B 0 0 1 1 1 0 1 1 XOR 1 0 0 1 0 1 0 0 1 Example : ARM Instruction � EOR R 0, #0 x 20
Exercise 4 : Logical XOR Operator What is the value store in register R 0 after the execution of program is done AREA ex 4_1, CODE, READONLY ENTRY start MOV a 2, #0 x 34 MOV a 3, #0 x 86 EOR a 1, a 2, a 3 END
Shift Operations Shift means move all the bits in a word to the left or right by number of bits In ARM, there are 3 types of shift operations � Fill emptied bits with 0 s (LSL, LSR) � Fill emptied bits with sign bits (ASR) � Fill emptied bits with the bits falling (rotation) (ROR)
Shift Operations (Type 1) Move all the bits in a word to the left or right by a number of bits, filling the emptied bits with 0 s Example : Given A an 8 -bit register which stores data 1100 1010 1 1 0 0 1 0 Shift right by 4 bits Shift left by 4 bits 0 0 1 1 0 0 1 0 1 0 0 0
LSL, LSR Instructions In ARM, we have 2 instructions for shift operation type 1 � LSL (Logical Shift Left) � LSR (Logical Shift Right) We can use 2 formats of instruction � Opcode dest_reg, reg_contains_number_of_bit_to_shift � ; R 1 must store number of bit to shift Opcode dest_reg, src_reg, constant_number_of_bit_to_shift LSL R 0, R 1 LSR R 0, R 1, #4 R 0 ; Shift right value in R 1 for 4 bits and then store it to ** Only R 0 -R 7 can do Shift operations **
Exercise 5: LSL, LSR Operations What is the value store in register R 0 after the execution of program is done (1) AREA ex 5_1, CODE, READONLY ENTRY (2) AREA ex 5_2, CODE, READONLY ENTRY start MOV a 1, #0 x 01 MOV a 1, #0 x. FF MOV a 2, #4 LSR LSL END a 1, a 2 a 1, #0 x 4
Shift Operations (Type 2) Move all the bits in a word to the right by a number of bits, filling the emptied bits with sign bits Example : � Given A an 8 -bit register which stores data 1100 1010 1 1 0 0 1 0 Shift right by 4 bits � 1 1 1 0 0 1 0 Given A an 8 -bit register which stores data 0100 1010 0 1 0 1 0 Shift right by 4 bits 0 0 1 1 0 0 1 0
ASR Instructions ASR (Arithmatic Shift Right) We can use 2 formats of instruction � Opcode dest_reg, reg_contains_number_of_bit_to_shift � ; R 1 must store number of bit to shift Opcode dest_reg, src_reg, constant_number_of_bit_to_shift ASR R 0, R 1, #4 R 0 ; Shift right value in R 1 for 4 bits and then store it to ** Only R 0 -R 7 can do Shift operations **
Exercise 6: ASR Operations What is the value store in register R 0 after the execution of program is done (1) AREA ex 6_1, CODE, READONLY ENTRY (2) AREA ex 6_2, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFF 10 MOV a 1, #0 x. FF MOV a 2, #2 ASR END a 1, a 2 a 1, #0 x 2
Shift Operations (Type 3) Move all the bits in a word to the right by a number of bits, filling the emptied bits with the bits falling of the right Example : Given A an 8 -bit register which stores data 1100 1010 Rotate right by 3 bits 0 0 1 1 1 0 0 1
ROR Instructions ROR (ROtate Right) We can use 2 formats of instruction � Opcode dest_reg, reg_contains_number_of_bit_to_shift � ; R 1 must store number of bit to shift Opcode dest_reg, src_reg, constant_number_of_bit_to_shift ROR R 0, R 1, #4 to R 0 ; Rorate right value in R 1 for 4 bits and then store it ** Only R 0 -R 7 can do Shift operations **
Exercise 7: ROR Operations What is the value store in register R 0 after the execution of program is done (1) AREA ex 7_1, CODE, READONLY ENTRY (2) AREA ex 7_2, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFF 10 MOV a 1, #0 x. FF MOV a 2, #4 ROR END a 1, a 2 a 1, #0 x 4
Review : 2 nd Operand 2 nd operand for operation : � � � register immediate (numerical constant) shifted register Example : � MOV R 0, R 1 R 0, #0 x 12 R 0, R 1, LSL #4 ; R 0 (R 1 << 4)
Barrel Shifter : The Second Operand 1 Operand 2 Register, optionally with shift operation applied Shift value can be either : � 5 bit unsigned integer Barrel Shifter � ADD R 0, R 1, R 2, LSL R 3 bits) value Immediate � ALU � � Result ; R 0 R 1 + (R 2 << 8 Specified in bottom of another register ADD R 0, R 1, R 2, LSL #8 bits) ; R 0 R 1 + (R 2 << 8 bit number Can be rotated right through as even number of position Assembler will calculate rotate for you from constant ADD R 0, R 1, #10 ; R 0 R 1 + 10 ADD R 0, R 1, #0 x. FF 00 ; R 0 R 1 + (0 x. FF << 16 bits) ADD R 0, R 1, #0 x. FFF ; ERROR
Assignment 5 What is the value store in register R 0 after the execution of program is done (explain the result of each instruction) (1) (2) AREA hw 5_1, CODE, READONLY AREA hw 5_2, CODE, READONLY ENTRY start MOV a 1, #0 x. FFFFFF 18 MOV a 1, #0 x. FF MOV a 2, #16 MOV a 2, #0 x. CC ROR MOV a 3, #16 a 1, a 2 MOV a 2, #0 x. FF 000000 ADD a 1, a 2, LSL a 3 AND EOR a 2, #0 x 65 ORR a 1, a 2, LSL #8 LSR END a 1, a 2, LSR #4 a 1, #16 END
- Slides: 30