assume cs:code, ds:data data segment strings db 'abccba', '1234321', 's2345','$' msg db 'The string is a palindrome',13,10,'$' msg2 db 'The string is not a palindrome',13,10,'$' data ends code segment start: mov ax, data mov ds, ax mov si, 0 ; 用si存储字符串的索引 outer_loop: mov di, si ; 用di存储字符串的另一个索引(从后往前) mov cx, 0 mov cl, 7 dec cx shr cx, 1 inner_loop: mov al, [strings+si] ; 从字符串中读取字符 mov ah, [strings+di] cmp al, ah jne not_palindrome inc si dec di cmp si, di jle inner_loop palindrome: lea dx, msg mov ah, 09h int 21h jmp next_string not_palindrome: lea dx, msg2 mov ah, 09h int 21h next_string: add si, 7 cmp si, 21 jl outer_loop mov ah, 4ch int 21h code ends end start分别判断这三个字符串是否是回文 字符串
时间: 2024-01-11 22:04:51 浏览: 28
下面是判断三个字符串是否为回文字符串的8086汇编代码:
```
assume cs:code, ds:data
data segment
strings db 'abccba', '1234321', 's2345','$'
msg db 'The string is a palindrome',13,10,'$'
msg2 db 'The string is not a palindrome',13,10,'$'
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, 0 ; 用si存储字符串的索引
outer_loop:
mov di, si ; 用di存储字符串的另一个索引(从后往前)
mov cx, 0
mov cl, 7
dec cx
shr cx, 1
inner_loop:
mov al, [strings+si] ; 从字符串中读取字符
mov ah, [strings+di]
cmp al, ah
jne not_palindrome
inc si
dec di
cmp si, di
jle inner_loop
palindrome:
lea dx, msg
mov ah, 09h
int 21h
jmp next_string
not_palindrome:
lea dx, msg2
mov ah, 09h
int 21h
next_string:
add si, 7
cmp si, 21
jl outer_loop
mov ah, 4ch
int 21h
code ends
end start
```
在这段代码中,我们使用了两个指针 si 和 di 分别指向字符串的开头和结尾字符。然后使用双指针法判断字符串是否为回文字符串。如果是回文字符串,则输出 "The string is a palindrome",否则输出 "The string is not a palindrome"。最后程序退出。