Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

assembly - Print decimal in 8086 emulator

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

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

    MOV CH, 0H
    MOV CL, L1; initial counter of first loop ( L1 -> length of N1 )
    DEC CX

    MUL_1:
        MOV COUNTER, CL ; store counter of first loop
        MOV CL, L2 ; initial counter of second loop ( L2 -> length of N2 )

        MUL_2:
            MOV BH, 0H
            MOV BL, COUNTER
            DEC BL
            MOV AL, N1[BX] ; get BX th byte of N1

            MOV BL, CL
            DEC BL

            MUL N2[BX] ; multiple N1 and N2 's bytes

            MOV BH, 0H
            MOV BL, COUNTER
            ADD BX, CX
            DEC BX

            ADD RES[BX], AL ; AL should be add into RES[loop1_counter + loop2_counter - 1]
            ADC RES[BX+1], AH; AH and carry should be add into RES[loop1_counter + loop2_counter ]
            ADC RES[BX+2], 0H; carry of above addition should be place here.
        LOOP MUL_2     

        MOV CL, COUNTER; retrieve loop 1 counter 
    LOOP MUL_1   

    RET ; end function 
MULTIPLY ENDP

So, i want to print it in Decimal Mode, i know how to print result in HexaDecimal :

PRINT_TABLE PROC
    MOV CX, 16D

    CASE:
        MOV BX, 16D
        SUB BX, CX

        MOV AL, RES[BX]

        CMP AL, 10D
        JB LBL1
        JAE LBL2


        LBL1:
            ADD AL, '0'
            JMP CONTINUE

        LBL2:
            ADD AL, 55D

        CONTINUE:

        MOV DL, AL
        MOV AH, 02H
        INT 21H        
    LOOP CASE
    RET
PRINT_TABLE ENDP

Could any one please help me to print my result in Decimal Mode ?

Thanks for your advance :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 
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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...