Created
January 7, 2014 17:56
-
-
Save long-long-float/8303549 to your computer and use it in GitHub Desktop.
"return" function
This file contains hidden or 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> | |
#include <cassert> | |
void _return(){ | |
__asm{ | |
pop ebp //フレームポインタ回収 | |
pop ebp //_returnから脱出 | |
pop ebp //呼び出し元のフレームポインタ回収 | |
ret | |
} | |
} | |
void _return(int val){ | |
__asm{ | |
mov eax, dword ptr [ebp + 8] //返り値セット | |
pop ebp | |
pop ebp | |
add esp, 4 //呼び出し側の仕事 | |
pop ebp | |
ret | |
} | |
} | |
void func1(){ | |
_return(); | |
assert(false); | |
return; | |
} | |
int func2(){ | |
_return(10); | |
assert(false); | |
return -1; | |
} | |
int main(){ | |
func1(); | |
printf("%d\n", func2()); //=> 10 | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment