Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

The solution I suggest is considering you always have two hexadecimal digits (in the example you used 4 and 7, but it could be a A and a E). * and 7 is 0111, not 1111 as written

Here is a tip that 1 hexadecimal digit correspond to exactly 4 binary digits. So, for example 0xA245 is 1010 (A) 0010 (2) 0100 (4) 0101 (5).

Finally, my suggestion is only to execute a right rotation of 4 bits to obtain the high nibble (4). To obtain the 7, just mask the byte against a 0 at the high nibble (0x0F).

In C it would be something like this: highNibble = value >> 4; lowNibble = Value & 0x0F;

I hope that I made myself clear and that it helps you to solve this problem.

You're basically dealing with BCD representation here. There are many resources on converting from binary to BCD, after which it's incredibly easy to get the individual digits.

For example, use these instructions to convert from binary to bcd, then simply loop over the subaddresses of the BCD digits, reading each as a single standalone byte. The code is simple, fast, and to the point.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top