# Decimal Adjust Accumulator on the 4004

Kyle Owen kylevowen at gmail.com
Thu Jan 28 19:30:44 CST 2016

```I'm currently writing a bit of code for the 4004 at the moment, and playing
with it in the online Javascript emulator found here:
http://e4004.szyc.org/emu/

According to http://e4004.szyc.org/iset.html (which I believe is copied
straight from the MCS-4 Users Manual), the DAA instruction should increment
the accumulator by 6 if carry is set or if the accumulator is greater than
9. Carry should be set following the instruction if the resulting addition
generated a carry; otherwise it's unaffected.

Let's say the accumulator is currently 9, carry is not set. I add another
9. Accumulator is now 2 with a carry. Running DAA should turn this into 8
with carry set, indicating that 9+9=(1)8. Am I thinking through this
correctly?

I ask, because according to the simulator's source code, DAA won't do that,
if I'm following it correctly:

function opDAA() { //DAA Decimal Adjust Accumulator
if(A_reg > 9) A_reg += 6;
C_flag=0; if (A_reg & 0xf0) {A_reg&=0xf; C_flag=1;}
incPC();
}

It says that it'll only add 6 if the accumulator is greater than 9, not if
a carry is already set. It will then reset carry and set it if and only if
there was a carry.

Have I found a bug in the simulator? Am I misreading the MCS-4 Users
Manual?

In any event, this is my proposed fix to better match what the instruction
description says:

function opDAA() { //DAA Decimal Adjust Accumulator
if((A_reg > 9) | (C_flag)) A_reg += 6;
if (A_reg & 0xf0) {A_reg&=0xf; C_flag=1;}
incPC();
}

Seems to work as I would expect it to.

Thanks,

Kyle
```