Getting Parameters in IA32 Assembly...

Started by
18 comments, last by LessBread 18 years, 6 months ago
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...?
Advertisement
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.
That's the right way generally speaking - what's not working?
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.
Quote:Original post by Krisc
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.
Atoi is probably a cdecl function is which case the caller is responsible for removing the argument afterwards.
pushl %eaxcall atoiaddl 4,%esp
I found an article about calling conventions:

http://www.codeproject.com/cpp/calling_conventions_demystified.asp

Also found this one:

http://www.hackcraft.net/cpp/MSCallingConventions/
I should mention that I am using C on a Linux machine... gcc is the compiler...
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!
enum Bool { True, False, FileNotFound };
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?
Quote:Original post by Krisc
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?
Correct.

This topic is closed to new replies.

Advertisement