Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do procedure calls work in assembler?

I just started tinkering with ASM and I'm not sure if my understanding of procedure calls is correct.

say at some point in the code there is a procedure call

call dword ptr[123]

and the procedure consists of only one command, ret:

ret 0004

what would be the effect of this procedure call, and where would the return value be stored? I read somewhere that a return value of 2 bytes would be stored in AX, but when I replace the procedure call by

mov AX, 0004

(together with the necessary NOPs) the program crashes.

like image 512
int3 Avatar asked Aug 09 '09 09:08

int3


People also ask

How does call work in assembly?

In assembly language, the call instruction handles passing the return address for you, and ret handles using that address to return back to where you called the function from. The return value is the main method of transferring data back to the main program.

How the procedure is called from the main program in assembly language?

The main program calls a procedure named display, which displays the ASCII character set.

What is a procedure in assembly language?

A procedure is a block of logically-related instruction that can be called by the main program or another procedure. • Each procedure should have a single purpose and be able to do its job independent of the rest of the program.

What happens when we call a procedure?

A call to a procedure does not return any value. When a procedure with definer's rights is called, the current default schema is set to the eponymously named schema of the definer. For example, if the defining user is called OWNER, the default schema will also be set to OWNER.


1 Answers

in x86 assembler the parameter to the ret instruction means:

RET immediate

Return to calling procedure and pop immediate bytes from the stack.

(quoting from Intel® 64 and IA-32 Architectures Software Developer's Manuals Vol 2B)

So when you type:

ret 0004

You're telling the CPU to return to the instruction immediately after the call, and to pop 4 bytes off the stack. This is great if you pushed 4 bytes onto the stack before the call.

push eax
call dword ptr[123]

Note that this has nothing to do with the return value. In fact, a procedure in Assembly has no way of specifying that a value is a return value. This is all done by convention. Most compilers of which I am aware will use EAX to hold the return value, but this is true only because the calling function will expect the result there.

So your calling code would be:

call dword ptr [123]
mov dword ptr [result], eax

and your function that returns the value 4 would be:

mov eax, 4
ret
like image 195
Nathan Fellman Avatar answered Nov 02 '22 19:11

Nathan Fellman