Professional Documents
Culture Documents
DEC 30043
MICROPROCESSOR FUNDAMENTALS
ASSEMBLY LANGUAGE :
PROCESSING DATA
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
ASSEMBLY LANGUAGE : PROCESSING DATA
4.1 Understanding the instruction of data process
4.1.1 Explain Arithmetic operation
4.1.2 Explain Logical operation
4.1.3 Explain Shift and rotate operation
4.1.4 Elaborate reverse data bytes in the register
CHAPTER4
ARITHMATIC SHIFT RIGHT, LOGICAL SHIFT LEFT, LOGICAL
SHIFT RIGHT , ROTATE RIGHT, ROTATE operation.
4.2.7 Examine the changes of status register (flag) when the related
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
instructions in section 4.2.6 are executed.
4.1 UNDERSTANDING
THE INSTRUCTION
OF DATA
PROCESS
4.1.1 Arithmetic operation
Operation Instruction
ADD ADD Rd, Rn, Rm ; Rd = Rn + Rm
ADD Rd, Rd, Rm ; Rd = Rd + Rm
ADD Rd, #immed ; Rd = Rd + #immed
ADD Rd, Rn, # immed ; Rd = Rn + #immed
ADD with carry ADC Rd, Rn, Rm ; Rd = Rn + Rm + carry
ADC Rd, Rd, Rm ; Rd = Rd + Rm + carry
ADC Rd, #immed ; Rd = Rd + #immed + carry
SUBTRACT SUB Rd, Rn, Rm ; Rd = Rn − Rm
SUB Rd, #immed ; Rd = Rd − #immed
SUB Rd, Rn,#immed ; Rd = Rn − #immed
SUBTRACT with SBC Rd, Rm ; Rd = Rd − Rm − borrow
borrow (not carry) SBC.W Rd, Rn, #immed ; Rd = Rn − #immed − borrow
SBC.W Rd, Rn, Rm ; Rd = Rn − Rm − borrow
The Cortex-M4 also supports 32-bit multiply instructions and multiply accumulate instructions that
give 64-bit results. These instructions support signed or unsigned values
Operation Instruction
32-bit multiply SMULL RdLo, RdHi, Rn, Rm ; {RdHi,RdLo} = Rn * Rm
instructions for signed SMLAL RdLo, RdHi, Rn, Rm ; {RdHi,RdLo} += Rn * Rm
values
32-bit multiply UMULL RdLo, RdHi, Rn, Rm ; {RdHi,RdLo} = Rn * Rm
instructions for UMLAL RdLo, RdHi, Rn, Rm ; {RdHi,RdLo} += Rn * Rm
unsigned values
Another group of data processing instructions are the logical operations instructions.
These instructions can be used with or without the “S” suffix to determine if the APSR should
be updated.
Operation Instruction
Bitwise AND AND Rd, Rn ; Rd = Rd & Rn
AND.W Rd, Rn,#immed ; Rd = Rn & #immed
AND.W Rd, Rn, Rm ; Rd = Rn & Rd
Bitwise OR ORRRd, Rn ; Rd = Rd | Rn
ORR.W Rd, Rn,#immed ; Rd = Rn | #immed
ORR.W Rd, Rn, Rm ; Rd = Rn | Rd
Bit clear BIC Rd, Rn ; Rd = Rd & (~Rn)
BIC.W Rd, Rn,#immed ; Rd = Rn &(~#immed)
BIC.W Rd, Rn, Rm ; Rd = Rn &(~Rd)
Bitwise OR NOT ORN.W Rd, Rn,#immed ; Rd = Rn | (~#immed)
ORN.W Rd, Rn, Rm ; Rd = Rn | (~Rd)
Bitwise Exclusive OR EOR Rd, Rn ; Rd = Rd ^ Rn
EOR.W Rd, Rn,#immed ; Rd = Rn | #immed
EOR.W Rd, Rn, Rm ; Rd = Rn | Rd
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
4.1.3 Shift and rotate operation
The rotate operation can be combined with other operations (for example, in memory address
offset calculation for load/store instructions).
A 32-bit version of the instruction is used if “S” suffix is not used and if UAL syntax is used.
Operation Instruction
Arithmetic shift right ASR Rd, Rn,#immed ; Rd = Rn » immed
ASRRd, Rn ; Rd = Rd » Rn
ASR.W Rd, Rn, Rm ; Rd = Rn » Rm
Logical shift left LSLRd, Rn,#immed ; Rd = Rn « immed
LSLRd, Rn ; Rd = Rd « Rn
LSL.W Rd, Rn, Rm ; Rd = Rn « Rm
Logical shift right LSRRd, Rn,#immed ; Rd = Rn » immed
LSRRd, Rn ; Rd = Rd » Rn
LSR.W Rd, Rn, Rm ; Rd = Rn » Rm
Rotate right ROR Rd, Rn ; Rd rot by Rn
ROR.W Rd, Rn,#immed ; Rd = Rn rot by immed
ROR.W Rd, Rn, Rm ; Rd = Rn rot by Rm
Rotate right extended RRX.W Rd, Rn ; {C, Rd} = {Rn, C}
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
Shift and rotate instruction
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
4.1.4 Reverse data bytes in the register
These instructions are usually used for conversion between little endian and big endian data.
Operation Instruction
Reverse bytes in word REV Rd, Rn ; Rd = rev(Rn)
Examples: Examples:
EXAMPLE: CONNECTOR
BASIC SHAPES - CONNECTORS
Examples:
Elements of a
flow chart
Programming
Based on the flow chart we can now write out a program,
Data 1 R0 as shown below
Data 2 R1
START
MOVS R0, #12
R0 + R1 R0 MOVS R1, #34
ADD R0, R0, R1
MOVS R2, R0
B START
R0 Register R2 END
START
Input R2
R1 = R1 x R2 MULS R2, R1, R2
END
END B START
END
Try It Yourself
1. Draw a flowchart and the programming to calculate an area of a circle. The radius is equal to 4.
2. Draw a flowchart and the programming that performs 2’s complement on a number in R0.
3. Draw a flowchart and the programming to inspect the contents R4 and if the contents are greater
than 0x55, divide the value with 0x5 and store the result at register R5. Otherwise, multiply it with 0x3
and store the result in register R6.
The ARM processor normally contains at least the Z, N, C, and V flags, which are updated by
execution of data processing instructions.
Z (Zero) flag: This flag is set when the result of an instruction has a zero value or when a
comparison of two data returns an equal result.
N (Negative) flag: This flag is set when the result of an instruction has a negative value (bit 31
is 1).
C (Carry) flag: This flag is for unsigned data processing - for example, in add (ADD) it is set
when an overflow occurs; in subtract (SUB) it is set when a borrow did not occur (borrow is the
invert of carry).
V (Overflow) flag: This flag is for signed data processing; for example, in an add (ADD), when
two positive values added together produce a negative value, or when two negative values
added together produce a positive value.
4.2.3 ARITHMETIC : ADD OPERATION ADD Rd, Rn, Rm ; Rd = Rn + Rm
Examples 1:
If R2 and R3 contain 0x22446688 and 0x77553311, respectively, what are the results of ADDS R1, R2, R3?
V- Overflow Bit
APSR Set during arithmetic/divide overflow.
Z = 0 (result non zero) For ADD, SUB & CMP:
N = 1 (MSB = 1) V=1
P+P=N
C = 0 (No carry) N+N=P * P = Positive Number (MSB = 0)
V = 1 (P + P = N) P–N=N N = Negative Number (MSB = 1)
N–P=P
Examples 4:
What are the results of ADDS R1, R2, # 99, if R2 contains 0xEEFFEEFF?
Instruction Data Calculation Answer
ADDS R1, R2, # 99 R2 = 0xEEFFEEFF 99 dec hex 63 R2 = 0xEEFFEF62
0xEEFFEEFF APSR
+ 63 Z= N=
EEFFEF62 in Hex C= V=
Examples 10:
MULS R4, R5, R4 ;R4=0x0000000A, R5=0x00000064
;R5=R5*R4=10*100=1000=0x000003E8
AND INSTRUCTION
R1 = 0x3795AC5F (0011 0111 1001 0101 1010 1100 0101 1111)
R2 = 0xB6D34B9D (1011 0110 1101 0011 0100 1011 1001 1101)
ANDS R1, R1, R2 ; ANS = R1=0x3691081D 0x3795AC5F 0011 0111 1001 0101 1010 1100 0101 1111
0xB6D34B9D 1011 0110 1101 0011 0100 1011 1001 1101
AND 0011 0110 1001 0001 0000 1000 0001 1101
3 6 9 1 0 8 1 D
EOR INSTRUCTION
R1 = 0x3691081D (0011 0110 1001 0001 0000 1000 0001 1101)
R3 = 0x0000898B (0000 0000 0000 0000 1000 1001 1000 1011)
0x3691081D 0011 0110 1001 0001 0000 1000 0001 1101
0x0000898B 0000 0000 0000 0000 1000 1001 1000 1011
EORS R1, R1, R3 ; ANS = R1=0x36918196
E-OR 0011 0110 1001 0001 1000 0001 1001 0110
3 6 9 1 8 1 9 6
Try It Yourself
Based on the instructions below, determine the results
START
LDR R1, =0x2375A2B4
LDR R2, =0xC9D8E735
LDR R3, =0x87A21083
LDR R4, =0xFE459001
ANDS R1, R1, R2 ; 0x0150A234
ORRS R2, R2, R3 ; 0xCFFAF7B7
BICS R3, R3, R4 ; 0x01A20082
MVNS R4, R4 ; 0x01BA6FFE
EORS R1, R1, R3 ; 0x00F2A2B6
B START
END
Examples 1: R0=0x4375A2B4
Solution :
Before R0=0x2375A2B4
After R1 = 0x86EB4568 / After R2 = 0x0DD68AD0
After 1st 1000 0110 1110 1011 0100 0101 0110 1000
SHIFT
Zero will
MSB set to C 8 6 E B 4 5 6 8 enter
After 2nd 0000 1101 1101 0110 1000 1010 1101 0000
SHIFT
MSB set to C 0 D D 6 8 A D 0
Examples 2: R0=0x8375A2B7
Solution :
Before R0=0x8375A2B4
After R1 = 0x41BAD15B / After R2 = 0x20DD68AD
After 1st 0100 0001 1011 1010 1101 0001 0101 1011
SHIFT
MSB set
Zero will enter 4 1 B A D 1 5 B to C
After 2nd 0010 0000 1101 1101 0110 1001 1010 1101
SHIFT
Zero will enter 2 0 D D 6 8 A D
Examples 3: R0=0x8375A2B7
Solution :
Before R0=0x8375A2B4
After R1 = 0xC1BAD15B / After R2 = 0xE0DD68AD
After 1st 1100 0001 1011 1010 1101 0001 0101 1011
SHIFT
MSB remains C 1 B A D 1 5 B MSB set
there to C
After 2nd 1110 0000 1101 1101 0110 1001 1010 1101
SHIFT
MSB remains E 0 D D 6 8 A D
there
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
4.2.6 SHIFT AND ROTATE
Examples 4 :
LDR R0, =0x8375A2B7
MOVS R1, #1
RORS R0, R0, R1
RORS R0, R0, R1
Solution :
Before R0=0x8375A2B4
After R0 = 0xC1BAD15B / After R0 = 0xE0DD68AD
After 1st 1100 0001 1011 1010 1101 0001 0101 1011
SHIFT
MSB remains MSB set
C 1 B A D 1 5 B
there to C
After 2nd 1110 0000 1101 1101 0110 1001 1010 1101
SHIFT
MSB remains E 0 D D 6 8 A D
there
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
4.2.6 SHIFT AND ROTATE
Try It Yourself
EXERCISES 1:
Obtain the answer for each instruction. Calculate using theory and compare the answers with Kiel µ5.
START
LDR R0, =0xAAAAAAAA
LDR R1, =0xAAAAAAAA
LDR R2, =0xAAAAAAAA
LDR R3, =0xBBBBBBBB
LDR R4, =0xBBBBBBBB
LDR R5, =0xBBBBBBBB
LSLS R0, R0, #1 ; 0x55555554, C=1
LSLS R1, R1, #2 ; 0xAAAAAAA8, N=1
LSLS R2, R2, #3 ; 0x55555550, C=1
LSRS R3, R3, #1 ; 0x5DDDDDDD, C=1
LSRS R4, R4, #2 ; 0x2EEEEEEE, C=1
LSRS R5, R5, #3 ; 0x17777777
B START
END
Try It Yourself
EXERCISES 2:
Obtain the answer for each instruction. Calculate using theory and compare the answers with Kiel µ5.
START
LDR R0, =0xCCCCCCCC
LDR R1, =0xCCCCCCCC
LDR R2, =0xCCCCCCCC
LDR R3, =0xDDDDDDDD
LDR R4, =0xDDDDDDDD
MOVS R5, #1
MOVS R6, #2
ASRS R0, R0, #1 ; 0xE6666666, N=1
ASRS R1, R1, #2 ; 0xF3333333, N=1
ASRS R2, R2, #3 ; 0xF9999999, N=1, C=1
RORS R3, R3, R5 ; 0xEEEEEEEE, N=1, C=1
RORS R4, R4, R6 ; 0x77777777
B START
END
These instructions are usually used for conversion between little endian and big endian data.
Examples 1 : 0x3795AC5F 37 95 AC 5F
R1 = 0x3795AC5F
REV R0, R1 ; ANS = R0=5FAC9537 REV 5F AC 95 37
Examples 2 : 0xB6D34B9D B6 D3 4B 9D
R2 = 0xB6D34B9D
REV16 R4, R2 ; ANS = R4= 0xD3B69D4B REV16 D3 B6 9D 4B
0x456789AB 45 67 89 AB
Examples 3 :
R3 = 0x456789AB / R3 = 0xA0A0AA55
REVSH FF FF AB 89
REVSH R5, R3 ; ANS = R5= 0xFFFFAB89 Sign extend to 11111111 11111111 10101011
; ANS = R5= 0x000055AA 32 bit
Prepared by : Siti Munaliza binti Moharad ~JKE_PSIS
C4
DEC 30043
MICROPROCESSOR FUNDAMENTALS
END OF CHAPTER 4