我有一个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;
}
我检查我的数组,它具有真实值。
这是我的鼻祖代码:
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
的地址可能不是?
这是read_bmp:
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;
}
我的最终目标是获取数组的元素(间隔为0-255),并在255字节大小的数组中增加相应的值。例如,如果第一个数组中的第一个元素为55,则在255字节大小的数组中,我将第55个元素增加一个。因此,我需要访问从c传递过来的数组的元素。
答案 0 :(得分:2)
当您拥有 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.
等等。