Intel 4004 emulator SUB

Back to General discussions forum

nalbe666     2020-06-05 17:57:53

Hi. Please, explain subtraction algorithm of emulator 4004.
At wiki you say "Carry is set to 1 if Acc was greater than or equal to reg - i.e. if there was no "borrow";"
intel4004-emu/wiki/Arithmetic-instructions
But for asm code below result carry = 0 for 5 - 5 (when acc equal to reg). What i missed?

clc
ldm 5           ;put 5 into reg
xch r0
ldm 5           ;put 5 into acc
sub r0          ;subtract 5 - 5

jcn c1 carry_1  ;if carry = 1 put 'a' into r1, else goto end
jun end

carry_1:
ldm 10
xch r1

end:

;;output: 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;;but expected output is: 5 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Alexandr Milovantsev     2020-06-08 01:10:37

5 is equal to 5 , so no borrow happens and carry bit sets to 1. Beware if your are have experience with other assemblers. In other asseblers carry usually is set when borrow did happens, but not in this asm.

nalbe666     2020-06-08 01:24:56

Yep, it should be 1 but it's not. I forgot to say this is about online version of emulator.

Prosthetic Conscience     2020-06-08 21:42:38

As a simple test of your program, try executing:

clc
ldm 5           ;put 5 into reg
xch r0
ldm 5           ;put 5 into acc
sub r0          ;subtract 5 - 5
xch r0          ;put subtraction result back to r0 to check

jcn c1 carry_1  ;if carry = 1 put 'a' into r1, else goto end
jun end

carry_1:
ldm 10
xch r1

end:

You will see that the output is:

;;output: f 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

So for SUB command to work correctly, you should set the carry to 1 beforehand, if you're not subtracting several words in a row.

nalbe666     2020-06-09 00:18:34

Thank's, man. That's what i missed. I read wiki again and saw what you're talking about.
"...Note that Carry should be set before this operation...". It's clear now.

Please login and solve 5 problems to be able to post at forum