Skip to content

Instantly share code, notes, and snippets.

@clausecker
Last active December 2, 2015 16:31
Show Gist options
  • Select an option

  • Save clausecker/af6ad9283b3b1845cccd to your computer and use it in GitHub Desktop.

Select an option

Save clausecker/af6ad9283b3b1845cccd to your computer and use it in GitHub Desktop.
Routines to convert BCD to DPD
.section .text
.type bcd2dpd,@function
.globl bcd2dpd
# convert three digits in packed BCD to DPD
# input in edi, all but the lower three nibbles are ignored
# does not check if nibble > 9
.align 8
bcd2dpd:
# prepare arguments for call to decimal2dpd
mov %edi,%edx
mov %edi,%esi
movzbl %dh,%edi
and $0xf,%esi
shr $4,%edi
and $0xf,%edx
jmp decimal2dpd
.size bcd2dpd,.-bcd2dpd
.type decimal2dpd,@function
.globl decimal2dpd
# convert three digits abc to DPD
# SysV calling convention: a in edi, b in esi, c in edx
# assumes 0 <= a, b, c < 10
.align 8
decimal2dpd:
mov %esi,%eax
shl $4,%eax
or %edx,%eax
and $0021,%eax
shl $7,%edi
or %edi,%eax
and $016,%edx
btr $10,%eax
# at this point, eax = (a & 7) << 4 | (b & 1) << 4 | (c & 1) and CF is set if a > 7
# and edi is free for use
jc .Lagt7
# here a < 8
shl $4,%esi
btr $7,%esi
jc .Lbgt7
# here a < 8 and b < 8
or %esi,%eax
or %edx,%eax
ret
.align 8
.Lbgt7: # here a < 8 and b > 7
or $0012,%eax
shl $4,%edx
mov $0104,%edi
btr $7,%edx
cmovc %edi,%edx
or %edx,%eax
ret
.align 8
.Lagt7: # here a > 7
shl $7,%edx # eax = (a & 1 | c & 6) << 7 | (b & 1) << 5 | (c & 1)
or %edx,%eax # now eax[10] is set if c > 7
btr $10,%eax
jc .Lcgt7
# here a > 7 and c < 8
or $0014,%eax
shl $4,%esi
mov $0002,%edi
btr $7,%esi
cmovc %edi,%esi
or %esi,%eax
ret
.align 8
.Lcgt7: # here a > 7 and c > 7
and $016,%esi
or $0056,%eax
shl $7,%esi
mov $0100,%edi
btr $10,%esi
cmovc %edi,%esi
or %esi,%eax
ret
.size decimal2dpd,.-decimal2dpd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment