Bruce
Bruce

Reputation: 35285

Stack walk with inline asm for VC++

I have inserted the following asm code in my C++ code. I am using a VC++ compiler.

char c;
curr_stack_return_addr = s.AddrFrame.Offset; //I am doing a stack walk
    __asm{  
            push bx
            mov eax, curr_stack_return_addr  
            mov bl, BYTE PTR [eax - 1]
            mov c,bl
            pop bx

     }

I get the correct value in c for my functions but it crashes when it reaches system functions on stack. I get no compiler errors. What did I do wrong?

Resolved: I forgot to check for end of stack! The return address in last frame is 0. Thanks everyone.

Upvotes: 0

Views: 174

Answers (2)

Yakov Galka
Yakov Galka

Reputation: 72549

I see two problems here:

  1. push bl and pop bl don't exist. You can only push and pop word or dwords. The compiler warns by the way.

  2. How do you know that eax points to a legal address?

Upvotes: 4

Eugene Smith
Eugene Smith

Reputation: 9398

You have no way of knowing the value of eax when your program enters the asm block.

Upvotes: 3

Related Questions