Home > Blockchain >  Convert a 16-bit unsigned integer to an ASCII string consisting of the hexadecimal digits
Convert a 16-bit unsigned integer to an ASCII string consisting of the hexadecimal digits

Time:12-11

I have this inputs and outputs:

  • Inputs: The number in 2 registers (R5, R6), start address of the ASCII string (pointer)
  • MOV R5,#HIGH(NUMBER)``MOV R6,#LOW(NUMBER)
  • Output: Converted ASCII string starting at the given address (R7)
  • MOV R7,#STR_ADDR_IRAM

The code works well but I have to correct this problem:

  • The output bytes are calculated after each other and put into register R7, but every new resulting character overwrites the previous one.
    MOV A, R6                    ;Get hexadecimal data byte from RAM location R6                   
    MOV R2, A                    ;Store in R2                  
    ANL A, #0FH                  ;Get the lower nibble               
    ACALL ASCII                  ;Convert to ASCII                    
    MOV R7, A                    ;Store the lower digit's ASCII code            
    MOV A, R2                    ;Get back the number              
    SWAP A                       ;Swap nibbles in A                  
    ANL A, #0FH                  ;Get the upper BCD digit               
    ACALL ASCII                  ;Convert to ASCII                      
    MOV R7, A                    ;Store the upper digit´s ASCII code          
    MOV A, R5                    ;Get hexadecimal data byte from RAM location R5                   
    MOV R2, A                    ;Store in R2                    
    ANL A, #0FH                  ;Get the lower nibble                 
    ACALL ASCII                  ;Convert to ASCII                
    MOV R7, A                    ;Store the lower digit's ASCII code           
    MOV A, R2                    ;Get back the number                  
    SWAP A                       ;Swap nibbles in A                         
    ANL A, #0FH                  ;Get the upper BCD digit                
    ACALL ASCII                  ;Convert to ASCII                        
    MOV R7, A                    ;Store the upper digit´s ASCII code            
    RET                                       
    ASCII : CLR C                ;ASCII conversion                      
            MOV R4, A            ;Store A to R4                      
            SUBB A, #0AH         ;Substract accumulator                        
            JC next              ;If carry, jump to next                         
            MOV A, R4            ;No carry so bring back R4 to A                   
            ADD A, #07H          ;No carry, so add 7 to A                     
            SJMP SUM             ;Jump to SUM                     
            next:   MOV A, R4    ;Bring back R4 to A                   
            SUM:    ADD A, #30H  ;Add 30 to A                     
    RET                             
    END                            

CodePudding user response:

I think you misunderstood the requirement for output: R7 holds the address in IRAM where you should store the conversion result.

You can use R0 or R1 to store values indirectly in IRAM. Therefore, you will copy R7 to, let's say, R0. Then store each ASCII character at the pointed location, and don't forget to increment the pointer:

    MOV     A, R7                ;Get IRAM address from register R7
    MOV     R0, A                ;Set IRAM address to register R0

    MOV     A, R5                ;Get upper data byte from register R5
    SWAP    A                    ;Swap nibbles in A
    ANL     A, #0FH              ;Get the upper BCD digit
    ACALL   ASCII                ;Convert to ASCII
    MOV     @R0, A               ;Store the upper digit´s ASCII code
    INC     R0                   ;Increment IRAM pointer

    MOV     A, R5                ;Get upper data byte from register R5
    ANL     A, #0FH              ;Get the lower nibble
    ACALL   ASCII                ;Convert to ASCII
    MOV     @R0, A               ;Store the lower digit's ASCII code
    INC     R0                   ;Increment IRAM pointer

    MOV     A, R6                ;Get lower data byte from register R6
    SWAP    A                    ;Swap nibbles in A
    ANL     A, #0FH              ;Get the upper BCD digit
    ACALL   ASCII                ;Convert to ASCII
    MOV     @R0, A               ;Store the upper digit´s ASCII code
    INC     R0                   ;Increment IRAM pointer

    MOV     A, R6                ;Get lower data byte from register R6
    ANL     A, #0FH              ;Get the lower nibble
    ACALL   ASCII                ;Convert to ASCII
    MOV     @R0, A               ;Store the lower digit's ASCII code
    RET                                       
  • Related