Home CPSC 305

Average of 4 Exercise



To get experience writing ARM assembly code for the GBA using arithmetic and shift operations.


For this lab, you will write an ARM assembly function to calculate the average of four parameter values. Your function will take 4 integers as parameters (the most that can be passed solely using registers), and compute the average value of them.

  1. Start by downloading a GBA skeleton program with an empty average4 function:
    wget https://ianfinlayson.net/class/cpsc305/labs/average4.tar.gz
    tar xvf average4.tar.gz
  2. Compile the program so that you can see you have everything set up correctly:
    gbacc average4.s main.c
  3. Run the program.gba program this creates. It should print that the average is 160. This is because the first value, 160, is passed in as r0 which is also used as the return value. Since the function does nothing, the first parameter is used as a return value.
  4. Edit the average4 function in average4.s so that it computes the average of the four parameter values. Remember that there is no division instruction on the GBA! How else could we divide by 4?
  • When the program works, it should return the correct value for the average of those four numbers which is 130.



    When you are done, please submit the assembly code under the assignment in Canvas.

    Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.