16bit binary to bcd

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does anyone have an assembler 16bit binary to bcd converter, or a link in the right direction? I know how to do 8 bits, but the 16 is throwing me for a loop.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.avr-asm-tutorial.net/...

Go to

Quote:
From binary to BCD
; Bin2ToBcd5
; ==========
; converts a 16-bit-binary to a 5-digit-BCD

RES

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Many thanks!!
I never heard of this site. Looks kinda cool

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a more efficient method "shift-plus-3".

I have written the sample code as below. The input hex value
put in R17:R16, and this function will return R20:R19:R18
with the BCD value.

; r17:r16 = HEX value
; r20:r19:r18 = BCD value
hexToBcd:
    push    r16
    push    r17
    push    r21
    push    r22
    push    xl
    push    xh
    clr     r18
    clr     r19
    clr     r20
    clr     xh
    ldi     r21, 16
hexToBcd1:
    ldi     xl, 20 + 1
hexToBcd2:
    ld      r22, -x
    subi    r22, -3
    sbrc    r22, 3
    st      x, r22
    ld      r22, x
    subi    r22, -0x30
    sbrc    r22, 7
    st      x, r22
    cpi     xl, 18
    brne    hexToBcd2
    lsl     r16
    rol     r17
    rol     r18
    rol     r19
    rol     r20
    dec     r21
    brne    hexToBcd1
    pop     xh
    pop     xl
    pop     r22
    pop     r21
    pop     r17
    pop     r16
    ret
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
;-----------------------------------------------------------------------------
; bin2BCD16p5 - 16-bit binary to 5-digit packed BCD conversion (0..65535)
; prevod 16-bit binarniho cisla na peticiferne cislo v BCD
;
; BCD16res0:BCD16res1:BCD16res2 <- bin16r0:bin16r1
;
; words:  29 + ret
; cycles: 29/200 + ret [min/max]
;
; author: Stanislav Maslan, s.maslan@seznam.cz
;-----------------------------------------------------------------------------
	.def	BCD16res0=R13
	.def	BCD16res1=R14
	.def	BCD16res2=R15
	.def	bin16r0=R16
	.def	bin16r1=R17
	.def	BB16_cnt1=R30
	.def	BB16_cnt2=R31

bin2BCD16p5:	ser	BB16_cnt2
	mov	BCD16res2,BB16_cnt2
BB16p5_L10k:	inc	BCD16res2
	subi	bin16r0,low(10000)
	sbci	bin16r1,high(10000)
	brcc	BB16p5_L10k
	subi	bin16r0,low(-10000)
	sbci	bin16r1,high(-10000)
	ldi	BB16_cnt1,(256-16)
BB16p5_L1k:	subi	BB16_cnt1,(-16)
	subi	bin16r0,low(1000)
	sbci	bin16r1,high(1000)
	brcc	BB16p5_L1k
	subi	bin16r0,low(-1000)
	sbci	bin16r1,high(-1000)
	mov	BCD16res1,BB16_cnt2
BB16p5_L100:	inc	BCD16res1
	subi	bin16r0,low(100)
	sbci	bin16r1,high(100)
	brcc	BB16p5_L100
	subi	bin16r0,low(-100)
	or	BCD16res1,BB16_cnt1
	ldi	BB16_cnt1,(256-16)
BB16p5_L10:	subi	BB16_cnt1,(-16)
	subi	bin16r0,10
	brcc	BB16p5_L10
	subi	bin16r0,-10
	mov	BCD16res0,BB16_cnt1
	or	BCD16res0,bin16r0
	ret
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Great stuff!!

I have used the shift-plus 3 method in CPLD's. I'll keep this in mind for future micro jobs

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user