Why won't the program print the sum of the array?

▼魔方 西西 提交于 2019-12-02 02:31:55

The DOS character input function gives you characters.
When you key in 1 DOS presents you with AL='1' meaning you get 49 where you might expect 1.
When you key in 2 DOS presents you with AL='2' meaning you get 50 where you might expect 2.
When you key in 5 DOS presents you with AL='5' meaning you get 53 where you might expect 5.
That's why we subtract 48 in these cases.

Enter:
    mov ah, 1
    int 21h
    mov bl, i
    mov bh, 0
    SUB AL, '0'        ;Same as SUB AL, 48
    mov array[bx], al

This way your array will contain the values 1, 1, 2, and 5 (No longer the characters '1', '1', '2', and '5')

Now you can safely do the additions, yielding 9.

Because sum now holds the value 9, but you need the character '9', you simple add 48 to do the conversion:

    mov dl, sum
    ADD DL, '0'        ;Same as ADD DL, 48
    mov ah, 02h
    int 21h
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!