我有一个 bmp 文件,我在 ac 函数中读取它并将像素值存储为无符号整数。我想将这个无符号整数数组传递给 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
地址?
这是 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 个元素递增 1。所以我需要访问我从 c 传递的数组元素。
当你有一个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.
等等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句