MOV 30H,#63
MOV A,30H
CALL HEX_2_BCD
MOV 31H,A
HEX_2_BCD:
MOV B,#10
DIV AB
SWAP A
ADD A,B
RET
The code that converts to BCD is wrong. Upon returning from CALL HEX_2_BCD
, the A
register holds the BCD. Fine, but then the code needlessly falls through in the conversion code. This is not very exemplary. Nonetheless, you can extract from it the essential instructions that perform the BCD conversion:
MOV B, #10
DIV AB
SWAP A
ADD A, B
Because in the new program every byte from the source range will have to be written to the destination range, it will be easiest if we conditionally jump for the bytes that have an odd values and that can be written unmodified.
Furthermore, if we modify the test for even/odd a bit, we wouldn't have to reload the A
register. Remember the A
register is a Special Function Register whose bits are bit addressable using bit addresses 0E0h to 0E7h.
MOV R0, #30h
MOV R1, #40h
Loop:
MOV A, @R0 ; Read from source
JB 0E0h, IsOdd ; Go write unmodified
IsEven:
...
IsOdd:
MOV @R1, A ; Write to destination
INC R1
INC R0
CJNE R0, #40h, Loop
It's probably a good idea to verify if the number that you want to convert to BCD is less than 100, else the converted result will be kinda worthless!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…