7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ PS, please do animate mode for testing. Run mode is buggy. \$\endgroup\$ – Nip Dip May 4 at 3:01
  • \$\begingroup\$ I think you should use the [tips] tag. \$\endgroup\$ – the default. May 4 at 3:35
  • \$\begingroup\$ 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)". \$\endgroup\$ – Bubbler May 4 at 3:40
  • \$\begingroup\$ Ok, some of my code is setting the inputs so thanks for the clarification! \$\endgroup\$ – Nip Dip May 4 at 3:49
3
\$\begingroup\$

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
80      ADD R0
        :TEST
71 08   ISZ R1, LOOP
E0      WRM

If you omit the first 3 instructions and the last instruction then the inputs become R0 and the accumulator and the accumulator becomes the output.

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Incoming carry can be ignored by using CLB instead of LDM 0, but I hadn't finished reading the instruction set at the time. \$\endgroup\$ – Neil May 4 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.