# HP Forums

You're currently viewing a stripped down version of our content. View the full version with proper formatting.

To the MCODE gurus

There seems to be an interesting discrepancy between the behavior of MCODE in the 41 (currently using a HEPAX) and in SDK41. Given that the SDK41 uses the original roms, this surprised me.

Even more surprised am I that the instruction C=-C-1 MS can create a carry with a very benign positive number in C (MS=0). The code below runs as shown in SDK41 and produces for 100 in x-reg the correct value of 71. However when run within an MLDL it produces -29 as the ?NCXQ in line x11F and x124 are not run. If I insert a NOP infront of them the code runs in the MLDL as well, producing the correct result.

Please see the code snippet below (the code does not do anything useful, itβs a preamble to a Prime-Twin generator, my usual way of learning a new language). Any thoughts with regards to

• How C=-C-1 for a benign positive number can set the carry
• Difference between SDK41 and MLDL
would be much appreciated.

Thanks so much !

Cheers

Peter

```;calculate startvalue for search for prim-twins
;given a number in X, startvalue = 30*Int((X+19)/30) β 19
;implemented as (X+19) β (X+19)Mod30 -19
x107	2A0		SetDec
x108	1A0		A=B=C=0
x109	35C		R=12		;load 19 into C
x10A	050		LD@R 1
x10B	250		LD@R 9
x10C	226		C=C+1 S&X
x10D	070		N=C All		;save for later use
x10E	10E		A=C All		;prepare regs for AD2_10
x110	01D060	?NCXQ [AD2_10]	        ;this one works fine
x112	10E		A=C All		;A=X+19
x113	158		M=C All		;save in M for later
x114	04E		C=0 All		;Load 30 into C to prep for [MOD10]
x115	35C		R=12
x116	0D0		LD@R 3
x117	226		C=C+1 S&X
x118	171064	?NCXQ [MOD10]	        ;A Mod C
x11A	0EE		B<>C		;prep for subtraction via AD2_10
x11B	198		C=M All		;recall X+19
x11C	10E		A=C All
x11D	0CE		C=B All		;(X+19) Mod 30
x11E	2BE		C=-C-1 MS	;This should simply make it negative. But it sets Carry!!
x11F	01D060	?NCXQ [AD2_10]	        ;this only works if I have a NOP in front to clear carry when run in MLDL
;it calcs (X+19) β (X+19)Mod30
x121	10E		A=C All		;prep to subtract 19
x122	0B0		C=N All		;recall 19 from N
x123	2BE		C=-C-1		;same problem it sets carry when run in MLDL
x126	0E8		WRIT 3(x)	;save result to x
x127	3E0		RTN
```

I don't know anything about SDK41, but you should expect to always get the carry flag set as the result of c=-c-1. For subtract instructions, the carry flag indicates a "borrow" out of the leftmost digit. What you're really doing is subtracting from zero, and that will always have a borrow. If you were working on a two-digit field of C that contained 23, after c=-c-1 you should have 76 with a carry.

In the hardware, subtraction is really implemented by addition of a nine's complement (assuming decimal mode), with both the carry-in and carry-out inverted. For the normal add and subtract instructions, the carry-in is zero, but for 0=-c-1 the carry-in is one (before being inverted).

that makes sense! Thanks Eric for helping me out!

I got confused as in Ken's book the instruction C=-c-1 is explained as 'doing the nine's complement' (p26) and I picked up the 'trick' in one of the books but I must have overlooked the NOP before the jump instruction there.. Anyway, thanks a lot again!

Cheers

Peter