Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数组作为参数从C传递给x86函数_C_Assembly_X86_Nasm_Calling Convention - Fatal编程技术网

将数组作为参数从C传递给x86函数

将数组作为参数从C传递给x86函数,c,assembly,x86,nasm,calling-convention,C,Assembly,X86,Nasm,Calling Convention,我有一个bmp文件,我在c函数中读取它,并将像素值存储为无符号整数。我想将此无符号整数数组传递给x86,但失败了。 这是我的c代码: 我有以下特点: extern int func(char *a); unsigned char* image; 我的主要方法是: int main(void){ image = read_bmp("cur-03.bmp"); int result = func(image); printf("\n%d\n", result); return 0;

我有一个bmp文件,我在c函数中读取它,并将像素值存储为无符号整数。我想将此无符号整数数组传递给x86,但失败了。 这是我的c代码:

我有以下特点:

extern int func(char *a);
unsigned char* image;
我的主要方法是:

int main(void){
  image = read_bmp("cur-03.bmp");
  int result = func(image);
  printf("\n%d\n", result);
  return 0;
}
我检查数组,它的值为真

这是我的nasm代码:

section .text
global  func

func:
    push ebp
    mov ebp, esp
    mov ecx , DWORD [ebp+8] ;address of *a to eax


    pop ebp
    ret

section .data
    values: TIMES   255         DB      0   
我希望ecx拥有数组的第一个元素,但我得到的不是这个元素,而是
1455843040
和地址

以下是阅读内容:

unsigned char* read_bmp(char* filename)
{
    int i;
    FILE* f = fopen(filename, "rb");
    unsigned char info[54];
    fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

    // extract image height and width from header
    int width = *(int*)&info[18];
    int height = *(int*)&info[22];
    int heightSign =1;
    if(height<0){
        heightSign = -1;
    }

    int size = 3 * width * abs(height);
    printf("size is %d\n",size );
    unsigned char* data = malloc(size); // allocate 3 bytes per pixel
    fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
    fclose(f);

    return data;
}
unsigned char*read\u bmp(char*filename)
{
int i;
FILE*f=fopen(文件名为“rb”);
未签名字符信息[54];
fread(info,sizeof(unsigned char),54,f);//读取54字节的头
//从标题中提取图像高度和宽度
整数宽度=*(整数*)和信息[18];
整数高度=*(整数*)和信息[22];
整数高度符号=1;
如果(高度当您有一个C原型
extern int func(char*a);
您正在传递一个指向堆栈上字符数组
a
的指针。您的汇编代码执行以下操作:

push ebp
mov ebp, esp
mov ecx , DWORD [ebp+8] ;address of *a to eax
EBP+8是一个内存操作数(在堆栈上),调用函数将
a
的地址放置在该操作数中。您最终从堆栈中检索到指向
a
(1455843040)的指针。您需要做的是进一步取消对指针的引用以获取单个元素。您可以使用以下代码执行此操作:

push ebp
mov ebp, esp
mov eax , DWORD [ebp+8] ; Get address of character array into EAX
mov cl, [eax]           ; Get the first byte at that address in EAX. 
要获取数组中的第二个字节,请执行以下操作:

mov cl, [eax+1]         ; Get the second byte at that address in EAX.

以此类推。

可能是地址吗?-是的,数组将衰减为指向该上下文中第一个元素的指针。但您没有数组。如何在read_bmp()中分配内存?用C编写一个简单的
func
,编译它并查看汇编程序是什么。将其用作函数的模板。您如何知道此图像的大小?!我的集合中的所有图像都具有相同的大小t:1)始终检查(!=NULL)来自
fopen()的返回值
以确保操作成功。如果未成功,请在调用
fread()时调用
peror()
以输出错误消息和系统认为错误发生在
stderr
.2)的文本原因
,始终将返回值与第三个参数进行比较,以确保操作成功。如果操作不成功,请如上所述调用
peror()