Topic 6: Bitwise Instructions

Slides:



Advertisements
Similar presentations
ARM versions ARM architecture has been extended over several versions.
Advertisements

Multiplication – Microprocessor
1 ECE 5465 Advanced Microcomputers Group 11: Brian Knight Benjamin Moore Alex Williams.
Embedded System Design Center ARM7TDMI Microprocessor Data Processing Instructions Sai Kumar Devulapalli.
COMP3221 lec10-logical-II&mul.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 10: C/Assembler Logical and Shift – II & Multiplication.
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
Modified from the notes by
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
What is a program? A sequence of steps
Instruction Set Architectures Early trend was to add more and more instructions to new CPUs to do elaborate operations –VAX architecture had an instruction.
ARM Shifts, Multiplies & Divide??. MVN Pseudo Instructions Pseudo Intruction: Supported by assembler, not be hardware.
Binary Logic (review) Basic logical operators:(Chapter 7 expanded) NOT AND – outputs 1 only if both inputs are 1 OR – outputs 1 if at lest one input is.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
MIPS Procedures II, Logical & Shift Ops. Review Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN
Logic Gates and Logic Circuits
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN
Chapter 4 Operations on Bits.
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
IT 251 Computer Organization and Architecture
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
Assembly Language Assembly Language
Morgan Kaufmann Publishers
Sequential Logic.
Binary Arithmetic Binary arithmetic is essential in all digital computers and in many other types of digital systems. Addition, Subtraction, Multiplication,
IT 251 Computer Organization and Architecture
Number Systems Give qualifications of instructors:
March 2006 Saeid Nooshabadi
Digital Systems Section 8 Multiplexers. Digital Systems Section 8 Multiplexers.
Bit Operations Horton pp
Give qualifications of instructors: DAP
March 2006 Saeid Nooshabadi
The University of Adelaide, School of Computer Science
Optimized Implementation of Logic Function
ECE/CS 552: Arithmetic and Logic
Optimized Implementation of Logic Function
MISP Assembly.
Chapter 14 Bitwise Operators Objectives
Arithmetic Circuits (Part I) Randy H
Digital Logic & Design Lecture 02.
There is one handout today at the front and back of the room!
Sequential Circuits: Latches
Digital Systems Section 12 Binary Adders. Digital Systems Section 12 Binary Adders.
Multiplication by small constants (pp. 139 – 140)
Optimized Implementation of Logic Function
MIPS Assembly.
Arithmetic and Logic Chapter 5
The ARM Instruction Set
Instructions in Machine Language
Branching instructions
March 2006 Saeid Nooshabadi
Overheads for Computers as Components 2nd ed.
MIPS Assembly.
March 2006 Saeid Nooshabadi
CS501 Advanced Computer Architecture
Circuit Analysis Procedure by Dr. M
Immediate data Immediate operands : ADD r3, r3, #1 valid ADD r3, #1,#2 invalid ADD #3, r1,r2 invalid ADD r3, r2, #&FF ( to represent hexadecimal immediate.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Shift and Rotate Instructions.
Bit Operations Horton pp
Presentation transcript:

Topic 6: Bitwise Instructions 12/8/2018 Topic 6: Bitwise Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego

Overview Logic Instructions Shifts and Rotates ARM Arithmetic Datapath credential: bring a computer die photo wafer : This can be an hidden slide. I just want to use this to do my own planning. I have rearranged Culler’s lecture slides slightly and add more slides. This covers everything he covers in his first lecture (and more) but may We will save the fun part, “ Levels of Organization,” at the end (so student can stay awake): I will show the internal stricture of the SS10/20. Notes to Patterson: You may want to edit the slides in your section or add extra slides to taylor your needs.

Logical Operators Basic logical operators: AND: outputs 1 only if both inputs are 1 OR: outputs 1 if at least one input is 1 XOR: outputs 1 if exactly one input is 1 In general, can define them to accept >2 inputs, but in the case of ARM assembly, both of these accept exactly 2 inputs and produce 1 output Again, rigid syntax, simpler hardware

Logical Operators Truth Table: standard table listing all possible combinations of inputs and resultant output for each Truth Table for AND, OR and XOR A B A AND B A OR B A XOR B A BIC B 0 0 0 1 1 0 1 1 1 1 1 1

Uses for Logical Operators Note that ANDing a bit with 0 produces a 0 at the output while ANDing a bit with 1 produces the original bit. This can be used to create a mask. Example: 1011 0110 1010 0100 0011 1101 1001 1010 0000 0000 0000 0000 0000 1111 1111 1111 The result of ANDing these: 0000 0000 0000 0000 0000 1101 1001 1010 mask: mask last 12 bits

Uses for Logical Operators Similarly, note that ORing a bit with 1 produces a 1 at the output while ORing a bit with 0 produces the original bit. This can be used to force certain bits of a string to 1s. For example, 0x12345678 OR 0x0000FFF results in 0x1234FFFF (e.g. the high-order 16 bits are untouched, while the low-order 16 bits are forced to 1s).

Uses for Logical Operators Additionally, note that XORing a bit with 1 produces flips the bit (0 -> 1, 1 -> 0) at the output while XORing a bit with 0 produces the original bit. This can be used to force certain bits of a string to 1s. For example, 0x12345678 OR 0x0000FFF results in 0x1234FFFF (e.g. the high-order 16 bits are untouched, while the low-order 16 bits are forced to 1s).

Uses for Logical Operators Finally, note that BICing a bit with 1 resets the bit (sets to 0) at the output while BICing a bit with 0 produces the original bit. This can be used to force certain bits of a string to 0s. For example, 0x12345678 OR 0x0000FFFF results in 0x12340000 (e.g. the high-order 16 bits are untouched, while the low-order 16 bits are forced to 0s).

Bitwise Logic Instruction Syntax Syntax of Instructions: 1 2, 3, 4 where: 1) instruction by name 2) operand getting result (“destination”) 3) 1st operand for operation (“source1”) 4) 2nd operand for operation (“source2”) Syntax is rigid (for the most part): 1 operator, 3 operands Why? Keep Hardware simple via regularity

Bitwise Logic Operations Bitwise AND in Assembly Example: AND r0,r1,r2 (in ARM) Equivalent to: a = b & c (in C) where ARM registers r0,r1,r2 are associated with C variables a, b, c Bitwise OR in Assembly Example: ORR r3, r4, r5 (in ARM) Equivalent to: d = e | f (in C) where ARM registers r3,r4,r5 are associated with C variables d, e, f

Bitwise Logic Operations Bitwise XOR in Assembly Example: EOR r0,r1,r2 (in ARM) Equivalent to: a = b ^ c (in C) where ARM registers r0,r1,r2 are associated with C variables a, b, c Bitwise Clear in Assembly Example: BIC r3, r4, r5 (in ARM) Equivalent to: d = e & (!f) (in C) where ARM registers r3,r4,r5 are associated with C variables d, e, f

Assignment Instructions Assignment in Assembly Example: MOV r0,r1 (in ARM) Equivalent to: a = b (in C) where ARM registers r0 are associated with C variables a Example: MOV r0,#10 (in ARM) Equivalent to: a = 10 (in C)

Assignment Instructions MVN – Move Negative – moves one complement of the operand into the register. Assignment in Assembly Example: MVN r0,#0 (in ARM) Equivalent to: a = -1 (in C) where ARM registers r0 are associated with C variables a Since ~0x00000000 == 0xFFFFFFFF

Shifts and Rotates LSL – logical shift by n bits – multiplication by 2n LSR – logical shift by n bits – unsigned division by 2n ASR – logical shift by n bits – signed division by 2n ROR – logical shift by n bits – 32 bit rotate C … … C … C … C

Shifts and Rotates Shifting in Assembly Rotating in Assembly Examples: MOV r4, r6, LSL #4 ; r4 = r6 << 4 MOV r4, r6, LSR #8 ; r4 = r6 >> 8 Rotating in Assembly MOV r4, r6, ROR #12 ; r4 = r6 rotated right 12 bits ; r4 = r6 rotated left by 20 bits (32 -12) Therefore no need for rotate left.

Variable Shifts and Rotates Also possible to shift by the value of a register Examples: MOV r4, r6, LSL r3 ; r4 = r6 << value specified in r3 MOV r4, r6, LSR #8 ; r4 = r6 >> 8 Rotating in Assembly MOV r4, r6, ROR #12 ; r4 = r6 rotated right 12 bits ; r4 = r6 rotated left by 20 bits (32 -12) Therefore no need for rotate left.

Constant Multiplication Constant multiplication is often faster using shifts and additions MUL r0, r2, #8 ; r0 = r2 * 8 Is the same as: MOV r0, r2, LSL #3 ; r0 = r2 * 8 Constant division MOV r1, r3, ASR #7 ; r1 = r3/128 Vs. MOV r1, r3, LSR #7 ; r1 = r3/128 The first treats the registers like signed values (shifts in MSB). Thelatter treats data like unsigned values (shifts in 0). int vs unsigned int >>

Constant Multiplication Constant multiplication with subtractions MUL r0, r2, #7 ; r0 = r2 * 7 Is the same as: RSB r0, r2, r2, LSL #3 ; r0 = r2 * 7 ; r0 = -r2 + 8*r2 = 7*r2 RSB r0, r1, r2 is the same as SUB r0, r2, r1 ; r0 = r1 – r2 Multiply by 35: ADD r9,r8,r8,LSL #2 ; r9=r8*5 RSB r10,r9,r9,LSL #3 ; r10=r9*7 Why have RSB? B/C only the second source operand can be shifted.

Using a Barrel Shifter Shift value can be either be: Register, optionally with shift operation Shift value can be either be: 5 bit unsigned integer Specified in bottom byte of another register. Used for multiplication by constant Immediate value 8 bit number, with a range of 0-255. Rotated right through even number of positions Allows increased range of 32-bit constants to be loaded directly into registers Result Operand 1 Barrel Shifter Operand 2 ALU

Loading Constants Constants can only be 8 bit numbers Allows assignment of numbers between 0-255 or [0x00 – 0xFF] Why? Constants stored in code and there is limited space (only 32 bits). Assignment in Assembly Example: MOV r0,#0xFF (in ARM) Equivalent to: a = 255 (in C) where ARM registers r0 C variable a

Immediate Constants The data processing instruction format has 12 bits available for operand2 4 bit rotate value (0-15) is multiplied by two to give range 0-30 in steps of 2 Rule to remember is “8-bits rotated right by an even number of bit positions” 7 11 8 immed_8 Shifter ROR rot x2 0xFF000000 MOV r0, #0xFF, 8

Loading Constants Rotate Value Binary Decimal Hexadecimal 000000000000000000000000xxxxxxxx 0-255 0-0xFF Right, 30 bits 0000000000000000000000xxxxxxxx00 4-1020 0x4-0x3FC Right, 28 bits 00000000000000000000xxxxxxxx0000 16-4080 0x10-0xFF0 Right, 26 bits 000000000000000000xxxxxxxx000000 128-16320 0x40-0x3FC0 … Right, 8 bits xxxxxxxx000000000000000000000000 16777216- 255x224 0x1000000-0xFF000000 Right, 6 bits xxxxxx0000000000000000000000xx - Right, 4 bits xxxx0000000000000000000000xxxx Right, 2 bits xx0000000000000000000000xxxxxx This scheme can generate a lot, but not all, constants. ~50% of constants between [-15,15]; ~90% between [-512,512] Others must be done using literal pools (more on that later)

Conclusion Instructions so far: Previously: ADD, SUB, MUL, MULA, [U|S]MULL, [U|S]MLAL New instructions: RSB AND, ORR, EOR, BIC MOV, MVN LSL, LSR, ASR, ROR Shifting can only be done on the second source operand Constant multiplications possible using shifts and addition/subtractions