The Stack And The Stack Pointer

1y ago
15 Views
2 Downloads
732.07 KB
20 Pages
Last View : 12d ago
Last Download : 3m ago
Upload by : Grant Gall
Transcription

The stack and the stackpointerIf you “google” the word stack, one of the definitions you will get is:A reserved area of memory used to keep track of a program's internal operations, including functions, return addresses, passed parameters, etc. A stack isusually maintained as a "last in, first out" (LIFO(LIFO)) data structure, so that the last item added to the structure is the first item used.Sometimes is useful to have a region of memory for temporary storage,which does not have to be allocated as named variables.When you use subroutines and interrupts it will be essential to have such a storageregion.Such region is called a Stack0x3BFA0x3BFB0x3BFC0x3BFD0x3BFE0x3BFF0x3C00The Stack Pointer (SP) register is used to indicate the location of the last item put ontothe stack.When you PUT something ONTO the stack (PUSH(PUSH onto the stack), the SP is decrementedbefore the item is placed on the stack.Memoryusedby MCU(Debug12Data)When you take something OFF of the stack (PULL(PULL from the stack), the SP is incrementedafter the item is pulled from the stack.Before you can use a stack you have to initialize the SP to point to one value higher thanthe highest memory location in the stack.For the HC12 use a block of memory from about 3B00 to 3BFF for the stack.For this region of memory, initialize the stack pointer to 3C00. 3C00. Use LDS (Load StackPointer) to initialize the stack pointer.The stack pointer is initialized only one time in the program.

The stack is an array of memory dedicated totemporary storageSP points to location last item placed in blockSP decreases when you put an item on the stackSP increases when you pull the item from the stackFor the HC12, use 0x3c00 as initial 0x3BFE0x3BFF0x3C00STACK:A 3C00#STACKBDMemoryusedby MCUEQULDSXYSPPCCCR

An example of some code whichuses the stackStack pointer:Initialize ONCE before the first use (LDS #STACK)Points to last used storage locationDecreases when you put something on stack, and increases when you take something off C00Memoryusedby MCUequ 3C00ldsldaaldxpshapshxclraldx#STACK# 2e# 1254# ffffAXCODE THAT USES A & XpulxpulaSP

An example of some code whichuses the stack

SubroutinesA subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of the program.Example:sqrt:org 1000-Math functions, such as square root (sqrt)::call sqrt::call sqrt::swiBecause a subroutine can be called from different places in a program, you cannot getout of a subroutine with an instruction such as?compute square root::jmp labeljmp labelBecause you would need to jump to different places depending upon which section ofthe code called the subroutine.When you want to call the subroutine your code has to save the address where thesubroutine should return to.to. It does this by saving the return address on the stack.stack.- This is done automatically for you when you get to the subroutine byusing JSR (Jump to Subroutine) or BSR (Branch to Subroutine)instruction. This instruction pushes the address of the instructionfollowing the JSR (BSR) instruction on the stackAfter the subroutine is done executing its code, it needs to return to the address savedon the stack.stack.- This is done automatically when you return from the subroutine byusing RTS (Return from Subroutine) instruction. This instruction pullsthe return address off the stack and loads it into the PC.

SubroutinesCaution: The subroutine will probably need to use some HC12 registers to do its work. However, the calling code may be using its registers form somereason – the calling code may not work correctly if the subroutine changes the values of the HC12 registers.To avoid this problem, the subroutine should save the HC12 registers before it uses them, and restore the HC12 registers after it is done with them.

Example of a subroutine to delay for certainamount of time; Subroutine to wait for 100 0Loop1Loop2What is the problem with this subroutine?It changes the values of the registers that are most frequently used: A and XHow can we solve this problem?

Example of a subroutine to delay for certainamount of timeTo solve, save the values of A and X on the stack before using them, and restore them before returning.; Subroutine to wait for 100 lxpularts#250#800Loop1Loop2; restore registers

A sample program; Program to make binary counter on LEDS; The program uses a subroutine to insert a delay between equequequequ 1000 3C00 0000 0001 0002 0003orgldsldaastaaclrjsrincbraprog#STACK# ffDDRAPORTAdelayPORTAloop; Subroutine to wait for 100 lxpularts#250#800loop1loop2; initialize stack; put all 1s into DDRA; to make PORTA output; put 00 into PORTA; wait a bit; add 1 to PORTA; repeat forever

JSR and BSR place return address on stackRTS returns to instruction after JSR or BSRSTACK:1000 CF 3C 001003 16 10 071006 7F1007 CE 12 34100A 0x3BFE0x3BFF0x3C00MY SUB:A 3C00 1000#STACKMY SUB# 1234BDMemoryusedby MCUequorgldsjsrswildxrtsXYSPPCCCR

Another example using a subroutineUsing a subroutine to wait for an event to occur then take actionWait until bit 7 of address 00CC is set.Write the value of ACCA to address 00CF; This routine waits until the HC12 serial port is ready, then send a byte of data to the serial portputchar:brclrstaarts 00CC,# 80,putchar 00CF; Data Terminal Equip. ready; Send char; Program to send the word “hello” to the HC12 serial portloop:done:str:ldxldaabeqjsrbraswi#str1,x doneputcharloopfccdc.b“hello” 0a, 0d,0; form constant character; CR-LF

Another example using a subroutineA complete program to write to the screenprog:data:stack:equequequ 1000 2000 3c00loop:orgldsldxldaabeqjsrbraprog#stack#str1,x doneputcharloopcharacterdone:putchar:readystr:; initialize stack; load pointer to “hello”; is done then end program; write character to screen; branch to read nextswibrclr 00CC, 80,putchar; check is serial port isstaarts 00CF; and sendorgfccdc.bdata“hello” 0a, 0d,0; form constant character; CR-LF

JSR and BSR place return address on stackRTS returns to instruction after JSR or BSR

Using DIP switches to get data into the HC12DIP switches make or break a connections (usually to ground)5V

Using DIP switches to get data into the HC12To use DIP switches, connect one end of each switch to a resistorConnect the other end of the resistor to 5VConnect the junction of the DIP switch and the resistor to an input port on the HC125VPB0PB1When the switch is open, the input port sees a logic 1 ( 5V)When the switch is closed, the input sees a logic 0 (0V)5V

Looking at the state of a few input pinsWant to look for a particular pattern on 4 input pins-For example want to do something if pattern on PB3-PB0 is 0110Don’t know or care what are on the other 4 pins (PB7-PB4)Here is the wrong way to do it:ldaacmpabeqPORTB#b0110taskIf PB7-PB4 are anything other than 0000, you will not execute the task.You need to mask out the Don’t Care bits before checking for the pattern on the bits you are interested , whatever pattern appears on PB7-4 is ignored

Using an HC12 output port to control an LEDConnect an output port from the HC12 to an LED.Using an output port to control an LEDPA0Resistor, LED, andGround connected internally insidebreadboardWhen a current flowsThrough an LED, it emits light

Making a pattern on a 7-segement LEDWant to make a particular pattern on a 7-segmen LED.Determine a number (hex or binary) that will generate each element of the pattern-For example, to display a 0, turn on segments a, b, c, d, e, and f, or bits 0, 1, 2, 3, 4, and 5 of PTH. The binary pattern is 00111111,or 3f-To display 0, 2, 4, 6, 8, the hex numbers are 3f, 5b, 66, 7d, 7f.Put the numbers in a tableGo through the table one by one to display the patternWhen you get to the last element repeat the loopafbgecd

Flow chart to display the patterns on a 7-segementLEDStarttabletable end0x3f0x5b0x660x7d0x7f XPort HOutputPoint toFirst entryldaa # ffstaa DDRHL1:ldx #tableL2:Get entryldaa 0,xOutput toPORT Hstaa PORTHInc pointerX endinxcpx #end tablebls L2bra L1

Program to display the patterns on a 7-segementLED; Program to display uequorgldsldaastaaldxldaastaajsrcpxblsbra 1000 2000 3C00 0260 0262prog#stack# ffDDRH#table1,x PTHdelay#table endL2L1delay:Loop2:Loop1:table:table dc.bdc.bdc.bdc.b#250#8000Loop1Loop2data 3f 5b 66 7d 7f

Before you can use a stack you have to initialize the SP to point to one value higher than the highest memory location in the stack. For the HC12 use a block of memory from about 3B00 to 3BFF for the stack. For this region of memory, initialize the stack pointer to 3C00. Use LDS (Load Stack Pointer) to initialize the stack pointer.

Related Documents:

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

Chính Văn.- Còn đức Thế tôn thì tuệ giác cực kỳ trong sạch 8: hiện hành bất nhị 9, đạt đến vô tướng 10, đứng vào chỗ đứng của các đức Thế tôn 11, thể hiện tính bình đẳng của các Ngài, đến chỗ không còn chướng ngại 12, giáo pháp không thể khuynh đảo, tâm thức không bị cản trở, cái được

Food outlets which focused on food quality, Service quality, environment and price factors, are thè valuable factors for food outlets to increase thè satisfaction level of customers and it will create a positive impact through word ofmouth. Keyword : Customer satisfaction, food quality, Service quality, physical environment off ood outlets .

In the midst of Michel’s awakening to the sensuous, sensual existence, to the Dionysian world in nature and himself, he observes: [Marceline] led the way along a path so odd that I have never in any country seen its like. It meanders indolently between two fairly high mud walls; the shape of the gardens they enclose directs it leisurely course; sometimes it winds; sometimes it is broken; a .