ARM Cortex-M4 Programming Model Logical and Shift Instructions
1
ARM Cortex-M4 Programming Model Logical and Shift Instructions - - PowerPoint PPT Presentation
ARM Cortex-M4 Programming Model Logical and Shift Instructions References: Textbook Chapter 4, Sections 4.1, 4.2, 4.3, 4.5, 4.6, 4.9 ARM Cortex-M Users Manual, Chapter 3 1 CPU instruction types Data movement operations
1
2
Data movement operations
memory-to-register and register-to-memory
includes different memory “addressing” options “memory” includes peripheral function registers
register-to-register constant-to-register (or to memory in some CPUs)
Arithmetic operations
add/subtract/multiply/divide multi-precision operations (more than 32 bits)
Logical operations
and/or/exclusive-or/complement (between operand bits) shift/rotate bit test/set/reset compare
Flow control operations
branch to a location (conditionally or unconditionally) branch to a subroutine/function return from a subroutine/function
3
Yifeng Zhu Lecture Slides
Bard, Gerstlauer, Valvano, Yerraballi
AND{S} {Rd,} Rn, <op2> ; Rd=Rn & op2 ORR{S} {Rd,} Rn, <op2> ; Rd=Rn | op2 EOR{S} {Rd,} Rn, <op2> ; Rd=Rn ^ op2 BIC{S} {Rd,} Rn, <op2> ; Rd=Rn & (~op2) (bit clear) ORN{S} {Rd,} Rn, <op2> ; Rd=Rn | (~op2) MVN{S} Rd, <op2> ; Rd=(~op2) (complement all bits)
Bard, Gerstlauer, Valvano, Yerraballi
1 1
6
a7 a6 a5 a4 a3 a2 a1 a0 1 a7 a6 1 a4 a3 a2 a1 a0
a 1 << k a | (1 << k)
Bard, Gerstlauer, Valvano, Yerraballi
1 1 1 1 1 1
8
a7 a6 a5 a4 a3 a2 a1 a0 1 1 1 1 1 1 1 1 a7 a6 a4 a3 a2 a1 a0
a ~(1 << k) a & ~(1<<k) (1 << k)
Bard, Gerstlauer, Valvano, Yerraballi
1 1 1 1 1 1 0 0
Similar to AND, but more “straightforward” - bits to be cleared are designated.
Bard, Gerstlauer, Valvano, Yerraballi
b7 b6 b5 b4 b3 b2 b1 b0 value of R1
1 0 0 0 0 0 0 0
0x80 constant ~b7 b6 b5 b4 b3 b2 b1 b0 result of the EOR
11
a7 a6 a5 a4 A3 a2 a1 a0 1 a7 a6 NOT(a5) a4 a3 a2 a1 a0
a 1 << k a ^= 1<<k a5 1 a5⊕1 1 1 1 1 Truth table of Exclusive OR with one Without knowing the initial value, a bit can be toggled by XORing it with a “1”
Bard, Gerstlauer, Valvano, Yerraballi
The and operation to extract, or mask, individual bits: Pressed = GPIO_PORTA; //true if PA6 switch pressed Assembly: LDR R0,=GPIO_PORTA LDRB R1,[R0] ; read port A ANDS R1,#0x40 ; clear all bits except bit 6 BNE SwitchSet ; branch if Switch pulled high a7 a6 a5 a4 a3 a2 a1 a0 value of R1
1
0x40 constant
0 a6 0 0 0 0 0 0
result of the AND
Input port LM3S or TM4C +3.3V 10kΩ s Input port +3.3V 10kΩ t LM3S or TM4C Open Closed Not pressed Pressed Negative logic Positive logic Note: If 8-bit result is zero, then we know a6 = 0 If the 8-bit result is non-zero, then a6 = 1
Bard, Gerstlauer, Valvano, Yerraballi
The TST operation to extract, or mask, individual bits: Pressed = GPIO_PORTA; //true if PA6 switch pressed Assembly: LDR R0,=GPIO_PORTA LDRB R1,[R0] ; read port A TST R1,#0x40 ; clear all bits except bit 6 BNE SwitchSet ; branch if Switch pulled high
hand
(R1 above) remains unchanged –
the flags are set.
Input port LM3S or TM4C +3.3V 10kΩ s Input port +3.3V 10kΩ t LM3S or TM4C Open Closed Not pressed Pressed Negative logic Positive logic Note: If 8-bit result is zero, then we know a6 = 0 If the 8-bit result is non-zero, then a6 = 1
Bard, Gerstlauer, Valvano, Yerraballi
31 30 29 28 27 26 1 C
LSR ASR LSL ROR RRX Logical Shift Right Arithmetic Shift Right Logical Shift Left Rotate Shift Right Rotate Right Extended 1<n<32 0<n<31 1<n<32 1<n<32 n=1
Formats: LSL Rd, Rm, # imm ; imm = # bit positions to shift (n) LSL Rd, Rm, Rs ; Rs = # bit positions to shift (n) Use the ASR instruction when manipulating signed numbers, and use the LSR instruction when shifting unsigned numbers
15
Bard, Gerstlauer, Valvano, Yerraballi
High and Low are unsigned 4-bit components, which will be combined into a single unsigned 8-bit Result. Result = (High<<4)|Low;
LDR R0,=High LDR R1,[R0] ; read value of High LSL R1,R1,#4 ; shift into position LDR R0,=Low LDR R2,[R0] ; read value of Low ORR R1,R1,R2 ; combine the two parts LDR R0,=Result STR R1,[R0] ; save the answer
h3 h2 h1 h0 value of High in R1 h3 h2 h1 h0 0 after last LSL l3 l2 l1 l0 value of Low in R2 h3 h2 h1 h0 l3 l2 l1 l0 result of the ORR instruction
17