flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
thecf 17 Feb 2007, 17:04
which is the fastest way to do a byte comparison in a big loop?
Code: cmp byte [esi+ecx],0 je @f or mov al,byte [esi+ecx] test al,al jz @f |
|||
![]() |
|
thecf 17 Feb 2007, 17:32
ok I'm writing a string length function heres the code:
Code: ; in ; esi = string ; out ; ecx = string length strlen: xor ecx,ecx @@: cmp byte [esi+ecx],0 je @f inc ecx jmp @b @@: ret |
|||
![]() |
|
tantrikwizard 17 Feb 2007, 18:37
thecf wrote: ok I'm writing a string length function heres the code: It should work fine, theres many alternatives to string length functions. Heres one of mine: Code: ;in ;edi = pointer to string string ;out ;eax = string length _strlen: mov ecx, 0xffffffff xor eax, eax cld rep scasb mov eax, 0xffffffff sub eax, ecx ret |
|||
![]() |
|
asmfan 17 Feb 2007, 18:52
OK, suggestions:
prefer registers over immediates, immediates over memory - store 0 in register |
|||
![]() |
|
LocoDelAssembly 17 Feb 2007, 19:12
Just an alternative but not sure if it's faster
Code: ;in ;edi = pointer to string string ;out ;eax = string length _strlen: mov ecx, -1 xor eax, eax repne scasb not ecx ; Uncomment the line below if you don't want to include the NULL char ; dec ecx ret The one I'm using in a small project is "inlined" and is this Code: xor ecx, ecx xor eax, eax not ecx repne scasb not ecx dec ecx |
|||
![]() |
|
MCD 17 Feb 2007, 23:17
tantrikwizard wrote:
you mean scasb? |
|||
![]() |
|
IceStudent 21 Feb 2007, 19:18
if you want realy faster code, you should see on this function (from C runtime library):
Code: public strlen ; int strlen (const char * str) strlen: mov ecx,[esp+4] ; ecx -> string test ecx,3 ; test if string is aligned on 32 bits je .main_loop .str_misaligned: ; simple byte loop until string is aligned mov al,byte [ecx] add ecx,1 test al,al je .byte_3 test ecx,3 jne .str_misaligned add eax,dword 0 ; 5 byte nop to align label below align 16 ; should be redundant .main_loop: mov eax,dword [ecx] ; read 4 bytes mov edx,7efefeffh add edx,eax xor eax,-1 xor eax,edx add ecx,4 test eax,81010100h je .main_loop ; found zero byte in the loop mov eax,[ecx - 4] test al,al ; is it byte 0 je .byte_0 test ah,ah ; is it byte 1 je .byte_1 test eax,00ff0000h ; is it byte 2 je .byte_2 test eax,0ff000000h ; is it byte 3 je .byte_3 jmp .main_loop ; taken if bits 24-30 are clear and bit ; 31 is set .byte_3: lea eax,[ecx - 1] mov ecx,[esp+4] sub eax,ecx retn .byte_2: lea eax,[ecx - 2] mov ecx,[esp+4] sub eax,ecx retn .byte_1: lea eax,[ecx - 3] mov ecx,[esp+4] sub eax,ecx retn .byte_0: lea eax,[ecx - 4] mov ecx,[esp+4] sub eax,ecx retn |
|||
![]() |
|
rugxulo 26 Feb 2007, 20:18
Okay, this is probably not useful for most of you, but it works under DOS (or compatibles: e.g., DR-DOS, Win XP, etc.). The length in CX includes the terminating NUL. (No idea why there are two functions for the same thing.)
Ralf Brown wrote:
Ralf Brown wrote:
|
|||
![]() |
|
f0dder 27 Feb 2007, 00:17
Fast string length?
Code: mov eax, [edi.string.length] ![]() The question of fast string length routines have been brought up countless times anyway, and it depends on various factors. For small/medium sized strings, one of the best approaches is the dword/masking thing that I first saw in agner fog's optimization manual. For longer strings, MMX or SSE. |
|||
![]() |
|
Madis731 27 Feb 2007, 11:39
http://www.azillionmonkeys.com/qed/asmexample.html (5.) and check the update from Norbert Juffa's discovery - this can be implemented to detect ANY byte value (except there was a problem with value 80h or 7Fh, can't remember).
|
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.