That's great Relvinian it's working.
I changed it to:
long _cdecl GetCharOffset(void* str1, void* str2)
{
long s = SCALE;
_asm
{
mov esi, [str1]
mov edi, [str2]
mov ecx, 0
mov eax, ecx
LoopStart:
cmp byte ptr [esi+ecx], 0
je EndLoop
cmp byte ptr [edi+ecx], 0
je EndLoop
inc ecx
mov al, byte ptr [esi+ecx-1]
mov dl, byte ptr [edi+ecx-1]
cmp al, dl
je LoopStart
EndLoop:
mov eax, ecx
}
}
However I'm unsure about:
How it knows what the size of the individual elements it's comparing are. They are presumably bytes?
If they are bytes how can I make them DWORDs until it finds a different one, then make
them bytes again? (i'm a beginner so please explain it...)
----- Relvinian wrote: -----
This isn't the most efficient ASM method but it'll do your job. This is
MASM format. From here, you can play with it and learn the hows and whys.
; prototype
;int GetCharOffset(void* array1, void* array2);
GetCharOffset proc uses esi edi A1:dword, A2:dword
mov esi, [A1]
mov edi, [A2]
mov ecx, 0
Loop:
cmp byte ptr [esi+ecx], 0
je EndLoop
cmp byte ptr [edi+ecx], 0
je EndLoop
inc ecx
cmp byte ptr [esi+ecx-1], byte ptr [edi+ecx-1]
jne Loop
EndLoop:
mov eax, ecx
ret
GetCharOffset endp
Beeeeeves said:
For the masm guys, which I hope will be able to make mincemeat out
of
this,
please read from the bottom up. It would make my day.
But this is where I'm up to with 'the pecker languages'. looking
for and
skip
a) the string won't be 4-byte aligned, and
b) it should still work if it is, shouldn't it? strings
(including how to create the 4-byte aligned strings in the first place)
that shows an incorrectness.
Or better still how to write it in assembly language...
That
was
with the DLL in release mode.)
Anyone able to beat this with some assembly language?
#define SCALE (long)(sizeof(long) / sizeof(_TCHAR))
long __cdecl FirstDifferentIndex(_TCHAR* str1, _TCHAR* str2)
{
long minlen = min((long)_tcslen(str1), (long)_tcslen(str2)),
minlongs = minlen / SCALE,
i = 0, t = 0;
long* skipper[] = {(long*)str1, (long*)str2};
for(;(i < minlongs) && (skipper[0] == skipper[1]); i++);
for(t = i * SCALE; (t < minlen) && (str1[t] == str2[t]); t++);
return t == minlen ? -1 : t;
}
long _cdecl AmountSameAtEnd(_TCHAR* str1, _TCHAR* str2)
{
long len[] = {(long)_tcslen(str1), (long)_tcslen(str2)},
off[] = {len[0] % SCALE, len[1] % SCALE},
pos[] = {(len[0] - off[0])/SCALE, (len[1] - off[1])/SCALE},
t = 0,
* skipper[] = {
(long*)(str1 + SCALE - off[0]),
(long*)(str2 + SCALE - off[1])};
for(;(pos[0] >= 0) && (pos[1] >= 0) &&>>>> (skipper[0][pos[0]] == skipper[1][pos[1]]);
pos[0]--, pos[1]--);
long fin[] = {
min((pos[0] + 1) * SCALE - 1, len[0] - 1),
min((pos[1] + 1) * SCALE - 1, len[1] - 1)};
for(;(fin[0] >= 0) && (fin[1] >= 0) && (str1[fin[0]] == str2[fin[1]]);
fin[0]--, fin[1]--);
return len[0] - fin[0] - 1; //should be the same as len[1] - fin[1] - 1
}
In a 32 bit environment, the fastest approach would be find the 4-byte
boundary and start reading and comparing 4 bytes at a time. When a
mismatch
is found, you'll dig in and compare them at byte/word level
(depending
looping