在8051中将四位数的十六进制转换为ASCII
我们已经看到了如何将十六进制数字转换为其等效的ASCII。在本节中,我们将看到如何将两字节(4位)的十六进制数字转换为ASCII。这些数字的每个半字节都转换为其ASCII值。
我们使用一个子例程将十六进制数字转换为ASCII。在此程序中,我们多次调用该子例程。
在内存中,我们在20H和21H位置存储2字节的十六进制数。转换后的ASCII值存储在位置30H到33H。
十六进制数是2FA9H。ASCII等效值为32464139。
。
。
。
。
。
。
程序
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在此程序中,我们将数字带入累加器。然后,为了分别获取十六进制数字,我们将应用屏蔽逻辑。
在这里,HEX2ASC子例程使用寄存器A和B。A的作用类似于输入自变量,而B用作输出。
输出结果
。
。
。
。
。
。