Long multiply, 8 bits at a time You are given a 16-bit machine and told to implement multiplication of arbitrary-sized integers.  Your registers can only hold 16-bit numbers, and the biggest multiply instruction takes two 8-bit inputs and generates a 16-bit result.

Your program must take as input two arbitrary-sized positive numbers and output their product.  Each input number is encoded on its own line as a little-endian byte array where each byte is a 2-digit hex number.  The output must be similarly formatted.  Perhaps best explained with an example:

input

1f 4a 07
63 a3


output

fd 66 03 a7 04


which encodes the multiplication 477727*41827=19981887229.

You can assume that the last (most significant) byte of each input number is nonzero, and the last chunk of the number you output must be nonzero.  Both input numbers will be at most 100 bytes long.

Smallest code wins.

Remember, the biggest multiply you are allowed to use is 1 byte * 1 byte, and no integer types bigger than 2 bytes!
Perl, 137 characters

($x,$y)=&lt;&gt;;while($x=~s/.. *//s){$e=hex$&amp;;$i=0;$s=$r[$i]+=$e*hex,$r[$i]&amp;=255,$r[++$i]+=$s&gt;&gt;8 for$y=~/.. */gs;$y="00$y"}printf'%02x 'x@r,@r


Caveats


Sometimes prints an extra 00 byte at the end of the result. Of course the result is still correct even with that extra byte.
Prints an extra space after the last hex byte in the result.


Explanation

The explanation is going to be a bit long, but I think most people here will find it interesting.

First of all, when I was 10 years old, I was taught the following little trick. You can multiply any two positive numbers with this. I will describe this using the example of 13 × 47. You start by writing the first number, 13, and dividing it by 2 (round down each time) until you reach 1:

13
 6
 3
 1


Now, next to the 13 you write the other number, 47, and keep multiplying it by 2 the same number of times:

13     47
 6     94
 3    188
 1    376


Now you cross out all the lines where the number on the left is even. In this case, this is only the 6. (I can’t do strike-through in code, so I’ll just remove it.) Finally, you add all the remaining numbers on the right:

13     47
 3    188
 1    376
     ----
      611


And this is the right answer. 13 × 47 = 611.

Now, since you are all computer geeks, you will have realised that what we’re actually doing in the left and right columns is x &gt;&gt; 1 and y &lt;&lt; 1, respectively. Furthermore, we add the y only if x &amp; 1 == 1. This translates directly into an algorithm, which I’ll write here in pseudocode:

input x, y
result = 0
while x &gt; 0:
    if x &amp; 1 == 1:
        result = result + y
    x = x &gt;&gt; 1
    y = y &lt;&lt; 1
print result


We can re-write the if to use a multiplication, and then we can easily change this so that it works on a byte-by-byte basis instead of bit-by-bit:

input x, y
result = 0
while x &gt; 0:
    result = result + (y * (x &amp; 255))
    x = x &gt;&gt; 8
    y = y &lt;&lt; 8
print result


This still contains a multiplication with y, which is arbitrary-size, so we need to change that into a loop too. We’ll do that in Perl.

Now translate everything to Perl:


$x and $y are the inputs in hex format, so they have the least significant byte first.
Thus, instead of x &gt;&gt; 8 I do $x =~ s/.. *//s. I need the space+star because the last byte might not have a space on it (could use space+? too). This automatically puts the removed byte (x &amp; 255) into $&amp;.
y &lt;&lt; 8 is simply $y = "00$y".
The result is actually a numerical array, @r. At the end, each element of @r contains one byte of the answer, but halfway through the calculation it may contain more than one byte. I’ll prove to you below that each value is never more than two bytes (16 bits) and that the result is always one byte at the end.


So here is the Perl code unravelled and commented:

# Input x and y
($x, $y) = &lt;&gt;;

# Do the equivalent of $&amp; = x &amp; 255, x = x &gt;&gt; 8
while ($x =~ s/.. *//s)
{
    # Let e = x &amp; 255
    $e = hex $&amp;;

    # For every byte in y... (notice this sets $_ to each byte)
    $i = 0;
    for ($y =~ /.. */gs)
    {
        # Do the multiplication of two single-byte values.
        $s = $r[$i] += $e*hex,
        # Truncate the value in $r[$i] to one byte. The rest of it is still in $s
        $r[$i] &amp;= 255,
        # Move to the next array item and add the carry there.
        $r[++$i] += $s &gt;&gt; 8
    }

    # Do the equivalent of y = y &lt;&lt; 8
    $y = "00$y"
}

# Output the result in hex format.
printf '%02x ' x @r, @r


Now for the proof that this always outputs bytes, and that the calculation never generates values greater than two bytes. I’ll prove this by induction over the while loop:


The empty @r at the beginning clearly has no values greater than 0xFF in it (because it has no values in it at all). This concludes the base case.
Now, given that @r contains only single bytes at the beginning of each while iteration:


The for loop explicitly &amp;=s all values in the result array with 255 except the last one, so we only need to look at that last one.
We know that we always remove only one byte from $x and $y:


Therefore, $e*hex is a multiplication of two single-byte values, which means it’s in the range 0 — 0xFE01.
By the inductive hypothesis, $r[$i] is one byte; therefore, $s = $r[$i] += $e*hex is in the range 0 — 0xFF00.
Therefore, $s &gt;&gt; 8 is always one byte.

$y grows an extra 00 in each iteration of the while loop:


Therefore, in every iteration of the while loop, the inner for loop runs for one more iteration than it did in the previous while iteration.
Therefore, the $r[++$i] += $s &gt;&gt; 8 in the last iteration of the for loop always adds $s &gt;&gt; 8 to 0, and we already established that $s &gt;&gt; 8 is always one byte.

Therefore, the last value stored in @r at the end of the for loop is also a single byte.



This concludes a wonderful and exciting challenge. Thanks a lot for posting it!