Mixing C and Assembly

Hi all

Pardon me for my ignorance as I am new to assembly programming. Would like to seek help in this problem of mine.

I have a function which needs to be called in C and implemented in assembly. In C, the function is called "char_put(&c)" where a pointer is passed in.

1) How do I access this pointer in assembly?

2) How do I store data into the memory location pointed to by the
pointer? (Suppose to be a 8bit data)

3) When completed, the procedure needs to return a "1" if data is
store or "0" if otherwise. How do I return a value to C from
assembly?

Thank you so much in advance. Any advice is appreciated.

Regards
Wilson

Comments

  • it depends on the language,how parameters are actually passes and returned from/to subfunctions.c/c++ however pushes them (from right to left) onto the stack.parameters which are bigger then one stack-item,are passed through a (far-)pointer to it.thats the reason,why its faster to pass references either than larger structs,because they have to be copied so the function cant change them.

    results are passed through the AX-register or by a pointer (far-) pointer in (DS:)AX.in C/C++ the caller have to remove the parameters from the stack after the function has returned by popping the items from the stack.

    its important to understand the STACK-concept and know a little about the CPU-registers to understand how this all works.
  • Hi,
    I assume you use 32-bit protected mode compiler. My examples have MASM syntax.

    : I have a function which needs to be called in C and implemented in assembly. In C, the function is called "char_put(&c)" where a pointer is passed in.

    : 1) How do I access this pointer in assembly?

    Use LOCAL c :BYTE (or WORD, DWORD) at the beginning of your function.

    : 2) How do I store data into the memory location pointed to by the
    : pointer? (Suppose to be a 8bit data)

    LEA EAX,c
    MOV [EAX],your_data

    : 3) When completed, the procedure needs to return a "1" if data is
    : store or "0" if otherwise. How do I return a value to C from
    : assembly?

    MOV EAX,bool_result
    and you may test it in C as return value from function.

    : Thank you so much in advance. Any advice is appreciated.
    :
    : Regards
    : Wilson

    Good luck, I expect it is correct.


    [blue]First of all - sorry for my bad english... but I'm learning...[/blue]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories