Introduction#
This lab is for you to get some extra practice with ARM assembly functions, and converting C functions into ARM assembly.
We expect that by now you’re able to write functions in ARM.
Make sure you’re completed all the tasks in the previous two labs:
If you don’t remember how the calling convention works, make sure you read up on that again before you attempt this week’s lab exercises.
Here’s an ARM assembly function template for you to use/copy for use in these exercises.
.syntax unified
.global exercise_N
.type exercise_N, %function
exercise_N:
@ ...
bx lr
.size exercise_N, . - exercise_N
We don’t have an lab pack template for this week’s lab. You can use your regular main.s in either your lab-07 or lab-08 folders in the second lab pack.
If you’d like to test yourself, you can try doing these questions on pen-and-paper under exam conditions.
Exercise 1#
Write an ARM function called exercise_1, which takes 2 numbers as input, and returns the smallest.
Then, write another function called exercise_1b which does the same, but for 3 inputs.
We don’t have a CI for this week’s lab! Please discuss with your tutor if you get stuck or you want us to check if your solutions are correct.
Exercise 2#
Write the ARM equivalent to this C function:
int exercise_2(int x, int min, int max) {
if (x < min) {
return min;
}
if (x > max) {
return max;
}
return x;
}
Forgotten how to convert C syntax into ARM assembly? Check out the relevant lecture slides:
What does this function do?
Exercise 3#
Write the ARM equivalent to this C function:
int exercise_3(int n) {
int result = 1;
while (n > 1) {
result *= n;
n--;
}
return result;
}
Relevant lecture slides:
What does this function do?
Exercise 4#
Write the ARM equivalent to this C function:
int exercise_4(int n) {
int count = 0;
while (n != 0) {
count += n & 1;
n >>= 1;
}
return count;
}
Relevant lecture slides:
What does this function do?
Exercise 5#
Write a function called exercise_5, which takes in an integer array as its first input, and the length of the array
(in elements) as its second input, and returns the sum of all the elements.
Exercise 6#
Write the ARM equivalent to this C function:
int exercise_6(int array[], int length, int target) {
for (int i = 0; i < length; i++) {
if (array[i] == target) {
return i;
}
}
return -1;
}
Relevant lecture slides:
What does this function do?
Exercise 7#
Write the ARM equivalent to this C function:
int exercise_7(int array[], int length) {
int idx = 0;
for (int i = 1; i < length; ++i) {
if (array[i] > array[idx]) {
idx = i;
}
}
return idx;
}
Relevant lecture slides:
What does this function do?
Exercise 8#
Write the ARM equivalent to this C function:
int exercise_8(int n) {
int total = 0;
while (n > 0) {
if (n > 10) {
n -= 3;
} else {
n -= 1;
}
total += 1;
}
return total;
}
Exercise 9#
Write the ARM equivalent to this C function:
void exercise_9(int array[], int length) {
for (int i = 0; i < length; ++i) {
int c = array[i];
if (c >= 'a' && c <= 'z') {
array[i] = c - 32;
}
}
}
What does this function do?
Exercise 10#
Write the ARM equivalent to this C program.
int sub_function(int y, int w, int x) {
return y * w + x;
}
void exercise_10(int w, int h, int array[]) {
int index = 0;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
array[index] = sub_function(y, w, x);
index += 1;
}
}
}
Exercise 11#
Write the ARM equivalent to this C function.
int exercise_11(int x) {
if (x < 0) {
return -1;
}
int y = 15;
switch (x) {
case 0:
y = 10;
break;
case 2:
y = 500;
break;
default:
y += x;
break;
}
return 2 * x + y;
}
Relevant lecture slides:
Extra Challenge 1#
These last few problems are gnarlier than the ones above!! We don’t expect you to get through these.
Write the ARM equivalent to this C function:
void challenge_1(int* array, int length) {
for (int i = 1; i < length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
What does this function do?
Extra Challenge 2#
Write the ARM equivalent to this C function:
int challenge_2(int* array, int len, int target) {
int lo = 0;
int hi = len - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return -1;
}
What does this function do? Are there special requirements on the array input?
Extra Challenge 3#
Write the ARM equivalent to this C program:
int challenge_3_helper(int* array, int lo, int hi, int target) {
if (lo > hi) {
return -1;
}
int mid = lo + (hi - lo) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
return challenge_3_helper(array, mid + 1, hi, target);
} else {
return challenge_3_helper(array, lo, mid - 1, target);
}
}
int challenge_3(int* array, int length, int target) {
return challenge_3_helper(array, 0, length - 1, target);
}