Converting Hexadecimal/Decimal numbers (Assemly-TASM)

micheller picture micheller · Sep 21, 2012 · Viewed 9.1k times · Source

I am trying to print the numbers simply in the sequence i.e

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

using Loop, First i converted each number into Hexa printed it reset it to the decimal increment by 1 and then print the next until the number is equal to 9, When the number is equal to 9 i used DAA to simply the number and after rotating and shifting the number i eventually stored the result in the string.

The output is just fine till the 16, but after 16 the sequence repeats itself,

Desired output:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

Current Output 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,11,12,13,14,15

Why does it happens so ???

Here is my Code,

MOV CX,20 ;Number of Iterations


MOV DX,1



L1:
    PUSH DX
    ADD DX,30H  
    MOV AH,02H        ;PRINT Content of DX
    INT 21H
    POP DX
    ADD DX,1
    CMP DX,09d        ;If number is Greater than 9 jump to L2   
    JA L2
LOOP L1


    L2: 
        PUSH DX
        MOV AX,DX
        DAA           ;Convert to the Decimal
        XOR AH,AH         ;SET AH to 0000


        ROR AX,1    
        ROR AX,1    
        ROR AX,1    
        ROR AX,1    

        SHR AH,1
        SHR AH,1
        SHR AH,1
        SHR AH,1

        ADC AX,3030h
        MOV BX,OFFSET Result
        MOV byte ptr[BX],5           ; Length of the String
        MOV byte ptr[BX+4],'$'       ;5th position of string , $=Terminator
        MOV byte ptr[BX+3],AH        ;2nd Number onto 4th position
        MOV byte ptr[BX+2],AL        ;3rd number onto 3rd Position 

        MOV DX,BX
        ADD DX,02     ;1st 2 positions of String are type of string and    
                                  length respectively 
        MOV AH,09H ;to print the string
        INT 21H         

        POP DX      
        ADD DX,1

    LOOP L2

MOV AH,4CH  ;Return control to the DOS
INT 21H

P.S: I took help from this chart in understanding the numbers.

http://www.cheat-sheets.org/saved-copy/ascii.png

Answer

Frank Kotler picture Frank Kotler · Sep 22, 2012

8086 code only allowed an immediate of 1 (or cl) for a count on shifts and rotates. To enable 286 code, tell Tasm ".286" at the top of your file. That's a guess.

The way I remember I used to print a two-digit number in al:

aam
add ax, 3030h
xchg al, ah
int 29h
mov al, ah
int 29h