Bitwise Logic and Immediate Operands Bitwise operation Zero Extension Immediate operand instructions (ori, andi, xori) Textbook: Appendix A – A.10. MIPS R2000 assembly language. Central Connecticut State University, MIPS Tutorial. Chapter 11.
Bitwise Operation 0110 1100 operand 0101 0110 operand ------------ A bitwise operation is where a logical operation is performed on the bits of each column of the operands. With a bitwise logic operation two bit patterns are "lined up" Then a logic operation (such as OR, AND, …) is performed between pairs of bits. Here is the bitwise OR between two 8-bit patterns. 0110 1100 operand 0101 0110 operand ------------ 0111 1110 result
Zero Extension of decimal numbers When doing operations with decimal numbers everyday we do zero extension of that numbers intuitively if they have different amount of digits.
Zero Extension or padding with zeros with binary numbers Suppose we have this 16 bit number as a first operand 0000 0000 0000 0010 And this 32 bit number as a second operand 0000 0000 0000 0000 0000 0000 0000 0000 We want to perform OR operation between 2 operands. First we should do zero extension of the first operand Then perform the operation with the full length of both operands. zero extended part ---- ---- ---- ---- 0000 0000 0000 0000 0000 0000 0000 0010 -- zero extended 1st operand 0000 0000 0000 0000 0000 0000 0000 0000 -- 2nd operand --------------------------------------- 0000 0000 0000 0000 0000 0000 0000 0010 -- result
What is immediate operand ? A machine instruction can use some of its bits as one of the operands for a machine operation. This is called an immediate operand. Using this kind of operations the program can set up the registers with the desirable known values. Immediate operand example address machine code assembly code —————————— —————————— ————————————— 0x00400000 0x34080002 ori $8,$0,0x2 The last 16 bits (4 nibbles) of the machine instruction contain the immediate operand 0x0002.
Zero Extension of immediate operand ori $8,$0,0x2 16 bits of immediate operand 0000 0000 0000 0010 32 bits of register zero 0000 0000 0000 0000 0000 0000 0000 0000 zero extension ---- ---- ---- ---- 0000 0000 0000 0000 0000 0000 0000 0010 --------------------------------------- An OR operation is done in each column. The 32-bit result is placed in register $8.
OR Immediate used with Non-zero Operands ori d,s,const register d <-- bitwise OR of const with the contents of register $s(source) const represents a positive integer 0 <= const <= 65535 (0xFFFF -16 bits) The three operands of the assembly instruction d, $s, and const must appear in that order.
OR programming tricks What is the value of Xs ? 0000 0000 0000 0000 0000 0000 000X 00X0 - R1 0000 0000 0000 0000 0000 0000 0001 0000 - R2 --------------------------------------- 0000 0000 0000 0000 0000 0000 0001 0010 – R3 Set the value of a bit to 1 Get the value of a bit
OR Immediate used with Zero Register – setup the register ori d,$0,const register d <-- const. (d – destination) const represents a positive integer 0 <= const <= 65535 (0xFFFF -16 bits) . The three operands of the assembly instruction d, $0, and const must appear in that order.
ORI Example ## Program to bitwise OR two patterns .text .globl main main: ori $8,$0,0x0FA5 ori $10,$8,0x368F ## End of file
AND Immediate Instruction andi d,s,const register d <-- bitwise AND of immediate operand const and the contents of register $s. const is a 16-bit pattern 0x0000 ... const ... 0xFFFF Simple AND Rules AND-ing the Register with 0s sets the Register value to 0s. AND-ing the Register with the pattern (mask) 0s and 1s selects the original Register’s bits by mask of 1s. The original values are not changed only where the mask bits are 1s. The other bits are set to 0s.
AND programming tricks What is the value of Xs ? 0000 0000 0000 0000 0000 0000 XXXX XXXX - R1 0000 0000 0000 0000 0000 0000 0001 1111 - R2 --------------------------------------- XXXX XXXX XXXX XXXX XXXX XXXX 0001 0010 – R3 What is the value of R1 ? XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX - R1 1111 1111 1111 1111 1111 1111 1111 1111 - R2 --------------------------------------- 0000 0000 0000 0000 0000 0000 0000 0000 – R3
Exclusive Or Immediate (XOR) Simple XOR Rules XOR the Register with itself sets the Register value to 0s. XOR the Register with 1s inverts the original Register’s content. XOR compares 2 operand and sets 1s in result bits where the operands are different. xori d,s,const register d <-- bitwise XOR of immediate operand const and the contents of register $s. const is a 16-bit pattern 0x0000 ... const ... 0xFFFF
XOR programming tricks Invertion - What is the value of X ? 0000 0000 0000 0000 0000 0000 000X 0000 - R1 0000 0000 0000 0000 0000 0000 0001 0000 - R2 --------------------------------------- 0000 0000 0000 0000 0000 0000 0001 0000 – R3 Comparison - Is R1==R2 ? XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX - R1 XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX - R2 --------------------------------------- 0000 0000 0000 0000 0000 0000 0000 0000 – R3 if (R3==0) then ...
Example Program .text .globl main main: ori $15, $0,0x0FA5 ## Program to bitwise OR, AND, and XOR two patterns .text .globl main main: ori $15, $0,0x0FA5 ori $8,$15,0x368F andi $9,$15,0x368F xori $10,$15,0x368F ## End of file