https:
eclass.yorku.ca/pluginfile.php/4314832/mod_resource/content/1/LabF_AVR_MoreAssembler_Student_EECS2021_F2022
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
EECS 2021 – Lab F: More Assembler on the ATMEGA328P
Dr. James Andrew Smith, PEng.
Background
We are continuing our work on developing Assembler code from a C base. Here you will work
towards being able to return a value from an Assembler function back into C.
References:
1. XC8 document
(https:
ww1.microchip.com/downloads/en/DeviceDoc/MPLAB%20XC8%20C%20Compi
ler%20UG%20for%20AVR%20MCU%2050002750C.pdf)
2. ATMEL AT1886: Mixing Assembly and C with AVRGCC
(https:
ww1.microchip.com/downloads/en/appnotes/doc42055.pdf). File:
https:
www.microchip.com/en-us/application-notes/an42055
Marking Guide
You can either demonstrate the individual parts to the TA over Zoom or you can do it as a
ecording submitted in video form to eClass by the Sunday after your lab.
• Part 1: 0.2 pts. You need to demonstrate the LED flashing, using assembler code, as
instructed. Without hardware you can do this for half points and a memory view in
simulation.
• Part 2: 0.4 pts. All or nothing. You must demonstrate the step-by-step progress in the .S
file.
• Part 3: 0.4 pts. All or nothing. You must show the transition from C to Assembler and
ack again. It should be apparent that the math operations were conducted in
Assembler and that the co
ect value was returned to C via register R24.
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
Part 1: Calling an Assembler function from C
Review the video at https:
youtu.be/iztabJQNMEw.
Start a new MPLAB X project. Create a regular C Main file
In the main function of the C file, set Bit 5 on Port B to be an
output bit, because it is connected to an LED.
Then, write three more lines in C to turn Bit 5 on PORT B off
and then on and then off again.
Then, call the assembler function, which you’ll call later write in a
file called changePort.S. The function should be called like this:
changePort();
Then, write an assembler file that represents a function, as shown in that video. Remember that
the assembler file needs to have a .S file extension.
Below is a template for the assembler file:
#include
Prototype the assembler function
extern void changePort(void);
int main(void) {
Set up PORT B
Call the assembler function.
put an infinite while loop here
return(0);
}
#include
.section .text
.global changePort
changePort:
; fill in your code here.
; fill in your code here.
; fill in your code here.
ret
.end
Template for main.c Template for changePort.S
Now, demonstrate to your TA that you can step through your program, showing the PB5 LED
changing off-on-off within the C main function and then, after you enter the assembler function,
the LED turns on again. If you cannot demonstrate in hardware but can in simulation using the
I/O memory view you will get half marks.
Figure 1 This video gives you an overview of creating a
stand-alone assembler file in a project that is otherwise
written in C. https:
youtu.be/iztabJQNMEw.
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
Part 2: Perform Math Operations in Assembler
In this course, students are be expected to perform math operations in Assembler, using temporary registers. Math
operations include adding, subtracting, multiplying, AND’ing, OR’ing, etc. Here is an example:
OR two binary patterns 0b0010 and 0b0001 to result in 0b0011, stored in register r22
As a result, Register r22 will contain the result 0b XXXXXXXXXX:
This was done by creating a C main file in MPLAB X, as we did in Part 1, but we called an assembler function called
doMath, which we wrote in a file called doMath.S.
Demonstrate to the TA during the lab session (or in a recorded video) that you can perform a standalone math
operation like an AND, ADD, SUB, etc. as a set of assembler commands in a stand-alone assembler function, called
y a C main function, as above. Simulate or run your program on the real ATMEGA328 board, sharing your screen
and stepping through the math operation to show the change in the register.
Table 1 Tasks for Part 2. Write a program for the ATMEGA328 that can perform one of these tasks. Select the task based on
your last name.
Version First letter of
family (last) name
Problem to solve
2i S-Z add 4 and 5 together, store the
esult in register R16. (should
e 0x09)
2ii A-F Subtract 4 from 5, store the
esult in register R16 (should
e 0x01)
2iii G-M Store 6 in r17. Increment it
and store in r17. (should be
0x07)
2iv N-R Store 0xF in r16. Decrement
it twice and store in r16.
(should be 0x0D)
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
Part 3: Return the Assembler Math Result into C
We’re going to do more math problems now. This time, however, we’re going to return the
value of the assembler routine to C. We’ll do that by storing the result in Register pair R24 and
R25. That allows us to store up to 16 bits of data in the return operation, however we’ll only use
the bottom 8-bits found in Register R24.
The way we do this is outlined in an Application Note at Microchip, AN 42055
(https:
www.microchip.com/en-us/application-notes/an XXXXXXXXXXBasically, it works the same as
it did in Part 2. However, you’ll need to store the result in R24 and clear the value in R25 before
the “return” command (RET).
Also, set a variable watch on your return variable in the main function.
*
* File: mainMathReturn.c
* Author: drsmith
*/
#include
assembler function prototype
extern uint8_t doMathAgain(void);
int main(void) {
volatile uint8_t myReturnValue = 0;
asm("nop");
eakpoint here.
call the assembler function and return a value.
myReturnValue = doMathAgain();
make sure to halt here so that you can show the TA
that your return variable is co
ect.
use the return value to prevent a compiler e
or.
myReturnValue ++;
infinite loop.
while(1){
XXXXXXXXXXasm("nop");
}
return (0);
}
what goes here?
doMathAgain:
what should you write in here?
et
value returned is held in
R25 (high byte) and R24 (low byte)
.end
mainMathReturn.c doMathAgain.S
In your particular work, you will want to follow these steps:
1. Do your math operation.
2. Store the result of the math operation in a general register like R16
3. Move (MOV) the result into R24, the bottom byte of the 16-bit return register
4. Clear (CLR) the top byte in R25
5. Use the return command (RET).
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
Version First letter of
family (last) name
Problem to solve
3i G-M add 4 and 5 together, return
the result
3ii N-R Subtract 4 from 5, return the
esult
3iii A-F Store 6 in r17. Increment it
and return the value.
3iv S-Z Store 0xF in r16. Decrement
it twice and return the result
EECS 2021 – Lab F: More ATMEGA Assembler PROF VERSION F2022
You do not have permission to share or distribute this document outside of York University.
Demonstrate to the TA that you math operation in Assembler is called and that it returns the
co
ect value to the C main function. You need to show the transition from C to Assembler and
ack to C while you single step with the debugger.
FEES DE demu e %- %- %- b-R-B.30 BO 0 0 &:* cc @ = Fg 8 vv aseachen Q
Projects | Files | Services = | startPage © |Z]