我对汇编编程很陌生。我正在使用带有 GCC (Linux) 的 x86 平台。
我有一个我想从 C 调用的函数:
myfunc ( unsigned char * s1, unsigned char * s2, int someint );
该函数将获取 s1 和 s2 内存位置并比较它们,然后递增和比较等,同时进行一些处理。这有点像 memcmp,但我做得更多。
我的问题:如果我将指针传递给汇编函数?然后我怎么说“给我存储在这个内存地址的值”?
这是我到目前为止所拥有的:
为了从堆栈中取出第一个函数 arg ("s1"),我这样做(someaddress 是一个 32 位整数,我正在使用 32 位处理器):
movl 8(%esp), %ecx
movl %ecx, someaddress
如果我放入somevar
( %eax
or%ebx
等) 然后用 printf 它%p
,我看到它的地址和s1
我传递的 unsigned char 指针 "" 的地址是相同的。但我怀疑我实际上所做的是获取内存地址,将其转换为整数,然后将该整数放入某个地址。
例如,如果我这样做:
movl pos1, %eax
movl pos2, %ebx
cmp (%eax),(%ebx)
我得到“错误:`cmp' 的内存引用过多”。我不完全确定这意味着什么,除了“你搞砸了”;-)
所以...
- 如何传递一个指针并将其保留为指针?
- 如何在汇编中使用所述指针的值?(例如,就像
*ptr
在 C 中一样)
我要查看 LEA 操作数吗?
我使用 Richard Blum 的“Professional Assembly Programming”作为我的指南,但 Blum 似乎没有涵盖这种情况。
更新
非常感谢您的学习回复!
不幸的是,我仍然无法取消引用。
这是一个简化的例子。汇编函数接受一个指针并且应该回显它。相反,我得到:
first_ptr points to 81 (should be 81) <-- from C program
the value is -1543299247 <-- printf called from within assembler
the value is -6028513 <-- printf called from within assembler
my function returned -6028513 <-- return value printed from C program
C程序:
#include <stdio.h>
#include <string.h>
int main (void) {
unsigned char first;
unsigned char * first_ptr;
first = 'Q';
first_ptr = &first;
printf ("first_ptr points to %i (should be 81)\n",*first_ptr);
printf ("my function returned %i\n", myfunc(first_ptr));
return 0;
}
组装程序:
.section .data
msg:
.asciz "the value is %i\n"
.section .bss
.lcomm str, 8
.section .text
.type myfunc, @function
.globl myfunc
myfunc:
# save stack
pushl %ebp
movl %esp, %ebp
# save string arg from stack to "str"
movl 8(%esp), %ecx
movl %ecx, str
# let's try printing the ecx dereference
pushl (%ecx)
pushl $msg
call printf
# put the value of str on the stack
# and call printf
pushl (str)
pushl $msg
call printf
# now return the character at pos1
movl (str), %eax
# restore the stack
movl %ebp, %esp
popl %ebp
ret