views:

136

answers:

3

how are an int and char handled in an asm subroutine after being linked with a c++ program? e.g. extern "C" void LCD_ byte (char byte, int cmd_ data); how does LCD_ byte handle the "byte" and "cmd_ data"? how do I access "byte" and "cmd_ data" in the assembly code? thanks

+1  A: 

Well a lot depends on the calling convention which in turn, AFAIK, depends on the compiler.

But 99.9%% of the time it is one of 2 things. Either they are passed in registers or they are pushed on to the stack and popped back off inside the function.

Goz
There may be compiler specific function attributes that you can use to choose a calling convention. These are like fastcall, stdcall, regparm and others.
Zan Lynx
+2  A: 

This very much depends on the microprocessor you use. If it is x86, the char will be widened to an int, and then both parameters are passed on the stack. You can find out yourself by compiling C code that performs a call into assembly code, and inspect the assembly code.

For example, given

void LCD_byte (char byte, int cmd_data);

void foo()
{
   LCD_byte('a',100);
}

gcc generates on x86 Linux the code

foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    $100, 4(%esp)
        movl    $97, (%esp)
        call    LCD_byte
        leave
        ret

As you can see, both values are pushed on the stack (so that 'a' is on the top), then a call instruction to the target routine is made. Therefore, the target routine can find the first incoming parameter at esp+4.

Martin v. Löwis
A: 

Look up the documentation for your platform. It tells you which calling convention is used for C.

The calling convention specifies how parameters are passed, which registers are caller-saves and which are callee-saves, how the return address is stored and everything else you need to correctly implement a function that can be called from C. (as well as everything you need to correctly call a C function)

jalf