This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 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 38 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 38 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 38 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 38 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 38 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 38 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 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SubroutinesWhy Subroutines?C Subroutines (aka. Function)Implementing Subrotuine call/return with gotoThe StackData Storage via the StackThe PIC18 Stackcall Instructionrcall, return, retlw InstructionsCall/Return ExampleStack Overflow/UnderflowSTKPTR RegisterAccessing the Stack via a Programvlshift Function in Assembly LanguageSubroutine call to vlshiftBack to Parameter PassingArrays and Pointers in CArrays and Pointers in C (example)Arrays and Pointers in C (example cont.)char* Pointer Example with Mem addressesPointer arithmeticPointer Example with int dataIndirect Addressing and Pointer registersPIC18 Indirect AddressingUsing FSRn/INDFn to implement C pointersPostInc/PostDec/PreInc/PluswPOSTINCn usagePLUSWn Usage with char ArraysPLUSWn Usage with int ArraysA C Function with PointersCalling lcase() from main()Program Memory vs. Data MemoryTables in Program MemoryAccessing Program Memorytblrd, tblwr instructionsBack to lcase.asms1_init: Table Read ExampleWhat do you have to know?V 1.0 1Subroutines•A subroutine is a block of code that is called from different places from within a main program or other subroutines.– Saves code space in that the subroutine code does not have to berepeated in the program areas that need it; only the code for the subroutine call is repeated.• A subroutine can have zero or more parameters that control its operation• A subroutine may need to use local variables for computation.• A subroutine may pass a return value back to the caller. • Space in data memory must be reserved for parameters, local variables, and the return value.V 1.0 2Why Subroutines?Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 1.0 3C Subroutines (aka. Function)The statement i << j is valid in C, this is only used for example purposes.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 1.0 4Implementing Subrotuine call/return with gotoNeed specialized instructions to implement subroutine call/return that have more capability than goto as we need to remember where to return to after a subroutine call.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 1.0 5The Stack•In µPs, the stack is a memory area intended for storing temporary values.• Data in the stack is usually accessed by a special register called a stack pointer.• In the PIC18Fxx2, the stack is used to store the return address of a subroutine call.– The return address is the place in the calling program that is returned to on subroutine exit.– On the PIC18Fxx2, the return address is PC+4, if PC is the location of the call instruction (PC is the location of the call instruction). The return address is PC+2 if it is a rcall instruction.V 1.0 6Data Storage via the StackThe word ‘stack’ is used because storage/retrieval of words in the stack memory area is the same as accessing items from a stack of items.Visualize a stack of boxes. To build a stack, you place box A, then box B, then box C.A ABABCNotice that you only have access to the last item placed on the stack (the Top of Stack – TOS). You retrieve the boxes from the stack in reverse order (C then B then A). A stack is also called a LIFO (last-in-first-out) buffer.V 1.0 7The PIC18 StackThe PIC18 stack has limited capability compared to other µPs. It resides within its on memory, and is limited to 31 locations.21 bitsFor a call, address of next instruction (nPC) is pushed onto the stack A push means to increment STKPTR, then store nPC at location [STKPTR].STKPTR++; [STKPTR] ← nPCA return instruction pops the PC off the stack. A pop means read [STKPTR] and store to the PC, then decrement STKPTR (PC ←[STKPTR], STKPTR-- )reset value, not writeable0:STKPTR3: 0x????2: 0x????1: 0x????31 writeable locations30: 0x????29: 0x????31: 0x????V 1.0 8call InstructionB B B B B B B B B B B B B B B B1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 05 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0call k 1 1 1 0 1 1 0 s k k k k k k k k01 1 1 1 k19k k k k k k k k k k kcall (call subroutine at location k) : Push PC of next instruction (nPC = PC+4) onto stack.If s = 1, push W, STATUS, BSR registers into shadow registers(aka, the fast register stack). By default (s = 0).Then do PC[20:1] ← k A call saves the return address on the stack so that it knows where to return. The shadow registers are really only useful for interrupts; will discuss them when interrupts are covered.V 1.0 9rcall, return, retlw InstructionsB B B B B B B B B B B B B B B B1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 05 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0return (ret from subroutine): PC ← pop top-of-stackif s = 1, restore W, STAUS, BSR from shadow registersretlw (return with literal in w): w ← k, PC ← pop top-of-stackreturnrcall k1 1 0 1 1 n n n n n n n n n n n0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 sretlw k0 0 0 0 1 1 0 0 k k k k k k k krcall (relative call) – pushes PC+2 (nPC) on stack, then does a branch always to subroutine, branch offset is 11 bits (-1024 to +1023). Advantage over call is that it only takes one word.V 1.0 10Call/Return Examplemain....0x0040 call Sub_A....Sub_A....0x006A call Sub_B....returnSub_B....0x13C rcall Sub_CreturnSub_C....return 123456123456For call, nPC = PC + 4.For rcall, nPC = PC +20:STKPTRSTKPTR STKPTRafter step 61: 0x00442: 0x006E3: 0x013E4: 0x????after step 32: 0x????1: 0x????0: 0x????V 1.0 11Stack Overflow/Underflow• Stack overflows on the 32ndcall instruction without a return– By default, the processor self-resets itself on stack overflow. After reset, can check a status bit (STKFUL) to determine if stack overflow caused reset.– Can configure the processor to simply freeze the stack, and set an error bit when the stack overflows.• Stack underflow occurs if attempt to do a returnwhile STKPTR = 0.– In this case, PC gets set to 0x0 (which is the reset vector), and an error bit is set (STKUNF) – so reset code can determine if stack underflow occurred.V 1.0 12STKPTR RegisterLower 5-bits used to access stack locationsUpper two bits are the stack overflow and underflow status bits.V 1.0 13Accessing the Stack via a Program• The value on the top of the stack can be accessed by three registers: TOSU (upper, bits 20-16), TOSH (high, bits 15-8), TOSL (lower, bits 7-0).• PUSH and POP instructions can be used to push the current PC onto the stack. • This method of accessing the stack values is extremely primitive, and very difficult to use effectively. – We will ignore this


View Full Document

MSU ECE 3724 - Subroutines

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download Subroutines
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 Subroutines 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 Subroutines 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?