Palindrome detection: Difference between revisions

Add 8086 assembly
(Add ARM Assembly)
(Add 8086 assembly)
Line 139:
racecar: yes
level: yes
rosetta: no</pre>
 
=={{header|8086 Assembly}}==
<lang asm> cpu 8086
org 100h
section .text
jmp demo
;;; Check if the $-terminated string in [DS:SI] is a palindrome.
;;; Returns with zero flag set if so.
;;; Destroyed: AL, CX, SI, DI, ES.
palin: push es ; Set ES=DS.
pop ds
mov al,'$' ; Find end of string
mov cx,-1
mov di,si
repne scasb
dec di ; Move back to last actual character
.loop: cmp si,di
ja .ok ; If SI > DI, it is a palindrome
lodsb
dec di ; Compare left character to right character
cmp al,[di]
jne .no ; If not equal, not a palindrome
jmp .loop ; Otherwise, try next pair of characters
.ok: cmp al,al ; Set zero flag
.no: ret ; Return
;;; Try the routine on a couple of strings
demo: mov si,words
.loop: lodsw ; Grab word pointer
test ax,ax ; Zero?
jz .done ; Then we are done
mov dx,ax ; Otherwise, print word
mov ah,9
int 21h
xchg bp,si ; Keep array pointer in BP
xchg si,dx ; Put word pointer in SI
call palin ; Check if it is a palindrome
mov dx,yes ; Print 'yes'...
jz .print ; ...if it is a palindrome
mov dx,no ; Otherwise, print 'no'
.print: int 21h
xchg si,bp ; Restore array pointer
jmp .loop ; Get next word.
.done: ret
yes: db ': yes',13,10,'$' ; Yes and no
no: db ': no',13,10,'$'
words: dw .w1,.w2,.w3,.w4,.w5,0
.w1: db 'rotor$' ; Words to check
.w2: db 'racecar$'
.w3: db 'level$'
.w4: db 'redder$'
.w5: db 'rosetta$'</lang>
 
{{out}}
 
<pre>rotor: yes
racecar: yes
level: yes
redder: yes
rosetta: no</pre>
 
2,124

edits