Take the 2-minute tour ×

i want to call one function based on its address. but after i try to do that, some trouble come to my eyes. i cant deal with those trouble. i dont know what is going wrong. please somebody help me to finish the calling action. image link

#include <stdio.h>

void main()
    char* mat = "%s\n";
    char* str = "abc";

        mov         eax,dword ptr [ebp-8]
        push        eax
        mov         ecx,dword ptr [ebp-4]
        push        ecx
        mov         eax,00401090h //function 'printf' address
        call        [eax]
        //call        printf //dont call like this, we shall call based on its address.

share|improve this question

closed as not a real question by false, Jonathan Leffler, Drew Dormann, Dan, ChrisF Jan 9 '12 at 10:39


Why is the code a link to a separate file? And why is it a picture? –  Cody Gray Jan 8 '12 at 4:59
Please copy and paste your code inline so people can test it in their own environment. Use the {} button with the code selected to format your code. –  bdonlan Jan 8 '12 at 5:01
How did you determine that printf is at 00401090? Are you expecting it to always be there? –  Drew Dormann Jan 8 '12 at 5:01
@CodyGray: Probably because it's worth a thousand words? –  Mehrdad Jan 8 '12 at 5:12
@pezcode, if you favorite a post you'll be notified when it is updated. I don't really use them as posts I really like, I use them for posts where I want to go back later to see how things turned out. So, ironically, I tend to use favs on low-quality posts where I want to see if the OP straightened out whatever they did wrong the first time around :) –  bdonlan Jan 8 '12 at 5:56
show 7 more comments

2 Answers

You can not rely on printf being at a hard-coded address.


mov    eax,00401090h
call   [eax]


call    printf
share|improve this answer
based on address, not based on function name. –  user859974 Jan 8 '12 at 5:28
The function name IS the address. –  Jens Björnhager Jan 8 '12 at 5:31
And here is the obvious call-an-adress approach: mov eax, printf; call [eax];. –  jørgensen Jan 8 '12 at 6:14
add comment

The load address of the DLL containing printf can (and does!) vary. This means that the address of printf changes - it might even change across two runs of the same application!

You can do as Drew suggests and replace it with a literal printf - the linker will replace this with the proper address of a trampoline routine (which, in turn, may be moved from one compilation to another, or even at runtime if you have a relocatable executable) that will be pointed to the real printf at runtime.

Or you can use OS-specific routines to find printf's address yourself at runtime, then invoke using that address (the details are left as an exercise for the reader - I would recommend prototyping this in C first, then translating it a bit at a time to asm). On windows, look for GetProcAddress; on Unixen, look for dlsym.

share|improve this answer
Too much OS-specific while question does not specify any OS. –  m0skit0 Jan 8 '12 at 13:44
@m0skit0, he's using Visual C++ inline assembler syntax, so Windows is a good guess, but I made it a bit less OS-specific anyway –  bdonlan Jan 8 '12 at 19:21
add comment
