You are not logged in.

#1 2012-12-28 09:36:36

leetow2003
Member
Registered: 2012-04-08
Posts: 25

How to use system call in assembler

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

#2 2012-12-28 15:29:34

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 986

Re: How to use system call in assembler

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

#3 2012-12-29 01:32:51

leetow2003
Member
Registered: 2012-04-08
Posts: 25

Re: How to use system call in assembler

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

#4 2012-12-29 02:27:01

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 986

Re: How to use system call in assembler

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

#5 2012-12-29 09:39:34

leetow2003
Member
Registered: 2012-04-08
Posts: 25

Re: How to use system call in assembler

Thank you very much!!!

Offline

Board footer

Powered by FluxBB