CS代写 ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engine

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: Adding Two Integers
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Copyright By PowCoder代写 加微信 powcoder

Adding Two Integers
int x = 1; int y = 2; int z;
C Statement
If values are in registers
 Value of x in r0 Assembly Statement
 Value ofyinr1  Value ofzinr2
z = x + y;
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Adding Two Integers
int x = 1; int y = 2; int z;
C Statement
If values are in registers
 Value of x in r0  Value ofyinr1  Value ofzinr2
Assembly Statement
z = x + y;
Destination
Source Operand 2 Source Operand 1
ECE 3375b (A. Reyhani)
ADD r2, r1, r0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4

Adding Two Integers
uint x = 1; uint y = 2; uint z;
C Statement
If values are in registers
 Value of x in r0 Assembly Statement
 Value ofyinr1  Value ofzinr2
z = x + y;
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Adding Two Integers
uint x = 1; uint y = 2; uint z;
C Statement
If values are in registers
 Value of x in r0  Value ofyinr1  Value ofzinr2
Assembly Statement
z = x + y;
ADD r2, r1, r0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)
ADD works for both signed and unsigned add operations.

Adding Two Integers
int x = 1; int y = 2; int z;
C Statement
If addresses are in registers
 Address of x in r0  Address of y in r1  Address of z in r2
Assembly Statements
z = x + y;
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Adding Two Integers
int x = 1; int y = 2; int z;
C Statement
z = x + y;
If addresses are in registers
 Address of x in r0  Address of y in r1  Address of z in r2
Assembly Statements
LDR r3, [r0] ; Read x LDR r4, [r1] ; Read y ADD r5, r3, r4
STR r5, [r2] ; Write z
Load, modify, and store
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Adding Two Integers (after compile and load)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Adding Two Integers (after running)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: Arithmetic Instructions
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Example Arithmetic Instructions
 SUB  SBC
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Arithmetic Instructions
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
; r0 = r1 * r2, product limited to 32 bits
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Arithmetic Instructions
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
; r0 = r1 * r2, product limited to 32 bits
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Arithmetic Instructions
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
r0, r1, r2
r0, r1, r2
r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
; r0 = r1 * r2, product limited to 32 bits
; Unsigned divide, r0 = r1 / r2 ; Signed divide, r0 = r1 / r2
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Arithmetic Instructions
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
r0, r1, r2
r0, r1, r2
r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
; r0 = r1 * r2, product limited to 32 bits
; Unsigned divide, r0 = r1 / r2 ; Signed divide, r0 = r1 / r2
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Arithmetic Instructions
r0, r1, r2
r0, r1, r2
r0, r1, r2 r0, r1, r2
r0, r1, r2
; r0 = r1 + r2
; Add with carry, r0 = r1 + r2 + carry
; r0 = r1 – r2
; Subtract with borrow, r0 = r1 – r2 – (1 – carry)
; r0 = r1 * r2, product limited to 32 bits
; Unsigned divide, r0 = r1 / r2 ; Signed divide, r0 = r1 / r2
r0, r1, r2
r0, r1, r2
 SMULL r0, r1, r2, r3 ; Signed multiply (64-bit product), r1:r0 = r2 * r3
 UMULL r0, r1, r2, r3 ; Unsigned multiply (64-bit product), r1:r0 = r2 * r3
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: Logical, Shift and Data Transfer Instructions
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Example Logical Instructions
 AND r0, r1, r2 ; Bitwise AND, r0 = r1 AND r2
AND r0, r1, r2
r1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
r2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
10000000000000010000000000000001
Bit-wise Logic AND
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Example Logical Instructions
 AND r0, r1, r2 ; Bitwise AND, r0 = r1 AND r2
 ORR r0, r1, r2 ; Bitwise OR, r0 = r1 OR r2
 EOR r0, r1, r2 ; Bitwise Exclusive OR, r0 = r1 EOR r2
AND r0, r1, r2
r1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
r2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
10000000000000010000000000000001
Bit-wise Logic AND
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Logical Instructions
 AND r0, r1, r2 ; Bitwise AND, r0 = r1 AND r2
 ORR r0, r1, r2 ; Bitwise OR, r0 = r1 OR r2
 EOR r0, r1, r2 ; Bitwise Exclusive OR, r0 = r1 EOR r2  BIC r0, r1, r2 ; Bit clear, r0 = r1 & ~r2
AND r0, r1, r2
r1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
r2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
10000000000000010000000000000001
Bit-wise Logic AND
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Shift & Rotate Instructions
 LSL r0, r1, r2 ; Logical shift left, r0= r1<>r2
Logical Shift Left (LSL)
Logical Shift Right (LSR)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Shift & Rotate Instructions
 LSL r0, r1, r2 ; Logical shift left, r0= r1<>r2
 ASR r0, r1, r2 ; Arithmetic shift right, r0= r1>>r2
Logical Shift Left (LSL)
Logical Shift Right (LSR)
Arithmetic Shift Right (ASR)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Shift & Rotate Instructions
 LSL r0, r1, r2 ; Logical shift left, r0= r1<>r2
 ASR r0, r1, r2 ; Arithmetic shift right, r0= r1>>r2
 ROR r0, r1, r2 ; Rotate right, r0 = r1 rotate by r2 bits
Logical Shift Left (LSL)
Logical Shift Right (LSR)
Arithmetic Shift Right (ASR)
Rotate Right (ROR)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example Data Transfer Instructions
MOV r0,r1;Move,r0=r1
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Example Data Transfer Instructions
MOV r0,r1;Move,r0=r1
 MVN r0, r1 ; Move NOT, r0 = bitwise NOT r1
MVN r0, r1
r1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 r0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
Bit-wise Logic NOT
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: Updating NZCV Flags
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Updating NZCV flags in PSR
⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶
Most instructions update NZCV flags only if S suffix is present
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)
Flags not changed
ADD SUB MUL UDIV AND ORR LSL MOV
Flags updated
ADDS SUBS MULS UDIVS ANDS ORRS LSLS MOVS

Updating NZCV flags in PSR
Flags not changed
ADD SUB MUL UDIV AND ORR LSL MOV
Flags updated
ADDS SUBS MULS UDIVS ANDS ORRS LSLS MOVS
⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶
Most instructions update NZCV flags only if S suffix is present
Some instructions update NZCV flags even if no S is specified. • CMP: Compare, like SUBS but without destination
CMP r1, r2 vs SUBS r0, r1, r2
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Updating NZCV flags in PSR
Flags not changed
ADD SUB MUL UDIV AND ORR LSL MOV
Flags updated
ADDS SUBS MULS UDIVS ANDS ORRS LSLS MOVS
⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶ ⟶
Most instructions update NZCV flags only if S suffix is present
Some instructions update NZCV flags even if no S is specified.
• CMP: Compare, like SUBS but without destination register
• CMN: Compare Negative, like ADDS but without destination register
• TST: Test, like ANDS but without destination register
• TEQ: Test equivalence, like EORS but without destination register
CMP r1, r2 vs SUBS r0, r1, r2
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

ADD vs ADDS
ADD r0, r1, r2 ; r0 = r1 + r2, NZCV flags unchanged
ADDS r0, r1, r2 ; r0 = r1 + r2, NZCV flags updated  ADD does not update flags
 ADDS updates flags
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

ADD vs ADDS
ADD r0, r1, r2 ; r0 = r1 + r2, NZCV flags unchanged
ADDS r0, r1, r2 ; r0 = r1 + r2, NZCV flags updated  ADD does not update flags
 ADDS updates flags
 CPSR.N = bit 31 of result
 CPSR.Z = IsZero(result)
 CPSR.C = carry, assuming r1 and r2 representing unsigned integers  CPSR.V = overflow, assuming r1 and r2 representing signed integers
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Suffix S: Update Flags
LDR r0, =0xFFFFFFFF LDR r1, =0x00000001 ADDS r3, r0, r1
0xFFFFFFFF r0
0x00000001 r1
0x00000000 sum
N (Negative) = 0 Z (Zero) = 1
C (Carry) = 1
V (oVerflow) = 0
ECE 3375b (A. Reyhani)

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: 64-bitAddition
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Example: 64-bit Addition
Most-significant (Upper) 32 bits Least-significant (Lower) 32 bits
00000002FFFFFFFF 0000000400000001
0000000700000000
• A register can only store 32 bits
• A 64-bit integer needs two registers
• Split 64-bit addition into two 32-bit additions
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example: 64-bit Addition
; Two 64-bit integers A (r1,r0) and B (r3,r2). ; Result C (r5, r4)
; A = 00000002FFFFFFFF
; B = 0000000400000001
LDR r0, =0xFFFFFFFF ; A’s lower 32 bits LDR r1, =0x00000002 ; A’s upper 32 bits LDR r2, =0x00000001 ; B’s lower 32 bits LDR r3, =0x00000004 ; B’s upper 32 bits
ADDS r4, r2, r0 ; C[31..0] = A[31..0] + B[31..0], update Carry ADC r5, r3, r1 ; C[63..32] = A[63..32] + B[63..32] + Carry
Upper 32 bits Lower 32 bits
stop B stop
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example: 64-bit Addition
r4 Sum 00000002FFFFFFFF
0000000400000001 0000000700000000
ECE 3375b (A. Reyhani)

Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 4 ARM Arithmetic and Logic Instructions Example: 64-bit Subtraction
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,

Example: 64-bit Subtraction
; Two 64-bit integers A (r1,r0) and B (r3,r2). ; Result C (r5, r4)
; A = 00000002FFFFFFFF
; B = 0000000400000001
LDR r0, =0xFFFFFFFF ; A’s lower 32 bits LDR r1, =0x00000002 ; A’s upper 32 bits LDR r2, =0x00000001 ; B’s lower 32 bits LDR r3, =0x00000004 ; B’s upper 32 bits
; Subtract B from A
SUBS r4, r0, r2 ; C[31..0]= A[31..0] – B[31..0], update Carry SBC r5, r1, r3 ; C[63..32]= A[63..32] – B[63..32] – NOT (Carry)
stop B stop
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example: 64-bit Subtraction 00000002FFFFFFFF
-0000000400000001 FFFFFFFEFFFFFFFE
Borrow=1 → Carry=0
Borrow=0 → Carry=1
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
ECE 3375b (A. Reyhani)

Example: 64-bit Subtraction 00000002FFFFFFFF
-0000000400000001 FFFFFFFEFFFFFFFE
Borrow=1 → Carry=0
Borrow=0 → Carry=1
C[63..32]=(0000 0002)-(0000 0004+Borrow) =(0000 0002)+2’sComp.(0000 0004)
=(0000 0002)+(FFFF FFFC)
C[31..0]=(FFFF FFFF)-(0000 0001)
=(FFFF FFFF)+2’sComp.(0000 0001)
00000002FFFFFFFF +FFFFFFFCFFFFFFFF FFFFFFFEFFFFFFFE
Carry=1→ Borrow=0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4
=(FFFF FFFF)+(FFFF FFFF)
ECE 3375b (A. Reyhani)

Example: 64-bit Subtraction
00000002FFFFFFFF -0000000400000001
FFFFFFFEFFFFFFFE
Borrow=1 → Carry=0
Borrow=0 → Carry=1
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

Example: 64-bit Subtraction
Borrow=1 → Carry=0
Borrow=0 → Carry=1
00000002FFFFFFFF 0000000400000001
FFFFFFFEFFFFFFFE
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 4 ECE 3375b (A. Reyhani)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com