what is the purpose of "use" in pocedure/function?
how can i declare LOCAL byte array of size 1024 with null balues(szbuff db 1024 dup(0)) in my proc
i.e LOCAL szByte:BYTE????
how can i declare LOCAL byte array of size 1024 with null balues(szbuff db 1024 dup(0)) in my proc
i.e LOCAL szByte:BYTE????
I have never used the "Use" keyword in my assembly programs but i have certainly seen a lot of examples that have. I *think* when for example declaring "Use EDX, EBX" the assembler would automatically push those two registers onto the stack when it assembles the procedure and pops them off of the stack before the RET or the RETF instructions.
To allocate space in the stack for local procedures or functions, you have create a stack frame. At the beginning of your procedure, push the base pointer (BP/EBP) onto the stack and then move the stack pointer (SP/ESP) to the base pointer (BP/EBP) and then subtract the value of the stack pointer (SP/ESP) by the number of bytes you want to allocate locally. The example below is written is a 16bit code which allocated N bytes of space in a procedure where N is indicated by the AX register.
To allocate space in the stack for local procedures or functions, you have create a stack frame. At the beginning of your procedure, push the base pointer (BP/EBP) onto the stack and then move the stack pointer (SP/ESP) to the base pointer (BP/EBP) and then subtract the value of the stack pointer (SP/ESP) by the number of bytes you want to allocate locally. The example below is written is a 16bit code which allocated N bytes of space in a procedure where N is indicated by the AX register.
Posted on 2006-09-01 05:00:08 by XCHG
First, it's "uses" and not "use". The purpose is indeed to save registers, and you use :) it when you overwrite ESI, EDI or EBX in your proc (since those have to be preserved according to the Intel ABI).
The advantage of "uses" versus manual push/pop is that MASM will, by default, add pushing and popping code for you. The pretty smart thing is that pop code won't just be added at the proc end - it will be added where you use the "ret" instruction. (Yes, "RET" is thus a macro, you need "RETN" if you want the raw instruction).
Thus, you can have multiple procedure exit points without too much hassle.
As for your array thing, try the following. Note that local variables will NOT automatically be zeroed, but will have "whatever value happens to be on the stack" - so you need to do the zeroing yourself, if it's necessary.
The advantage of "uses" versus manual push/pop is that MASM will, by default, add pushing and popping code for you. The pretty smart thing is that pop code won't just be added at the proc end - it will be added where you use the "ret" instruction. (Yes, "RET" is thus a macro, you need "RETN" if you want the raw instruction).
Thus, you can have multiple procedure exit points without too much hassle.
As for your array thing, try the following. Note that local variables will NOT automatically be zeroed, but will have "whatever value happens to be on the stack" - so you need to do the zeroing yourself, if it's necessary.
hellohello PROC
local buf[1024]:byte
hellohello ENDP
thks f0dder and xchg(once again g8t)
f0dder: Sorry to piggyback on this thread, but it's kind of relevant. Where can I find the macro declarations for USES and RET in masm32? Or are they handled internally?
another good thing about USES is that it allows you to RET even after you left something on stack.
is
and this code crashes
but with uses:
code becomes:
this code works allright
that means, that in second case you don't have to pop things you pushed from stack.
sorry if this code doesn't work, i am not MASM programmer
a PROC
push ebx
...
push 1234
jmp return
...
return:
pop ebx
RET
a ENDP
is
a:
push ebp
mov ebp, esp
push ebx
...
push 1234
jmp return
...
return:
pop ebx
mov esp, ebp
pop ebp
and this code crashes
but with uses:
a PROC USES ebx
...
push 1234
jmp return
...
return:
RET
a ENDP
code becomes:
push ebp
push ebx
mov ebp, esp
...
push 1234
jmp return
...
return:
mov esp, ebp
pop ebx
pop ebp
this code works allright
that means, that in second case you don't have to pop things you pushed from stack.
sorry if this code doesn't work, i am not MASM programmer
Timbo: it is handled internally. If you want a "direct" ret, use "retn" instead.
Thanks. I thought it might be. I always thought it was a little odd that all my RET instructions *magically* appeared as RETN in ollydbg, but now I get it.