DOC PREVIEW
UNCC ECGR 4101 - C Programming Language Review and Dissection I Lecture 3

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C Programming Language Review and Dissection ITodayC: A High-Level LanguageA C Code “Project”Compiling a C ProgramCompilerUseful TipRemember the Memory Map for Our MCUClassifying DataSection Names and ContentsExample of SectionsSection Sizes and LocationsAllocating Space for VariablesActivation Record / Stack FrameStorage of Local and Global VariablesInitializationAssignmentControl StructuresIf-elseGenerating Code for If-ElseSwitchGenerating Code for SwitchWhileGenerating Code for WhileForGenerating Code for ForASCII TableMaskingExample - upper/lower case ASCII3-1Embedded SystemsC Programming Language Review and Dissection ILecture 3Embedded Systems 3-2TodayHigh-level review of C conceptscoupled with . . . In-depth examination of how they are implemented in assembly languageReading Assignment –MCPM Chapter 1 and Chapter 2 (Section 2.1) Memory Mapping–Review P&P Chapters 14, 15, 16Embedded Systems 3-3C: A High-Level LanguageGives symbolic names to values–don’t need to know which register or memory locationProvides abstraction of underlying hardware–operations do not depend on instruction set–example: can write “a = b * c”, even ifCPU doesn’t have a multiply instructionProvides expressiveness–use meaningful symbols that convey meaning–simple expressions for common control patterns (if-then-else)Enhances code readabilitySafeguards against bugs–can enforce rules or conditions at compile-time or run-timeEmbedded Systems 3-4A C Code “Project”•You will use an “Integrated Development Environment” (IDE) to develop, compile, load, and debug your code.•Your entire code package is called a project. Often you create several files to spilt the functionality:–Several C files–Several include (.h) files–Maybe some assembly language (.a30) files–Maybe some assembly language include (.inc) files•A lab, like “Lab7”, will be your project. You may have three .c, three .h, one .a30, and one .inc files.•More will be discussed in a later set of notes.Embedded Systems 3-5Compiling a C ProgramEntire mechanism is usually called the “compiler”Preprocessor–macro substitution–conditional compilation–“source-level” transformations•output is still CCompiler–generates object file•machine instructionsLinker–combine object files(including libraries)into executable imageCSource andHeader FilesC PreprocessorCompilerSource CodeAnalysisTarget CodeSynthesisSymbol TableLinkerExecutableImageLibraryObject FilesEmbedded Systems 3-6CompilerSource Code Analysis–“front end”–parses programs to identify its pieces•variables, expressions, statements, functions, etc.–depends on language (not on target machine)Code Generation–“back end”–generates machine code from analyzed source–may optimize machine code to make it run more efficiently–very dependent on target machineSymbol Table–map between symbolic names and items–like assembler, but more kinds of informationEmbedded Systems 3-7Useful TipConfigure Project Editor to tell compiler to generate assembly code for examination with debug information–Option Browser -> select CFLAGS, select Mod…, select Category et cetera -> check –dsourceAlso, do not use spaces in file names or directories.Embedded Systems 3-8Remember the Memory Map for Our MCUEmbedded Systems 3-9Classifying DataVariables–Automatic – declared within a function•Only exist while the function executes•Are re-initialized (re-created, in fact) each time the function is called–Static – declared outside of all functions, always exist•Can make an automatic variable retain its value between invocations by using the “static” keywordEmbedded Systems 3-10Section Names and Contents“Block startedby symbol”Embedded Systems 3-11Example of SectionsjEmbedded Systems 3-12Section Sizes and Locations Map Viewer - Shows memory map with sections and symbolsBuilder gives summaryEmbedded Systems 3-13Allocating Space for VariablesStatic data section–All static variables stored here(including global variables)–There is a fixed (absolute) addressRun-time stack–Used for automatic variables–SP and FB point to storage area (frame, activation record) at top of stack–New storage area for each block(goes away when block exited)Examples–Global: sub.w _inGlobal,R1–Local: mov.w -2[FB],R0•Offset = distance from beginningof storage areainstructions0xFFFF0x0000PCstatic datastackSPFBEmbedded Systems 3-14Activation Record / Stack FrameRead Patt & Patel Chapter 14 for a thorough explanation of conceptsSee Section 2.4 of MCPM for more implementation detailsSee Section 1.2.2 of MCPM for size of variablesOld Frame pointer also called dynamic link0xFFFFF0x00000FBSPArgumentsof funcAutomaticVariablesof funcOld FramePointerCaller’s Stack FrameReturn AdxSpace to Save Regs.More Auto. VariablesMoreArgumentsStack Grows to Smaller AddressesEmbedded Systems 3-15Storage of Local and Global Variablesint inGlobal;void chapter12() { int inLocal; int outLocalA; int outLocalB; /* initialize */ inLocal = 5; inGlobal = 3; /* perform calculations */ outLocalA = inLocal++ & ~inGlobal; outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);}Embedded Systems 3-16Initialization;## # FUNCTION chapter12;## # FRAME AUTO (outLocalB) size 2, offset -6;## # FRAME AUTO (outLocalA) size 2, offset -4;## # FRAME AUTO ( inLocal) size 2, offset -2;## # ARG Size(0) Auto Size(6)Context Size(5);## # C_SRC : inLocal = 5;mov.w #0005H,-2[FB] ; inLocal ._line 19;## # C_SRC : inGlobal = 3;mov.w #0003H,_inGlobal._line 22Embedded Systems 3-17Assignment;## # C_SRC: outLocalA = inLocal++ & ~inGlobal;mov.w _inGlobal,R0not.w R0mov.w -2[FB],-4[FB] ; inLocal outLocalA and.w R0,-4[FB] ; outLocalA add.w #0001H,-2[FB] ; inLocal ;## # C_SRC: outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);mov.w -2[FB],R0 ; inLocal add.w _inGlobal,R0mov.w -2[FB],R1 ; inLocal sub.w _inGlobal,R1sub.w R1,R0mov.w R0,-6[FB] ; outLocalBEmbedded Systems 3-18Control Structuresif – elsewhile loopfor loopEmbedded Systems 3-19If-elseif (condition) action_if;else action_else;conditionaction_if action_elseT FElse allows choice between two mutually exclusive actions without re-testing condition.Embedded Systems 3-20Generating Code for If-Elseif (x){ y++; z--;}else { y--; z++;}L1:;## # C_SRC : if (x) {cmp.w #0000H,-6[FB] ; x jeq L5;## # C_SRC : y++;add.w #0001H,-4[FB] ; y ;## # C_SRC : z--;sub.w #0001H,-8[FB] ; z ;## # C_SRC : } else {jmp


View Full Document

UNCC ECGR 4101 - C Programming Language Review and Dissection I Lecture 3

Documents in this Course
Load more
Download C Programming Language Review and Dissection I Lecture 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Programming Language Review and Dissection I Lecture 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Programming Language Review and Dissection I Lecture 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?