How to input from user number more than one digit in assembly?

懵懂的女人 提交于 2021-02-16 17:54:35

问题


I need to find an interrupt that can receive from a user a number with more than 1 digit. ;code

mov [0],0
mov si,0
lop:    
    mov ah,1
    int 21h
    cmp al,'q'
    je finishedInput
    xor ah,ah 
    add [word ptr si], ax
    jmp lop

finishedInput:

I have already tried to do an end less loop that each time uses the

mov ah,1 
int 21h 

combination until the user press 'q' and the endless loop will stop and. However, I am almost convinced that I have seen a code that do the same thing with interrupt instead.

I want to stop using this block and use short interrupt that does the work better


回答1:


In most cases, it makes it a lot easier if input is taken in as a string and then converted to an integer. int 21h/ah=0ah can read buffered input into a string pointed to at DS:DX.

Once you have that, you can take that string and convert it to an integer. This sounds like a homework problem, so rather than give you code for it, here is a high level algorithm for converting a string of ASCII characters containing a number in base-10 into an actual integer (pseudocode):

accum = 0
i = 0
while(string[i] != '\r')
    accum *= 10
    accum += (string[i] - '0')
    i++

Robust code would check for overflow and invalid characters as well. You're in luck here, since in ASCII the characters representing numbers ('0'...'9') are stored consecutively, and the x86 has a FLAGS register that you can check for overflow.



来源:https://stackoverflow.com/questions/54696513/how-to-input-from-user-number-more-than-one-digit-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!