Identifying Palindromic Words in Strings
; Emu 8086 programming assignment
include "emu8086.inc"
org 100h
PRINTN "Please enter a string:"
lea di, string; set up pointer di to the input string
mov dx, 100 ; the maximum size of the string that can be retrieved from user input
call GET_STRING
; here, cx has the number of characters in a string
lea di, string;set up pointer di to input string
mov bx, 0 ; bx will be the word length
mov dx, 0 ; dx will have the number of palindrome words found
findwords:
mov al,[di] ; retrieve char from string
cmp al, 0 ; see if we reached the end of the string
je endsrch ; if so, end loop
cmp al, 'A'; see if it's a letter between A and Z
jl newWord
cmp al, 'Z'
jle increment
cmp al, 'a' ; see if it's a letter between a and z
jl newWord
cmp al, 'z'
jle increment
newWord:
cmp bx, 0 ; if we have a word, check if it's a palindrome
je skip; else, we don't have a word, skip
mov ax, di; save word start in ax
sub ax, bx
call check palindrome
add dx, ax; increment dx if the word was a palindrome
skip:
mov bx, 0 ; start a new word
jmp nextChar
increment:
inc bx
nextChar:
inc di; increment di register
jmp findwords ; repeat for all characters in a string
endsrch:
cmp bx, 0 ; if we have a word, check if it's a palindrome
je tstcount ; else, we don't have a word, skip
mov ax, di ; save word start in ax
sub ax, bx
call checkPalindrome
add dx, ax ; increment dx if the word was a palindrome
tstcount:
PRINTN " "
cmp dx, 0 ; see if we found palindromic words
je notfound ; if not, print a not found message
PRINT "Found "
mov ax, dx ; print the number we found
call PRINT_NUM_UNS
PRINTN " palindromic words"
ret
notfound:
PRINTN "No palindromic words found"
ret
; check if the word starting in ax and length bx is a palindrome
; returns ax = 0 if it's not a palindrome, ax = 1 if it's a palindrome
checkPalindrome:
push di
mov di, ax ; save word start pointer in di
mov si, di ; point to last char with si
add si, bx
dec si
chkloop:
cmp di, si ; see if the pointers have crossed
jge ispal ; if they crossed, it's a palindrome
mov al, [di] ; else, compare characters at both ends
mov ah, [si]
cmp al, ah
jne nopal ; if they are not equal, is not a palindrome
inc di ; else advance to next character at the front
dec si; and advance to the previous character at the end
jmp chkloop
ispal: ; it's a palindrome, return 1
mov ax, 1
jmp retpal
nopal: ; it's no palindrome, return 0
mov ax, 0
retpal:
pop di
ret
; Program Data
string DB 100 dup(?)
key equ 37
define_GET_STRING
define_PRINT_STRING
define_PRINT_NUM_UNS
define_PRINT_NUM
end