assume cs:code,ds:data
data segment
tip db 'what is your string?',13,10,'$'
buf db 30
len db ?
sg1 db 30 dup (?);原始字符串
sg2 db 30 dup (0);倒置后字符串
final1 db 'yes',13,10,'$'
final2 db 'no',13,10,'$'
en db 13,10,'$'
data ends
stack segment
db 30 dup (0)
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov sp,30;缺省
lea dx,tip
mov ah,9
int 21h;提示语
lea dx,buf
mov ah,10
int 21h;输入字符串
sub ax,ax
mov al,len
mov si,ax;获得字符串长度
lea bx,sg1;字符串定位
mov cx,si
push si
push bx
push ax;入栈保护寄存器里的值
s: mov al,ds:[bx+si-1]
mov ds:[bx].30,al
dec si
dec si
inc bx
loop s;倒置
pop ax
pop bx
pop si;出栈还原寄存器
mov cx,si
s0: mov al,ds:[bx+30]
mov ah,ds:[bx]
cmp ah,al
jne t
inc bx
loop s0;比对
lea dx,en
mov ah,9
int 21h;先输出换行
lea dx,final1
mov ah,9
int 21h;是回文串
jmp short final
t: lea dx,en
mov ah,9
int 21h;先输出换行
lea dx,final2
mov ah,9
int 21h;再输出不是回文串
jmp short final
final: mov ax,4c00h
int 21h
code ends
end start
回文串输入后倒置,用倒置后的字符串和原始的字符串对比