News:

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

Basic emulation space$() macro.

Started by hutch--, June 20, 2008, 08:13:37 AM

Previous topic - Next topic

hutch--

I have been porting some basic code of late and this one helped to make some of it easier and faster.


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

    charfill PROTO :DWORD,:DWORD,:DWORD

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

; EXAMPLE

    local spc$  :DWORD          ; DWORD handle for string
    .....
    mov spc$, space$(1024)      ; allocate a space filled string
    .....
    free$ spc$                  ; deallocate it when finished

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

    space$ MACRO bcnt
      push esi
      mov esi, alloc$(bcnt)
      invoke charfill,esi,bcnt,32
      mov eax, esi
      pop esi
      EXITM <eax>   ;; dealloc with free$
    ENDM

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

    align 16

charfill proc buff:DWORD,bcnt:DWORD,char:DWORD

    mov ecx, [esp+4]        ; data address
    mov edx, [esp+8]        ; fill count
    mov eax, [esp+12]       ; fill character
    sub ecx, 1

  @@:
    add ecx, 1
    mov [ecx], al
    sub edx, 1
    jnz @B

    ret 12

charfill endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

NightWare

hi,
it should be a bit faster if you accept 07FFFFFFFh as max value for fill count :
    mov ecx, [esp+4]        ; data address
    mov edx, [esp+8]        ; fill count
    mov eax, [esp+12]       ; fill character
    add ecx,edx
    neg edx
  @@:
    mov [edx+ecx], al
    add edx, 1
    jnz @B