Lab 2 - Intro To ASM Programming
Lab 2 - Intro To ASM Programming
Background
Generally speaking, a microcontroller is a "computer-on-a-chip." In addition to the registers file and all the
arithmetic and logic elements (the CPU), the device also incorporates read-only and read-write memory (ROM
and RAM), input/output interfaces (I/O ports), and timers.
Microcontrollers are frequently used in automatically controlled products and devices, such as automobile
engine control systems, office machines, appliances, power tools, and toys.
Architecture
AVR microcontrollers are designed after a Harvard
architecture, i.e., data memory and program memory
are physically separated and signals travel over different
buses.
This is the block diagram of the ATmega324PB chip,
which is the one we will be using throughout the lab. It
is important to mention that in this architecture,
instructions are 16-bits long (2 bytes), but only 8-bits (1
byte) are used for data. Therefore, results and operands
connect to an 8-bit bus that, in turn, connects with
memory and input/output lines. In contrast,
instructions travel on a 16-bit bus, which is connected
with any instruction-related components like the
program memory, the instruction decoder, or the ALU.
Port Modules
The microcontroller interfaces with external hardware through
ports, a collection of wires and registers to send or detect digital
signals. The actual point where hardware and microcontroller
connected physically is known as a pin.
The ATmega324 has bidirectional pins, that is, each one can be
programmed to work as either data input or as data output. To
allow this flexible behavior, they are associated with the three
registers, which form an I/O module.
The ATMega324 has 5 ports, each with 8 I/O pins. Ports are
identified with the letters from A to E. The figure at the left
represents the general structure of any port in the device.
DDRA (Data Direction Register) configures port A through the registers Port A and Pin A. The 8-bit value in
DDR indicates what pins will be used for data input (pin register) and what pins will be used for data output
(port register). Thus, when the appropriate command is issued, Port A gets data from the data bus and writes
it to any pin that has been set as output in DDRA. In a similar way, Pin A gets data from any pin set as an
input in DDRA and writes it to the data bus. All of the ports on the AVR are configured the same way using
DDRA – DDRE.
Instruction Set
The table below summarizes the AVR architecture instruction set:
For more information on what each of these instructions does or how they work, you should refer to your
textbook or the Atmel 8-bit AVR Instruction Set manual.
Procedure
1) Atmel Studio Tutorial
a. In order to become familiar with the basics of Atmel Studio 7 and its debugging functionality,
you should complete the Assembly Programming in Atmel Studio 7 – Step by Step Tutorial.
2) Delay Loops
a. The code you created during the tutorial was written to toggle all of the LEDs connected to Port
D on and off. After programming the AVR Simon Board with the code in the tutorial, you may
notice that the LEDs don’t seem to turn on and off. They actually are turning on and off but
because of the lack of any delay, the LEDs are flashing very quickly. In order to fix this
problem, you need to create a delay subroutine into your assembly code so you can actually
observe the LEDs flashing on and off. Since a register can only hold a value up to 255, you will
probably need to use nested loops to do this and CALL it when you need to use it.
Deliverables
For this lab, you will submit the three different programs you wrote. Each of these programs should be
thoroughly documented so that someone else who has not completed this activity can follow your code and
understand how your solution works. Make sure each of your programs has an appropriate comment block
at the top of the file (see MST Computer Science Department’s Coding Standards). In future labs, you will be
required to submit a lab report.
Questions/Observations
1. Are the LEDs active low or active high? Did your code use a 0 or a 1 to turn the LED on? Verify your
answer on the Simon Board datasheet to determine whether a logical 0 or 1 turns on the LED.
2. Are the push buttons active low or active high? Did your code use a 0 or a 1 to indicate that a button
has been pushed? (Or, what is the constant value of the button when it is not pushed?) Verify your
answer on the Simon Board datasheet to determine whether a logical 0 or 1 is used to indicate a
button push.
3. On the Simon Board datasheet, list which port pin each LED is connected (for all 9 LEDs).
References
● Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi. 2010. AVR Microcontroller and Embedded
Systems: Using Assembly and C (1st ed.). Prentice Hall Press, Upper Saddle River, NJ, USA.
● Vasconcelos, Jorge, 2009. Brief Guide to the AVR Architectures and the AT90USBkey Demonstration
Board. John Hopkins University, Department of Computer Science.