用于回文检查的8086 masm程序

时间:2014-01-11 15:48:50

标签: assembly masm x86-16 8085

.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是'字符串不是palinrome' 'cmp al,0dh'在此代码中执行了什么操作?

3 个答案:

答案 0 :(得分:2)

没有提到这段代码的来源,但它不完整(例如。,正如Mario所指出的那样:没有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)结束并读取字符串(到位置str)。它还提供了count中读取的字符数。未定义strcountmsg1的位置。

答案 1 :(得分: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

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

答案 2 :(得分:0)

此代码从用户获取字符串,并检查它是 Palindrome还是

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$"