为什么中断 int 21h 在写入程序执行期间填充的字符串后忽略所有字符串? [英] why does the interruption int 21h ignore all strings after writing a string filled durring the execution of the program?

查看:25
本文介绍了为什么中断 int 21h 在写入程序执行期间填充的字符串后忽略所有字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印两个 16Bit 值的总和,因此我正在计算总和,然后构建包含它的字符串,但问题是在我打印所述字符串后任何写入中断 mov ah,09 int 21h 完全被忽略了,我使用的是 emu8086,这是代码

org 100hmov ax,2000mov bx,3200添加 ax,bxmov si,offset 结果+5mov [si],'$'mov bx,10填:十二月移动 dx,0div bx添加 dl,48mov [si],dl测试斧,斧jnz 填充mov dx,偏移结果移动啊,09h21 小时移动啊,09hmov dx,offset msg21 小时回复结果 db 5 dup (?)msg db "是总和$"\n db 0ah,0dh, '$'

它只打印result并忽略msg

解决方案

result db 5 dup (?)msg db "是总和$"

有了这些数据定义,内存设置如下:

result 0 <- 偏移量结果+00 <- 偏移结果+10 <- 偏移结果+20 <- 偏移结果+30 <- 偏移结果+4msg <- 偏移量结果+5 <- 偏移量 msg+0i <- 偏移味精+1s <- 偏移量 msg+2<-偏移味精+3t <- 偏移量 msg+4h <- 偏移味精+5e <- 偏移味精+6<- 偏移味精+7s <- 偏移味精+8u <- 偏移味精+9m <- 偏移味精+10$ <- 偏移味精+11

当您在 offset result+5 处为 result 编写终止 $ 时,您覆盖msg 文本开始.这反过来意味着,当您要求 DOS 显示 msg 文本时,DOS 会立即偶然发现字符串终止符,因此不会显示任何内容.

解决办法不是在offset result+4

处写$字符

相反,您需要扩大结果缓冲区,以便它可以容纳 6 个字节.
您转换的 AX 寄存器中的值可能需要 5 位数字.因此,您需要一个 5+1 字节的缓冲区.

result db 6 dup (?)

作为避免必须通过代码编写 $ 字符的快捷方式有:

result db 5 dup (?), '$'

<小时><块引用>

mov dx,offset 结果移动啊,09h21 小时

使用 result 的偏移量可能会在屏幕上产生垃圾!
这里好的解决方案是在转换结束时使用 SI 寄存器中的值.它已经指向号码的第一位.好方便!

 org 100hmov ax, 2000mov bx, 3200添加 ax, bxmov si, 偏移结果+5mov [si], '$'mov bx, 10填:十二月异或 dx, dxdiv bx添加 dl, '0'mov [si], dl测试斧,斧头jnz 填充mov dx, si <- 这是第一位的偏移量.移动啊,09h21 小时移动啊,09hmov dx,偏移味精21 小时回复结果 db 6 dup (?)msg db "是总和$"

i am trying to print the sum of two 16Bit values so am calculating the sum then constructing the string that contains it but the problem is after i print said string any writing interruptions mov ah,09 int 21h are completely ignored, i am using emu8086, here is the code

org 100h

    mov ax,2000
    mov bx,3200
    add ax,bx
    mov si,offset result+5
    mov [si],'$'
    mov bx,10

    Fill:

        dec si
        mov dx,0
        div bx
        add dl,48
        mov [si],dl
        test ax,ax

    jnz Fill

    mov dx,offset result
    mov ah,09h
    int 21h

    mov ah,09h
    mov dx,offset msg
    int 21h

ret

result db 5 dup (?)
msg db " is the sum$"
\n db 0ah,0dh, '$'

it just prints result and ignors msg

解决方案

result db 5 dup (?)
msg    db " is the sum$"

With these data definitions, memory is set up as follows:

result 0    <- offset result+0
       0    <- offset result+1
       0    <- offset result+2
       0    <- offset result+3
       0    <- offset result+4
msg         <- offset result+5   <- offset msg+0
       i                         <- offset msg+1
       s                         <- offset msg+2
                                 <- offset msg+3
       t                         <- offset msg+4
       h                         <- offset msg+5
       e                         <- offset msg+6
                                 <- offset msg+7
       s                         <- offset msg+8
       u                         <- offset msg+9
       m                         <- offset msg+10
       $                         <- offset msg+11

When you write the terminating $ for your result at offset result+5, you overwrite that space character by which the msg text starts. This in turn means that when you ask DOS to display the msg text, DOS immediately stumbles upon the string terminator and so nothing gets displayed.

The solution is NOT to write the $ character at offset result+4

Instead you need to enlarge the buffer for the result so it can hold 6 bytes.
The value in the AX register that you convert could require 5 digits. Therefore you need a buffer of 5+1 bytes.

result db 6 dup (?)

As a shortcut that avoids having to write the $ character via the code have:

result db 5 dup (?), '$'


mov dx,offset result
mov ah,09h
int 21h

Using the offset of result could produce garbage on screen!
The good solution here is to use the value in the SI register at the end of the conversion. It already points at the first digit of the number. How convenient!

    org  100h

    mov  ax, 2000
    mov  bx, 3200
    add  ax, bx
    mov  si, offset result+5
    mov  [si], '$'
    mov  bx, 10

Fill:

    dec  si
    xor  dx, dx
    div  bx
    add  dl, '0'
    mov  [si], dl
    test ax, ax
    jnz  Fill

    mov  dx, si            <- This is offset of first digit.
    mov  ah, 09h
    int  21h

    mov  ah, 09h
    mov  dx, offset msg
    int  21h
    ret

result   db 6 dup (?)
msg      db " is the sum$"

这篇关于为什么中断 int 21h 在写入程序执行期间填充的字符串后忽略所有字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆