Continue to Site

# How Convert 32 bit to BCD in 8085 Assembly Code

Status
Not open for further replies.

#### panda1234

##### Full Member level 2
Hi,
I have a 32 bit binary and i want to convert it into 4 BCD digit how do that in 8085?

Here is a possible algorithm to do the conversion.

In this example:
0xEF8DA573 = a hexadecimal 32 bit number to convert to bcd
14, 15, 8, 13, 10, 5, 7, 3 = decimal equivalents of each nibble above
4,019,037,555 = the decimal result we want to end up with
4,294,967,295 = maximum possible decimal value of a 32 bit hexadecial number = 5 bytes long

we know the decimal value represented by each place of the 32 bit hexadecimal number
starting at the least significant

0000000001 constant decimal values of each nibble (5 bytes long)
0000000016
0000000256
0000004096
0000065536
0001048576
0016777216
0268435456

put each of these 5 byte long BCD numbers in memory

0000000000 the answer 5 bytes long

set aside 5 bytes in memory for the answer

take the first decimal value (0000000001) and add it to the answer 3 times
add all five bytes to the answer beginning with the least significant
the Decimal Adjust Accumulator instruction keeps the sum in bcd format,
otherwise it is in binary

next add in the second value (0000000016), to the answer 7 times

The code below is written to resemble Z80 assembly language. The Z80 runs 8085 machine code but Intel may have different mnemonics for 8085 assembly language. This is just an outline for writing the program for the 8085. It is not written to be compiled. It will not compile properly on any assembler.

Code:
              ; START OF THE PROGRAM
LD BC 03
LD BC 07     ; BC IS LOADED WITH EACH OF THE EXAMPLE HEADECIMAL  ;VALUES
CALL ADDSUB  ; 0xEF8DA573 STARTING WITH LEAT SIGNIFICANT NIBBLE
LD BC 05
LD BC 0A
LD BC 0D
LD BC 08
LD BC 0F
LD BC 0e
RESET        ; END OF THE PROGRAM

LOOPA     INC HL   ; INCREMENT HL BACK TO LSB OF THE CURRENT CONSTANT
INC HL
INC HL
INC HL
INC HL

SUB A       ; CLEAR THE CARRY FLAG AT THE BEGINNING
LD C 05     ; LD C WITH 5 TO COUNT THE NUMBER OF BYTE IN ANSWER

LOOPB    LD A (DE)   ; LOAD A BYTE OF THE ANSWER INTO THE ACCUMULATOR
ADDC A (HL) ; ADD WIT CARRY A BYTE OF THE CONSTANT
LD (DE) A   ; PUT ADJUSTED VALUE BACK INTO THE ANSWER LOCATION
DEC HL      ; MOVE THROUGH THE 5 BYTES OF THE CONSTANT
DEC DE      ; MOVE TO NEXT BYTE OF ANSWER
DEC C       ; DECREMENT C TO COUNT ADDING ALL 5 BYTES OF ANSWER
JNZ LOOPB   ; LOOP TILL ALL 5 BYTES ARE ADDED
DEC B
JNZ LOOPA   ; LOOP TO ADD THE CONSTANT TO THE ANSWER THE                            ; NUMBER  OF TIMES IN THE B REGISTER
RET         ; RETURN

;START OF DATA

DB 02   ;0268435456 CONSTANT FOR 16 TO THE 7TH POWER
DB 68
DB 43
DB 54
DB 56

DB 00  	;0016777216 CONSTANT FOR 16 TO THE 6TH POWER
DB 16
DB 77
DB 72
DB 16

DB 00  	;0001048576 CONSTANT FOR 16 TO THE 5TH POWER
DB 01
DB 04
DB 85
DB 76

DB 00 	;0000065536 CONSTANT FOR 16 TO THE 4TH POWER
DB 00
DB 06
DB 55
DB 36

DB 00	;0000004096 CONSTANT FOR 16 TO THE 3RD POWER
DB 00
DB 00
DB 40
DB 96

DB 00	;0000000016 CONSTANT FOR 16 TO THE 1ST POWER
DB 00
DB 00
DB 00
DB 16

DB 00	;0000000001 CONSTANT FOR 16 TO THE 0 POWER
DB 00
DB 00
DB 00
CONSTS  DB 01

DB 00	;ANSWER (WILL BECOME 04019037555)
DB 00   ;HAS TO BE INITIALIZED TO ALL 0'S
DB 00
DB 00
ANSWR    DB 00

panda1234

Points: 2