1,105,625 Community Members

Palindrome

Member Avatar
Vongola_Takeshi
Newbie Poster
21 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Good day ^^

I want to create a program using tasm that will tell if the inputted string is a palindrome or not....but i don't know how will i get the first and last char to compare..unlike in C..i can use the strlen....XD

Please make the codes very simple ^^..tnx in advance XD

Member Avatar
sDJh
Posting Whiz in Training
268 posts since Aug 2005
Reputation Points: 39 [?]
Q&As Helped to Solve: 32 [?]
Skill Endorsements: 0 [?]
 
0
 

Whole codes? Well hint's are okay, but I won't cause fuss and try to figure out a completely running programme.

Think about how you determine the length of a string? Null-terminated? Fine.
I'd start like that:
Run through the string, till \0 comes. Decrease the register (let's call it the backcounter). Now run a second loop till the \0-character. Now compare the character of your backcounter with the one of your forward-counter. After each step you're decreasing the backcounter and increase the forward-counter.

That should be all.
Hope it helps.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You could start out with getting the user input of the word and then figure out the word length based on the left-over terminators.

Start with something like this:
http://www.daniweb.com/software-development/assembly/code/270832

...then count the characters based on the start position until you run into a terminator.
After that, you can copy and reverse the word and compare it.

Member Avatar
Vongola_Takeshi
Newbie Poster
21 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Actually I know what should i do to tell if it is palindrome or not.. I already done it in turbo c but my problem is what is the equivalent code of strlen (to get the number of char) of tc in TASM..... i dont know how to get each character in tasm that is my only problem...XD

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

[Just getting string length]
Assuming a 10-char max string and a specific location,
this code compares the target string against a string of terminating dollar-signs.

;This determines a string length by
; producing a reverse count and subtracting
mov cx, 10	; Max string length
mov dx, 9	; one less than max (offset for $)
mov si, string1
mov di, string2
repne cmpsb
sub dx, cx	; Actual string length in DX

int 20h
string1:	db 'One$$$$$$$'
string2:	db '$$$$$$$$$$'
Member Avatar
Vongola_Takeshi
Newbie Poster
21 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for all the help...me and my two other classmates successfully finished the program ^^..XD

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

Great!
Here's what I hacked out (FYI):

;-------------------------------80x86----------------------------------------
; 8-Aug-2011 - THINES01
; Palindro.asm: to be compiled with A86 (http://eji.com/a86/)
; This program shows how to get user input with echo
; and test to see if it is a palindrome
; ---------------------------------------------------------------------------
        mov dx, askString;
        call doPrint
        mov cx, 0ah
        mov si, strOrig;                ; Place to store the orig string
getString:
        mov ah, 01h
        int 21h                         ; Get input with echo
        
        cmp al, 0dh                     ; 
        je printQuit                    ; Quit if Carriage-return
        
        mov [si], al                    ; Put entered char in orig string
        
        cmp byte ptr [finalStop], 24h   ; is 10th dollar-sign gone?
        jne printQuit                   ; if so, buffer is full?, quit
        
        inc si                          ; move the position
        loop getString;                 ; Keep going (looping)
printQuit:
        mov dx, youEntered              ;
        call doPrint                    ; Print the finished message
; ---------------------------------------------------------------------------
strLen:
        mov cx, 0bh                     ; max string length (plus terminator)
        mov dx, 0ah                     ; minus 1
        mov si, strOrig                 ;
        mov di, strComp                 ; All terminators
        repne cmpsb                     ;
        sub dx, cx                      ; dx now holds num chars to reverse
; ---------------------------------------------------------------------------
doReverseCopy:
        mov cx, dx                      ; copy the length for the next op
        mov si, strOrig                 ;
        mov di, strDest                 ;
        add di, cx                      ;
        dec di                          ; minus 1
reverseCopy:
        mov bl, byte ptr [si]           ; copy the chars from source
        mov byte ptr[di], bl            ; paste chars at destination
        inc si                          ; next char forward
        dec di                          ; next char backward
        loop reverseCopy
; ---------------------------------------------------------------------------
compareStrings:
        mov cx, 0ah                     ; max string length
        mov si, strOrig                 ; Original string
        mov di, strDest                 ; Compare string
        repe cmpsb                      ;
        cmp cx, 0h                      ;
        jnz noMatch
; ---------------------------------------------------------------------------
isMatch:
        mov dx, strIsMatch
        call doPrint
        jmp quit
strIsMatch:       db 0ah, 0dh,'IS a palindrome!', 0ah, 0dh, 024h
; ---------------------------------------------------------------------------
noMatch:
        mov dx, strNoMatch              ; Display mismatch message
        call doPrint
        jmp quit
strNoMatch:       db 0ah, 0dh,'NOT a palindrome!', 0ah, 0dh, 024h
; ---------------------------------------------------------------------------
doPrint:
	mov ah, 09h
	int 21h
	ret
; ---------------------------------------------------------------------------
quit:   int 20h                         ; *** DROP TO DOS ***
; ---------------------------------------------------------------------------
askString:        db 'Enter a string of up to 10 characters: $'
youEntered:       db 0ah, 0dh, 'You entered: '
strOrig:          db '$$$$$$$$$$'
finalStop:        db '$$'
strDest:          db '$$$$$$$$$$$'
strComp:          db '$$$$$$$$$$$'
Question Answered as of 2 Years Ago by thines01 and sDJh
Member Avatar
Vongola_Takeshi
Newbie Poster
21 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Great!
Here's what I hacked out (FYI):

;-------------------------------80x86----------------------------------------
; 8-Aug-2011 - THINES01
; Palindro.asm: to be compiled with A86 (http://eji.com/a86/)
; This program shows how to get user input with echo
; and test to see if it is a palindrome
; ---------------------------------------------------------------------------
        mov dx, askString;
        call doPrint
        mov cx, 0ah
        mov si, strOrig;                ; Place to store the orig string
getString:
        mov ah, 01h
        int 21h                         ; Get input with echo
        
        cmp al, 0dh                     ; 
        je printQuit                    ; Quit if Carriage-return
        
        mov [si], al                    ; Put entered char in orig string
        
        cmp byte ptr [finalStop], 24h   ; is 10th dollar-sign gone?
        jne printQuit                   ; if so, buffer is full?, quit
        
        inc si                          ; move the position
        loop getString;                 ; Keep going (looping)
printQuit:
        mov dx, youEntered              ;
        call doPrint                    ; Print the finished message
; ---------------------------------------------------------------------------
strLen:
        mov cx, 0bh                     ; max string length (plus terminator)
        mov dx, 0ah                     ; minus 1
        mov si, strOrig                 ;
        mov di, strComp                 ; All terminators
        repne cmpsb                     ;
        sub dx, cx                      ; dx now holds num chars to reverse
; ---------------------------------------------------------------------------
doReverseCopy:
        mov cx, dx                      ; copy the length for the next op
        mov si, strOrig                 ;
        mov di, strDest                 ;
        add di, cx                      ;
        dec di                          ; minus 1
reverseCopy:
        mov bl, byte ptr [si]           ; copy the chars from source
        mov byte ptr[di], bl            ; paste chars at destination
        inc si                          ; next char forward
        dec di                          ; next char backward
        loop reverseCopy
; ---------------------------------------------------------------------------
compareStrings:
        mov cx, 0ah                     ; max string length
        mov si, strOrig                 ; Original string
        mov di, strDest                 ; Compare string
        repe cmpsb                      ;
        cmp cx, 0h                      ;
        jnz noMatch
; ---------------------------------------------------------------------------
isMatch:
        mov dx, strIsMatch
        call doPrint
        jmp quit
strIsMatch:       db 0ah, 0dh,'IS a palindrome!', 0ah, 0dh, 024h
; ---------------------------------------------------------------------------
noMatch:
        mov dx, strNoMatch              ; Display mismatch message
        call doPrint
        jmp quit
strNoMatch:       db 0ah, 0dh,'NOT a palindrome!', 0ah, 0dh, 024h
; ---------------------------------------------------------------------------
doPrint:
	mov ah, 09h
	int 21h
	ret
; ---------------------------------------------------------------------------
quit:   int 20h                         ; *** DROP TO DOS ***
; ---------------------------------------------------------------------------
askString:        db 'Enter a string of up to 10 characters: $'
youEntered:       db 0ah, 0dh, 'You entered: '
strOrig:          db '$$$$$$$$$$'
finalStop:        db '$$'
strDest:          db '$$$$$$$$$$$'
strComp:          db '$$$$$$$$$$$'

thanks a lot thines01 hehehe your code is shorter than ours ^^

Member Avatar
bettybarnes
Posting Whiz in Training
273 posts since Nov 2009
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

hi thines why is it unexpected end of file encountered?

i included .model small
.stack
.code
org 100h
start:
at the beginning of ur code

Member Avatar
bettybarnes
Posting Whiz in Training
273 posts since Nov 2009
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

hi thines why is it unexpected end of file encountered?

i included .model small
.stack
.code
org 100h
start:
at the beginning of ur code

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

Maybe your compiler also needs the .model.

Member Avatar
bettybarnes
Posting Whiz in Training
273 posts since Nov 2009
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

yah i included .model small

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: