Nine Years of Service
Posts: 98
Threads: 10
Points: 0€
RE: [FASM] Compare 2 strings 12-04-2016, 12:57 AM
#2
(12-02-2016, 12:39 AM)Jochen Wrote: Code:
include 'win32ax.inc'
str1 db '1111111111',0
str2 db '1111111112',0
start:
stdcall CmpStr,str1,str2
test eax,eax
jz not_the_same
invoke MessageBox,0,'Both strings are alike!!','FASM',MB_OK
jmp exit
not_the_same:
invoke MessageBox,0,'Both strings are Wrong','FASM',MB_OK
exit:invoke ExitProcess,0
proc CmpStr uses esi edi ecx,str1,str2; returns 0 when strings are not the same.
cld
mov ecx,0ah
mov esi,[str1]
mov edi,[str2]
repe cmpsb
jnz @f
mov eax,-1
ret
@@:
xor eax,eax
ret
endp
.end start
I would just do:
Code:
mov ecx,cStr
lea edx,[ecx+1h]
next:
mov al,[ecx]
inc ecx
test al,al
jne next
sub ecx,edx
; ecx = strlen
^ full manual approach using pointer arithmetic and comparing null-byte with test.
Essentially my approach was to load the address of the string into ecx, load the address of the string offset by 1 into edx (because of the last inc to ecx before the test), compare bytes from ecx register by moving them into lower eax (al) 8-bit segment, then subtract edx (string address + 1) from ecx to get the length. In the case of "", ecx is still incremented, which will match edx+1 to return 0.
(This post was last modified: 12-04-2016, 01:18 AM by bitm0de.)
- mostly braindead monkeys on this forum.
Nine Years of Service
Posts: 98
Threads: 10
Points: 0€
RE: [FASM] Compare 2 strings 12-05-2016, 06:28 PM
#4
(12-05-2016, 10:30 AM)Jochen Wrote: Quote:I would just do:
Code:
mov ecx,cStr
lea edx,[ecx+1h]
next:
mov al,[ecx]
inc ecx
test al,al
jne next
sub ecx,edx
; ecx = strlen
^ full manual approach using pointer arithmetic and comparing null-byte with test.
Essentially my approach was to load the address of the string into ecx, load the address of the string offset by 1 into edx (because of the last inc to ecx before the test), compare bytes from ecx register by moving them into lower eax (al) 8-bit segment, then subtract edx (string address + 1) from ecx to get the length. In the case of "", ecx is still incremented, which will match edx+1 to return 0.
Hi bitm0de: Your talking about getting the length of a string ?
For that i use this atm
Code:
proc ; strlen, strInput
mov ecx,-1
mov al,0
mov edi,[strInput]
cld
repne scasb
not ecx
dec ecx
ret
endp; String length is in ECX
Ahh yeah. Correct. Change test to a cmp and increment an offset to another memory space for comparison. I don't know why I read this as Strlen rather than Compare... :S This would be the naive version of it though, the actual implementation of strlen() is not so simple, and involves a lot of bitwise arithmetic tricks:
https://sourceware.org/git/?p=glibc.git;...8f68425dc2
My ASM is very small byte size though too.
You like using all of the special string instructions over doing things manually don't you?
(This post was last modified: 12-05-2016, 06:30 PM by bitm0de.)
- mostly braindead monkeys on this forum.
Nine Years of Service
Posts: 98
Threads: 10
Points: 0€
RE: [FASM] Compare 2 strings 12-06-2016, 03:53 AM
#6
(12-06-2016, 01:09 AM)Jochen Wrote: Quote:You like using all of the special string instructions over doing things manually don't you?
I like to learn new things. I find ASM interesting because you can understand the inner workings of things ![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
I first started out with Delphi. I would like to learn C/C++. If you want to create big stuff you can't always rely on ASM.
Delphi create big PE files with many sections. I believe you can control that in C/C++ ? (FileSize) etc ..
And i always can use inline ASM under C/C++ to.. BitmOde can you give me a few tips to get started ? What compiler do you use ? Btw i dont like Visual Studio ![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
The language has less to do with PE output and it's more compiler based. Saying you can achieve better control over file alignment and section size is not a programming language dependent matter IMO. You can always manipulate the PE after it's gone through compilation.
Visual Studio is an IDE though but if you're on Windows, it would be my suggestion for C++, if you want to do C, then just grab a build of MinGW for gcc.
- mostly braindead monkeys on this forum.