User defined functions?

Posts 1–3 of 3 · Page 1 of 1
User defined functions?
Hey, I'm back with a few more questions on assembly

Anyways, I've been trying to make my own function that adds 2 numbers but I ran into a couple of problems. Hopefully you guys can help me out a little.

Code:
.386
.model flat,stdcall
option casemap:none

include\masm32\include\windows.inc

.code

main:
	
	Addition proc x:DWORD, y:DWORD
	
	mov eax,[ebp+8]
	add eax,y
	Ret
Addition EndP

	push 10
	push 5
	call Addition
	
end main
Okay, so I have a question about what happens after you call the function. For some reason after I do "call Addition" all code after that is completely ignored and the program just closes. No idea why that's happening.

Second question, I sort of understand how to use the base pointer register. Inside the function I can do [ebp+8],[ebp+12]..etc for the parameters to the function but... what happens to the base pointer right after the function is called? Will [ebp+8] still give me the first parameter?

Err.. Third question. What the hell does RET do. It's killing me... I have absolutely no idea why it's there. As soon as I typed "Addition proc" into WinAsm studio it automatically puts it there for me so I'm guessing it's important.

Last question. I've seen people do "esp+12" sometimes. And I believe it's because there are 3 4 byte addresses pushed onto the stack and I guess it "removes" them.. Not sure about that and that's why i'm asking this question. ALSO... why would you ever do "esp+12", if you don't clear the stack will it affect the next time I call a function?

Alright I'm done.. Sorry for all the questions. Thanks to anyone who takes their time to even look at this.

~David
If this is your actual listing, then there is no code after you call the function so the program just exits.
Your store your result in EAX so if you wanted to do something with the answer you can write some more code
that deals with that after the 'call Addition' line.

Ret is like 'return' in C++, and just is there so the program knows to return to the line after which the call
was made (as you probably read somewhere: ebp = old ebp value, ebp+4 = return address, ebp+8=1st argument etc, so basically
'ret' says 'jmp to ebp+4' and ebp should be restored to the old ebp value
Say you push 2 32bit values before a function call. If you see 'ret 8' in the function it means it's cleaning up the stack those 8 bytes for you (updating where esp points). Depending on the caling convention the caller may need to do the cleanup instead, and that's when you see:
Code:
push eax
push ebx 
call someFunction  
add esp, 8			;This cleans up the stack pointer to 'get rid' of the 2 arguments we pushed earlier
Depending on how a function call is setup, ESP can point to 'local variables' in the function. Wikipedia had this interesting example:
Code:
As an example, the following C code:

void MyFunction3(int x, int y, int z)
{
  int a, int b, int c;
  ...
  return;
}

Will create the following assembly code:

push ebp
mov ebp, esp
sub esp, 12 ; sizeof(a) + sizeof(b) + sizeof(c)
;x = [ebp + 8], y = [ebp + 12], z = [ebp + 16]
;a = [ebp - 12] = [esp], b = [ebp - 8] = [esp + 4], c = [ebp - 4] = [esp + 8]
mov esp, ebp
pop ebp
ret 12 ; sizeof(x) + sizeof(y) + sizeof(z)

Hope that clears things up a bit
Thanks, I'm starting to understand the EBP and ESP registers alot more now. And as for the code I posted, I tryed opening a Message box after calling just to test it and nothing happened, it just closed.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?