NO

Author Topic: LeftTrim function  (Read 3956 times)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
LeftTrim function
« on: May 01, 2012, 08:13:33 PM »
Here is a LeftTrim function example. It reads a NULL terminated string and stops a the first non space or TAB character.

Code: [Select]

.386
.model flat,stdcall
option casemap:none

includelib  \PellesC\lib\Win\kernel32.lib
includelib  \PellesC\lib\Win\user32.lib

ExitProcess PROTO :DWORD
LeftTrim    PROTO :DWORD
StdOut      PROTO :DWORD

.data

LeftTrimTable   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

teststr     db '   This is a test',0

.code

start:

    invoke  LeftTrim,ADDR teststr
    invoke  StdOut,eax
    invoke  ExitProcess,0


LeftTrim PROC USES esi _str:DWORD

    mov     edx,1
    mov     eax,_str
    mov     esi,OFFSET LeftTrimTable
    sub     eax,edx
@@:
    add     eax,edx
    movzx   ecx,BYTE PTR [eax]
    cmp     BYTE PTR [esi+ecx],dl
    je      @b
    ret

LeftTrim ENDP

END start
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: LeftTrim function
« Reply #1 on: May 02, 2012, 08:12:46 PM »
Here is another version without the the lookup table :

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

includelib  \PellesC\lib\Win\kernel32.lib
includelib  \PellesC\lib\Win\user32.lib

ExitProcess PROTO :DWORD
LeftTrim    PROTO :DWORD
StdOut      PROTO :DWORD

.data

teststr     db '   This is a test',0

.code

start:

    invoke  LeftTrim,ADDR teststr
    invoke  StdOut,eax
    invoke  ExitProcess,0


LeftTrim PROC _str:DWORD

    mov     edx,1
    mov     eax,_str

    sub     eax,edx
@@:
    add     eax,edx
    movzx   ecx,BYTE PTR [eax]
    cmp     ecx,32
    je      @b
    cmp     ecx,9
    je      @b
    ret

LeftTrim ENDP

END start
Code it... That's all...