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


.
.
.

更新於:2020年6月27日

516 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告