0% found this document useful (0 votes)
39 views5 pages

Question Bank #2 For MID Exam Based On Our Discussions of ARM Assembly Programming in 331,453 Lecture Classes

Uploaded by

istiaqmahmud9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

Question Bank #2 For MID Exam Based On Our Discussions of ARM Assembly Programming in 331,453 Lecture Classes

Uploaded by

istiaqmahmud9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Implement solutions in ARM Assembly.

#Problem 1: Calculate Simple Interest (15 mins)

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]

LDR R3, =rate @ Load rate


LDR R4, [R3]

LDR R5, =time @ Load time


LDR R6, [R5]

MUL R7, R2, R4 @ Multiply principal by rate


MUL R7, R7, R6 @ Multiply by time

MOV R8, #100 @ Divide by 100


UDIV R7, R7, R8

STR R7, [R1, #4] @ Store the result in interest

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

MOV R3, #0 @ Turn LED off


STR R3, [R1, R2] @ Write 0 to LED pin
BL delay @ Delay

B blink_loop @ Repeat loop

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.

Example Input: Assume r0 is initialized to a value of 25.

Expected Output: r1 should store 2 (indicating "Warm").

Additional Task: Write comments in the code to explain each step of the logic.

#Problem 4: Sum of Even Numbers using a For Loop (15 Minutes)

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.

1. Implement a function square that:


o Accepts a value in r0.
o Calculates the square of the value.
o Returns the result in r2.
2. In the main program:
o Call the function square using BL (Branch with Link).
o Test the function by setting r0 to 5.
o Print the result in r2.

Expected Output: For r0 = 5, r2 should store 25 (5 squared).


Problem 6: Calculate the Sum and Average of an Array (15 Minutes)

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:

1. Create an array of 20 integers in the .data segment.


2. Use a loop to traverse through the array and compute the sum.
3. Calculate the average by dividing the sum by the number of elements (20).
4. Use only basic arithmetic operations like ADD, SUB, and CMP.
5. Since ARM does not have a direct division instruction, implement the average calculation using repeated subtraction or
shifting.

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:

• r8 should store the sum of the array.


• r9 should store the average (truncated value).

Additional Instructions:

• Use detailed comments to explain each step of the program.

Handle potential errors such as dividing by zero.

Problem 7: Reverse the Elements of an Array (15 Minutes)

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:

1. Create an array numbers of 20 integers in the .data segment.


2. Create an empty array reversed_numbers to store the reversed elements.
3. Implement a loop to iterate through the original array (numbers) and store the elements in reverse order in
reversed_numbers.
4. Use the following instructions: LDR, STR, ADD, SUB, CMP, BNE.
5. Store the reversed array values in a new location and display the result by printing the values in the new array.

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:

• Identify any logical errors in your code and provide corrections.


#Problem 8: (10 Minutes): Implementing a Pseudo-Divide Instruction

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.

Expected Output: For r0 = 100 and r1 = 7:

• r2 = 14
• r3 = 2

You might also like