News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

[ USES ]

Started by ic2, April 12, 2007, 07:22:06 AM

Previous topic - Next topic

ic2

Hello Everybody

Is this how USES is written out?   If not, how would I write it  and use it manually.

MyProc PROC uses ebx edx

PUSH edx
PUSH ebx

........
........
........

POP ebx
POP edx

ret

MyProc ENDP

MichaelW

Yes, but if the procedure has a stack frame the pushes must follow the code that sets up the stack frame and reserves space for local variables, and the pops must precede the leave instruction.

eschew obfuscation

ic2

I must really be getting uses to masm, finally ...  but it going to take me a minute to understand the last part of what you just explained.  I guest LEAVE is same as RET.  I'll better check in with masm32 help files..

Thanks again MichaelW

MichaelW

I actually should have added "or equivalent", which would be mov esp, ebp / pop ebp.

xxx proc uses eax ebx
  local yyy:dword
  ret
xxx endp

00401000 55                     push    ebp
00401001 8BEC                   mov     ebp,esp
00401003 83C4FC                 add     esp,0FFFFFFFCh
00401006 50                     push    eax
00401007 53                     push    ebx
00401008 5B                     pop     ebx
00401009 58                     pop     eax
0040100A C9                     leave
0040100B C3                     ret
eschew obfuscation

hutch--

ic2,

It is a matter of personal taste but I would suggest to you to manually handle your own PUSH and POP sequences so you know what is happening in your procedure. As you can see in Michael's code,the LEAVE restores EBP and ESP, the RET balances the stack if needed and returns to the next instruction after the last call.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ic2

Thanks Guys, I get it now.  One more thing i don't completely understand and that is the word FUNCTION.  I think it's just a PROCEDURE.  Is this right?

Is it a PROC without a stack frame or does it matter?.   Can it even be some code that you jump to and never return back from?

What is consider as a strong Function and a weak Function ... in terms when ASM programmers speak of them from time to time? 

When is it right for a programmer to call a  block of code a Function in his program?

Sometimes i get an impression of it being an API call.  At other times i hear it's a form of app protection.

I'm trying to get an understanding of these things i once took for granted to be what my wild guest concluded.

Timbo

Greetings,

You basically have 2 types of procedures.  Function and subroutine.

A function is a procedure with an anticipated return value (in eax, for example, or other register).  Subroutines do not have an anticipated return value.

Hope this helps,

Timbo

hutch--

ic2,

Many of these questions break down to language only. In assembler you write PROCEDURES which can emulate many high level formats with common terms like FUNCTION, ROUTINE and so on. In most instances the difference is whether a value is returned or not and in fact if you need the return value. Usually its in EAX for an integer value but it can be in a FP register as well or even MMX or XMM registers.

With or without a stack frame only matters on very short procedures that are hit at high repeat rates, otherwise it does not matter much.

The notion of a "block of code" is tied to the processor capacity to CALL an address and return to the next instruction after that CALL when it executes a RETN instruction. Its bare form is like this.


label:
  ; write code here
  RETN [number to balance stack if needed]


This form can be with or without a stack frame and you can transfer data to it in any way ypu like, globals, registers, the stack etc ....

For what it is worth, a Windows API is probably best called a FUNCTION as it will be written in a language that called the block of code a FUNCTION. Usually C or C++.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

sinsi

Doesn't MASM take care of push/pop when you write a proc with USES?
So there is no need for you to push/pop.
Light travels faster than sound, that's why some people seem bright until you hear them.

hutch--

Most compilers do it as well but there are advantages in manually writing your own code here, especially if you use multiple exits from a procedure in that if you must preserve registers at the start, you have the option of jumping to one end location where USES duplicates the pops on every RETN.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

sinsi

Yes, but the original question was
Quote from: ic2 on April 12, 2007, 07:22:06 AM
Is this how USES is written out?   If not, how would I write it  and use it manually.
My point is that if you use USES then the push/pop isn't needed, so to use it you would do

MyProc PROC uses ebx edx
........
ret

and to do it manually you would do

MyProc PROC
push ebx
push edx
........
pop edx
pop ebx
ret


If you use multiple exits from a routine that uses registers, surely it's better to use USES and not have to keep typing "pop esi,ret" all the time?
(I was always warned about using multiple exits anyway...)
Light travels faster than sound, that's why some people seem bright until you hear them.

hutch--

This is why I made the comment,

> you have the option of jumping to one end location where USES duplicates the pops on every RETN.

You use a procedure tail end something like this,


  outa_here:
  pop etc ....
  retn


Then simply jump to the label from anywhere in the proc instead of the duplication of the stack correction for every instance of the RETN.

There are other reasons of course, with code like a traditional WndProc it is both simpler and more reliable to handle register preservation needs on a message by message basis rather than at the entry point of the procedure as the USES operator performs.

There are too many advantages of using the manual form over the USES operators, thats why I made the comment in the first place.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php