MCODE question - C=-C-1 MS sets carry in MLDL but not in SDK41


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 !



;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
Add HexCode Command Comment
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
x10F 0F8 Read 3(x)
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
x124 01D060 ?NCXQ [AD2_10]
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!



Possibly Related Threads...
Thread Author Replies Views Last Post
  MS advert shows spreadsheet with obvious error BruceH 3 1,465 11-14-2013, 09:50 AM
Last Post: Bill (Smithville, NJ)
  HP-41 MCODE: The Last Function - at last! Ángel Martin 0 682 11-08-2013, 05:11 AM
Last Post: Ángel Martin
  41-MCODE: Auto XEQ+ALPHA possible? Ángel Martin 5 1,325 05-29-2013, 06:15 AM
Last Post: Ángel Martin
  HP 41 Mcode related Questions Michael Fehlhammer 4 1,285 05-10-2013, 07:09 PM
Last Post: Michael Fehlhammer
  I still have wp34s crystal and cap sets David Griffith 3 967 11-01-2012, 05:13 PM
Last Post: Dale Reed
  41-MCODE: Breaking the FAT barrier. Ángel Martin 0 632 09-03-2012, 06:31 AM
Last Post: Ángel Martin
  Driving force of HP Calc function sets Matt Agajanian 11 2,302 07-27-2012, 02:03 AM
Last Post: Mike Morrow
  41-MCODE: Dr. Jekyll & Mr. Hyde Ángel Martin 9 2,146 07-09-2012, 09:41 AM
Last Post: Monte Dalrymple
  HP41C: Factorial (kind of) in MCODE Frido Bohn 7 1,625 05-26-2012, 09:18 AM
Last Post: Frido Bohn
  OT: Old windows and ms/dos on 3.5 disks Ethan Conner 7 1,620 05-20-2012, 02:20 AM
Last Post: Marcus von Cube, Germany

Forum Jump: