在一个简单的x86 DOS汇编程序的异常输出 [英] Unexpected output in a simple x86 DOS assembly program

查看:235
本文介绍了在一个简单的x86 DOS汇编程序的异常输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是汇编语言程序提示用户输入一个数字小于5时,加倍的数量和输出结果。

This is an assembly language program that prompts a user to enter a number less than 5, doubles the number and outputs the result.

; DOUBLE: This program prompts the user to enter a number < 5, doubles the number and outputs the result

    name double
    .model small
    .stack
    .data

prompt db 0ah, 0dh, 'Enter a number < 5:', '$'
msg db 0ah, 0dh, 'Double your number is: '
result db ?, 0ah, 0dh, '$'

    .code
start:
    mov ax, @data
    mov ds, ax
    lea dx, prompt; loads the message into the variable prompt
    mov ah, 090h; dos fn to output string up to $
    int 21h
    mov ah, 01h; dos fn to input byte into ah
    int 21h
    sub al, 30h; convert from ascii to integer
    add al, al
    add al, 30h; convert back to ascii
    mov result, al
    lea dx, msg
    mov ah, 06h
    int 21h
    mov ax, 4c00h; 4c in ah is dos exit
    int 21h
end start

这个程序的问题是,如果用户输入大于或等于5的数,它不会显示出需要的输出

The problem with this program is that if the user enters a number greater than or equal to 5, it will not display the desired output.

有人可以帮助我解决这个问题,并解释我做错了什么?

Can someone help me fix this and explain what I did wrong?

推荐答案

只需添加一个检查为输入。

Just add a checking for the input.

替换行加人,人通过以下code。

Replace the line add al, al by the following code.

    cmp al, 5
    jae l_error
    add al, al
    jmp l_ok
l_error:
    xor al, al    ; or print some error message
l_ok:

这篇关于在一个简单的x86 DOS汇编程序的异常输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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