简体   繁体   中英

Passing various parameters from C to Assembler

I have to write an assembler subroutine dswap(int, double*, int, double*, int) which is called from a C program and assembled by NASM. My task is to manipulate the two given vectors (double*) based on the other three int parameters. Right now I am stuck to find out how exactly these parameters can be accessed in my assembler code and how to access specific elements from the vectors. Can you point me to some documentation/examples for this?

You want to look at "stack frames", and in case you're using x86, look at what the micro-coded instructions "enter" and "leave" do, but don't use them. Instead, use the coded out equivalent, to set up stack frame such that you can access your own passed arguments.

Consider that, in C, parameter handling is done such that you can pass many arguments to a function/subroutine, but only one can be returned.

I made a small investigation and it turns out MinGW gcc produces COFF-compliant object files. As to nasm - there is a command line - -f coff that you can use to force it produce COFF-format object file. Next it's quite restricted in form of defining procedures. In your assembly listing don't forget to specify SEGMENT _text before coding your actual function. The function must be defined in a way:

SEGMENT _text
global _dswap ;; underscore here is important - it's cdecl style.
_dswap:
push ebp ;; look below
mov ebp, esp ;; cdecl function prologue. Entering stack frame.
sub esp, 20h ;; This is for your local variables if needed.
;;do your work here. Use ebp as the origin for parameters. The very first passed parameter to the stack is ebp + 8(ebp + 4 is a return address).
;;
add esp, 20h ;; if you performed your local variables allocation. You can access them with mov eax, [esp + 10] for instance. But I recommend you to speci fy constants with EQU macro to make the code more readable. From your signature I see that you pass pointers to doubles. They reside on the regular stack. You have to pop them to floating point stack within this function before you can do something with these variables.
pop ebp ;; Leaving stack frame
ret ;; cdecl assumes that the caller shall clean parameters.

In your c file you just declare prototype without any extern keywords. Function prototypes are by default extern.

Did you intent to swap two double vectors through the use of this function?

As a hint - Open msys MinGW console(You must install every it's component before), type the following test.c file:

double foo(double lhl, double rhl)
{
    return lhl + rhl;
}

main()
{
    foo(4.5, 2.7);
    return 0;
}

compile it with gcc -c test.c Next you will see test.o file. And this is the time to examine how this stuff works. You need to have dump bin utility or any other disassembly utility:

dumpbin /disasm:nobytes test.o > listing.asm

And yet I recommend you reading http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM