You are not logged in.
I write codes to display the dictionary using C,
and it can run well,and then I want to use system call
in assembler to realize the same function ,but it doesn't
display the dictionary,how to correct it?
Look:
//C
#include <stdio.h>
int main()
{
char *name[2];
name[0]="/bin/ls";
name[1]=NULL;
execve(name[0],name,NULL);
return 0;
}
//Assembler
.data
msg:.string "/bin/ls"
.text
.global _start
_start:
movl $0xb,%eax
movl $msg,%ebx
mov $msg,%ecx
movl $0,%edx
int $0x80
movl $1,%eax
movl $0,%ebx
int $0x80
Offline
If you compile with -Wwrite-strings, as I like to do for new code, you'll notice it warns about assigning the address of read-only memory to a plain char *. You might want to declare name as follows to avoid accidentally trying to modify it:
char const *name[2];
But as for your question. The execve system call requires a pointer to a list (array) of pointers to strings in %ecx. You are passing it a pointer to a single string, which means it will try to interpret the string ("/bin/ls")as a series of pointers to strings, with potentially disastrous (and certainly weird) results.
Last edited by Trent (2012-12-28 15:31:38)
Offline
You say:The execve system call requires a pointer to a list (array) of pointers to strings in %ecx.
I want to know how to correct codes?
Offline
Well, you need to declare space for an array containing two pointers (a pointer to msg, and a null pointer to terminate). You can use the .int (or .long) directive for this, although I'm not sure it's the preferred way.
.data
msg: .string "/bin/ls"
arr: .int msg,0
Then put the immediate value of arr, not msg, into %ecx before the interrupt.
Offline
Thank you very much!!!
Offline