0

I need to extract the digits from a binary number.

For example, I have 62 number in decimal and the two digits are 6 and 2. In binary:

62 = 00111110  --> 8 bit value

digit 6 = 0110 --> 4 bit value
digit 2 = 0010 --> 4 bit value

I need an algorithm to extract the digits and I'm using the asm for pic16f628a microcontroller.

There is an example of I want:

ORG     0x00
MOVLW   B'00111110' ; 62
MOVWF   0X30
; ... CODE TO EXTRACT THE DIGITS FROM VALUE IN REGISTER 0X30
; The register 0X31 is loaded with '00000110' (digit 6)
; The register 0X32 is loaded with '00000010' (digit 2)

But it can also be with C or C ++.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ignacior
  • 897
  • 2
  • 15
  • For x of 2 digits, can you use this? ((x/10)<<4)|((x%10)) – Mohamed Akram Jul 03 '21 at 00:07
  • [this link](https://www.gabrielececchetti.it/Teaching/CalcolatoriElettronici/Docs/i8086_instruction_set.pdf) has all 8086 assembly instructions, I think AAA instruction will do the work – Mohamed Akram Jul 03 '21 at 00:10
  • 1
    @MohamedAkram: PIC is a different architecture from 8086. Also, I don't think AAA is useful for the general case, you'd need AAM (division) to separate a number into 2 decimal digits. – Peter Cordes Jul 03 '21 at 00:22
  • 1
    @Ignactior: to get *decimal* digits out, you need to divide and modulo by 10. You might implement that with a bit-manipulation trick, but there's nothing really efficient like there would be to get the binary digits or the hex digits. – Peter Cordes Jul 03 '21 at 00:26
  • For numbers from 0..99 you can use lookup table hex to dec. – GJ. Jul 11 '21 at 20:40

0 Answers0