Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Mastering AVR Assembly: From Signed Binary to GCD with Macros

Learn AVR assembly programming through real-world tasks: signed binary to decimal conversion, integer square root, and GCD using macros. Perfect for COMP9032 Lab 1 students.

AVR assembly tutorial signed binary to decimal assembly AVR integer square root GCD assembly macro COMP9032 Lab 1 microprocessors and interfacing assembly programming best practices Microchip Studio simulation two's complement conversion Euclidean algorithm assembly embedded systems assembly AVR register manipulation assembly code comments AVR macro example signed number conversion AVR assembly language lab

Introduction: Why AVR Assembly Still Matters in 2026

In an era of high-level languages and AI-assisted coding, understanding assembly language might seem like a relic. Yet, for embedded systems, IoT devices, and even modern gaming peripherals, assembly gives you ultimate control over hardware. This tutorial is designed to help you tackle COMP9032 Lab 1 Microprocessors and Interfacing by breaking down three core tasks: signed binary to decimal conversion, integer square root, and GCD using macros. By the end, you'll not only complete your lab but also gain skills that are directly applicable to optimizing code for devices like the latest AI-powered drones or smart home controllers.

Task 1: Signed Binary to Decimal in AVR Assembly

The first task asks you to convert a signed binary number stored in a register into its decimal ASCII representation. For example, the binary 11111011 (which is -5 in two's complement) should become the ASCII characters '-' and '5' stored in consecutive registers.

Understanding Two's Complement

AVR uses two's complement for signed numbers. To convert a negative number, you can check the most significant bit (bit 7). If it's 1, the number is negative. Then, to get the magnitude, you compute the two's complement (invert all bits and add 1). This is a fundamental concept that also appears in financial algorithms where signed integers represent profit/loss.

Step-by-Step Assembly Logic

  1. Check sign: Use sbrc (skip if bit in register is clear) or sbrs to test bit 7.
  2. If negative: Output a minus sign (ASCII 0x2D), then negate the number using com and inc.
  3. Convert to decimal digits: Repeatedly divide by 10 using div (or a loop with subtraction) to extract digits from least significant to most significant. Store them in a buffer.
  4. Store ASCII: Add 0x30 to each digit to get its ASCII code, then store in registers (e.g., R4 and R5 for two digits).

Here's a snippet to illustrate the division loop:

; Assume R16 holds the positive magnitude
clr R17 ; quotient
loop:
cpi R16, 10
brlo done
subi R16, 10
inc R17
rjmp loop
done:
; R16 = remainder (units), R17 = tens

This approach is similar to how smartwatch firmware converts sensor readings into human-readable display values.

Task 2: Integer Square Root of an Unsigned Byte

Task 2 requires computing a√a (which is a * sqrt(a)) and rounding down to the nearest integer. For example, if a = 5, then 5√5 ≈ 11.18, so result = 11.

Algorithm: Babylonian Method or Simple Loop

Since a is a byte (0-255), you can implement a simple integer square root using a loop that checks squares. For sqrt(a), you can iterate from 0 upward until i*i > a, then i-1 is the floor sqrt. Then multiply by a. This is efficient enough for a one-byte value.

; Compute sqrt(a) in R16, a in R20
clr R17 ; i = 0
loop_sqrt:
mul R17, R17 ; i*i -> R0:R1 (but we only need low byte for small a)
cp R0, R20
brsh found
inc R17
rjmp loop_sqrt
found:
dec R17 ; floor sqrt
; Now multiply R17 (sqrt) by R20 (a) -> result in R16 (low byte)
mul R17, R20
mov R16, R0

This kind of integer math is used in game physics engines for collision detection, especially in retro-style games that run on microcontrollers.

Task 3: GCD of an Array Using Macros

The final task involves computing the GCD of an array of up to 15 unsigned bytes. The array size is in R0, and elements start at R1. You must use macros to improve code readability.

Why Macros?

Macros allow you to reuse code snippets without the overhead of function calls. In AVR assembly, macros are expanded inline, making your program faster and more modular. For instance, you can define a macro for swapping registers or for the Euclidean algorithm step.

Euclidean Algorithm in Assembly

The GCD of two numbers can be found by repeatedly replacing the larger number with the remainder of division. AVR has no hardware divide, so you implement modulo via subtraction.

; Macro: GCD of R16 and R17, result in R16
.macro gcd_two
local loop
loop:
cp R16, R17
breq done
brlo swap
sub R16, R17
rjmp loop
swap:
mov R18, R16
mov R16, R17
mov R17, R18
rjmp loop
done:
.endm

Then, for the array, you load the first element into R16, then repeatedly apply gcd_two with the next element. This is analogous to how blockchain consensus algorithms verify data integrity by repeatedly applying a reduction function.

Testing Your Code

Use Microchip Studio's simulator to set register values before execution. For example, to test Task 3 with an array of {12, 18, 24} (size=3), set R0=3, R1=12, R2=18, R3=24. The expected GCD is 6. Record a demo video showing these test cases, as required by the lab.

Best Practices for Assembly Programming

Throughout your lab, maintain readability:

  • Header comments: Include your name, date, and program description.
  • Inline comments: Explain why, not just how. For example, ; Subtract 10 because we need decimal digit.
  • Consistent layout: Align labels, instructions, and comments in columns.

These practices are not just for school; they are standard in professional embedded systems where code is maintained for years.

Conclusion

By completing these three tasks, you will have mastered signed number conversion, integer square root, and GCD computation in AVR assembly. These skills are foundational for any career in embedded systems, from autonomous vehicles to wearable health monitors. Remember to test thoroughly and comment your code well. Good luck with your lab!