https://eclass.yorku.ca/pluginfile.php/4300748/mod_resource/content/1/LabE_EECS2021_MixC_ASM_AVR_Student_VERSION_F2022EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION...

1 answer below »
it is three part for C programming
you will need my last name first letter and it is A







https://eclass.yorku.ca/pluginfile.php/4300748/mod_resource/content/1/LabE_EECS2021_MixC_ASM_AVR_Student_VERSION_F2022 EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. EECS 2021 – Lab E: A Beautiful Ensemble: C & Assembler on the ATMEGA328PB Dr. James Andrew Smith, PEng. Figure1Summaryoftasksforthislab.ThedemoshouldbedoneonATMEGAhardware,butcanbedonein simulationwitha25%penalty. Summary: You will combine C programming and Assembler on the ATMEGA328P. In this lab, with the ATMEGA328, you will see how to can write your work in C while also inserting assembler operations “in-line”. (You need the Grove Beginner Kit for Arduino hardware for this lab.) Pre-Lab: You are to prepare for the lab by doing the following 1. Finish Labs A through D 2. Watch any videos on eClasss 3. Review notes on XC8 and the ATMEGA328 https://bit.ly/3jB0DhB(MixC& Assembler) There are three parts to this lab: 1. Part 1 of the lab. • Demo to the TA with hardware (blinking light) • No hardware? Simulate it. 2. Part 2: Inline Assembler to solve a simple math problem • Demo to the TA in hardware (if you have it) • … or in simulation demo if you don’t have working hardware 3. Part 3: Inline Assembler and return the result of a simple math problem to C. • Demo to the TA in hardware (if you have it) • … or in simulation demo if you don’t have working hardware Figure2YoucanuseAssemblerinmultipleways withinaCorC++project. EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. Marking: • DemoPart1.(ToggleLEDsviaAssembler) o 0.2marks.0.1marksifyouareonlypartiallysuccessful.0ifyoudonotattempt. o Labdemorequired(webcam).YoucanshowtheLEDchangingwhilestepping throughthedisassembledcode. o Three-quartermarksinyou’rerunninginsimulation.(i.e.non-functionalhardware). • DemoPart2(ShowtheAssemblermathoperationworks) o 0.4marks.0.2marksifyouareonlypartiallysuccessful.0ifyoudonotattempt. o Labdemoreq’dbysharingscreenorrecordedvideo.Runprogramonhardware, showdebugview. o Three-quartermarksifyou’rerunninginsimulation.(i.e.non-functionalhardware). • DemoPart3(ShowyoucantransferAssemblermathresultbacktoC) o 0.4marks.0.2marksifyouareonlypartiallysuccessful.0ifyoudonotattempt. o Labdemoreq’dbysharingscreenorrecordedvideo.Runprogramonhardware, showdebugview. o Three-quartermarksifyou’rerunninginsimulation(i.e.non-functionalhardware). It is important that the TA see the project settings for your program in the shared screen or video. It should show that you have the Snap debugger connected to your hardware. It is up to you to make sure that this is visible to the TA, otherwise it counts as non-hardware (simulated). If you do not have working hardware: o Youcandothethreedemonstrations,butyou’llneedtodosoinsimulation. § ForPart1,you’llneedtoshowthatthePB5bitchanges. § ForParts2and3,you’llneedtoshowhoweitheravariableoraregister changesvalues.Thiscanbedone,forinstance,usingawatchoptionin MPLABX. o Simulations,withoutworkinghardware,areworththree-quartersofthemark associatedwithdoingitwithworkinghardware. Figure3Aregularprogram,writteninC(1)canhaveinlineAssemblercommands(line26)orcanmakefunction callsonline38tostand-alonefunctionswritteneitherinCorAssembler(2).Evenifyou’renotwritinganythingin Assembler,youcanstillrequestthe“Disassembled”viewofyourentireproject,asshownin(3).Combinedwith memoryandregisterviewslike(4)youcanobtainalotofinsightsintohowyourmachineisactuallyworking. EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. Introduction To balance speed of development with efficient use of processor resources real-world engineers and computer scientists combine Assembler and C in embedded systems like, ARM, RISC-V, PIC32 or the ATMEGA328. This middle-of-the-road approach is very effective, and, in this lab, you will see how to do so in very practical ways In this lab, with the ATMEGA328, you will see how to can write your work in C while also inserting assembler operations “in-line”. In a future lab, you may see how a similar process can be followed but by having a main function in C and then calling a special function written in Assembler, as previewed in https://youtu.be/iztabJQNMEw. There are three major types of applications that require at least a superficial understanding of Assembler and the interactions between your compiler and the machine under the hood: embedded device developers, operating system & device driver developers and computer security professionals. To interact and understand the system “under the hood” one typically develops an understanding for, at minimum, being able to read the Assembler listings produced by your C compiler. MPLAB X and MCUXpresso, like most modern IDEs will provide you with a “disassembly” tool that gives you a representation of the machine codes, written as Assembler calls, that the compiler believes represents the intention of your C program. A really convenient way of examining the Assembler version of your high-level program for a multitude of languages (C, C++, Ada and many, many others), compilers (e.g. gcc and clang) and computer architectures (AVR, MIPS, ARM Cortex m0+ 1 , x86, etc.) is Godbolt’s Compiler Explorer, shown below. Your MPLAB X IDE can also create disassembled code, as shown in the videos: https://youtu.be/8_VLufBGIIs. 1Ifyou’reinterestedinuseCompilerExplorerforexaminingchipsliketheKinetisorSAMDor STM32,includingspecificflavoursliketheARMCortexm0+(ARMGCC8.x)usethefollowing modifiers:-mcpu=cortex-m0plus-mthumb Figure4Manycompilertoolscanproduce"disassembled"viewsofyour program.Thisshowsyouthechip-specificcommandsthatitimplementsasa resultofthecompilingprocess.CompilerExplorerisaveryusefultoolto examinethedifferenceincompilersfordifferentarchitectures. EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. Background2 Simple Inline Assembler Occasionally you’ll need to request a special low-level command that has no equivalent in standard C. The “no operation” or “enable interrupts” Assembler commands (“mnemonics”) are commonly seen in embedded systems code. In all of the architectures described in this book, their native Assemblers all refer to “no operation” as “NOP” and an example would look something like this: void main(void) { int my_variable = 1; asm("nop"); // a special function call in C to request // that compiler insert the assembler mnemonic // NOP within the main function } If you wanted to have the same mnemonic called four times you could write asm( "nop\n\t" // 1st NOP "nop\n\t" // 2nd NOP "nop\n\t" // 3rd NOP "nop\n\t"); // 4th NOP Where the \n and \t mean “new line” and “tab” and are inserted by the C compiler into the Assembler program it generates prior to sending it off to the Assembler program to create executable code. Warning When mixing Assembler and C solutions it is important to keep in mind that you cannot make assumptions about the register usage, the stack, etc. as you transition from one to the other. The XC8 guides has a particular warning that is worthwhile keeping in mind: “When using in-line assembler code, it is extremely important that you do not interact with compiler-generated code. The code generator cannot scan the assembler code for register usage; so, it will remain unaware if registers are clobbered or used by the assembly code.” (http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf) Boxing in any elaborate assembler routines within a callable function is often a good idea. You can see an example of that in this video: https://youtu.be/8_VLufBGIIs. 2MaterialinthislabcomesfromanupcomingbookbyJamesAndrewSmithandLorenWyard-Scott. Figure5Addingasingleinlineassembler instructionintoafunctionotherwisecompletely writteninC.Theoriginalversionisontheleft andthedecompiledversionisontheright. CompilerExplorer(godbolt.org)wasusedto illustratethisprocess. EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. Slightly More Complex Assembler & C You will sometimes need to write entire functions in Assembler or you may wish to write functions in Assembler but leave the epilogue and prologue to the C compiler. While we can input inline assembler using mnemonics that don’t require additional information like literals or register names: asm ("nop"); // no operation __asm ("nop"); // no operation // three "no operations", all declared volatile to // force compiler to keep. asm volatile ("nop \n" // first no operation "nop \n" // second no operation "nop "); // third no operation asm(“sei”); // enable interrupts asm(“cli”); // clear interrupts __asm(“sei); // enable interrupts Most assembler calls do, in fact, require you to name affected registers or literal values that are used. For instance, you can represent an “immediate3 load value 18 into register 25” as asm(“ldi r25, 18 \n”); // load value 18 into Register R25 Insert this into an MPLAB X C program or try Compiler Explorer, using the “Arduino Uno” setting (C or C++ compiler) and you’ll see the Assembler operation appear in the disassembled listings. Now, try a slightly more complex program that stores that value, 18, into SRAM, at a location defined by a global variable (source: https://tinyurl.com/y6t6psf3 or https://bit.ly/31e8c6N) #include #include // global variable. Will be located in SRAM, // e.g. address 0x100. (needs to be global) char volatile my_var = 0; int main(void) { asm("nop \n"); // no operation asm("ldi r26, 42 \n"); // load 42 into register R26 asm("sts (my_var), r26 \n"); // store the contents of R26 // @ the location of my_var return (EXIT_SUCCESS); } Here, we used the parentheses (which I highlighted in blue) to denote a memory location. Which memory location? The one where the global variable my_var’s value is stored. This is a “pointer”, which a term commonly discussed in C or C++ programming. 3“Immediate”operationsmeanonesthatuseconstants. EECS2021–LabE:Assembler&ContheATMEGAStudentVERSION F2021 CopyrightJamesAndrewSmith;Youdonothavepermissiontodistributeoutsideof YorkUniversity. Even More Complex Assembler & C You can also call more elaborate, extended assembler instructions inline in C. Due to the limitations in C the syntax is a little unwieldy, but is workable and maintainable. In the complier documentation and in online sources you’ll find templates that look like this: asm("template" [ : [ "constraint"(output-operand) [ , ... ] ] [ : [ "constraint"(input-operand) [ , ... ] ] [ "clobber"
Answered 1 days AfterOct 18, 2022

Answer To: https://eclass.yorku.ca/pluginfile.php/4300748/mod_resource/content/1/LabE_EECS2021_MixC_ASM_AVR_Stu...

Jahir Abbas answered on Oct 20 2022
51 Votes
Part 1 Toggle LEDs in C and Assembler.
Disassembly
004E B195 IN R25, 0x05
004F E280 LDI
R24, 0x20
0050 2789 EOR R24, R25
0051 B985 OUT 0x05, R24
Modified program
/*
* File: main_labE_v1.c
* Author: jahir
*
* Created on 20 October, 2022, 6:24 PM
*/
#include...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here