Print decimal in 8086 emulator

前端 未结 3 1824
感动是毒
感动是毒 2020-12-04 04:23

I implemented the multiplication of two big integer in emu8086 with code as below :

; MULTIPLY N1 * N2 = RES
MULTIPLY PROC
    MOV BH, 0H                


        
3条回答
  •  猫巷女王i
    2020-12-04 04:34

    Unfortunately, converting a value to decimal is not as simple as converting it to hexadecimal. This is because base-10 is not a related base of base-2 (i.e. 10 is not a power of 2). We need to use modulus and division to achieve the conversion. The general algorithm in C would look something like this :

    unsigned int val = 58932; // assume int is 32-bit
    char buf[11] = { 0 }, *chr = buf+9; // 11 characters is enough because log10(2^32) = 9,63, +1 for \0
    do
    {
        *chr = (val % 10) + '0'; // to ascii
        --chr;
    } while((val /= 10) != 0);
    ++chr;
    

    Upon completion, chr will point to a null-terminated char* array which will hold the ASCII representation of the base-10 value of val.

    You can achieve it in assembly with the DIV instruction. Most optimizing C compilers optimize it out to a multiplication operation, which is much faster than division (it can be done only if the divisor is constant, though).

提交回复
热议问题