0

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?

Community
  • 1
  • 1
Ashir
  • 511
  • 2
  • 8
  • 24

3 Answers3

3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thank you, but this does not answer my question about geting a modulus in a pseudo division by consecutive subtraction... – Ashir Oct 21 '11 at 02:41
  • 1
    @Ashir: I suggest you look again. The modulus is what's left in the units variable after all the repeated subtractions. – paxdiablo Oct 21 '11 at 03:45
  • Yes, you are right, but I don't have number in decimal (47) I have it in accumulator as 8 bit binary (0010 1111). I need to split that 0010 1111 so I can show on 15 segment display the 4 (0000 0100) and the 7 (0000 0111). Your solution applies to decimal numbers not binary, besides, I don't know what number is going to be stored in accumulator. – Ashir Oct 23 '11 at 20:34
  • 1
    @Ashir, I'm not sure I totally understand your point. You talk about decimal and binary but that's irrelevant here - your _value_ is in the accumulator and the CPU is quite capable of manipulating these values by subtracting ten or adding one. If you have the binary value 00101111 (47) in the accumulator and you subtract 00001010 (10), you'll end up with 00100101 (37). I think you just need to try it and you'll find it works okay. – paxdiablo Oct 23 '11 at 22:32
  • I can't put in a 15 segment display a binary 47 (0010 1111), why? because the 15 segment display used in Intel 8085 used a 8 bit binary sequence (or a hex digit) to turn on the segments. So, considering the a 15 segment display has two parts you use an 8 bit sequence to turn on the inner or external part. If I want to "print" a 4 in a 15 segments display it would be CPI 18H (or 0001 1000 in binary) to turn on segments 11 and 12, and then MVI A,45H (or 0100 0101 in binary) to turn on segments 6, 2 and 0. Pretty much the same for "printing" a 7. – Ashir Oct 23 '11 at 23:20
  • Now, if I use a 47 binary value to turn on a 15 segment display I'm going to get a mess. If I turn on the inner leds using 0010 1111 I will turn on segments 13, 11, 10, 9 and 8. That's not a 4, not even a 7. If I use the same value to turn on external led it will turn on segments 5, 3, 2, 1 and 0 (note that segment 3 does not exist so it will not turn on even when you turn in on using a 1 bit on segment 3 space). – Ashir Oct 23 '11 at 23:46
  • If still don't understand what I'm talking about, here (http://www.megapix.com/?p=LBHZQ1RH.jpg) is a capture of the IDE I'm using. You can also try this (http://pastebin.com/Re3k6rG6) code in this ide (http://www.megaupload.com/?d=KMEEBDZI) – Ashir Oct 23 '11 at 23:47
  • 1
    @Ashir, I think I have it now. The confusion arose because your question mentions nothing about driving a LED device, just on how to get the integer division and remainder on a CPU that has no division operation. I'll update the answer with more info. – paxdiablo Oct 24 '11 at 00:51
  • My problem is not how to print anything on a 15 segments display but how to print something on a 15 segment display when you can't predict what you are going to print. If I add 9+9 (0000 1001 + 0000 1001) I will get 18 (0001 0010), but how do I split that 0001 0010 so I can get the 1 (0000 0001) and the 8 (0000 1000) separate so I can compare the binary value of each one and if the binary value for 8 match, then I know it's an 8 and I will send an 8 to the display. I need to split whatever value I get in accumulator so I can get individual digits. – Ashir Oct 24 '11 at 01:21
  • 1
    @Ashir, I've shown you how to do that. That's what the algorithms in the first part of the question do. They split a number like 47 into the two digits 4 and 7, or 247 into 2, 4 and 7. I'm not sure how much more explicit I can make that without writing the 8085 code for you. – paxdiablo Oct 24 '11 at 02:34
  • Yeah, it doit, when it's a decimal number, but what I store in accumulator is not 47 but 0010 1111... – Ashir Oct 24 '11 at 03:24
  • 1
    @Ashir: you need to explain more clearly what the problem is. The value in the accumulator is both 47 _and_ 00101111 _and_ 0x2f. It's the _value,_ not the representation of that value. The base you use for specifying it is irrelevant. If you want that value to be split into tens and units, my algo will work. Given the comments length on this Q indicating that there's some communication problem between us, maybe you should consider a new question, specifying exactly what you need. That will open your problem up to a wider audience. – paxdiablo Oct 24 '11 at 03:35
  • Wait, you mean that if I do 0010 1111 - 32 the result will be 0000 1111 (15) even if I did the operation en decimals and not binary?. If that so, then your code will perfectly help me achive what I whant to do. – Ashir Oct 24 '11 at 03:52
  • 1
    @Ashir: yes, the representation of the number is not stored in the accumulator, just its value. You can start with hex 20, add decimal 7, then subtract binary 111 and you'll end up with octal 40, your original starting value :-) – paxdiablo Oct 24 '11 at 03:57
0

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.

  • That's what I tough at first, but as I say in my question, the quickest way (found here) would be do a division and put the modulus on a stack. My problem is that I can't divide so I must subtract till I get zero to make a pseudo division. How can I get a modulus doing such thing? Oh, and I dir write it alright, 47 in binary it's 0010 1111, and 7 it's 0111. I just space the bits in 4 bits block. – Ashir Oct 21 '11 at 02:39
  • @osmar oxcf, you're pre-supposing BCD storage but it's in fact binary. If it was BCD it would be easily doable with bit masks and shifts but, unfortunately, that's not the case. – paxdiablo Oct 21 '11 at 03:51
0

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.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125