News:

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

Any Table for Registers used in Functions ?

Started by Rainstorm, November 28, 2006, 11:10:37 AM

Previous topic - Next topic

Rainstorm

Hi.

Is there any Table that can be looked up to see which Registers are used in the Procedures, macros &
Functions in Masm32 ?

primarily from the point of view of having to push/pop them in my code.

thanks

gabor

Hi!

I guess the same register preservation is used in the masm libs and the win32api. The api preserves ebx, edi and esi. The return value is stored in eax; ecx and edx are not preserved.
So, I suppose the table would contain preservation info about ecx and edx only. Did you think of something like this?

Greets, Gábor

hutch--

Rainstorm,

The trick is to use the OS specified register preservation rules as the entire masm32 project does this so that it is reliable.

ESP and EBP are preserved between calls if no stack frame is used and if they are used in a procedure, EBX ESI and EDI are preserved as well. It sounds complicated but its not in practice. If you use a normal stack frame (PROC in MASM) ESP and EBP are handled for you. If you use any of EBX ESI or EDI, preserve and restore them in the normal reverse order.


yourproc proc args etc .....

    push ebx
    push esi
    push edi

  ; write your code here

    pop edi
    pop esi
    pop ebx

yourproc endp


Note that if you don't use any of these three registers you don't have to preserve them but the still must be preserved and restored in reverse order if you use any of them.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

thanks for your replies..

one reason i asked if there was a table is because some of the procedures don't use all the registers (i.e eax, ecx & edx) - so with the table i would immediately know what to push & pop, & could do that just for those registers

- another reason is sometimes ebx seems to be used in the masm32 procedures. (at least it seems that way or i may be wrong  & i need to push/pop it)
usually when am trying out code, it takes some time for me untill i finally realise that its a reg preservation issue that's causing the prob (& i don't get an assembly error) just that the code won't work like its supposed to.

in this example it took me a while to realise i needed to push/pop eax

Quote.code
start:
mov eax, 2852126880
mov esi, 0

@@:

shl eax, 1
setc bl
movzx ebx, bl

push eax

print "carry flag = "
print ustr$(ebx),13,10

pop eax
add esi, 1
cmp esi, 15
jnz @b

inkey
exit
end start

ty

hutch--

This is the basic logic of what you are doing, I have fixed the register preservations so they are safe calling other procedures.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    show_carry PROTO :DWORD

    .code

start:

    invoke show_carry,2852126880
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

show_carry proc value:DWORD

    push ebx                    ; preserve the three used registers
    push esi
    push edi

    mov edi, value
    xor esi, esi                ; set ESI to zero

  @@:
    shl edi, 1
    setc bl
    movzx ebx, bl

  ; --------------------------------------------------------------
  ; you don't have to preserve any registers as the proc used with
  ; "print" already observes the rule on register preservation.
  ; --------------------------------------------------------------
    print "carry flag = "
    print ustr$(ebx),13,10

    add esi, 1
    cmp esi, 15
    jnz @b

    pop edi                     ; restore the three used registers
    pop esi
    pop ebx

    ret

show_carry endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start


Where you need to preserve any other registers in within the procedure that calls another procedure (in this cae the proc that "print" uses) as it can ALSO freely modify EAX ECX & EDX.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

PBrennick

Rainstorm,
Remember this, since EAX is the usual return value from an API, you need to preserve that register before every call if you wish to carry an existing value in EAX past the API all. It is important to never expect a library procedure to preserve EAX, either as a lot of them, also, store a return value in EAX (error errata, mostly). So, in general, unless you want to start wading through source code written by others (assuming it is available), always assume the worst and preserve what you do not want to lose. As I can see from your example, the same rule applies to macro call, also.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Human

true, on 64bit cpu's there is more to preserve a lot of new r08-r15 regs. due they are used for params no more pushing

Rainstorm

thanks for that example hutch...

thanks all for your replies

-