News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Two reciprocal algorithms, basic string to memory and memory to basic string

Started by hutch--, November 10, 2021, 07:51:07 PM

Previous topic - Next topic

hutch--

These two algorithms allow you to write basic dynamic string to allocate memory and later write the memory back to a basic dynamic string.

Basic dynamic string can contain ascii zeros which makes it suitable for writing binary data to allocated memory which in turn makes it useful to use external apps like a DLL written in another language or store data in a memory mapped file.


' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

  ' ---------------------------------------------------------------------------------------
  ' write basic dynamc string to allocated memory and allocated memory back to basic string
  ' basic string must be written to allocated memory before writing it back to basic string
  ' ---------------------------------------------------------------------------------------

    #compiler PBCC
    #compile exe "mtst.exe"
    #include "\basic\include\win32api.inc"

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain as LONG

    #REGISTER NONE

    LOCAL pMem as DWORD

    pMem = GlobalAlloc(%GMEM_FIXED,1024*1024*1024)      ' allocate 1 gig

  ' ---------------------------
  ' write string data to memory
  ' ---------------------------
    bas2mem repeat$(1000,"abcdefghijklmnopqrstuvwxyz"),pMem

  ' ---------------------------
  ' write memory to string data
  ' ---------------------------
    outp$ = mem2bas(pMem)

    GlobalFree pMem                                     ' release allocated memory

    StdOut outp$                                        ' display data at console
    waitkey$

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION bas2mem(bStr$,ByVal mem as DWORD) as DWORD

    #REGISTER NONE

    LOCAL slen as DWORD
    LOCAL pSrc as DWORD

    pSrc = StrPtr(bStr$)                                ' get the source address
    slen = len(bStr$)                                   ' get the string length+

    ! mov ebx, mem                                      ' memory address in ebx
    ! mov ecx, slen                                     ' string address in ecx
    ! mov DWORD PTR [ebx], ecx                          ' write length to 1st 4 bytes
    ! add ebx, 4                                        ' add 4 to ebx for data start location

    ! mov esi, pSrc                                     ' source in esi
    ! mov edi, ebx                                      ' destination in edi
    ! rep movsb                                         ' copy data AFTER string length

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION mem2bas(ByVal mem as DWORD) as STRING

    #REGISTER NONE

    LOCAL slen as DWORD
    LOCAL pDst as DWORD
    LOCAL pSrc as DWORD

    ! mov ebx, mem                                      ' memory address in ebx
    ! mov ecx, DWORD PTR [ebx]                          ' length in ecx
    ! mov slen, ecx                                     ' copy ecx to variable
    ! add ebx, 4                                        ' add 4 to get start location
    ! mov pSrc, ebx                                     ' load ebx into variable

    dst$ = nul$(slen)                                   ' create destination buffer
    pDst = StrPtr(dst$)                                 ' get its address

    ! mov esi, pSrc                                     ' source in esi
    ! mov edi, pDst                                     ' destination address into edi
    ! mov ecx, slen                                     ' string length back into ecx
    ! rep movsb                                         ' copy data back to basic string

    FUNCTION = dst$

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤