How can a STACK be FIFO? - stack

Got astonished when I read on the page 4 of this article that stack in 8086 uses FIFO!!!
How can a STACK be FIFO?
Is the article wrong or there is a concept behind it??? I searched for about an hour but some websites said that it is FIFO and some LIFO How can it be both!
Expecting the correct answer here...
Please help...

The stack pointed to by the SP register on '86 and later Intel is a push-down stack - last in, first out. 'FIFO' is a mistype, I expect.

Related

FreeRtos how to store function address while context switching

I using freertos on my project. My code is stuck in hardfault handler, I want know last executed function address or last executed line address for debugging.How to debug code when PC is pointing Hardfault handler.
That information is 100% dependent on which microcontroller you are using, and also which tool chain you are using as some IDEs will do this for you. You failed to provide either piece of information, so are asking people to guess on your behalf. A good question is one that cannot only possibly be answered by another question.
I am going to guess you are using a Cortex-M microcontroller, in which case information on debugging a hard fault can be found on lots of links found by Google, including the following: http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

how to solve overflow errors on mikroC for PIC?

My program has several images, I want to do something like a menu with LCD and a keypad.
It was working fine untill the moment that I got two erros:
> Recursion or cross-calling of 'lcd_write'
> Not enough RAM for call stack
I read something about a stack overflow. How can I solve this problem?
I´m using PIC16F877a and mikroC for PIC v6 Compiler.
The method to solve this problem is - go back to the last compilable version, see what changes you have introduced that result in another call to lcd_write, restructure your program so that eliminate that call because it results in recursion. An example would be putting the lcd write data into a buffer instead of writing it immediately, and writing it later when you find something in the buffer.
Recursion is bad in an embedded environment because it uses the call stack by an unknown-at-compile-time amount, and small micros such as the PIC often have hardware call stacks of 8 (eg PIC16F877a) or even as small as 2 levels.
Embedded Gurus has a good explanation of problems with call stacks.
Go to the last compilable version, compile it succesfully, then go to the view Tab, then "Statistics"
In this menu you can see the call function tree and see where the stack is at it's limit. Got the same problem with the same controller and re-structured my I2C LCD functions so that they don't call a function in another called function.
If you really can't optimize your code, consider upgrading to a PIC18.

Why is it important for a stack to be a FILO structure for the services of interrupts in a microprocessor?

I am trying to understand stacks and their implementations, but the most thing that has been bugging me is "Why is it important for a stack to be a FILO structure for the services of interrupts in a microprocessor?" What do I need to understand first before I can understand the Stack concept. Can anyone please give me a comprehensive explanation on this. Thank you
FILO is useful because it allows for data within the stack to be restructured before it is accessed. Specifically, FILO allows things to be reliably reversed. If you have a situation where inner elements are returned before outer elements (as might be common with recursion) then the FILO is perfect.
http://www.i-programmer.info/babbages-bag/263-stacks.html?start=2 Has an excellent explanation of the utility of the FILO paradigm.
As for interrupts, again the utility is that subroutines will call an interrupt before anything else.
There are actually a few reasons that FILO can be useful for interrupts. http://www.eee.metu.edu.tr/~cb/e447/Chapter%204%20-%20v2.0.pdf has an introduction to the concepts. One example would be when it is desirable for registers to be pulled from a program and restored to a program in reverse order.
This article has an animation that demonstrates the exact utility of FILO:
http://users.ece.utexas.edu/~valvano/assmbly/stack.htm
When an interrupt is being called it stores the current content + next instruction address on stack.
suppose you encounter one nested interrupt (i.e interrupt within interrupt)
then you will require to store things in sequence like data + next address of 1st interrupt on encountering 1st interrupt, executing few instruction there and again encountering next interrupt, here you will require to store data + address of 2ndinterrupt.
while returning you will go back to last address where interrupt was called from there again completion of that interrupt you will return to 1st interrupt (FILO).
Hence we use FILO.

Why should we copy blocks rather than retain?

I recently met a problem using blocks with Facebook's app switching. I needed to call a block after the Facebook login.
First my block was destroyed when the app switched back ('cause it was on the stack), so I decided to retain it. But that didn't work, and I messed with that problem :/. I found a solution on that blog and also here.
My question is simply : why copy works and retain does not ?
Because when you create a block there is nothing to retain, since it doesn't exist in the heap until you copy it there with Block_copy. This is covered in the WWDC lectures about blocks.
More info: http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/
See my recent answer to another similar question:
By default blocks are created on the stack. Meaning they only exist in the scope they have been created in.
[...]
Read Stack and Heap Objects in Objective-C by Mike Ash for more info on stack vs. heap.

stack corruption checking method

1) how to initialize the stack with some unique pattern? so i can check it on the exit? sample program plz
2) how to add values in prolog and check it in epilog ? sample program plz
valgrind and electric fence doesnt work with my multithreaded app it is too bulky i want some simple trick like
add const value in prolog
check it back in epilog
thanks,
Vj
In your first question I think you are talking about preventing the execution stack from being overran. There are different technique to archive this, but I think the one closest to "some unique pattern" is the canary.
Theory:
The canary is a (random) check value that is placed just below the functions return address. Before returning from the function, the system checks if the canary has the same value as before. If not, the stack has been overran, since the memory is written from lower to higher addresses, and you can't trust the return address.
How it's done:
When the return address are placed
on the stack, the canary is placed
there as well.
When the function exits, the canary is checked. If the canary has been altered, terminate the program (or whatever you find appropriate).
More information about canary values can be found here.
This (or some other stack overrun prevention technique) are generally implemented in modern compilers.
I have no idea about your second question.

Resources