8051中四位十六进制转换为ASCII
我们已经了解了如何将十六进制数字转换为其ASCII等价物。在本节中,我们将了解如何将两字节(4位)十六进制数转换为ASCII。这些数字的每个半字节都转换为其ASCII值。
我们使用一个子程序将十六进制数字转换为ASCII。在这个程序中,我们多次调用该子程序。
在内存中,我们将2字节的十六进制数存储在20H和21H位置。转换后的ASCII值存储在30H到33H位置。
十六进制数为2FA9H。ASCII等价物为32 46 41 39。
地址 | 值 |
---|---|
. . . | |
20H | 2FH |
21H | A9H |
. . . | |
30H | 00H |
31H | 00H |
32H | 00H |
33H | 00H |
. . . |
程序
MOVR0,#20H;set source address 20H to R0 MOVR1,#30H;Set destination address 30H to R1 MOVR5,#02H;Set the counter as 2 for 2-bytes LOOP: MOVA,@R0; Get the first byte from location 20H MOVR4,A; Store the content of A to R4 SWAPA; Swap Nibbles of A ANLA,#0FH; Mask upper nibble of A ACALL HEX2ASC; Call subroutine to convert HEX to ASCII MOV@R1,B; Store the ASCII to destination INCR1; Increment the dest address MOVA,R4; Take the original number again ANLA,#0FH; Mask upper nibble of A ACALL HEX2ASC ; Call subroutine to convert HEX to ASCII MOV@R1,B; Store the ASCII to destination INCR1; Increment the destination address INCR0; Increase R0 for next source address DJNZR5,LOOP ; Decrease the byte count, and iterate HALT: SJMP HALT ;Stop the program ;This is a subroutine to convert Hex to ASCII. It takes A and B registers. A is holding the input, and B is for output HEX2ASC: MOVR2,A; Store the content of A into R2 CLRC;Clear the Carry Flag SUBBA,#0AH;Subtract 0AH from A JCNUM ;When carry is present, A is numeric ADDA,#41H;Add41H for Alphabet SJMP STORE ;Jumpto store the value NUM: MOVA,R2;Copy R2 to A ADDA,#30H;Add 30H with A to get ASCII STORE: MOVB,A;Store A content to B RET
在这个程序中,我们将数字放入累加器。然后,为了分别获取十六进制数字,我们将应用掩码逻辑。
这里的HEX2 ASC子程序使用寄存器A和B。A充当输入参数,B充当输出。
输出
地址 | 值 |
---|---|
. . . | |
20H | 2FH |
21H | A9H |
. . . | |
30H | 32H |
31H | 46H |
32H | 41H |
33H | 39H |
. . . |
广告