Author Topic: Pseudo push & pop: not thread save case.  (Read 359 times)

HSE

  • Member
  • *****
  • Posts: 1079
  • <AMD>< 7-32>
Pseudo push & pop: not thread save case.
« on: May 15, 2019, 11:18:08 AM »
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:
Code: [Select]
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:
Code: [Select]
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.
« Last Edit: May 16, 2019, 01:48:02 AM by HSE »