Thursday, March 17, 2011

This program determines whether or not a given string is a palindrome. - By Professor Ronald J. Hart

; Title: CISS 360 Demo Program
;------------------------------------------------------------
; Description: This program determines whether or not a given
; string is a palindrome.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Sunday, January 25, 2011 4:05 PM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h ; header file for console input/output
.STACK 4096 ; reserve 4096-byte (1024 DWORD) stack
;------------------------------------------------------------
; Input/Output Facility
;------------------------------------------------------------
; Macro Operand
;
; put_ch DWORD/Reg (Displays character in parameter)
; get_ch None (Loads character from keyboard into EAX)
; put_str BYTE (Displays string beginning at given address)
; put_i DWORD/Reg (Displays integer value in parameter)
; put_fp REAL4/Reg (Displays float value in parameter)
; get_i DWORD (Loads integer entered at keyboard into parameter)
; get_fp REAL4 (Loads float entered at keyboard into parameter)
; get_str BYTE (Loads string entered at keyboard into parameter)
;------------------------------------------------------------

.DATA ;****************************************************
;* Data Section *
;****************************************************

prompt BYTE "Enter a string: ", 0
input_str BYTE 256 DUP(0)
palin_yes BYTE "Input string is a palindrome",0
palin_no BYTE "Input string is not a palindrome",0
no_input_str BYTE "String not entered",0
one_byte_str BYTE "String has only one byte",0

newline DWORD 10

.CODE ;****************************************************
;* Code Section *
;****************************************************

_MainProc PROC

put_str prompt ; prompt user for input string
get_str input_str ; get the string
put_ch newline ; line space on display
mov ecx, 0 ; byte counter
lea edi, input_str ; store starting address of string

;; loop to count characters in string and to get address of last byte

loop_it:

cmp BYTE PTR [edi], 0 ; string terminator?
je stop_scan ; yes, end of scan
inc ecx ; count character
inc edi ; point at next character of string
jmp loop_it ; go check the next character

stop_scan:

cmp ecx, 0 ; bytes in input string?
je no_input ; no, go display appropriate message
cmp ecx, 1 ; yes, only one byte?
je one_byte ; yes, go display appropriate message
lea esi, input_str ; no, determine if input string is a palindrome
dec edi ; backup last address

next_bytes:

cmp esi, edi ; start address >= ending address?
jge yes_palin ; yes, input string is palindrome
mov bl, BYTE PTR [edi] ; set up to compare characters
cmp BYTE PTR [esi], bl ; characters equal?
jne no_palin ; no, not a palindrome
inc esi ; increment to point at next byte
dec edi ; decrement to point at preceding byte
jmp next_bytes ; go check next two bytes

yes_palin:

put_str palin_yes ; display input string is a palindrome
jmp main_done ; go finish program

no_palin:

put_str palin_no ; display input string is not a palindrome
jmp main_done ; go finish program

no_input:

put_str no_input_str ; tell user no string was input
jmp main_done ; go finish program

one_byte:

put_str one_byte_str ; tell user a one-character string was input

main_done:

put_ch newline ; display newline...
put_ch newline ; and another

done ; display "completion" message
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

;------------------------------------------------------------

END ; end of source code

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.