Lecture_22 - ECE 190 Lecture 22 Recursion Lecture Topics Recursion Simple examples factorial& Fibonacci numbers Recursion vs iteration Binary

Info iconThis preview shows pages 1–11. 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
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE 190 Lecture 22 Recursion Lecture Topics Recursion Simple examples: factorial & Fibonacci numbers Recursion vs. iteration Binary search using recursion C function to LC—3 assembly conversion example Lecture materials Textbook Ch 17 Homework Machine problem MP4.2 due 4/14 Announcements April 7, 2011 V. Kindratenko , , {pp/ TOE/7%)} ADM: ~> Recursi‘mt I; ~> Qle £544" ‘0 ML "4 W [*3 0M Maul Mug/.2 4 mm W (\Wwfiw, W Prurglem [a weaem/ ioa 9W ~£6h9K [QQuEszg ch98] (147)6{01’42 wwmaAag game 901” WWW SJ—ops <H<2 muavfi 3e ime +0 QWWQ Mk IEWHMM. 03000139. W anal/44374 flaw/Z WWW MQQ Wow $9 mes Moé _W%+&sl:ttt°3 Jegfl/W fit»: “Lew/n! 69¢ flw/f/ W1 Qaz (MWMVLHW Way/MK; 0M 03 - vangqu 0+ W 9’3 Ska/Wen ~W flask LL52: 19¢ ermz'r; Mgr? MOM New gob/49 , ,.;>EX-amég.e 1: Fashion:sz a h! : M . (IA-l) * “3° 2 a/ 40V m>0 $Je'fih/Lffl‘at’ 0“}! 25(2me 1 Hf: (1'5=2'/ "—2 Ll” 3/ 31 \ . “P éL Wewgaw Olar‘m 0% (18911344?ch 1 (U, 14 z _ 0 {la-(Mel)! 1‘4 ‘4 >0 0; 4-6?ch 0,0” <i‘723n591'4 OJ ml: 4 at”, -&-Wm-1W[ I? mks VWWV€ elegfg‘m‘jflm W& W WMJMI‘gJibg’, ’> M “(*(ILSK t‘g clef/zth i'u let/m8 wax/Kluch Ynéééemf 6W J—o Solve. ~> «WK {mg a W ~ Pew/I“ SW2 I Caz / [4349/ age cage [(9 {5‘3 Mama/L, Whalj r2 390(4wa 0 (,1 lazy/Oz, (914 (Q 3mm VMQI‘E’M OdL (PM hi: 14:01—01 amd (144)! ("g g Kym/MM“ 46VWGIMP/K W3 @[zj I" 010 mar/we aim wge W i Wain”? vLi‘wg; ('11 WW VSan 11.! / \ ‘4 X ’33 / ‘\ 5 >( 2‘, / \ a x 1,1, /\ I x 0!, l 1% "99ie§>& I‘M Sol/gslfléii‘wg Vefuzg is emfluaé L/j if! T 1 W \ Se v" M X ‘5’. ~30", w/Y\‘->L /\ /\ 5 X2! g >6 ‘1! {X}! Z/x \1 /\ 1 >< 1 )4! LM q >6/3_\ t4/>€\€ b x & . . Q1 5th {didasz_ [M —> We Knew“ . _ _ , . . AfiflfiJ’OT/M _== 1 'é' 80ml!ng dye/“J We! _ _ jigginer {14) : mx #MQWW [Ly-I) 4’ #219104 -> . s ¥ Ml ,pzum'wg [IQ/ti MW) -{ L4 M-ng/W 2;: 0) 62,92 Vela/7'14; : y Leaviomw [MW/Mm, fiwuw 0% m new, 70mg 9M5? madam/Sm 0L0)» LQNfi/(LHM & (L41 #0109, 1% V V€WS¥V€ £61m "1/914 1'; J‘uSJ U119 52:43 0M jimcoll’m, _ e Z a/ a» 9 _ OJ Maw/£62”; Eli/'14 WA/r/lg €QF%‘ Wax/Megan [3 _9qu7 DJ 7%«6 foV’w/Z9u3_ W0 Ma W5 3 ‘92 1; i, 7/ 3 S, ‘V/ )3 72/, 39/??/_ 0'93, “’4, ‘ / 2 *7 PM ~ Pia-1+ F14..2_ .l_ C/ 7/; Q ,fi F0 = 0 Vecmym 3 M4 #774 F1 : 1 OJ 1819€Z+> L‘Mt/pgwfiog. 7214/0 '“gwflér” wmm 00! W 'PW/ffiem, FMKI Wo/ Fh,Z‘ : ‘ V “7 The £523 [3 Lu; g Mom, now/r9404? jam/MM MEI ,’ r0 db“ d F; r; 1 **> Fi/gpmaet‘ muW§ LWmZor [fink 8@(? a We Wow 15%; F0 2 (9 am! 2‘3?! Mi F1”: Fla—I Fri/1-2 W liéhzzfi). Tilt/May, elge [1/ (hr: releI/z £296 yV-Z‘lWl/l, ngflhflda/ FII’gm’Waa/ L 3 ,,,,..,;,iecuraifl0’m 1/} Illém {I’m ,,ééfifiew'kgmjf7Mflw j , MW 419%: V . M WWW (m M) M flaw/(m) { pl grit/(1&20) it M; (£3); ) W “"l‘ 121' 54:14 z++ ‘ WWW: mélacim'aflh'l); 40):} :5». a4}; / B Pei-(Arm élse z‘qLQA/zu) Jyor(6:lez‘¢=hlc++) WE‘Lf/LMV'i‘ {44:2,}041“) e196 VBLUVV' Fl‘éomwm‘ [M'I)+ PM we MI [14 4)) S “'7Fw 90M Fraglemé) WWW, M rev/W996 Very/974 1‘s 9%va WJ More elegawk -7 $054ng 09L whit»; Pry/(W we mafia/“Mg ego/91%; 964 PM 62 w) Ree/army“ 001/ng 521» 0; 09392 ' OLW” hum; ,LW galacK 61ch Jumaiflim w” DVMW due (7Z0 W L0 0775 Olfiwlfl W40 $Hfllz. VeWy’t/e mam an «+7. .gjvw... V Mo; g aflJ é (2523 é ... arm], QLW‘L) 6’ QMLWM FL; 2‘02 Jay («’01 W lat/mg (a, my #1 VJ 25 does 1/2/04 €X/13clliI/r (91. '9 6/me ("S (97 WW3 ma waea 04L CQOCWW 35‘} «Mg K. ‘ .7 91,68) } ‘_ “engine/Ly Jim W14 fifl/flwv/ (OF/’48:) /- (Q j ’7 g equ/Zg 40 J44? Value 62% m/‘t/W/‘ww/j rev/am l‘wiax <94 M lef W794 1/42/2006 if fl ’7 5:; ’5 1‘3 [as aLQaM/z M l/LQW 6h! W’ W”’” / (9150) 10 92.1293: idle aw r . l 1 11 ?;Z £1 ‘18 Log 1 Sip/fl“. 1117134109) MAM, PQPJOVVM WW (3Lng dmmk an) L7 mu alt/wt $0 on. @ [Mm/m J>> 9w.» W Eer-V'b g7 £1144. will $Ml/ emu/)P IL 891i m‘JJZe : [fwd-f /2> . [avid Cfiiml) fag. (We gugmflmg/g he}; [zoo YQJCWI/t — I; I‘ V. elsé 1%? (aLmI‘JJlej:: 6) 1* W? 9900” ’ */ Vellum Mariel le)‘ ezgfi [7L 4 97 CWLJJQJ) he 3&1er 1‘14 W 0bng "7 Wwwmwé’; a, :an, W164); alga gmwbgwp/g (g, 91/ mz‘Jolle—H) @140”; L‘mi W‘m ( ) 03 ML (,QV‘Y‘lM/b c n: :4 mez, 91,98, {03/(10) :55, $54, H in! him : 10%; iML ilde : ‘fDaI‘ngwi/W wrmfl/ 0/ [082‘ 3 —~> gymfla (pm/M gwk ) W, 4:91! [email protected] ~> Vunm’mg 9w W2 \> VIEW/erg oQL (Home; ~> [Lie/cw» 447 145617 cow/Milk»; ECE190 LechweZZ Apfil7,2011 C function to LC—3 assembly conversion example Factorial function: int factorial(int n) { int fn; if (n > 1) fn = n * factorial(n—l); else fn = 1; return fn; } It’s activation record £03 assembiy FACTORIAL: ; push callee’s bookkeeping info onto the run—time stack ; allocate space in the run—time stack for return value ADD R6, R6, #—1 ; store caller’s return address and frame pointer ADD R6, R6, #—1 STR R7, R6, #0 ADD R6, R6, #—1 STR R5, R6, #0 ; allocate memory for local variable fn ADD R6, R6, #—1 ADD R5, R6, 0 ; if (n>l) LDR R1, R5, 4 ADD R2, R1, #-1 Ban ELSE ; compute fn = n * factorial(n—l) ; caller—built stack for factorial(n—l) function call ; push n—l onto run—time stack ADD R6, R6, #-1 STR R2, R6, #0 ; call factorial subroutine JSR FACTORIAL ; pop return value from run—time stack (to RO) LDR R0, R6, #0 V ADD R6, R6, #1 ; pop function argument from the run—time stack ADD R6, R6, #1 2 V. Kindratenko ECE190 ; multiply n by the return value LDR R1, R5, MUL R0, R0, ; store result in memory for fn #0 STR R0, R5, ; done with this branch Banp RETURN ELSE: ; store value of l in memory for fn #0 AND R2, R2, ADD R2, R2, STR R2, R5, ; tear down the run—time stack and return RETURN: ; write return value to the return entry LDR R0, R5, #0 STR R0, R5, #3 ; pop local variable(s) ADD R6, R6, #1 ; restore caller’s frame pointer and return address LDR R5, R6, #0 ADD R6, R6, #1 LDR R7, R6, #0 ADD R6, R6, #1 ; return control to the caller function RET Lecture 22 (already in R0) R1 R0 <— n * factorial(n—1) #0 from the run—time stack Apfi|7,2011 V. Kindratenko ...
View Full Document

This note was uploaded on 01/22/2012 for the course ECE 191 taught by Professor Staff during the Spring '11 term at University of Illinois, Urbana Champaign.

Page1 / 11

Lecture_22 - ECE 190 Lecture 22 Recursion Lecture Topics Recursion Simple examples factorial& Fibonacci numbers Recursion vs iteration Binary

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

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