46.9k views
1 vote
Write an ARMv8 assembly function (sum_srs) to compute the equation y=∑

n=1
a

(a
n
+b). The inputs and to the function are passed using the register X1 and X2 respectively and the result is returned using the register X0. Write a ARMv8 assembly program (main) to test the function. The user inputs and is in registers X20 and X21 respectively and store the returned summation value in X22. Assume all the values are signed 64-bit non-zero positive integers and all the registers are used by the caller function to store some data before calling the function. Assemble, test, and simulate the assembly code using DS-5 simulator. Do not upload the entire DS-5 project. Only upload the assembly file (.S file) from the DS-5 project to Canvas. Comment your assembly code.

User Kerith
by
8.0k points

1 Answer

6 votes

The sum_srs function calculates the summation according to the provided formula, and the main function tests the sum_srs function with user inputs.

.global sum_srs

.global main

.section .text

sum_srs:

// Inputs: X1 (n), X2 (a), Outputs: X0 (result)

// Loop through n and calculate the sum

mov x0, 0 // Initialize result to 0

mov x3, 1 // Initialize n to 1

sum_srs_loop:

cmp x3, x1 // Compare n with the given value

b.gt sum_srs_done // If n > given value, exit the loop

mul x4, x2, x3 // a * n

add x4, x4, x2 // a * n + b

add x0, x0, x4 // result += a * n + b

add x3, x3, 1 // Increment n

b sum_srs_loop // Repeat the loop

sum_srs_done:

ret

// Example inputs (you can replace these with actual user inputs)

mov x20, 5 // n

mov x21, 2 // a

// Call sum_srs function

bl sum_srs

// Result is in X0, move it to X22

mov x22, x0

// Exit the program

User Manuel Spuhler
by
7.8k points