将二进制中的 2 位数字拆分为二进制中的一位数

发布于 2024-12-11 06:34:32 字数 248 浏览 1 评论 0原文

我有 47 个,二进制为 0010 1111。我需要拆分 47,所以我得到 4 个二进制 0000 0100 和 7 个二进制 0000 0111。

我看到 这个答案但我想知道,因为Intel 8085 没有 div 指令,我必须通过连续减法进行除法,如何获得模数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落成白 2024-12-18 06:34:32

如果您的处理器没有除法指令(或 BCD 转换指令,这将是另一种方式),您只需重复进行减法即可。其伪代码类似于:

    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.

对于相当基本的编码风格表示歉意,我只是试图使其更接近汇编语言的外观。否则,我会使用 while 循环:-)

一旦模数降至 10 以下,模数就是单位变量中剩下的值。

为了完整性(如果这是家庭作业,可能还有额外的分数,轻推轻推,眨眼眨眼),因为单个八位字节可以处理最多 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.

作为此方法有效的证明,您可以尝试以下 Python 代码:

val = 47

units = val
tens = 0
while units >= 10:
    units = units - 10
    tens = tens + 1

print "Tens = %d, Units = %d"%(tens,units)

它确实输出:

Tens = 4, Units = 7

使用这些值来驱动 LED 器件而言,简单的查找表可能是最简单的。构造一个需要写入两个 8 位内存地址的值数组,然后使用该数字查找这些值以写入设备。

我将使用简化的七段 LED(加上小数点),因为它更容易理解:

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

假设在此示例中,您将一个字节 abcdefgh 写入内存映射位置,并且将位写入该字节控制哪些段在上。

不同数字的值(不带小数点)是(未经测试,因此可能需要一些调整):

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

给定值 4,您只需在该表中查找偏移量 4 处的字节(使用基地址的简单相加)表和值),然后从内存中提取该字节并使用它来设置 LED 段。该值 01100110 将设置段 bcfg,给出你:

|   |
 ---
    |

你的具体情况有点复杂,因为你有一个 15 段 LED,并且必须写一个字而不是一个字节,但原理是完全相同的。

准备一个需要为每个数字写入的值表,并使用根据我上面的算法计算出的值来偏移到该表中。

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.

甜尕妞 2024-12-18 06:34:32

我建议的解决方案是考虑您始终有两个十六进制数字(在示例中您使用了 4 和 7,但也可能是 A 和 E)。
* 和 7 是 0111,而不是写的 1111

这里提示一下,1 个十六进制数字正好对应 4 个二进制数字。
因此,例如 0xA245 是 1010 (A) 0010 (2) 0100 (4) 0101 (5)。

最后,我的建议是仅执行 4 位的右旋转以获得高半字节 (4)。要获得 7,只需在高半字节 (0x0F) 处用 0 屏蔽该字节即可。

在 C 语言中,它会是这样的:
高半字节=值>> 4;
lowNibble = 值 & 0x0F;

我希望我已经说清楚了,这可以帮助您解决这个问题。

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.

爱情眠于流年 2024-12-18 06:34:32

您基本上在这里处理 BCD 表示。有很多关于从二进制转换为 BCD 的资源,之后获取各个数字变得非常容易。

例如,使用这些指令将二进制转换为bcd,然后简单地循环BCD 数字,将每个数字读取为单个独立字节。代码简单、快速、切题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文