I'm rather new to assembly programming. I'm using the x86 platform with GCC (Linux).
I have a function that I want to call from C as:
myfunc ( unsigned char * s1, unsigned char * s2, int someint );
The function will take the s1 and s2 memory locations and compare them, then increment and compare, etc., doing some processing as it goes. This is sort of like memcmp but I'm doing more.
My question: if I pass a pointer into an assembly function? And then how do I say "give me the value stored at this memory address"?
Here's what I have so far:
To get the first function arg ("s1") off the stack, I do this (someaddress is a 32-bit integer, and I'm working on a 32-bit processor):
movl 8(%esp), %ecx
movl %ecx, someaddress
If I put somevar
into %eax
(or %ebx
, etc.) and then printf it with %p
, I see that its address and the address of the unsigned char pointer "s1
" I passed it are the same. But I suspect that what I've actually done is taken the memory address, converted it to an integer, and then put that integer in someaddress.
For example, if I then do this:
movl pos1, %eax
movl pos2, %ebx
cmp (%eax),(%ebx)
I get "Error: too many memory references for `cmp'". I'm not entirely certain what that means, except "you screwed up" ;-)
So...
- how to pass a pointer in and keep it as a pointer?
- how to use the value of said pointer in assembly? (e.g., like
*ptr
in C)
Do I want to look at the LEA operand?
I'm using Richard Blum's "Professional Assembly Programming" as my guide, but Blum doesn't seem to cover this case.
Update
Thank you very much for your learned response!
Unfortunately, I'm still not able to dereference.
Here is a simplified example. The assembly function takes a pointer and should echo it back. Instead I get:
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 Program:
#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;
}
Assembly program:
.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