Page 1 of 1

ASM: Passing arguments to function by Stack

Posted: Wed Jul 20, 2005 3:25 pm
by hitachi
Hi!
I wan't to push the string on the stack, and then pass it, as an argument to function. I know this can be done by:

Code: Select all

string db 'my_string', 0
But I wan't it to do the stack way!
This is how far I get:

Code: Select all

section .text

   global_start:

   _start;

   push 0x65       ; d
   push 0x66       ; e
   push 0x66       ; f

   mov ebx, esp
      
   mov eax, 39
   mov ecx, 1
   int 0x80

   mov eax, 1
   mov eax, 0
   int 0x80
It creates a folder with name "f" not "def" and I allso get Segmentation fault! What is wrong? Thank you!

Re:ASM: Passing arguments to function by Stack

Posted: Wed Jul 20, 2005 6:33 pm
by AR
Why do you want to put it on the stack? That's a waste of clock cycles to push a static string on the stack (if you aren't trying to conserve clock cycles then why are you in assembly?). I would assume that you have forgotten or are not aware that on the x86 the stack expands down, eg:

Code: Select all

d (ESP+8)
e (ESP+4)
f (ESP)
The reason you're only getting one letter as you can hopefully see, you have actually pushed [tt]0x66 0x00 0x00 0x00 0x65 0x00 0x00 0x00 0x64 0x00 0x00 0x00[/tt] on the stack. As you can see there are a lot of NULLs, and it stops as soon as it hits the first one after the "f".

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 12:29 am
by Solar
Wouldn't it be smarter to push a pointer to the string on the stack?

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 3:25 am
by hitachi
Thank you for the replys!
Wouldn't it be smarter to push a pointer to the string on the stack?
That is what I wanted to do! But don't I need to put it on the stack? How can I get this pointer address? And I know the stack way isn't useful, but how can I do it? Thank you!

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 3:37 am
by Solar
push $string

...unless I (again) confused AT&T and Intel syntax and it's really "push string" without the '$'. "string" is the symbol associated with your character sequence, and you can take it's address.

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 8:01 am
by hitachi
Thanks!
I found this code example:
movl $0x66, %eax ;sock_syscall
movl $0x01, %ebx ;socket()

pushl $0x06 ;IPPROTO_TCP
pushl $0x01 ;SOCK_STREAM
pushl $0x02 ;AF_INET

movl esp,ecx ; ???
int $0x80
Because there are no general registers left to pass the arguments to the function, they are push'ed in to the stack. I don't seem to understand how does this function know witch values to pop off the stack?

What does movl esp,ecx mean? ESP = Stack Pointer, but what about ECX, its a general register used for counting! Why does it's value are stored in ESP? Thank you!

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 12:17 pm
by DennisCGc
hitachi wrote: Thanks!
I found this code example:
movl $0x66, %eax ;sock_syscall
movl $0x01, %ebx ;socket()

pushl $0x06 ;IPPROTO_TCP
pushl $0x01 ;SOCK_STREAM
pushl $0x02 ;AF_INET

movl esp,ecx ; ???
int $0x80
Because there are no general registers left to pass the arguments to the function, they are push'ed in to the stack. I don't seem to understand how does this function know witch values to pop off the stack?

What does movl esp,ecx mean? ESP = Stack Pointer, but what about ECX, its a general register used for counting! Why does it's value are stored in ESP? Thank you!

I think it's because the Linux kernel uses the ECX register for the stack "table".

Re:ASM: Passing arguments to function by Stack

Posted: Thu Jul 21, 2005 12:19 pm
by AR
ECX is a general register and is used to send parameters to the kernel for system calls. In that context, ECX in C would be something like socketparams_t* (A pointer to a struct). They simply built it on the stack for some reason (they could have just had "mov ecx, socketdata; socketdata: dd 0x02, 0x01, 0x06"), try:

Code: Select all

section .text
global _start:
_start:

   mov ebx, mystring   ;Put the pointer into EBX
      
   mov eax, 39
   mov ecx, 1
   int 0x80

   mov eax, 1
   mov eax, 0
   int 0x80

SECTION .data
mystring: db 'mystring',0

Re:ASM: Passing arguments to function by Stack

Posted: Fri Jul 22, 2005 4:22 am
by hitachi
Thank you!
I wan't to be sure that I understand this:
There are two ways to pass arguments to system calls: By Registers, and by Stack!

When Passing arguments by registers:
EAX - System Call ID
EBX - First Argument
ECX - Second Argument
EDX - Third Argument

When the System Call has more than three arguments, I need to pass them by Stack:
EAX - System Call ID ; SOCKETCALL();
EBX - Sub-System Call ID ; CONNECT();
ECX = ESP (If ECX contains memory address not a value, the kernel will know that I will pass arguments by stack?)
PUSH .. (Push the arguments on the stack!)

Am I wrong?

Re:ASM: Passing arguments to function by Stack

Posted: Fri Jul 22, 2005 4:29 am
by AR
Unfortunately I'm not familiar with the Linux Kernel interface, but I doubt it uses the stack at all, if the parameters won't fit then it will most likely use a pointer to a struct of data as one of the parameters.

A pointer points to anywhere, the stack is nothing special in this regard (neither is code for that matter).

Re:ASM: Passing arguments to function by Stack

Posted: Fri Jul 22, 2005 5:04 am
by Solar
...and only in the undertones of this thread does it become apparent that you are not talking about generally passing arguments in Assembler, but about passing arguments to specific Linux functions. You could just as well have spoken about your own code, the Win32 API or whatever...

In this case, I would recommend to refer to the Linux API docs, and perhaps read the "Programming from the Ground Up" book to make you get more comfortable with using Assembler.