# Multiplication algorithm on a 4004

So I was playing around with this emulator of the Intel 4004 and decided I wanted to set a challenge for myself. The first use for the 4004 was in a calculator, so I decided that I would try to code golf every operator on said calculator(addition, subtraction, multiplication, division, decimal points, and the square root) in hexadecimal. Now this was my first code golf challenge I ever set for myself, like ever, and I am relatively new at coding in general, but I thought it would be a fun thing to try out. This was the code for multiplication, specifically 3*4(In order to change it, simply replace the nybbles following the Ds[except for the 0] with any number you want as long as the product is below 16 and the second and third Ds have the same nybble trailing them):

 D4 B1 D3 B0 D3 B2 A0 82 B0 D0 B1 F8 F3 14 12 B1 40 06 B0 92 F3 E0


Are there any flaws in my 22-byte design, and is there a way to shorten the code? For reference, the instruction set for the 4004 in binary is in this table: http://www.e4004.szyc.org/iset.html

• PS, please do animate mode for testing. Run mode is buggy. May 4, 2020 at 3:01
• I think you should use the [tips] tag. May 4, 2020 at 3:35
• Note that hardcoding the inputs in the program is not allowed as per our site defaults. Looking at the emulator, I think a good compromise would be "assume two input values are loaded in two registers, and output to accumulator (or another register)". May 4, 2020 at 3:40
• Ok, some of my code is setting the inputs so thanks for the clarification! May 4, 2020 at 3:49

## 4004 machine code, 12 bytes

This is the best code I could come up with using the same I/O as the provided code:

D4      LDM 4
B0      XCH R0
D3      LDM 3
F4      CMA
B1      XCH R1
D0      LDM 0
40 09   JUN TEST
:LOOP

Note that this does not handle overflow very well at all; you can add in a CLC to ensure that it at least calculates it correctly modulo 16, or in case the caller has left the carry set.
• Incoming carry can be ignored by using CLB instead of LDM 0, but I hadn't finished reading the instruction set at the time.