Post Reply 
(42S) CARDIAC
10-16-2014, 09:49 PM
Post: #6
RE: (HP-42S) CARDIAC
Factor

This is a very simple CARDIAC-program to find the factors of a number:
Code:
    03      0   ; DATA
    04      2   ; DATA    
    05      n   ; DATA
    06      p   ; DATA
    07      m   ; DATA
    08      q   ; DATA
                
    10  005     ; start           INP n
    11  104     ;                 CLA 2
    12  606     ;                 STO p
    13  105     ; trial           CLA n
    14  607     ;                 STO m
    15  103     ;                 CLA 0
    16  608     ;                 STO q
    17  107     ; division        CLA m
    18  706     ;                 SUB p
    19  607     ;                 STO m
    20  325     ;                 TAC break
    21  108     ;                 CLA q
    22  200     ;                 ADD 1
    23  608     ;                 STO q
    24  817     ;                 JMP division
    25  206     ; break           ADD p
    26  607     ;                 STO m
    27  700     ;                 SUB 1
    28  333     ;                 TAC factor
    29  106     ;                 CLA p
    30  200     ;                 ADD 1
    31  606     ;                 STO p
    32  813     ;                 JMP trial
    33  506     ; factor          OUT p
    34  108     ;                 CLA m
    35  605     ;                 STO n
    36  704     ;                 SUB 2
    37  339     ;                 TAC prime
    38  813     ;                 JMP trial
    39  910     ; prime           HRS start

As long as you don't use self-modifying code it can be translated 1:1 to the HP-42S:
Code:
LBL "FACTOR"    ; start           
STO 05          ;                 INP n
RCL 04          ;                 CLA 2
STO 06          ;                 STO p
LBL 00          ; trial           
RCL 05          ;                 CLA n
STO 07          ;                 STO m
RCL 03          ;                 CLA 0
STO 08          ;                 STO q
LBL 01          ; division        
RCL 07          ;                 CLA m
RCL- 06         ;                 SUB p
STO 07          ;                 STO m
X<0?            ;                 TAC
GTO 02          ;                     break
RCL 08          ;                 CLA q
RCL+ 00         ;                 ADD 1
STO 08          ;                 STO q
GTO 01          ;                 JMP division
LBL 02          ; break           
RCL+ 06         ;                 ADD p
STO 07          ;                 STO m
RCL- 00         ;                 SUB 1
X<0?            ;                 TAC
GTO 03          ;                     factor
RCL 06          ;                 CLA p
RCL+ 00         ;                 ADD 1
STO 06          ;                 STO p
GTO 00          ;                 JMP trial
LBL 03          ; factor                
VIEW 06         ;                 OUT p
RCL 08          ;                 CLA m
STO 05          ;                 STO n
RCL- 04         ;                 SUB 2
X<0?            ;                 TAC
GTO 04          ;                     prime
GTO 00          ;                 JMP trial
LBL 04          ; prime           
END             ;                 HRS start

Just make sure to initialize registers 00, 03 and 04 with 1, 0 and 2:
1 STO 00
0 STO 03
2 STO 04
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(42S) CARDIAC - Thomas Klemm - 10-11-2014, 08:54 PM
RE: (HP-42S) CARDIAC - Thomas Klemm - 10-12-2014, 06:15 PM
RE: (HP-42S) CARDIAC - Dave Britten - 10-13-2014, 07:30 PM
RE: (HP-42S) CARDIAC - Thomas Klemm - 10-13-2014, 09:06 PM
RE: (HP-42S) CARDIAC - Dave Britten - 10-13-2014, 09:27 PM
RE: (HP-42S) CARDIAC - Thomas Klemm - 10-16-2014 09:49 PM



User(s) browsing this thread: 1 Guest(s)