Is it possible to multiply only with the &, |, + and - operators?

4

I have to create a program with assembly language K & S Model 2 that multiplies a number by another, the problem is that this language does not provide a multiplication operation. I thought of doing multiplications with additions, for example:

3 x 5 = 5 + 5 + 5 (3 vezes o 5) = 15

This language also does not provide a direct way to loop (or loop), nor a statement of comparison of records, for example to see if they are equal, or if it has the same number.

This assembly language provides only bitwise operators with which I am not very familiar. I know that doing AND bitwise is like doing a multiplication of numbers but converted into binaries, for example:

3 & 2

First we have to convert to binary:

  11 (3)
& 10 (2)
==== 
  10 (2)

I'm really not seeing how I can do a multiplication using only this operations, if I could at least loop it anyway ...

    
asked by anonymous 21.09.2014 / 19:07

2 answers

6

You can do a [integer] multiplication using only the sum ( + ) as long as you have access to a conditional drift operator that skips if that record is zero. For your link, there are two operators like this: BZERO (defaults if result in ALU is zero) and BNEG (defaults if result in ALU is less than zero).

I do not quite understand how your architecture works (nor do I intend to go into it), so I'll respond with pseudo-code:

  • Save -1 to A ;
  • Save the multiplier in B ;
  • Keep multiplying in record C ;
  • Save 0 to D ;
  • Add the records A with B and save the result in B ; (i.e., decreasing the multiplier)
  • If the result is less than zero, go to 9 ;
  • Add the records C and D and save the result in D ;
  • Return to 5 ;
  • End of program. (% with% contains multiplication result)
  • That is, each time you decrease the multiplier, you add the multiplier to the result. You will then be calculating D by adding n x m m times.

    Note: This algorithm works even when n is negative, but not when m is negative - it goes into an infinite loop. The solution in this case would be to have two algorithms: one that decrements until it reaches zero, and one that increases until it reaches zero, and choose between one and another depending on whether n is positive or negative.

        
    21.09.2014 / 19:40
    1

    I did it the following way and it worked:

    1) LOAD R1 22
    2) LOAD R2 23
    3) LOAD R3 24
    4) ADD R0 R0 R3
    5) STORE 21 R0
    6) ADD R2 R2 R1
    7) STORE 23 R2
    8) BZERO 9
    9) BRANCH 4
    10) HALT

        
    02.11.2016 / 23:24