News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Pseudo push & pop: not thread save case.

Started by HSE, May 15, 2019, 11:18:08 AM

Previous topic - Next topic

HSE

Hi Hutch!

I was going to have a little problem to make my 32 bit source code to run in 64 bit because so many puhs and pops.

Fortunatelly some qWord ideas are working to solve that problem! Here some rudimentary emulation of push and pop storing values in .data? section (not thread save!).

Test:main proc
    freg_reset           <-- requiered (in general, not in this example)
    mov rbx, 200
    mov rax, 50
    freg_push rax
    freg_push rbx
    freg_push ecx
   
    mov rax, 100
    mov rbx, 0
   
    freg_pop ecx
    freg_push rax
    freg_pop rax
   
    conout "rbx",tab
    freg_pop rbx
    conout str$(rbx),lf

    conout "rax", tab
    freg_pop rax
    conout str$(rax),lf

    ret
main endp


Macros:freg_push macro reg:REQ
    IFNDEF freg_line
freg_line = @Line
    ENDIF
@fregTLS_warning
freg_bool_byte TEXTEQU get_unique_reglbl(<freg_bool_byte_>)
       if unique_reg_cntr GT maximum_reg_cntr
           IF freg_line NE @Line
        freg_line = @Line
        .data?
               align 4
        .code
          ENDIF
       .data?
           freg_bool_byte db 8 dup (?)
           .code
           maximum_reg_cntr = unique_reg_cntr
       endif

    if TYPE(reg) eq BYTE
        mov byte ptr freg_bool_byte , reg
    elseif TYPE(reg) eq WORD
        mov word ptr freg_bool_byte , reg
    elseif TYPE(reg) eq DWORD
        mov dword ptr freg_bool_byte , reg
    elseif TYPE(reg) eq QWORD
        mov qword ptr freg_bool_byte , reg
    else
        .err reg is more than qword
    endif
endm

freg_pop macro reg:REQ

    freg_bool_byte TEXTEQU get_last_reglbl(<freg_bool_byte_>)
    set_previous_reglbl

    if TYPE(reg) eq BYTE
        mov reg, byte ptr freg_bool_byte
    elseif TYPE(reg) eq WORD
        mov reg, word ptr freg_bool_byte
    elseif TYPE(reg) eq DWORD
        mov reg,dword ptr freg_bool_byte
    elseif TYPE(reg) eq QWORD
        mov reg,qword ptr freg_bool_byte
    else
        .err reg is more than qword
    endif
endm



The "thread save" case will take a little  :biggrin:

UPDATED:

   This macros could use less memory than preserve_regs/restore_regs but requiere freg_reset in each proc  where macros are going to be used.
Equations in Assembly: SmplMath