Reading a number using INT 21h (DOS) & 8086 assmebly

前端 未结 2 1974
借酒劲吻你
借酒劲吻你 2020-12-06 21:06

I need to prompt to user a msg that tells him to write a number , then I store this number and do some operation on it After searching in INT 21h I found this :

         


        
2条回答
  •  不思量自难忘°
    2020-12-06 21:49

    Once you've got the string you have to convert it to number. The problem is, you have to code your own procedure to do that. This is the one I usually use (written in C though):

    int strToNum(char *s) {
        int len = strlen(s), res = 0, mul = 0;
        char *ptr = s + len;
    
        while(ptr >= s)
            res += (*ptr-- - '0') * (int)pow(10.0, mul++);
    
        return res;
    }
    

    Here's the explanation. First of all, *ptr-- - '0' gets the integer representation of a number (so that '9' - '0' = 9, then it decremenst ptr so that it points to the previous char. Once we know that number, we have to raise it to a power of 10. For example, suppose the input is '357', what the code does is:

    ('7' - '0' = 7) * 10 ^ 0 =   7 +
    ('5' - '0' = 5) * 10 ^ 1 =  50 +
    ('3' - '0' = 3) * 10 ^ 2 = 300 = 
    ---------------------------------
                               357
    

提交回复
热议问题