| | |
Comparing Strings
Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
For a program I'm making the user needs to enter a string and then it will be checked to see if it is a palindrome. So far I've managed to reverse the ordering of the string entered so that I have the original string and the reverse version of it.
The problem is that I don't know how to compare these two strings.
Please if you can help me I would appreciate it.
The problem is that I don't know how to compare these two strings.
Please if you can help me I would appreciate it.
Assembly Syntax (Toggle Plain Text)
CR equ 13 LF equ 10 JMP START prompt db CR,LF,'Please enter a word to be checked: $' outmsg db CR,LF,'The reverse word is: $' instring db 12,?,' $' outstring db '????????????$' correct db CR,LF,'The word that you have entered is a palindrome$' incorrect db CR,LF,'The word that you have entered is not a palindrome$' firstchar dw ? START: LEA DX,prompt MOV AH,9 INT 21h LEA DX,instring MOV AH,0ah INT 21h LEA SI,instring MOV BL,byte ptr[SI + 1] INC BL MOV BH,0 ADD SI,BX LEA firstchar,instring+2 LEA DI,outstring MOVECHAR: MOV BH,byte ptr[SI] MOV byte ptr[DI],BH CMP SI,firstchar JE DONE DEC SI INC DI JMP MOVECHAR DONE: INC DI MOV byte ptr[DI],'$' LEA DX, outmsg MOV AH,9 INT 21h LEA DX,outstring MOV AH,9 INT 21h FINISH: MOV AH,4ch INT 21h
•
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
The following will do it:
For the CMPSB instruction, cx = number of bytes to compare, es:di -> one string, ds:si -> the other string, REPE means continue to compare bytes while they are equal; abort as soon as they are not. REPNE would mean continue to compare bytes while they are not equal, abort as soon as they are. There is a similar instruction CMPSW for comparing words, and also in similar vein are:
movsb/mosw
stosb/stosw
lodsb/lodsw ;always used to load single byte/word into al/ax
Assembly Syntax (Toggle Plain Text)
xor ch,ch ;clear ch lea di, instring mov cl, [ di + 1 ] ;cx = length of string mov di,ds mov es,di ;es -> data segment lea di, outstring ;es:di -> output string mov si, offset instring + 2 ;ds:si -> input string repe cmpsb ;compare strings (abort as soon as two ;characters fail to match) je ;jump to relevant code if strings are the same
For the CMPSB instruction, cx = number of bytes to compare, es:di -> one string, ds:si -> the other string, REPE means continue to compare bytes while they are equal; abort as soon as they are not. REPNE would mean continue to compare bytes while they are not equal, abort as soon as they are. There is a similar instruction CMPSW for comparing words, and also in similar vein are:
movsb/mosw
stosb/stosw
lodsb/lodsw ;always used to load single byte/word into al/ax
Last edited by mathematician; Nov 30th, 2006 at 6:42 pm.
![]() |
Similar Threads
- Assembly language comparing strings (Assembly)
- comparing strings (C++)
- Error comparing strings from arrays (PHP)
- comparing strings (C++)
- Comparing Strings in C# (C#)
- comparing two strings with linear search.. (Java)
Other Threads in the Assembly Forum
- Previous Thread: help me see what i'm doing wrong
- Next Thread: get the file size?
Views: 4851 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Assembly
3d 68hc11 6811 80386 :( adress array asm assembler assembly boot bootloader buffer compression cursor debug directory division docs dos draw emulator endtask error exceptions file int10h integer intel interrupt interrupts language loop newbie nohau osdevelopment print program range read remainder shape string text theory tsr x86





