Bitwise Instructions

Similar documents
Binary Logic (review)

Lecture 4 (part 2): Data Transfer Instructions

Overview COMP 3221 Bitwise Logical Operations

Processor Status Register(PSR)

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

ARM Cortex-M4 Programming Model Logical and Shift Instructions

EE251: Tuesday September 5

ARM Cortex-M4 Architecture and Instruction Set 2: General Data Processing Instructions

Control Flow Instructions

Basic ARM InstructionS

ECE 471 Embedded Systems Lecture 5

Computer Architecture Prof. Smruthi Ranjan Sarangi Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Comparison InstruCtions

MNEMONIC OPERATION ADDRESS / OPERAND MODES FLAGS SET WITH S suffix ADC

SIGNED AND UNSIGNED SYSTEMS

Overview COMP Microprocessors and Embedded Systems. Lecture 11: Memory Access - I. August, 2003

LAB 1 Using Visual Emulator. Download the emulator Answer to questions (Q)

Assembly Language Programming

ARM Cortex-A9 ARM v7-a. A programmer s perspective Part 2

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

ARM Assembly Language. Programming

ARM Cortex M3 Instruction Set Architecture. Gary J. Minden March 29, 2016

ARM Instruction Set. Computer Organization and Assembly Languages Yung-Yu Chuang. with slides by Peng-Sheng Chen

Instruction Set Architecture (ISA)

ARM Instruction Set. Introduction. Memory system. ARM programmer model. The ARM processor is easy to program at the

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

ARM Assembly Programming

Architecture. Digital Computer Design

ARM Instruction Set Architecture. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 2 Instructions Sets. Hsung-Pin Chang Department of Computer Science National ChungHsing University

ARM Assembly Language

EE319K Fall 2013 Exam 1B Modified Page 1. Exam 1. Date: October 3, 2013

CMPSCI 201 Fall 2004 Midterm #2 Answers

CMPSCI 201 Fall 2005 Midterm #2 Solution

CSCI 2212: Intermediate Programming / C Chapter 15

18-349: Introduction to Embedded Real- Time Systems Lecture 3: ARM ASM

Lecture 6 Decision + Shift + I/O

Chapters 3. ARM Assembly. Embedded Systems with ARM Cortext-M. Updated: Wednesday, February 7, 2018

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Topic 10: Instruction Representation

ECE 498 Linux Assembly Language Lecture 5

Chapter 4. Operations on Data

EE319K Spring 2016 Exam 1 Solution Page 1. Exam 1. Date: Feb 25, UT EID: Solution Professor (circle): Janapa Reddi, Tiwari, Valvano, Yerraballi

The ARM Instruction Set

Assembly Language. CS2253 Owen Kaser, UNBSJ

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map

Arithmetic Operations

Instruction Set. ARM810 Data Sheet. Open Access - Preliminary

Writing ARM Assembly. Steven R. Bagley

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Chapter 3: ARM Instruction Set [Architecture Version: ARMv4T]

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

Exam 1 Fun Times. EE319K Fall 2012 Exam 1A Modified Page 1. Date: October 5, Printed Name:

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015

ARM Cortex-M4 Programming Model Arithmetic Instructions. References: Textbook Chapter 4, Chapter ARM Cortex-M Users Manual, Chapter 3

STEVEN R. BAGLEY ARM: PROCESSING DATA

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

EE319K Exam 1 Summer 2014 Page 1. Exam 1. Date: July 9, Printed Name:

3. The Instruction Set

AVR Logical and Bitwise Instructions

Exam 1. Date: Oct 4, 2018

CMPSCI 201 Fall 2004 Midterm #1 Answers

COMP 122/L Lecture 2. Kyle Dewey

ENE 334 Microprocessors

ARM Cortex-M4 Programming Model Memory Addressing Instructions

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Introduction to Embedded Systems EE319K (Gerstlauer), Spring 2013

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

3 Assembly Programming (Part 2) Date: 07/09/2016 Name: ID:

Computer Organization & Systems Exam I Example Questions

Embedded assembly is more useful. Embedded assembly places an assembly function inside a C program and can be used with the ARM Cortex M0 processor.

A block of memory (FlashROM) starts at address 0x and it is 256 KB long. What is the last address in the block?

Overview COMP Microprocessors and Embedded Systems. Lectures 14: Making Decisions in C/Assembly Language II

Integer Representation

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Beginning C Programming for Engineers

Exam 1. EE319K Spring 2013 Exam 1 (Practice 1) Page 1. Date: February 21, 2013; 9:30-10:45am. Printed Name:

ECE 571 Advanced Microprocessor-Based Design Lecture 3

ARM. Assembly Language and Machine Code. Goal: Blink an LED

AGENDA Binary Operations CS 3330 Samira Khan

Chapter 15. ARM Architecture, Programming and Development Tools

The PAW Architecture Reference Manual

ECE 471 Embedded Systems Lecture 6

Microcontroller Architecture and Interfacing HOMEWORK 3

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

ECE 471 Embedded Systems Lecture 9

CS 64 Week 1 Lecture 1. Kyle Dewey

ARM. Assembly Language and Machine Code. Goal: Blink an LED

ARM Processors ARM ISA. ARM 1 in 1985 By 2001, more than 1 billion ARM processors shipped Widely used in many successful 32-bit embedded systems

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Arithmetic and Bitwise Operations on Binary Data

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

COMP 303 Computer Architecture Lecture 6

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Transcription:

Bitwise Instructions CSE 30: Computer Organization and Systems Programming Dept. of Computer Science and Engineering University of California, San Diego

Overview v Bitwise Instructions v Shifts and Rotates v ARM Arithmetic Datapath

Logical Operators v Basic logical operators: v AND: outputs 1 only if both inputs are 1 v OR: outputs 1 if at least one input is 1 v XOR: outputs 1 if exactly one input is 1 v 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 v Again, rigid syntax, simpler hardware

Logical Operators v Truth Table: standard table listing all possible combinations of inputs and resultant output for each v 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 0 0 0 1 0 1 1 1 0 1 1 0 0 0 1 0

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

Uses for Logical Operators v Similarly, note that ORing a bit with 1 produces a 1 at the output while ORing a bit with 0 produces the original bit. v This can be used to force certain bits of a string to 1s. v 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 v 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. v This can be used to force certain bits of a string to 1s. v 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 v 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. v This can be used to force certain bits of a string to 0s. v For example, 0x12345678 BIC 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 v 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 ) v Syntax is rigid (for the most part): v 1 operator, 3 operands v Why? Keep Hardware simple via regularity

Bitwise Logic Operations v Bitwise AND in Assembly v 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 v Bitwise OR in Assembly v 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 v Bitwise XOR in Assembly v 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 v Bitwise Clear in Assembly v 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 v Assignment in Assembly v Example: MOV r0,r1 (in ARM) Equivalent to: a = b (in C) where ARM registers r0 are associated with C variables a v Example: MOV r0,#10 (in ARM) Equivalent to: a = 10 (in C)

Assignment Instructions v MVN Move Negative moves one complement of the operand into the register. v Assignment in Assembly v 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 v LSL logical shift by n bits multiplication by 2 n C 0 v LSR logical shift by n bits unsigned division by 2 n 0 C v ASR logical shift by n bits signed division by 2 n C v ROR logical shift by n bits 32 bit rotate C

v Shifting in Assembly Examples: Shifts and Rotates MOV r4, r6, LSL #4 ; r4 = r6 << 4 MOV r4, r6, LSR #8 ; r4 = r6 >> 8 v Rotating in Assembly Examples: 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 v Also possible to shift by the value of a register v Examples: MOV r4, r6, LSL r3 ; r4 = r6 << value specified in r3 MOV r4, r6, LSR #8 ; r4 = r6 >> 8 v Rotating in Assembly v Examples: 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 v 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 v Constant division Vs. MOV r1, r3, ASR #7 ; r1 = r3/128 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 v 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 Operand 1 ALU Result Operand 2 Barrel Shifter Register, optionally with shift operation v Shift value can be either be: v 5 bit unsigned integer v Specified in bottom byte of another register. v Used for multiplication by constant Immediate value v 8 bit number, with a range of 0-255. v Rotated right through even number of positions v Allows increased range of 32- bit constants to be loaded directly into registers

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

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

Loading Constants Rotate Value Binary Decimal Hexadecimal 0 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-255x2 24 0x1000000-0xF F000000 Right, 6 bits xxxxxx0000000000000000000000xx - - Right, 4 bits xxxx0000000000000000000000xxxx - - Right, 2 bits xx0000000000000000000000xxxxxx - - v This scheme can generate a lot, but not all, constants. v ~50% of constants between [-15,15]; v ~90% between [-512,512] v Others must be done using literal pools (more on that later)

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