0
.code
> 
>     start:
>     mov ax,03h
>     int 10h
>     mov ax,seg msg1
>     mov ds,ax
>     mov dx,offset msg1
>     mov ah,09h
>     int 21h
>     mov si,offset str
>     read:
>     mov ah,01h
>     int 21h
>     cmp al,0dh
>     je next
>     mov [si],al
>     inc si
>     inc count
>     jmp read
>     next:
>     mov di,offset str
>     mov al,count
>     mov cl,al
>     mov ch,00h
>     dec si
>     check:
>     mov al,[si]
>     cmp al,[di]
>     jne nt
>     dec si
>     inc di
>     loop check
>     mov ax,seg msg2
>     mov ah,09h
>     int 21h
>     jmp exit
>     nt:
>     mov ax,seg msg3
>     mov ds,ax
>     mov dx,offset msg3
>     mov ah,09h
>     int 21h
>     exit:
>     mov ax,4c00h
>     int 21h
>     END start

这是 8086 masm 代码的一部分,用于检查字符串是否为回文。msg1 是“输入字符串”,msg2 是“字符串是回文”,msg3 是“字符串不是回文” 'cmp al,0dh' 执行什么在这段代码中?

4

3 回答 3

2

没有提到此代码的来源,但它是不完整的(例如马里奥指出:没有next:标签)存在。但我们可以拼凑起来:

.code

start:
    mov ax,03h           ; Get cursor position and shape
    int 10h

    ; Display a message to the user
    ; (NOTE: we only know it's in "msg1" but don't know the contents
    ;
    mov ax,seg msg1      ; DS:DX to point to msg1
    mov ds,ax
    mov dx,offset msg1

    mov ah,09h           ; Write the string (pointed by DS:DX) to stdout
    int 21h

    mov si,offset str    ; Get the the destination string location, DS:SI

    ; Read a string in from the user, terminated by new line (0dh)
    ;
read:
    mov ah,01h           ; Read a character
    int 21h

    cmp al,0dh           ; if it's a line feed, then go to "next"
    je next


    mov [si],al          ; otherwise, store the char in "str" and get the next one
    inc si
    inc count            ; increment character count
    jmp read

    ; Below is where the actual code to compute a palindrome starts
next:
    mov di,offset str
    mov al,count
    mov cl,al
    mov ch,00h
    dec si
check:
    mov al,[si]
    cmp al,[di]
    jne nt
    dec si
    inc di
    loop check

    mov ax,seg msg2

所以这段代码所做的就是向用户显示一条消息,提示他们输入一个字符串,以换行符 (0dh) 终止,然后读取字符串 (到 location str)。它还提供读入的字符数count。其中str,countmsg1没有给出定义。

于 2014-01-11T16:24:31.527 回答
1

上面的汇编器确实说明了一切。它可能是(Microsoft)DOS 使用的一段代码,其中“int 21h”是入口点之王。

http://en.wikipedia.org/wiki/MS-DOS_API

顺便说一句,正如文档所说,上面的调用是指服务 01h (=AH),并且只是从控制台获取一个字符。一旦“int 21h”返回,实际输入的字符就存储在累加器的低字节中,即AL。

此时,“cmp”指令将AL与固定码0Dh进行比较(即CR=回车)。由于比较是通过减 AL 减去 0Dh 进行的,所以当结果为零时匹配成功。如果是这样,程序将跳转到标签“下一步”。

我真的不能说更多,但是在这个片段中根本没有“回文”检查!

更新:看起来片段已更改!

好吧,即使使用新代码,似乎也没有回文检查,至少乍一看是这样。

http://en.wikipedia.org/wiki/INT_10H

它看起来像一个带有回声的字符输入。但是,我有点生疏,我可能会弄错。

于 2014-01-11T15:59:04.200 回答
0

此代码从用户那里获取一个字符串并检查它是否是回文

org 100h

lea dx, string
mov ah, 0ah
int 21h

lea di, string+2
lea si, di
mov cl, string[1]
sub cl, 1
add si, cx
shr cx, 1

checkPal:
mov al, [di]
mov dl, [si]
cmp al, dl
jne printNotPal
inc di
dec si
loop checkPal

printPal:
lea dx, msgPal
jmp print

printNotPal:
lea dx, msgNotPal

print:
mov ah, 9h
int 21h

mov ah, 0
int 16h

string db  10 dup('$')
msgPal db  " is a Palindrome$"
msgNotPal db " is not a Palindrome$"
于 2017-02-02T14:25:54.800 回答