Author Topic: LeftTrim function  (Read 59 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1934
LeftTrim function
« on: April 15, 2019, 05:11:44 AM »
Here is a LeftTrim example. This function removes spaces and TABs from the beginning of a string.

Code: [Select]
.386
.model flat,stdcall
option casemap:none

.data

ltable db 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

LeftTrim PROC _str:DWORD

    mov     eax,DWORD PTR [esp+4]
    mov     ecx,OFFSET ltable
    sub     eax,1
@@:
    add     eax,1   
    movzx   edx,BYTE PTR [eax]
    cmp     BYTE PTR [ecx+edx],1
    je      @b
@@:   
    retn    4

LeftTrim ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF
END

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1934
Re: LeftTrim function
« Reply #1 on: April 20, 2019, 04:54:57 AM »
RightTrim function :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

StrLen PROTO :DWORD

.data

ltable db 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
       db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

RightTrim PROC _str:DWORD

    mov     eax,DWORD PTR [esp+4]
    invoke  StrLen,eax
    add     eax,DWORD PTR [esp+4]
    mov     ecx,OFFSET ltable
@@:
    sub     eax,1
    movzx   edx,BYTE PTR [eax]
    cmp     BYTE PTR [ecx+edx],1
    je      @b
@@:
    add     eax,1
    mov     BYTE PTR [eax],0
    mov     eax,DWORD PTR [esp+4]
    retn    4

RightTrim ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END