Home Page > > Details

COMP9032 Lab 1

 1

COMP9032 Lab 1
Sept. 2022
For this lab, your work is assessed individually.
1. Objectives
In this lab, you will learn
• AVR instructions, and 
• basic assembly programming.
2. Programming Style
The general practice, when you write an assembly program, is to maintain the 
readability and consistency of your code. For this reason, you are encouraged to 
adopt the following rules:
• Starting each source code file with a heading that includes:
o your name so that it is easy to see who is responsible for the 
file, the date of last modification and a version number, and 
o a description of what the program does, possibly with a 
pseudo-code for a high-level abstraction.
• Including appropriate comments that explain the “why", not just the 
“how" of the program throughout the source code.
• Using a sensible layout for your code - to make it easy to see the code 
structure, instructions, and any labels.
3. Tasks
There are three tasks in this lab.
3.1 Task 1 (10 marks, due your lab session in Week 3)
Write an assembly program to calculate the square root of a one-byte positive 
number. The result is rounded to integer directly, e.g 7.6 is rounded to 7.
2
Here we assume the input value is stored in register R5, which will be manually 
set to a value before execution (See explanation on page 5 of Lab 0 on how to 
set a register value), and the result will be saved in register R6. 
Run your program in Atmel Studio and demonstrate your work to your tutor.
3.2 Task 2 (5 marks, due your lab session in Week 4)
The ldi instruction can be used to load a digital character into a register. For 
example, the instruction, ldi r16, ‘6’, will set the value of register r16 to 0x36, 
where 0x36 is the ASCII code for character ‘6’ (See the ASCII table shown at the 
end of this document).
Write an assembly program that loads one digital character plus a sign, for a 
decimal number, into a group of two registers and converts the decimal 
number to a signed binary number in the 2’s complement format and saves the 
signed binary in a third register, as illustrated in the figure below, where each 
block represents a register. 
11111010 
signed binary ASCII code of 1-digit signed decimal
 - 
Rr+1 Rr+2 Rd
Figure 1: Decimal number -6 (in ASCII) in a two-register group is converted into 
signed binary number 11111010
Run your program in AVR Studio and demonstrate your work to your tutor. 
3.3 Task 3 (15 marks, due your lab session in Week 4)
The greatest common divisor (GCD) of two integers can be calculated in a way 
as given in a C-like pseudo code shown in Figure 2. Based on this calculation 
approach, write an assembly code to get the GCD of four integers. Here we 
assume all integers are 16-bit unsigned numbers; They are stored in registers
and the values can be set before execution (refer to Page 5 of Lab 0 on how to 
set a register value).
3
Figure 2: Pseudo Code gcd
For this task, you are required to use macro that is discussed in Week 2.
Run your program in Atmel Studio and demonstrate your work to your tutor.
NOTE: 
• You can put your code for each task in the same project in the Atmel 
Studio for this lab. Run the program for each task by setting it as the entry 
file, which was explained in Lab 0.
• All your programs should be well commented and easy to read. Up to 
10% marks will be deducted for each program without proper and 
sufficient comments.
• Your lab tutor will setup the environment for the assessment in Week 3. 
/* below is only part of C function
int a, b; /* 16-bit unsigned integer
while (a!=b)
{ /* Assume a, b > 0 */
if (a>b)
 a = a - b;
else
 b = b - a;
}
/* a and b both hold the result */
4
Appendix: ASCII Table 
Contact Us - Email:99515681@qq.com    WeChat:codinghelp
Programming Assignment Help!