Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Last active December 16, 2015 17:40
Show Gist options
  • Save MrTrick/5472030 to your computer and use it in GitHub Desktop.
Save MrTrick/5472030 to your computer and use it in GitHub Desktop.
Abusing the stack pointer for fun and profit
/*
* StackManipulation.c
*
* Created: 27/04/2013 2:34:22 PM
* Author: MrTrick
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
uint8_t depth = 8;
void cancel() {
SP = RAMEND; //Completely clear the stack
cli();
run(); //Run never returns
}
void funcx() {
if (--depth==0) {
depth = 8;
cancel();
}
funcx();
}
void run(void) {
while(1)
{
//Some pointless assignations to occupy the controller
PORTC = 0xF0;
PORTB = 0x0F;
//This function just takes too long... so it'll be cancelled
//after a few recursions, and execution will restart at the top of runMain()
funcx();
}
}
int main(void)
{
//init stuff...
//
run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment