Lecture17W_F

Lecture17W_F - Stack for Procedure Parameters Stack LIFO...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Stack for Procedure Parameters Stack LIFO data structure in memory Pointer to top of stack—last item placed on stack – System stack pointer: SP – Index (address) registers: X and Y Possible programmer-implemented stack pointers Put data onto stack—push (PSHReg) Address register indirect with predecrementing: -SP Get data off stack—pop or pull (PULReg) Address register indirect with postincrementing: SP+ HCS12 system stack Successive data placed in decreasing memory addresses - Bottom of stack—first element on stack; highest stack memory address - Top of stack—last element on stack; lowest used stack memory address Page 1 of 8 Stack for Procedure Parameters Page 2 of 8 Subroutine Power · Want to raise Base (byte) to Exponent (byte). · To allow for largest range of outcomes, write subroutine to return a long value. · Use EMULS to produce a long product to return. · Since EMULS requires word factors, pass Base as word value in Y. · Since EMULS uses D register, A and B will not be available for a byte parameter, so pass Exponent as word value. Power (Y=Base.W, X=Exponent.W) { Save a copy of Base D ß 1; /* Power computed so far */ repeat { Y ß Base; Y:D ß Y * D; X = X – 1; } until (X = 0) /* Y:D = Base.B ** Exponent.B */ } Stack for Procedure Parameters Page 3 of 8 Subroutine Power: parameters passed by value in registers MyCode SECTION ;Code memory main: lds #__SEG_END_SSTACK ;Init. stack ptr. ; Setup input paramenters for Power: bytes à words ldab Base ;Put base in B tfr b,y ;Sign-extend to word in y clra ldab tfr ; Call Power bsr ;Clear (for upper byte of D) Exponent ;Put exponent in D d,x Power ; Retrieve output paramemters from POWER movw y,ResultH movw d,ResultL ; . . . ; Subroutine Power ; Input: X = Exponent ; Input: Y = Base ; Output: Y:D = Base ** Exponent Power: pshx ;Save X pshy ;Save Y--Temp ldd #1 ;Initialize result to 1 PowerLoop: ldy 0,sp ;Y ß Base emuls ;Multiply result by base dbne x,PowerLoop ;Repeat if exponent > 0 ;Restore stack/registers pulx ;Consume temp Y pulx ;Restore X rts ;Done--return to caller MyData: SECTION ; Variables Base: DS.B 1 Exponent: DS.B 1 Result: ResultH: DS.W 1 ResultL: DS.W 1 END Stack for Procedure Parameters Page 4 of 8 Subroutine Power: parameters passed by reference in registers MyCode SECTION ;Code memory main: lds #__SEG_END_SSTACK ;Init. stack ptr. ; Setup input paramenters for POWER ldy #Base; ;Put base ptr. in Y ldx #Exponent ;Put exponent ptr. in X ldd #Result ;Put result ptr. in D ; Call Power bsr Power ; . . . ; Subroutine Power ; Input: X = Exponent Ptr. ; Input: Y = Base Ptr. ; Output: D = (Base ** Exponent) Ptr. Power: pshx ;Save exponent ptr. pshy ;Save base ptr. pshd ;Save output ptr. ; Copy input parameters to registers: BYTEs à WORDs ldab 0,y ;Put Base in B tfr b,y ;Sign-extend to word in y pshy ;Save copy of sign ext. Base clra ;Clear (for upper byte of D) ldab tfr ; Compute power ldd PowerLoop: ldy emuls dbne pulx ldx 0,x ;Exponent in D d,x ;Exponent in X #1 0,sp x,PowerLoop 0,sp ;Initialize result to 1 ;Y ß Base ;Multiply result by base ;Repeat if exponent > 0 ;Consume temp Base ;Pointer to output Stack for Procedure Parameters Page 5 of 8 ; Copy output parameter from registers sty 2,x+ std 0,x ;Restore stack/registers puld puly pulx rts MyData: SECTION ; Variables Base: DS.B 1 Exponent: DS.B 1 Result: ResultH: DS.W 1 ResultL: DS.W 1 END ;ResultH ß Y ;ResultL ß D ;Restore D ;Restore Y ;Restore X ;Done--return to caller Stack for Procedure Parameters Page 6 of 8 Subroutine Power: parameters passed by value on stack MyCode SECTION ;Code memory main: lds #__SEG_END_SSTACK ;Init. stack ptr. ; Setup input paramenters for Power: bytes à words ldab Base ;Put base in B tfr b,d ;Sign-extend to word in y pshd ;Base on stack clra ;Clear (for upper byte of D) ldab pshd ; Call Power bsr Exponent ;Put exponent in D ;Exponent on stack Power ; Retrieve output paramemters from Power movw 2,SP+,ResultH ;Pop high word of Result movw 2,SP+,ResultL ;Pop low word of Result ; . . . ; Subroutine Power ; Input: 4(SP) = Base ; Input: 2(SP) = Exponent ; Output: 2(SP) = Base ** Exponent Power: ldx 2,sp ; Computer power ldd #1 ;Put exponent in x PowerLoop: ldy 4,sp emuls dbne x,PowerLoop ;Put base in y ;Multiply result by base ;Repeat if exponent > 0 ;Initialize result to 1 ; Copy output parameter from registers sty MyData: Base: Exponent: Result: ResultH: ResultL: 2,sp ;Push result high word std rts 4,sp ;Push result low word ;Done--return to caller SECTION DS.B 1 DS.B 1 DS.W DS.W END 1 1 ; Variables ...
View Full Document

Ask a homework question - tutors are online