Page 166 - ARM 64 Bit Assembly Language
P. 166
152 Chapter 5
12 if(e < a)
13 a=e;
14 return a;
15 }
5.10. Write an AArch64 assembly function to calculate the average of an array of integers,
given a pointer to the array and the number of items in the array. Your assembly func-
tion must implement the following C function prototype:
int average(int *array, int number_of_items);
5.11. Write a complete function in AArch64 assembly that is equivalent to the following C
function. Note that a and b must be allocated on the stack, and their addresses must be
passed to scanf so that it can place their values into memory.
1 int read_and_add()
2 {
3 int a, b, sum;
4 scanf("%d",&a);
5 scanf("%d",&b);
6 sum=a+b;
7 return sum;
8 }
5.12. The factorial function can be defined as:
1 if x ≤ 1,
x!=
x × (x − 1)! otherwise.
The following C program repeatedly reads x from the user and calculates x! It quits
when it reads end-of-file or when the user enters a negative number or something that
is not an integer.
1 #include <stdio.h>
2
3 /* The factorial function calculates x! */
4 unsigned long factorial(int x)
5 {
6 if (x < 2)
7 return 1;
8 return x * factorial(x-1);
9 }
10
11 /* main repeatedly asks for x, and prints x! */
12 int main()
13 {
14 int x, goodval;
15 do {