lect21_notes

lect21_notes - Foundations of Embedded Systems A Term Fall...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Foundations of Embedded Systems A Term Fall 2008 Lecture #21: Functions and the Stack Reading for Today: MSP430 User's Guide Ch 3 Reading for Next Class: MSP430 User's Guide Ch 7 HW #5 (on web): Due Thursday 10/9/2008 in class HW #6 (on web): Due Monday 10/13/2008 in class Lab #3 (on web): Report due NOW! Lab #4 (on web): Due Thursday 10/16/2008 Last Class: Assembler directives and the format of assembly files --> directives tell IAR assembler program how to organize code & memory >> Using CMP (and TST) with conditional jumps (JNZ, JC, JEQ, etc) and JMP to create decision structures and loops —> see lect20.s43 for examples .M—I'V >>>>>>>> Remember: All JUMPS are ONEWAYtrips! <<<<<<<<< Jumps also have limited range (511 words backwards or 512 words forwards)much smaller than size of code memory 746%le Injlcoc/‘iiomg oJe Z) ‘l or (a bug 65> (0'43 9 Lou/x onlnj Jam) M125— Cns'lrucn‘loms 1 On oil/erado Q COClé Memoris Foundations of Embedded Systems C Term Spring 2008 Lecture #12: Procedures and the Stack Reading for Today: MSP430 User's Guide Ch 7 Reading for Next Class: MSP430 User‘s Guide Ch 7 HW #3 (on web): Due Next Thursday 2/7/2008 in class Lab #2 (on web): Report due 2/12/2008 Last Class: Assembler directives and the format of assembly files —-> directives tell IAR assembler program how to organize code & memory >> Using CMP (and TST) with conditional jumps (JNZ, JC, JEQ, etc) and IMF >> Decomposition of a programming problem to an assembly program > > > > > > > > Remember: A11 JUMPS are ONE WAY trips! < < < < < < < < < Jumps also have limited range, much smaller than size of code memory Can‘ul' return, Cum On lug Make «mafike/ “SUM? > > Procedures in MSP430 Assembly -- Like functions in C (often called functions or subroutines) -- Intended to perform specific, small function —— Meant to be reusable => Procedures are Aeslcaneci ‘10 {30 Came, bate/h! Syntax for a defining a function or procedure myProc ; Beginning of procedure should have label ( ‘Abe 5 OJ C “3 SOC ‘ 6‘46 A M WN’R acLércss ‘_ 3 ; code goes here RET ; procedure MUST have a return statement v—__——-. ——.. I“ ECE 2801 — Foundations of Embedded Systems Bonus Quiz #3 Name: ECE Box #2 1) The Program Counter (PC) is stored in R0. The PC always contains what? (a) The address of the beginning of program memory (b) The address of the instruction currently being executed (c) The address of the next instruction to be fetched and executed (d) The number of instructions executed so far 2) What is stored in R11? CLR R11 MOV #myVal, R12 ADD.B @R12, R11 a) The low byte of the address of myVal c) OxOOOO b) The byte stored at address labeled myVal d) No way to determine 3) What does this assembler code segment do? MOV #5, R5 MOV #10h, R6 CLR R7 top ADD R6, R7 DEC R5 CMP #O,R5 JNZ top 4) What is the final value of R7 in the code the above? a) OxOOOO b) 10h c) 50h d) Can not be determined What exactly is the Stack and how does it work? > > The Stack is temporary storage used in the course of program execution > > The Stack is a Last In First Out (LIFO) data struction > > R1 = Stack Pointer holds the address of the current Top of the Stack > > Words (16-bit words only) are placed on the stack using PUSH src > > Words are recalled from the stack using POP dest 0? R7 MOV #123411, R7 5“? a (sitcom suck 4W9 PUSH R7 - 0% Pp». J SP:sP—Z oQFEh P 0 1' Memory location 0x210 is labeled FOO \ ?0 Kl 39:532. Oc‘Fa‘ PUSH “00 5 'Pusk oauok POP R10 POP R7 SfC (Qush5rc;) S?:S'P—-t7_ %eV\ Slofc (1 lot/fies» OH’ address 0? 3;:4 ~77 Lou.) Bale ~H\~€'\ “(ilk L5 9 TDD? dear =9 0995+ :wonl/ Sloreé “47 “9 QAA<Q$$ Ln 5? men 3?: SP‘l‘ 2. Procedures can be called from a “main” loop or from other procedures main MOV A, R8 MOV b , R9 :2) CALL #bigger ; returns larger value in R9 smaller ; value in R8 MOV R9, big_Val; eye cutton con-41mm”, uJ'wHA +h1$ Ln$+luc4im After procedure call execution continues with next sequential instruction Procedure CALLS and RET auto-magically elect Stack and PC 6; e IE (7+ 2.\ . 5 Ll 3 CALL dest ; PUSH PC onto stack then sets PC = dest 4/ MOV R4, R6 > > At the end of a procedure RET pops the PC from the top of the stack Example: How does a procedure (or function) call affect the stack "W" Mov *0A00h.5p 3 in‘u‘l'xcd‘IEG 5? 40 flop 0‘: Ram MOV #3234 , R8 MOV #7650, R9 CALL #bigger 6”"“7 MOV R9, bigVal CALL 'PosHes ea. M40 SMKQ“ bigger CMP R9,R8 JN done PUSH R7 MOV R9, R7 MOV R8, R9 )SwaP valves ‘Ln PSYOWWS RC‘ MOV R7, R8 POP R7 done RET 3 30u\9 *0 éone quefl {2%(Zqu “Wmv w» “W "Mn-m» « WWW we. “mum...”- ,/Wa em em \3 e V‘- VUSH => §P= SP'Z ‘H‘en whit wori *0 \rhe skeet: ’\ sud? (@ *3 Klarmwen s P:SP+ z o 2 (:0 h mm. Procedure Etiquette: We r0 ‘9 3 >> One point of entry, one exit (RET statement!) >> Clean up after yourself 6 00 A >> Call other procedures well pr 0 CQAUV65> >> No Data Left Behind (i.e. match PUSHes and POPS) >> Always know were your data is Obeflfl Exiting: bad_exit ; do something ; error check jeq main_done ; On error jump back to main ; program -— DO NOT DO THIS 1:ngo more stuff ;) Lekvfs values (pg e{_c)o,‘ +ke Eafixék. better_exit: ; do something ; error check ; save error info jeq err ; On error exit procedure ; (error info saved) ; otherwise continue err: ret Clean—up After Yourself: >> A procedure should only modify registers and memory locations that it is ALLOWED to modify -—> All others must be restored before resumption of main program &: Procedure clean] receives operands in R12 and R14 and returns result in R12. The operations it performs also use R6, R7 and can affect the SR. Pugh Rt” 3 preseer R‘o \ 1 350me Co Ae USMS R!” + 2 Po? hx «ever&¢ O’AeV’ \S l ‘ because Sikflk 1 Call Other Procedures Well: ‘ >>Oneentry/oneexit. EGAN-l- SOMP 00+ O'F PFOC’edUi/es. _._———’-—' >> Work down then back up same code hierarchy CA—LL— PROL’L >C0\l\ \b‘fDCBL LRLL ?rOf'—Q r? Proceéwe 5 MIN“ JET J velar“ *0 main {-0 chi icon chm/l < Her Callecl, ~Hn€m “No Data Left Behind”... >> MUST match push with pop and call with ret (or else what?) right? " ~15 ms + an sled/k) C W “we Cer 40 (2.2T +0 bogus A‘id'fess 7904“ => ‘PusHes n Mk M Is Jrhe address <37”: Hex-1L instrU-CHO“ 3>In ProcecLuxffi,2 Wkldes Okra fibuskeé bu+ OMLA Q. value '13 Pop 0%; ==> 5616 ?C I {LOSK z. (AD/On“ deesj Look for data in all the right places: >> Procedure's data passing method must be FIXED and WELL DOCUMENTEDH >> Data passed in registers => Ed‘ks’é‘g b0+ «42/18.de number 0‘ masters _ (dues for S) prababrfi \AW {‘0 PfeSéfl/Q l/ mam >> Data passed in memory 2/4130 iksg Procedures need 4‘0 \Dbcds) t/eth/d VariaJUIé. hameS ( :> #CCessina aparautcls f/OM mama/“J libkefi [Dufjek kirhm [Adm Re%}g‘/€V\§ 53> (CLA 584’ UP 1C0." Lndktré’C‘L Mac/g 6%, . , . »‘./ Passing déreg (MCAQAOK L/l d/flfits er >> Or, data passed on stack 5) m05—F “Coo/\QllCoqlécl/l \Du‘l‘ ‘3 C (£095 In Whyeé )cuwfiucxfié CW v’mcLLm «J o... Slack Mug ; clack-It 15cm stack P05“ Rm PUSH R§ PUSK mtfivaj mov 513) kg I (all 3-Hch are; ADD lat-l, a? QGel—‘loddrx , 0 ~ 4 P06) mks val :00: @3ij Kb POP (MO 'QRQ K‘l “POP RS‘ RET w < RET < ’90P 62’;— RG 2 <4? K§+=mchJ ( RS- : {Lg‘rl ecwse of au‘ldlndemevf") R7 : @ RS :— RM) 5 => vass‘mfi Aa‘Lm on ‘H’xe S‘quck rafuves ; at'lévass mampu ‘ action 9}? ‘9 Don'+ Mess w‘l‘l—k S? Airec‘H‘ (50‘) ca“ “loose” \er +0p a": ‘H’xe => CC-Jpg3 S? LA+0 6L workina fies/derflme R5} +0 CaJCu‘AS‘QA aggressa a“: Sofia 0n er 5‘40ch 5y EISJ'. We “eel ‘40 Aeciie how 40 {34.55 Jada to our“ ProceAUreI . ‘ m. n ..-D£‘~A~mmmmmtw . We WW L M. «-WwwWJ-lm “""“""“"“ "m" WW 1k:Wfimapmaflmemmfimfimemmmmmofimmm fibtwlm$1wdmm. =9votss; Ln address 041 drama A in R5 Ami n-Pfs 1" [2"] org 0200h ; staring at address 0200h n_pts DEFINE 8 7 COHS‘I’LA“ VN-pé5 : g ‘F 4 $-A A_ D58 ILPtS ; aJ\oca+£ SPACfi fior AV\<KV““%O 3 b3 { ’ minA D38 5 anock+e Spa¢~€ {of \ b§fi4€ canfli MIA A main mov #OXOAOO, SP ; initialize top of stack CALL #fill_A ; fills A array with data (In mov thmm‘s, RN 3 RH = 8 Movaflflzug \ ‘25-; Aggress 04}, AC0} CALL #getmin J back br main ; go back to the beginning of main getmin ; write procedure here flue Mn use Rio So memo/«H OAS‘L‘LCQ PUSH Ru MDV¢S : R0: '51" \n A I me“: Dec RH L A€Cremen+ \oop roan-l TgT Rq 1' be ‘2‘1:o we are (Ix/\Rsfieé 3EQ Ame ‘ ‘ ' We as goweficunel incremen-I- +0 nexé AAAVCS‘S CW? @129, [up 3!. nexé ‘JIUMP when AU] >: Re, (KL (5 5+“, "WWW; NOV @RS) I) 0%WVLQ‘\$Q Rb; : he“) min}mum 3m? th+ dame WNDV Rb,eq J Re+urw yn1A1mUVA 1A Qq $DCJP R.b REiT ’Fhemzarecnherapproachesuu ...
View Full Document

This note was uploaded on 10/27/2008 for the course ECE 2801 taught by Professor Jarvis during the Fall '08 term at WPI.

Page1 / 10

lect21_notes - Foundations of Embedded Systems A Term Fall...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online