NO

Author Topic: Calculating the length of a Unicode string  (Read 5509 times)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Calculating the length of a Unicode string
« on: June 02, 2012, 07:39:56 PM »
Here is a quick example :

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

ExitProcess PROTO :DWORD

includelib  \PellesC\lib\Win\kernel32.lib

UniStrLen   PROTO :DWORD

.data

str1        dw 'This is a test',0

.code

start:

    invoke  UniStrLen,ADDR str1
    invoke  ExitProcess,0

UniStrLen PROC _string:DWORD

    mov     eax,_string
    mov     ecx,2
    sub     eax,ecx
@@:
    add     eax,ecx
    cmp     WORD PTR [eax],0
    jne     @b
    sub     eax,_string
    shr     eax,1
    ret

UniStrLen ENDP

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

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: Calculating the length of a Unicode string
« Reply #1 on: March 23, 2013, 11:54:44 AM »
Another version :

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

ExitProcess PROTO :DWORD

includelib  \PellesC\lib\Win\kernel32.lib

.data

str1        dw 'This is a test.',0

.code

start:

    push    OFFSET str1
    call    UniStrLen
    invoke  ExitProcess,0

align 16

UniStrLen:

    mov     eax,DWORD PTR [esp+4]
    mov     ecx,4
    sub     eax,ecx
@@:
    add     eax,ecx
    mov     edx,DWORD PTR [eax]
    test    dx,dx
    je      @f
    test    edx,0FFFF0000h
    jnz     @b
    add     eax,2
@@:   
    sub     eax,DWORD PTR [esp+4]
    shr     eax,1
    ret     4

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

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: Calculating the length of a Unicode string
« Reply #2 on: November 26, 2016, 10:46:36 AM »
Another one :

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

ExitProcess PROTO :DWORD
UniStrLen   PROTO :DWORD

includelib  \PellesC\lib\Win\kernel32.lib

.data

str1        dw 'This is a test.',0

.code

start:

    invoke  UniStrLen,ADDR str1
    invoke  ExitProcess,0


OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

UniStrLen PROC string:DWORD

    push    ebx
    mov     ebx,0FFFF0000h
    mov     eax,DWORD PTR [esp+8]
    mov     ecx,4
    sub     eax,ecx
@@:
    add     eax,ecx
    mov     edx,DWORD PTR [eax]
    test    dx,dx
    je      @f
    test    edx,ebx
    jnz     @b
    add     eax,2
@@:   
    sub     eax,DWORD PTR [esp+8]
    shr     eax,1
    pop     ebx
    retn    4

UniStrLen ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

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