Question Bank #2 For MID Exam Based On Our Discussions of ARM Assembly Programming in 331,453 Lecture Classes
Question Bank #2 For MID Exam Based On Our Discussions of ARM Assembly Programming in 331,453 Lecture Classes
Solution 1:
; ARM assembly
.data
principal: .word 1000
rate: .word 5
time: .word 2
interest: .word 0
.text
.global _start
_start:
LDR R1, =principal @ Load principal
LDR R2, [R1]
done:
MOV R7, #1 @ Exit system call
SVC 0
# Problem 2: Embedded System for LED Blinking. (15 mins)
For embedded systems, a simple problem is controlling the blinking of an LED, often with a delay.
Solution 2:
; ARM assembly
.data
GPIO_BASE: .word 0x50000000 @ Base address for GPIO
LED_PIN: .word 1
.text
.global _start
_start:
LDR R1, =GPIO_BASE
LDR R2, =LED_PIN
blink_loop:
MOV R3, #1 @ Turn LED on
STR R3, [R1, R2] @ Write 1 to LED pin
BL delay @ Delay
delay:
MOV R4, #500000 @ Some arbitrary delay
delay_loop:
SUBS R4, R4, #1
BNE delay_loop
BX LR
# Problem 3: Temperature Classification using If-Else Statement (15 Minutes)
Write an ARM assembly program that classifies a given temperature value stored in register r0 using the following rules:
1. If the temperature is above 30 degrees, store the value 1 in register r1 (indicating "Hot").
2. If the temperature is between 20 and 30 degrees, store the value 2 in register r1 (indicating "Warm").
3. If the temperature is below 20 degrees, store the value 3 in register r1 (indicating "Cold").
Requirements:
• Use the CMP instruction along with conditional branching (BGT, BLE, etc.).
• After completing the classification, end the program using the appropriate ARM assembly exit sequence.
Additional Task: Write comments in the code to explain each step of the logic.
Write an ARM assembly program that calculates the sum of even numbers from an array of 10 integers stored in memory. The array
starts at the label numbers. Store the result in register r8.
Program Specifications:
1. Data Segment:
o Create an array of 10 integers in the .data segment.
o Example values: 2, 7, 4, 3, 6, 9, 12, 11, 8, 10.
2. Code Segment:
o Initialize a counter to iterate through the array.
o Use a loop structure (B and CMP) to traverse the array.
o Check if a number is even using the AND instruction.
o Add even numbers to the sum stored in r8.
3. Output:
o Store the final sum of even numbers in r8.
Example Expected Output: If the array is 2, 7, 4, 3, 6, 9, 12, 11, 8, 10, the sum of even numbers would be 2 + 4 + 6 + 12 + 8 + 10 =
42.
#Problem 5: Write an ARM assembly program that includes a simple function to compute the square of a number stored in
register r0. The result should be stored in r2.
Write an ARM assembly program that calculates the sum and average of an array of 20 integers stored in the data segment. Store
the sum in register r8 and the average in r9.
Requirements:
Example Input:
• Array: 3, 7, 12, 5, 9, 15, 22, 8, 10, 14, 18, 16, 20, 11, 13, 19, 6, 2, 4, 17
Expected Output:
Additional Instructions:
Write an ARM assembly program that reverses the order of elements in a given array of 20 integers. The original array should be
stored at the label numbers, and the reversed array should be stored at the label reversed_numbers.
Requirements:
Example Input:
• Original Array: 3, 7, 12, 5, 9, 15, 22, 8, 10, 14, 18, 16, 20, 11, 13, 19, 6, 2, 4, 17
Expected Output:
• Reversed Array: 17, 4, 2, 6, 19, 13, 11, 20, 16, 18, 14, 10, 8, 22, 15, 9, 5, 12, 7, 3
Additional Task:
ARMv7 architecture does not include a direct divide instruction. Implement a simple division algorithm using repeated subtraction to
calculate the quotient and remainder for two given numbers.
1. Program Specifications:
o Assume r0 stores the dividend (e.g., 100).
o Assume r1 stores the divisor (e.g., 7).
o The quotient should be stored in r2.
o The remainder should be stored in r3.
2. Example Operation:
o For r0 = 100 and r1 = 7, the quotient would be 14 and the remainder would be 2.
3. Hints:
o Use a loop to subtract the divisor from the dividend until the remainder is less than the divisor.
o Increment r2 (quotient) for each subtraction.
o Store the remaining value in r3.
• r2 = 14
• r3 = 2