Desi991 0 Newbie Poster

MASM Assembly

TITLE Palindrome Checker        (main.asm)
002	 
003	;
004	;
005	;
006	 
007	INCLUDE Irvine32.inc
008	START = 128
009	 
010	.data
011	strPrompt   BYTE "Please enter a string of Integers: ",0
012	strMsg      BYTE "Original String of Integers: ",0;dh, 0ah
013	revMsg      BYTE "Reversed String: ",0
014	procFail        BYTE "Sorry, but this is not a Palindrome.",0dh,0ah
015	againPrompt BYTE "Would you like to try again? (y/n) >> ",0
016	inOriginal  BYTE START+1 DUP(0)
017	inRev       BYTE START+1 DUP(0)
018	procOrig        BYTE START+1 DUP(0)
019	procRev     BYTE START+1 DUP(0)
020	bufSize         DWORD ?
021	procSuccess BYTE "Yes, This is a Palindrome!",0dh,0ah
022	 
023	 
024	 
025	.code
026	main PROC
027	Again:
028	     
029	 
030	    call InputString
031	    mov edx, OFFSET strMsg
032	    call DisplayOriginal
033	    call Reverse
034	    mov edx, OFFSET revMsg
035	    call DisplayRev
036	     
037	    call SqueezeOrig
038	    call SqueezeRev
039	 
040	     
041	    ;call Crlf
042	    ;call Crlf
043	     
044	    ;mov edx, OFFSET procStrMsg
045	    ;call WriteString
046	     
047	    ;mov edx, OFFSET procOrig
048	    ;call WriteString
049	     
050	    ;call Crlf
051	    ;call Crlf
052	     
053	    ;mov edx, OFFSET procRevMsg
054	    ;call WriteString
055	     
056	    ;mov edx, OFFSET procRev
057	    ;call WriteString
058	    call Crlf
059	    call Crlf
060	     
061	    call PalCheck
062	     
063	    mov edx, OFFSET againPrompt
064	    call WriteString
065	    call ReadChar
066	    cmp al, 121
067	        je Again
068	    exit
069	main ENDP
070	 
071	 
072	InputString PROC
073	    pushad
074	    mov edx, OFFSET strPrompt
075	    call WriteString
076	    mov ecx, START
077	    mov edx, OFFSET inOriginal
078	    call ReadString
079	    mov bufSize, eax
080	    call Crlf
081	    popad
082	    ret
083	InputString ENDP
084	 
085	DisplayOriginal PROC
086	    pushad
087	    call    WriteString
088	    mov edx,OFFSET inOriginal   ; display the inOriginal
089	    call    WriteString
090	    call    Crlf
091	    call    Crlf
092	    popad
093	    ret
094	DisplayOriginal ENDP
095	 
096	DisplayRev PROC
097	    pushad
098	    call    WriteString
099	    mov edx,OFFSET inRev    ; display the inOriginal
100	    call    WriteString
101	    call    Crlf
102	    call    Crlf
103	    popad
104	    ret
105	DisplayRev ENDP
106	 
107	Reverse PROC
108	    pushad
109	    mov ecx, bufsize                ; loop counter
110	    mov ebx,ecx                     ; move ebx into ecx
111	    sub ebx,1                       ; Due to the fact that SIZEOF doesnt recognize the zero position as the
112	                                    ; starting point ebx has to be subtracted by one
113	    mov esi,0                        
114	    L1:                             ; begin loop 1
115	        mov al,inOriginal[ebx]      ; stores char into al at position ebx
116	        ;call WriteChar             ; displays char to the screen
117	        mov inRev[esi],al           ; moves char into target position esi
118	        inc esi                     ; increment esi
119	        dec ebx                     ; decrement ebx
120	        loop L1                     ; continue loop 1
121	    popad
122	    ret
123	Reverse ENDP
124	 
125	SqueezeOrig PROC                    ; this procedure will make a string and remove the spaces
126	                                     
127	    pushad                          ; (not referring to popad) this function makes the program crash
128	    mov ecx, bufsize               
129	                        ;subtract ecx (max loop value) by 1
130	    mov esi,0                       ;initalize esi 
131	    mov edi,0
132	     
133	    L2:
134	        mov al, inOriginal[esi]     ; move value of original string at index of esi to al...
135	         
136	        cmp al, 20h                 ; compare value to ASCII space
137	            je isSpace              ; if it's a space, jump to isSpace
138	        mov procOrig[edi],al        ; otherwise, move it to the squeezed string at index esi
139	            inc edi
140	        isSpace:
141	        inc esi
142	        cmp esi, bufsize
143	            je doneFirst
144	        jmp L2                      ; and go back to L2.
145	         
146	    doneFirst:
147	    popad
148	    ret
149	SqueezeOrig ENDP
150	 
151	SqueezeRev PROC                 ; this procedure will make a string and remove the spaces
152	                                    ; maybe there's an easier way to do this?
153	    pushad                          ; (not referring to popad) this function makes the program crash
154	    mov ecx, bufsize               
155	    ;sub ecx,1                      ;subtract ecx (max loop value) by 1
156	    mov esi,0                       ;initalize esi 
157	    mov edi,0
158	     
159	    L2:
160	        mov al, inRev[esi]      ; move value of original string at index of esi to al...
161	         
162	        cmp al, 20h                 ; compare value to ASCII space
163	            je isSpaceRev               ; if it's a space, jump to isSpace
164	        mov procRev[edi],al     ; otherwise, move it to the squeezed string at index esi
165	                                ; skip it. simply increment esi,
166	        inc edi
167	        isSpaceRev:
168	        inc esi
169	        cmp esi, bufsize
170	            je doneFirst
171	        jmp L2                      ; and go back to L2.
172	         
173	    doneFirst:
174	    popad
175	    ret
176	SqueezeRev ENDP
177	 
178	PalCheck PROC
179	 
180	    mov ecx, bufsize
181	    mov esi,0
182	     
183	    L2:
184	        mov al, procOrig[esi]
185	        cmp procRev[esi],al
186	            jne notPal
187	        inc esi
188	        cmp esi, bufsize
189	            jne L2
190	     
191	        mov edx, OFFSET procSuccess
192	        call WriteString
193	        jmp toRet
194	 
195	    notPal:
196	        mov edx, OFFSET procFail
197	        call WriteString
198	    toRet:
199	        ret
200	PalCheck ENDP
201	 
202	 
203	END main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.