Back to General discussions forum
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
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.
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.
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.