Search code examples
assembly8085

Split a 2 digits number in binary to single digit in binary


I have 47, in binary it's 0010 1111. I need to split 47 so I get 4 in binary 0000 0100 and 7 in binary 0000 0111.

I have see this answer but I wonder, as Intel 8085 doesn't have a div instruction I have to divide by doing consecutive subtractions, how do I get modulus as doing it so?


Solution

  • If your processor doesn't have a divide instruction (or BCD conversion instructions, which would be another way), you'll just have to do repeated subtraction. The pseudo-code for this would go something like:

        val = 47
    
        units = val
        tens = 0
    loop:
        if units < 10 goto done
        units = units - 10
        tens = tens + 1
        goto loop
    done:
        ;; tens = 4, units = 7.
    

    Apologies for the rather BASIC coding style there, I just tried to make it a little closer to how it would look in assembler language. Otherwise, I would have used a while loop :-)

    Tho modulus is what's left in the units variable once it drops below ten.

    For completeness (and possibly extra marks if this is a homework assignment, nudge nudge, wink wink), since a single octet can handle up to 255:

        val = 247
    
        units = val
        tens = 0
        hundreds = 0
    loop1:
        if units < 100 goto loop2
        units = units - 100
        hundreds = hundreds + 1
        goto loop1
    loop2:
        if units < 10 goto done
        units = units - 10
        tens = tens + 1
        goto loop2
    done:
        ;; hundreds = 2, tens = 4, units = 7.
    

    As proof this method works, you can try the following Python code:

    val = 47
    
    units = val
    tens = 0
    while units >= 10:
        units = units - 10
        tens = tens + 1
    
    print "Tens = %d, Units = %d"%(tens,units)
    

    which does indeed output:

    Tens = 4, Units = 7
    

    In terms of using those values to drive a LED device, a simple lookup table is probably easiest. Construct an array of values that you need to write to the two 8-bit memory addresses then use the digit to lookup those values for writing to the device.

    I'll use a simplified seven-segment LED (plus decimal point) since it's easier to understand:

        a
       ---
    f |   | b
       -g-
    e |   | c
       ---     . h
        d
    

    Let's say in this example that you write a single byte abcdefgh to a memory-mapped location and the bits in this byte control what segments are on.

    The values for the different digits (without the decimal point) are (untested, so may need some tweaking):

       abcdefgh
    0  11111100
    1  01100000
    2  11011010
    3  11110010
    4  01100110
    5  10110110
    6  10111110
    7  11100000
    8  11111110
    9  11110110
    

    Given the value four, you would simply look up the byte at offset 4 in that table (using simple addition of the base address of the table and the value), then extract that byte from memory and use it to set the LED segments. That value 01100110 will set segments b, c, f and g, giving you:

    |   |
     ---
        |
    

    Your particular situation is a little more complicated since you have a fifteen-segment LED and have to write a word rather than a byte, but the theory is exactly the same.

    Have a table of values that you need to write for each digit and use the values calculated from my algorithm above to offset into that table.