Public Group

# Getting Parameters in IA32 Assembly...

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I have a question regarding getting parameters in IA32 Assembly... How do I do it? I though this was how...
main:
pushl	%ebp
movl	%esp, %ebp

xor	%eax,%eax
xor	%ecx,%ecx

movl	8(%ebp),%eax	# eax is x
movl	12(%ebp),%ecx	# ecx is y

where 8(%ebp) is the first and 12(..) is the second...?

##### Share on other sites
That looks correct for most stack based calling conventions, the base offset of 8 corresponds to the pushed base pointer and function return address.
Unfortunately x86 has numerous calling conventions with different ideas about parameter passing. Fastcall transfers to two first parameters in ECX and EDX for instance, some push the data in reverse order and the return address may contain a segment too.
The two most common conventions, cdecl and stdcall, use the stack passing method you describe however. But a significant difference is that stdcall requires the callee to remove the parameters from the stack (ending the function with "RET 8" in this case).
IIRC all common conventions require you to preserve the ESI,EDI,EBX and EBP registers.

##### Share on other sites
That's the right way generally speaking - what's not working?

##### Share on other sites
I think I may need to call atoi actually as I need to transfer the parameters into integers, but I am unsure how to do this... if I just push and call it results in a seg fault.

##### Share on other sites
Quote:
 Original post by KriscI think I may need to call atoi actually as I need to transfer the parameters into integers, but I am unsure how to do this... if I just push and call it results in a seg fault.
Atoi is probably a cdecl function is which case the caller is responsible for removing the argument afterwards.
pushl %eaxcall atoiaddl 4,%esp

##### Share on other sites
I should mention that I am using C on a Linux machine... gcc is the compiler...

##### Share on other sites
The first argument to "main" is argc, which is an integer.

The second argument to "main" is argv, which is a pointer to a number of pointers to char strings.

Thus, you can't just push either of these and call atoi -- you can't call atoi(int) and you can't call atoi(char**); both will crash. However, because it's assembly, you don't have type checking to actually tell you that when you're compiling.

I have two suggestions for you, that will both help you a lot when learning these things:

1) run your program in gdb, and use "disas" to disassemble your function. Use "stepi" to step by single instruction, and look at what's in the registers. Use "x" to look at what's in memory at different places.

2) write the function you want to write in C first, compile it, and disassemble it using "objdump --disassemble"; that will give you something that works to start from. You may also wish to use "stepi" and "dias" in gdb on the C version of the function to see how it behaves.

Good luck!

##### Share on other sites
Duh, that makes sense. I completely forgot what the parameters were in actual C code.

okay, so the second one is an array of pointers. so what i need to do is add 4 to the memory spot of the second parameter each time i want to increment to the next spot in the array. correct?

##### Share on other sites
Quote:
 Original post by Kriscokay, so the second one is an array of pointers. so what i need to do is add 4 to the memory spot of the second parameter each time i want to increment to the next spot in the array. correct?
Correct.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013658
×