是否仅从ADD和NAND转移逻辑? [英] Shift Right Logical from just ADD and NAND?

查看:80
本文介绍了是否仅从ADD和NAND转移逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用一种非常简单的汇编语言制作一个乘法器,在该语言中,我具有BEQ,NAND和ADD来创建SRL。我还必须将乘数保持在50行以下(到目前为止已使用16行),因此希望解决方案可以循环抛出。

I'm making a multiplier in a very simple assembly language in which I have BEQ, NAND, and ADD to create a SRL. I also have to keep the multiplier under 50 lines (16 used thus far) so hopefully the solution can be thrown in a loop.

编辑:我的问题是我如何才能实现仅具有NAND和ADD的SRL

My question is how can I implement an SRL with just a NAND and an ADD

想法虽然效率很低,但是也许有人可以改善它:

Had an idea although it is very inefficient, maybe someone can improve it:

将减量a减1。将该值存储在b中。添加b和b并存储在c中。以a表示c,如果为true,则b是a(即srl)的一半。唯一的问题是在某些情况下它必须循环数千次。仍然对其他想法持开放态度。

Decrement say, a, by 1. Store that value in b. Add b and b and store in c. Beq c with a, if it's true then b is half of a, aka srl. Only problem is it would have to loop thousands of times in some cases. Still open to other ideas.

推荐答案

您实际上并不需要进行右移来实现乘法。看看如何做到这一点,在C中使用示例代码:

You don't really need a right shift to implement multiplication. See how this can be done, sample code in C:

#include <stdio.h>

typedef unsigned char uint8;
typedef unsigned short uint16;

uint16 Mul8x8(uint8 a, uint8 b)
{
  int cnt;
  uint16 prod = 0;

  for (cnt = 8; cnt > 0; cnt--)
  {
    prod += prod;

    if (a & 0x80)
      prod += b;

    a += a;
  }

  return prod;
}

const uint8 Multipliers[][2] =
{
  { 0x00, 0x01 },
  { 0x01, 0x00 },
  { 0x33, 0x10 },
  { 0x11, 0x0C },
  { 0x0F, 0x0F },
  { 0x80, 0x80 },
  { 0xFF, 0xFF },
};

int main(void)
{
  int i;

  for (i = 0; i < sizeof(Multipliers) / sizeof(Multipliers[0]); i++)
  {
    uint8 a = Multipliers[i][0];
    uint8 b = Multipliers[i][1];

    uint16 p = a * b;
    uint16 p2 = Mul8x8(a, b);

    printf("0x%02X * 0x%02X = 0x%04X %c= 0x%04X\n",
           a, b, p, "!="[p == p2], p2);
  }

  return 0;
}

输出([ideone])( http://ideone.com/NwsykN )):

Output ([ideone])(http://ideone.com/NwsykN)):

0x00 * 0x01 = 0x0000 == 0x0000
0x01 * 0x00 = 0x0000 == 0x0000
0x33 * 0x10 = 0x0330 == 0x0330
0x11 * 0x0C = 0x00CC == 0x00CC
0x0F * 0x0F = 0x00E1 == 0x00E1
0x80 * 0x80 = 0x4000 == 0x4000
0xFF * 0xFF = 0xFE01 == 0xFE01

这篇关于是否仅从ADD和NAND转移逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆