简体   繁体   English

将各种参数从C传递到汇编器

[英]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. 我必须编写一个汇编子程序dswap(int, double*, int, double*, int) ,该子程序从C程序调用并由NASM进行汇编。 My task is to manipulate the two given vectors (double*) based on the other three int parameters. 我的任务是根据其他三个int参数处理两个给定的向量(double *)。 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. 您需要查看“堆栈帧”,如果使用的是x86,请查看微输入指令“输入”和“离开”的作用,但不要使用它们。 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. 考虑到在C语言中完成了参数处理,因此您可以将许多参数传递给函数/子例程,但是只能返回一个。

I made a small investigation and it turns out MinGW gcc produces COFF-compliant object files. 我进行了一次小型调查,结果发现MinGW gcc生成了符合COFF的目标文件。 As to nasm - there is a command line - -f coff that you can use to force it produce COFF-format object file. 对于nasm-有一个命令行--f coff,您可以用来强制它生成COFF格式的目标文件。 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. 在程序集列表中,请不要忘记在对实际函数进行编码之前指定SEGMENT _text。 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. 在您的c文件中,您只声明了原型而没有任何外部关键字。 Function prototypes are by default extern. 函数原型默认为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: 提示-打开msys MinGW控制台(您必须先安装每个组件),然后键入以下test.c文件:

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. 用gcc -c test.c编译它。接下来,您将看到test.o文件。 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 但我建议您阅读http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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