Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How Convert 32 bit to BCD in 8085 Assembly Code

Status
Not open for further replies.

panda1234

Full Member level 2
Joined
Jan 22, 2015
Messages
125
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Activity points
1,172
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
using the Decimal Adjust Accumulator instruction after adding each byte
be sure to add in the "carry values" from Add and Decimal Adjust
the Decimal Adjust Accumulator instruction keeps the sum in bcd format,
otherwise it is in binary

your answer should now be 0000000003

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

your answer should now be 0000000115, (the decimal value of hexadecimal 73)

then add 0000000256, 5 times,
then add 0000004096, 10 times,
then add 0000054536, 13 times
then add 0001048576, 8 times
then add 0016777236, 15 times
then add 0268435456, 14 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
START   LD HL CONSTS ;HL IS LOADED WITH ADDRESS OF CONSTANTS
               LD BC 03   
               CALL ADDSUB  
               LD BC 07     ; BC IS LOADED WITH EACH OF THE EXAMPLE HEADECIMAL  ;VALUES
      	       CALL ADDSUB  ; 0xEF8DA573 STARTING WITH LEAT SIGNIFICANT NIBBLE
	       LD BC 05
               CALL ADDSUB 
               LD BC 0A
      	       CALL ADDSUB 
	       LD BC 0D
               CALL ADDSUB 
               LD BC 08
      	       CALL ADDSUB 
	       LD BC 0F
               CALL ADDSUB 
               LD BC 0e
      	       CALL ADDSUB 
               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
        
ADDSUB  LD DE  ANSWR   ; LOAD ADDRESS OF LSB OF ANSWER IN DE REGISTER
                 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
                DAA            ; DECIMAL ADJUST THE ANSWER TO BINARY CODED DECIMAL
                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
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top