当我创建一个与代码无关的变量或字符串不同时,汇编代码运行不同

问题描述

我有一个汇编程序,它应该接受一个字符串和 2 个数字,这些数字代表打印每个字母后等待的毫秒数,它首先计算字符串的长度,然后开始一个字母一个字母地打印它延迟 %cx:%dx 这是代码

IDEAL
MODEL small
STACK 100h   
DATASEG
    x db 02
    string db "hello,my name is ____",0Ah,0Dh,"and this is my game:",'$'
    string2 db "this is a test ",'$'
    
CODESEG
    
proc PrintString
    xor dx,dx
    push bp
    mov bp,sp
    mov bp,[bp+8h] ;string offset
    xor cx,cx
    WhileLoop:
        mov dl,[byte ptr ds:bp] ;string offset in the ds
        inc bp
        inc cx
        cmp dl,24h
        jne WhileLoop
    mov bp,sp
    mov bx,bp
    mov bp,[bp+8h] ; string offset
    sub cx,1
    PrintLoop:
        mov dl,[byte ptr ds:bp]
        mov ah,02h
        inc bp
        int 21h
        mov ah,86h
        push cx
        ;watiting cx:dx milliseconds
        mov cx,[ss:bx+6]     ;push 03h
        mov dx,[ss:bx+4]     ;push 01615h
        int 15h
        pop cx
        loop PrintLoop
    pop bp
    ret
endp
    
    
start:
    mov ax,@data
    mov ds,ax
    push offset string
    push 03h
    push 01615h
    call PrintString
    pop ax
    pop ax
    pop ax
    xor ax,ax
    push offset string2
    push 05h
    push 01615h
    call PrintString
    
exit:
    mov ax,4c00h
    int 21h
END start

代码看起来像这样时,一切正常。 buf 如果我删除了在我的代码中甚至一次都没有使用过的 x 变量 - 代码突然不起作用并且输出如下所示:

img1

它卡在 h 处,有时它也设法打印第二个字母 ('e') 此外,如果我保留变量 x 但我尝试将文本更改为其他内容,它也不起作用:

enter image description here

但是如果我让字符串真的很短,不管有没有变量 x 都可以工作:

enter image description here

解决方法

我不知道为什么,但你所要做的就是移动到零 mov al,0