assembly 将16位无符号整数转换为由十六进制数字组成的ASCII字符串

sxpgvts3  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(147)

我有这样的输入和输出:

  • 输入:2个寄存器(R5、R6)中的数字、ASCII字符串的起始地址(指针)
  • MOV R5,#HIGH(NUMBER)``MOV R6,#LOW(NUMBER)
  • 输出:从给定地址开始的转换ASCII字符串(R7)
  • MOV R7,#STR_ADDR_IRAM

代码运行良好,但我必须纠正这个问题:

  • 输出字节被逐个计算并放入寄存器R7,但每个新的结果字符覆盖前一个。
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
11dmarpk

11dmarpk1#

我想您误解了输出的要求:R7保存 * IRAM* 中应存储转换结果的地址。
你可以使用R0或R1来间接地将值存储到IRAM中。因此,你可以将R7复制到,比如说,R0。然后将每个ASCII字符存储到指定的位置,并且不要忘记递增指针:

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

相关问题