SlideShare a Scribd company logo
1 of 20
Download to read offline
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 1
ARM Logic Instructions
• AND dst = src1 AND src2 src1 & src2
• EOR dst = src1 EOR src2 src1 ^ src2
• ORR dst = src1 OR src2 src1 | src2
• MVN dst = NOT src2 ~src2
• ORN dst = src1 NOR src2 ~(src1 | src2)
• BIC dst = src1 AND NOT src2 (src1 & ~src2)
Examples
ORR R0, R1, R2 ; R0 = R1 | R2
AND R0, R0, #0x0F ; R0 = R0 & 0x0F
EORS R1, R3, R0 ; R1 = R3 ^ R0 + set condition code flags
• dst: register
• src1: register
• src2: register OR constant
C/C++/Java operator
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 2
AND
• dst = src1 & src2
• each bit in dst is the AND of the corresponding bits
in src1 and src2 (see truth table)
• can be used to clear selected bits
MOV R0, #0xAA
AND R0, R0, #0x0F
• if src2 used as a mask, clears bit if corresponding bit in mask is 0
0xAA 1010 1010
& 0x0F 0000 1111
0x0A 0000 1010
src1 src2 AND
0 0 0
0 1 0
1 0 0
1 1 1
truth table
clears most significant bits
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 3
OR
• dst = src1 | src2
• each bit in dst is the OR of the corresponding bits
in src1 and src2 (see truth table)
• can be used to set selected bits
MOV R0, #0x0A
ORR R0, R0, #0xF0
• if src2 used as a mask, sets bit if corresponding bit in mask is 1
0x0A 0000 1010
| 0xF0 1111 0000
0xFA 1111 1010
sets most significant bits
src1 src2 OR
0 0 0
0 1 1
1 0 1
1 1 1
truth table
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 4
EOR / XOR
• dst = src1 ^ src2
• each bit in dst is the EOR of the corresponding bits
in src1 and src2 (see truth table)
• can be used to invert selected bits
MOV R0, #0x0A
EOR R0, R0, #0x0F
• if src2 used as a mask, inverts bit if corresponding bit in mask is 1
0x0A 0000 1010
^ 0x0F 0000 1111
0x05 0000 0101
inverts least significant bits
src1 src2 EOR
0 0 0
0 1 1
1 0 1
1 1 0
truth table
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 5
MVN (Move NOT)
• dst = ~src2
• each bit in dst is the inverse (~ or NOT) of the corresponding bit
in src2 (see truth table)
• can be used to invert ALL bits
MOV R0, #0x0A
MVN R0, R0
~ 0x0000000A … 0000 1010
0xFFFFFFF5 … 1111 0101
src2 NOT
0 1
1 0
truth table
inverts ALL bits
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 6
ORN (NOR)
• dst = ~(src1 | src2)
• each bit in dst is the NOR of the corresponding bits
in src1 and src2 (see truth table)
MOV R0, #0x0A
ORN R0, R0, #0x0F
0x0000000A … 0000 1010
NOR 0x0000000F … 0000 1111
0xFFFFFFF0 … 1111 0000
src1 src2 NOR
0 0 1
0 1 0
1 0 0
1 1 0
truth table
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 7
BIC (bit clear)
• dst = src1 & ~src2
• each bit in dst is set to src1 & ~src2 using the
corresponding bits in src1 and src2 (see truth table)
• can be used to clear selected bits
MOV R0, #0xAA
BIC R0, R0, #0xF0
• if src2 used as a mask, clears bit if corresponding bit in mask is 1
0xAA 1010 1010
BIC 0xF0 1111 0000
0x0A 0000 1010
src1 src2 BIC
0 0 0
0 1 0
1 0 1
1 1 0
truth table
clear selected bits
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 8
How to Clear Bits
• write ARM instructions to clear bits 3 and 4 of R1 (LS bit is bit 0)
LDR R1, =0x12345678 ; load test value
LDR R2, =0xFFFFFFE7 ; AND mask to clear bits
AND R1, R1, R2 ; R1 = 0x12345660
• alternatively, the BIC (Bit Clear) instruction can be used with a
mask of 1’s in the bit positions that need to be cleared
LDR R1, =0x12345678 ; load test value
LDR R2, =0x00000018 ; AND mask to clear bits 3 and 4
BIC R1, R1, R2 ; R1 = 0x12345660
• in this case, can use an immediate mask saving one instruction
LDR R1, =0x12345678 ; load test value
BIC R1, R1, #0x18 ; R1 = 0x12345660
0x…78 0111 1000
& 0x…E7 1110 0111
0x…60 0110 0000
clear bits 3 and 4
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 9
How to Invert Bits
• write ARM instructions to invert bits 2 .. 5 of R1 (LS bit is bit 0)
• EOR mask = 0x3C (invert bit if corresponding bit in mask is 1)
LDR R1, =0x12345678 ; load test value
LDR R2, =0x0000003C ; EOR mask to invert bits 2 .. 5
EOR R1, R1, R2 ; R1 = 0x12345644
• in this case, can use an immediate mask saving one instruction
LDR R1, =0x12345678 ; load test value
EOR R1, R1, #0x3C ; R1 = 0x12345644
0x…78 0111 1000
^ 0x…3C 0011 1100
0x…44 0100 0100
inverts bits 2, 3, 4 and 5
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 10
ARM Shift and Rotate
• Logical Shift Left (LSL) a << n // logical shift left n places
• Logical Shift Right (LSR) a >> n // logical shift right n places
• Arithmetic Shift Right (ASR)
• Rotate Right (ROR)
• Rotate Right with eXtend (RRX)
• NB: these are NOT instructions in the same sense as ADD, SUB or ORR
C/C++/Java operator
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 11
Logical Shift Left (LSL)
• LSL one place (LSL #1)
• 0 shifted into LSB, MSB discarded
• LSL 3 places (LSL #3)
• can LSL 0 to 31 places
32 bits
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
 LSL #1  
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
1 1 1 1 0
0
32 bits
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0
 LSL #3  
0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0
0x00FF00FF => 0x07F807F8
0x00FF00FF => 0x01FE01FE
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 12
Logical Shift Right (LSR)
• LSR one place (LSR #1)
• 0 shifted into MSB, LSB discarded
• LSR 3 places (LSR #3)
• can LSR 0 to 31 places
0x00FF00FF => 0x001FE01F
0x00FF00FF => 0x007F807F
32 bits
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
 LSR #1  
0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
32 bits
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1
 LSR #3  
0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 13
ARM shift instructions
• ARM has NO dedicated shift/rotate instructions
• instead, ALL instructions can optionally shift/rotate the src2 operand before it is
used as input for the ALU operation (ADD, SUB, …)
• very ARM specific – unlike other CPUs
src2 to ALU can be:
1) register with an optional shift/rotate
• shift/rotate by constant number of places OR
• by the number places specified in a register
2) 8 bit immediate value rotated right by an even
number of places
barrel
shifter
src1 src2
ALU
dst
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 14
Shift using MOV
• ARM assembly language syntax to LSL src2 one
place before MOV operation
MOV R1, R0, LSL #1 ; R1 = R0 << 1
• logical shift left 5 places
MOV R1, R0, LSL #5 ; R1 = R0 << 5
• if NO shift specified, the default is LSL #0
MOV R1, R0, LSL #0 ; R1 = R0 (NO shift)
barrel
shifter
src1 NOT used
with MOV
R0
ALU
R1 =R0 << 1
LSL #1
R0 << 1
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 15
Logical Shift Left
• LSL one place is the same as multiplying by 2 (if NO carry/overflow)
LDR R0, =0xFF ; R0 = 0x00FF (255)
MOV R1, R0, LSL #1 ; R1 = 0x01FE (510)
• LSL n places is the same as multiplying by 2n
LDR R0, =0xFF ; R0 = 0x00FF (255)
MOV R1, R0, LSL #4 ; R1 = 0x0FF0 (255 x 24 = 255 x 16 = 4080)
• works for signed and unsigned integers
LDR R0, =0xFFFFFFFF ; R0 = 0xFFFFFFFF (-1)
MOV R1, R0, LSL #2 ; R1 = 0xFFFFFFFC (-4) = R0 x 4
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 16
Logical Shift Right …
• LSR one place is the same as integer division by 2 (if NO carry/overflow)
LDR R0, =0xFF ; R0 = 0xFF (255)
MOV R1, R0, LSR #1 ; R1 = 0x7F (127)
• LSR n places is the same as integer division by 2n
LDR R0, =0xFF ; R0 = 0xFF (255)
MOV R1, R0, LSR #4 ; R1 = 0x0F (255 / 24 = 255 / 16 = 15)
• works for unsigned integers
• for signed integers use arithmetic shift right (ASR) which will be covered later
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 17
Shift …
• can shift left or right by the number places specified in a register
MOV R1, R0, LSL R2 ; R1 = R0 << R2
• R2 can be a variable rather than a constant
• if MOVS is used instead of MOV, the last bit shifted out (left or right) is stored in
the CARRY flag
MOV R0, #0x55 ; R0 = 0x55
MOVS R1, R0, LSR #3 ; R1 = R0 >> 3
LSL R2 places (LS 5 bits)
0x55 0101 0101
>> 3 0x0A 0000 1010
CARRY = 1
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 18
Example Shift Operations
• shifts can be followed by any operation ALU operation
• what do the following instructions do?
ADD R0, R1, R1, LSL #3 ; R0 = R1 + R1 x 8 = R1 x 9
RSB R0, R5, R5, LSL #3 ; R0 = R5 x 8 ‐ R5 = R5 x 7
SUB R0, R9, R8, LSR #4 ; R0 = R9 ‐ R8/16
• write ARM instructions to set bit n of R0 where n is in R1 (n in range 0 .. 31)
MOV R1, #4 ; R1 = 4
MOV R2, #0x01 ; R2 = 1
ORR R0, R0, R2, LSL R1 ; R0 = R0 | R2 << n
barrel
shifter
R0 R2 = 1
ALU = OR
R2 << n = 0x10
LSL R1 (n = 0x04)
R0 = R0 | (R2 << n) = R0 | 0x10
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 19
Example Shift Operations …
• write ARM instructions to set R0 = nth bit of R2 where n is in R1
• example: if R2 = 0x55 and n is 4 then R0 = 1
MOV R0, #1 ; R0 = 1
AND R0, R0, R2, LSR R1 ; R0 = R0 & R2 >> R1
0x55 0101 0101
bit 4
barrel
shifter
R0 = 0x01 R2 = 0x55
ALU = AND
R0 = R0 & R2 >> R1 = 0x01 & 0x05 = 1
LSR R1 (n = 0x04)
R2 >> R1 = 0x05
LOGIC AND SHIFT INSTRUCTIONS
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 20
REMEMBER
• prepare for Mid-Term Test during Study Week
• ALL students Thurs 1st Nov @ 9am in Goldsmith Hall (instead of Tutorial)

More Related Content

Similar to 5 LogicAndShiftInstructions.pdf

Arm teaching material
Arm teaching materialArm teaching material
Arm teaching materialJohn Williams
 
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...Hsien-Hsin Sean Lee, Ph.D.
 
Quick Recap of RFC3345 BGP Persistent Route Oscillation Condition
Quick Recap of RFC3345 BGP Persistent Route Oscillation ConditionQuick Recap of RFC3345 BGP Persistent Route Oscillation Condition
Quick Recap of RFC3345 BGP Persistent Route Oscillation ConditionAnıl Alibeyoğlu
 
8086 module 1 & 2 work
8086 module 1 & 2   work8086 module 1 & 2   work
8086 module 1 & 2 workSuhail Km
 
Instruction set summary
Instruction set summary Instruction set summary
Instruction set summary janicetiong
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)Pratheesh Pala
 
PattPatelCh05.ppt
PattPatelCh05.pptPattPatelCh05.ppt
PattPatelCh05.pptDipakShow2
 
Microprocessors and microcontrollers
Microprocessors and microcontrollers Microprocessors and microcontrollers
Microprocessors and microcontrollers durga_sekar
 
2. ALU and MIPS Arcitecture introduction.pdf
2. ALU and MIPS Arcitecture introduction.pdf2. ALU and MIPS Arcitecture introduction.pdf
2. ALU and MIPS Arcitecture introduction.pdfbsse20142018
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorialFutura infotech
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operationNikhil Pandit
 
Module 2 PPT of ES.pptx
Module 2 PPT of ES.pptxModule 2 PPT of ES.pptx
Module 2 PPT of ES.pptxshruthi810379
 
8051 microcontroller notes continuous
8051 microcontroller notes continuous 8051 microcontroller notes continuous
8051 microcontroller notes continuous THANDAIAH PRABU
 
Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051logesh waran
 

Similar to 5 LogicAndShiftInstructions.pdf (20)

ARM instruction set
ARM instruction  setARM instruction  set
ARM instruction set
 
Arm teaching material
Arm teaching materialArm teaching material
Arm teaching material
 
Arm teaching material
Arm teaching materialArm teaching material
Arm teaching material
 
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...
Lec18 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- In...
 
Quick Recap of RFC3345 BGP Persistent Route Oscillation Condition
Quick Recap of RFC3345 BGP Persistent Route Oscillation ConditionQuick Recap of RFC3345 BGP Persistent Route Oscillation Condition
Quick Recap of RFC3345 BGP Persistent Route Oscillation Condition
 
8086 module 1 & 2 work
8086 module 1 & 2   work8086 module 1 & 2   work
8086 module 1 & 2 work
 
Instruction set summary
Instruction set summary Instruction set summary
Instruction set summary
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)
 
PattPatelCh05.ppt
PattPatelCh05.pptPattPatelCh05.ppt
PattPatelCh05.ppt
 
microprocessors
microprocessorsmicroprocessors
microprocessors
 
Microprocessors and microcontrollers
Microprocessors and microcontrollers Microprocessors and microcontrollers
Microprocessors and microcontrollers
 
14941634.ppt
14941634.ppt14941634.ppt
14941634.ppt
 
2. ALU and MIPS Arcitecture introduction.pdf
2. ALU and MIPS Arcitecture introduction.pdf2. ALU and MIPS Arcitecture introduction.pdf
2. ALU and MIPS Arcitecture introduction.pdf
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
IDEA.ppt
IDEA.pptIDEA.ppt
IDEA.ppt
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
 
Module 2 PPT of ES.pptx
Module 2 PPT of ES.pptxModule 2 PPT of ES.pptx
Module 2 PPT of ES.pptx
 
8051 microcontroller notes continuous
8051 microcontroller notes continuous 8051 microcontroller notes continuous
8051 microcontroller notes continuous
 
Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051
 
8051 instruction set
8051 instruction set8051 instruction set
8051 instruction set
 

Recently uploaded

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

5 LogicAndShiftInstructions.pdf

  • 1. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 1 ARM Logic Instructions • AND dst = src1 AND src2 src1 & src2 • EOR dst = src1 EOR src2 src1 ^ src2 • ORR dst = src1 OR src2 src1 | src2 • MVN dst = NOT src2 ~src2 • ORN dst = src1 NOR src2 ~(src1 | src2) • BIC dst = src1 AND NOT src2 (src1 & ~src2) Examples ORR R0, R1, R2 ; R0 = R1 | R2 AND R0, R0, #0x0F ; R0 = R0 & 0x0F EORS R1, R3, R0 ; R1 = R3 ^ R0 + set condition code flags • dst: register • src1: register • src2: register OR constant C/C++/Java operator
  • 2. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 2 AND • dst = src1 & src2 • each bit in dst is the AND of the corresponding bits in src1 and src2 (see truth table) • can be used to clear selected bits MOV R0, #0xAA AND R0, R0, #0x0F • if src2 used as a mask, clears bit if corresponding bit in mask is 0 0xAA 1010 1010 & 0x0F 0000 1111 0x0A 0000 1010 src1 src2 AND 0 0 0 0 1 0 1 0 0 1 1 1 truth table clears most significant bits
  • 3. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 3 OR • dst = src1 | src2 • each bit in dst is the OR of the corresponding bits in src1 and src2 (see truth table) • can be used to set selected bits MOV R0, #0x0A ORR R0, R0, #0xF0 • if src2 used as a mask, sets bit if corresponding bit in mask is 1 0x0A 0000 1010 | 0xF0 1111 0000 0xFA 1111 1010 sets most significant bits src1 src2 OR 0 0 0 0 1 1 1 0 1 1 1 1 truth table
  • 4. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 4 EOR / XOR • dst = src1 ^ src2 • each bit in dst is the EOR of the corresponding bits in src1 and src2 (see truth table) • can be used to invert selected bits MOV R0, #0x0A EOR R0, R0, #0x0F • if src2 used as a mask, inverts bit if corresponding bit in mask is 1 0x0A 0000 1010 ^ 0x0F 0000 1111 0x05 0000 0101 inverts least significant bits src1 src2 EOR 0 0 0 0 1 1 1 0 1 1 1 0 truth table
  • 5. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 5 MVN (Move NOT) • dst = ~src2 • each bit in dst is the inverse (~ or NOT) of the corresponding bit in src2 (see truth table) • can be used to invert ALL bits MOV R0, #0x0A MVN R0, R0 ~ 0x0000000A … 0000 1010 0xFFFFFFF5 … 1111 0101 src2 NOT 0 1 1 0 truth table inverts ALL bits
  • 6. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 6 ORN (NOR) • dst = ~(src1 | src2) • each bit in dst is the NOR of the corresponding bits in src1 and src2 (see truth table) MOV R0, #0x0A ORN R0, R0, #0x0F 0x0000000A … 0000 1010 NOR 0x0000000F … 0000 1111 0xFFFFFFF0 … 1111 0000 src1 src2 NOR 0 0 1 0 1 0 1 0 0 1 1 0 truth table
  • 7. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 7 BIC (bit clear) • dst = src1 & ~src2 • each bit in dst is set to src1 & ~src2 using the corresponding bits in src1 and src2 (see truth table) • can be used to clear selected bits MOV R0, #0xAA BIC R0, R0, #0xF0 • if src2 used as a mask, clears bit if corresponding bit in mask is 1 0xAA 1010 1010 BIC 0xF0 1111 0000 0x0A 0000 1010 src1 src2 BIC 0 0 0 0 1 0 1 0 1 1 1 0 truth table clear selected bits
  • 8. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 8 How to Clear Bits • write ARM instructions to clear bits 3 and 4 of R1 (LS bit is bit 0) LDR R1, =0x12345678 ; load test value LDR R2, =0xFFFFFFE7 ; AND mask to clear bits AND R1, R1, R2 ; R1 = 0x12345660 • alternatively, the BIC (Bit Clear) instruction can be used with a mask of 1’s in the bit positions that need to be cleared LDR R1, =0x12345678 ; load test value LDR R2, =0x00000018 ; AND mask to clear bits 3 and 4 BIC R1, R1, R2 ; R1 = 0x12345660 • in this case, can use an immediate mask saving one instruction LDR R1, =0x12345678 ; load test value BIC R1, R1, #0x18 ; R1 = 0x12345660 0x…78 0111 1000 & 0x…E7 1110 0111 0x…60 0110 0000 clear bits 3 and 4
  • 9. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 9 How to Invert Bits • write ARM instructions to invert bits 2 .. 5 of R1 (LS bit is bit 0) • EOR mask = 0x3C (invert bit if corresponding bit in mask is 1) LDR R1, =0x12345678 ; load test value LDR R2, =0x0000003C ; EOR mask to invert bits 2 .. 5 EOR R1, R1, R2 ; R1 = 0x12345644 • in this case, can use an immediate mask saving one instruction LDR R1, =0x12345678 ; load test value EOR R1, R1, #0x3C ; R1 = 0x12345644 0x…78 0111 1000 ^ 0x…3C 0011 1100 0x…44 0100 0100 inverts bits 2, 3, 4 and 5
  • 10. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 10 ARM Shift and Rotate • Logical Shift Left (LSL) a << n // logical shift left n places • Logical Shift Right (LSR) a >> n // logical shift right n places • Arithmetic Shift Right (ASR) • Rotate Right (ROR) • Rotate Right with eXtend (RRX) • NB: these are NOT instructions in the same sense as ADD, SUB or ORR C/C++/Java operator
  • 11. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 11 Logical Shift Left (LSL) • LSL one place (LSL #1) • 0 shifted into LSB, MSB discarded • LSL 3 places (LSL #3) • can LSL 0 to 31 places 32 bits 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1  LSL #1   0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 32 bits 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0  LSL #3   0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0x00FF00FF => 0x07F807F8 0x00FF00FF => 0x01FE01FE
  • 12. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 12 Logical Shift Right (LSR) • LSR one place (LSR #1) • 0 shifted into MSB, LSB discarded • LSR 3 places (LSR #3) • can LSR 0 to 31 places 0x00FF00FF => 0x001FE01F 0x00FF00FF => 0x007F807F 32 bits 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1  LSR #1   0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 32 bits 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1  LSR #3   0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
  • 13. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 13 ARM shift instructions • ARM has NO dedicated shift/rotate instructions • instead, ALL instructions can optionally shift/rotate the src2 operand before it is used as input for the ALU operation (ADD, SUB, …) • very ARM specific – unlike other CPUs src2 to ALU can be: 1) register with an optional shift/rotate • shift/rotate by constant number of places OR • by the number places specified in a register 2) 8 bit immediate value rotated right by an even number of places barrel shifter src1 src2 ALU dst
  • 14. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 14 Shift using MOV • ARM assembly language syntax to LSL src2 one place before MOV operation MOV R1, R0, LSL #1 ; R1 = R0 << 1 • logical shift left 5 places MOV R1, R0, LSL #5 ; R1 = R0 << 5 • if NO shift specified, the default is LSL #0 MOV R1, R0, LSL #0 ; R1 = R0 (NO shift) barrel shifter src1 NOT used with MOV R0 ALU R1 =R0 << 1 LSL #1 R0 << 1
  • 15. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 15 Logical Shift Left • LSL one place is the same as multiplying by 2 (if NO carry/overflow) LDR R0, =0xFF ; R0 = 0x00FF (255) MOV R1, R0, LSL #1 ; R1 = 0x01FE (510) • LSL n places is the same as multiplying by 2n LDR R0, =0xFF ; R0 = 0x00FF (255) MOV R1, R0, LSL #4 ; R1 = 0x0FF0 (255 x 24 = 255 x 16 = 4080) • works for signed and unsigned integers LDR R0, =0xFFFFFFFF ; R0 = 0xFFFFFFFF (-1) MOV R1, R0, LSL #2 ; R1 = 0xFFFFFFFC (-4) = R0 x 4
  • 16. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 16 Logical Shift Right … • LSR one place is the same as integer division by 2 (if NO carry/overflow) LDR R0, =0xFF ; R0 = 0xFF (255) MOV R1, R0, LSR #1 ; R1 = 0x7F (127) • LSR n places is the same as integer division by 2n LDR R0, =0xFF ; R0 = 0xFF (255) MOV R1, R0, LSR #4 ; R1 = 0x0F (255 / 24 = 255 / 16 = 15) • works for unsigned integers • for signed integers use arithmetic shift right (ASR) which will be covered later
  • 17. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 17 Shift … • can shift left or right by the number places specified in a register MOV R1, R0, LSL R2 ; R1 = R0 << R2 • R2 can be a variable rather than a constant • if MOVS is used instead of MOV, the last bit shifted out (left or right) is stored in the CARRY flag MOV R0, #0x55 ; R0 = 0x55 MOVS R1, R0, LSR #3 ; R1 = R0 >> 3 LSL R2 places (LS 5 bits) 0x55 0101 0101 >> 3 0x0A 0000 1010 CARRY = 1
  • 18. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 18 Example Shift Operations • shifts can be followed by any operation ALU operation • what do the following instructions do? ADD R0, R1, R1, LSL #3 ; R0 = R1 + R1 x 8 = R1 x 9 RSB R0, R5, R5, LSL #3 ; R0 = R5 x 8 ‐ R5 = R5 x 7 SUB R0, R9, R8, LSR #4 ; R0 = R9 ‐ R8/16 • write ARM instructions to set bit n of R0 where n is in R1 (n in range 0 .. 31) MOV R1, #4 ; R1 = 4 MOV R2, #0x01 ; R2 = 1 ORR R0, R0, R2, LSL R1 ; R0 = R0 | R2 << n barrel shifter R0 R2 = 1 ALU = OR R2 << n = 0x10 LSL R1 (n = 0x04) R0 = R0 | (R2 << n) = R0 | 0x10
  • 19. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 19 Example Shift Operations … • write ARM instructions to set R0 = nth bit of R2 where n is in R1 • example: if R2 = 0x55 and n is 4 then R0 = 1 MOV R0, #1 ; R0 = 1 AND R0, R0, R2, LSR R1 ; R0 = R0 & R2 >> R1 0x55 0101 0101 bit 4 barrel shifter R0 = 0x01 R2 = 0x55 ALU = AND R0 = R0 & R2 >> R1 = 0x01 & 0x05 = 1 LSR R1 (n = 0x04) R2 >> R1 = 0x05
  • 20. LOGIC AND SHIFT INSTRUCTIONS CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 16-Oct-18 20 REMEMBER • prepare for Mid-Term Test during Study Week • ALL students Thurs 1st Nov @ 9am in Goldsmith Hall (instead of Tutorial)