Author Topic: Determining the length of a Unicode string  (Read 3700 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1710
Determining the length of a Unicode string
« on: July 14, 2012, 06:50:43 AM »
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

jj2007

  • Member
  • *****
  • Posts: 7628
  • Assembler is fun ;-)
    • MasmBasic
Re: Determining the length of a Unicode string
« Reply #1 on: July 14, 2012, 08:24:51 AM »
Hi Erol,

Very nice code, 30 bytes only :t

I know almost nothing about PoAsm - can it handle the timings macros? I attach a testbed with your code.

All the best,
Jochen

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1710
Re: Determining the length of a Unicode string
« Reply #2 on: July 14, 2012, 08:34:21 AM »
Hi Jochen,

Thanks for the code. Poasm does not support function prologue \ epilogue and it's macro syntax has some differences. The Masm macros should be modified to function with Poasm.

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1710
Re: Determining the length of a Unicode string
« Reply #3 on: July 16, 2012, 04:10:15 AM »
Not the best algo but this one does the job too :

Code: [Select]
UniStrLen PROC _string:DWORD

    mov     eax,_string
    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,_string
    shr     eax,1
    ret

UniStrLen ENDP

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1710
Re: Determining the length of a Unicode string
« Reply #4 on: January 20, 2013, 08:01:28 PM »
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