The non-assembler version is more or less just this.
Code:unsigned int get_stack_address( void ) { unsigned int r = 0; return (unsigned int)&r; }
The non-assembler version is more or less just this.
Code:unsigned int get_stack_address( void ) { unsigned int r = 0; return (unsigned int)&r; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
r is a local variable on the stack.
You take it's address with &
The assembler "__asm mov dword ptr [r], esp" puts the current stack pointer into a variable.
The address of a local variable is a small constant offset from the current stack pointer within the same stack frame.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Got you , it's a tricky way you are trying to do.
like lets say main function its stack frame for instance 0x0 - 0x8 (assumption) now you are saying that getting address of r will like give us 0x9 lets say so by this somehow "approximately " we know know current stack frame size ..
Yes?
thanks.
Yes.
Because the offset is constant, when you subtract here
begin_address - get_stack_address()
the offset goes away.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
offset is actually the size of data type " unsigned intr = 0 " , but why offset goes away? I understand that we are doing subtraction ..
kindly , can you please give an example with some address to example like starting from calling main () and then one iteration rec() executed then to show how offset goes away?
thanks much.
Print all the values.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Add more until you understand what's going on.Code:unsigned int get_stack_address( void ) { unsigned int r = 0; __asm mov dword ptr [r], esp; printf("get_stack_address returning %u\n", r ); return r; } void rec( int x, const unsigned int begin_address ) { printf("rec: x=%d, begin_address=%u\n", x, begin_address)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
you can change your recursion to tail recursion What is Tail Recursion - GeeksforGeeks
i read book Mastering Algorithms with C Kyle Loudon it is in 3rd chapter and they say compilers are designed for detection tail recursion, it is reuse same stack frame sorry if i am offtopic.
Tail call optimization isn't a guaranteed feature of C. Many compilers can do it, but you can't count on it in general.