Lab Sheet 7 19EAC 285: Microcontroller Lab
Lab sheet 7: ARM7 Programming using Arithmetic and
Logical Instructions.
Subject: Microcontroller Lab Register Number:
Sub Code: 19EAC285 Name:
Experiment No. : 7 Date:
Course Outcome: CO4
DATA PROCESSING INSTRUCTIONS IN ARM
The data processing instructions manipulate data within registers. They are classified into:
i.Move instructions
i.Arithmetic instructions
i.Logical instructions
i.Comparison instructions
i.Multiply instructions.
Move instructions
Copies N into destination register Rd where N is a register or immediate value.
Arithmetic instructions
The arithmetic instructions implement addition and subtraction of 32-bit signed and
unsigned values.
Example 1: Perform 5x2 - 6x + 8 using data processing instructions in ARM. Assume that
the input value x is 7 and is loaded into register R0. Move the constants in the
1
Lab Sheet 7 19EAC 285: Microcontroller Lab
expression to appropriate registers and store the final result in register R6. Attach the
screenshot of the final status of the PC and relevant registers.
Logical instructions
Logical instructions perform bitwise logical operations on the two source registers.
Example 2: Compute bitwise calculation for the following Boolean expression.
Assume that A, B, C, and D are in R1, R2, R3, and R4, respectively. Load the values
A = 3, B = 7, C = 9, and D = 11 in the respective registers. Attach the screenshot of
the final status of the PC and relevant registers.
2
Lab Sheet 7 19EAC 285: Microcontroller Lab
Comparison instructions
The comparison instructions are used to compare or test a register with a 32-bit
value. They update the CPSR flag bits according to the result, but do not affect
other registers. For these instructions no needs to apply the S suffix for update the
flags.
Example 3: The following code checks whether the given input value (in register r2) is
odd or even using compare instruction; if the value is even then it moves E to register r0
else moves 0. Attach the screenshot of final status of PC and relevant registers. Briefly
specify how the condition codes get affected in CPSR.
Multiply Instructions:
Control structures in ARM
There are three basic types of control structures that may be implemented.
1. Sequential control
1. Decision-making and selection control
3
Lab Sheet 7 19EAC 285: Microcontroller Lab
1. Loop control
By default, the execution of a program is sequential.
Decision-making and selection control.
a. Simple if…..else
Example 1: The following code checks whether the given input value (in
register r2) is odd or even; if the value is even then it moves E to register r0 else
moves 0. Note that, if you replace AND with ANDS, the operation's result may
impact the Status Register (CPSR); so the BNE instruction works well without
CMP.
b. Nested if…. else structure
Example 2: Assume that an input value is loaded into register r1; if it is 1
then load register r2 with 0x0A; else if it is 2 then load 0x0B in register r2;
else if it is 3 then load 0x0C into register r2; else load 0x0D (default value to
be loaded in the register)
4
Lab Sheet 7 19EAC 285: Microcontroller Lab
Loop control
We will discuss how loop structures are implemented in ARM assembly language
Example 3: The following template executes an empty body six times; the loop control
variable is assumed to be in register r1, which is initialized with zero and incremented
up to 5; i.e. every time value in r1 is compared with 0x05, if it is less than or equal to 5
(i<=5), then it loops otherwise exits.
Conditional branches
To use the condition bits in the CPSR we can use condition codes that correspond to
various logical combinations of the bits. Here are the most common codes:
5
Lab Sheet 7 19EAC 285: Microcontroller Lab
These are the less common codes:
These codes can be added to a B instruction to modify the circumstances under which it
branches. Like BEQ for “branch if equal” or BGT for “branch if greater than”.
Rotate and Shift Instructions
6
Lab Sheet 7 19EAC 285: Microcontroller Lab
Exercise
1. Execute the following commands in the simulator and fill the
required fields (use red color). Add comments wherever
appropriate.
1 MOV R2, #0x01 ; R2 = ?
2. MOV R3, #0x02 ; R3 = ?
3.
4. ;Other examples to move immediate values
5. MOV R5, #0x3210 ; R5 = ?
6.
7.
8. LDR R7, = 0x87654321 ; R7 = ?
9.
10.
11. ADD R1,R2,R3 ; R1 = ?
12.
13. ADDS R1,R2,R3 ; R1 = ?
14. ; specify Condition Code updates
15.
16. SUBS R1,R2,R3 ; R1 = ?
17. ; specify Condition Code updates
18.
19. MOV R4, #0xFFFFFFFF ; R4 = ?
20. ADD R1,R2,R4 ; R1 = ?
21. ; How did that operation affect the flags in CPSR?
22.
23. ADDS R1,R2,R4 ; R1 = ?
24. ; Please specify Condition Code updates
25. ; and now what happened to the flags in the CPSR?
26.
7
Lab Sheet 7 19EAC 285: Microcontroller Lab
27.
28. MOV R2, #0x00000002 ; R2 = ?
29. ADDS R1,R2,R4 ; R1 = ?
30. ; again, what happened to the flags?
31.
32. MOV R2, #0x00000001 ; R2 = ?
33. MOV R3, #0x00000002 ; R3 = ?
34. ADDS R1,R2,R3 ; R1 = ?
35. ; Add some small numbers again
36. ; and check the flags again......
37.
38. ; Add numbers that will create an overflow
39. MOV R2, #0x7FFFFFFF ; R2 = ?
40. MOVR3, #0x7FFFFFFF ; R3 = ?
41.
42. ADDS R1,R2,R3 ; R1 = ?
43. ; Check the flags in the CPSR?
44.
Note: Line 5 gives error, but lined 39, 40 works. The explanation is in the following website
https://developer.arm.com/documentation/dui0068/b/Writing-ARM-and-Thumb-
Assembly-Language/Loading-constants-into-registers/Direct-loading-with-MOV-and-MVN
8
Lab Sheet 7 19EAC 285: Microcontroller Lab
2. Perform arithmetic operation for evaluating A + 6B – 4C where A = 12,
B = 7 and C = 5 using appropriate arithmetic instructions. Attach the
screenshot of the code and register window.
9
Lab Sheet 7 19EAC 285: Microcontroller Lab
3. Write an ALP to find the largest/smallest number in an array of 32
numbers.
4. Write an ALP to find the sum squares of the first 10 integer
numbers.
10
Lab Sheet 7 19EAC 285: Microcontroller Lab
5. Write an ALP to implement the following sequence
Signature of Faculty in charge:
11
Lab Sheet 7 19EAC 285: Microcontroller Lab
12