Jump to content
  • Advertisement
Sign in to follow this  
Krisc

Getting Parameters in IA32 Assembly...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 %eax
call atoi
addl 4,%esp

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!