Last active
October 14, 2017 06:56
-
-
Save if1live/bb4a0de7aada870bbf0cb1ffb5037ffa to your computer and use it in GitHub Desktop.
use stack address as loop ounter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void execute(int n, int total) { | |
printf("loop count : %d/%d\n", n, total); | |
} | |
int loop_core(int *root_addr, int *prev_addr, int num) { | |
int base_value = 0; | |
// comment or uncomment | |
printf("%d\n", base_value); | |
if(root_addr == prev_addr) { | |
// entry point | |
execute(0, num); | |
return loop_core(root_addr, &base_value, num); | |
} else { | |
// normal | |
const int loop_index = ((unsigned long)root_addr - (unsigned long)&base_value) / ((unsigned long)prev_addr - (unsigned long)&base_value); | |
if(loop_index < num) { | |
execute(loop_index, num); | |
return loop_core(root_addr, &base_value, num); | |
} else { | |
return 0; | |
} | |
} | |
} | |
int loop(int num) { | |
int base_value = 0; | |
return loop_core(&base_value, &base_value, num); | |
} | |
int main() { | |
loop(5); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment