The H67-806 Machine Language Computer Simulator

Back to main page | Email me at: Gene!


Well, this is one of the more esoteric programs I've typed up and put on this page, but it is an interesting one to use. It was originally written for the HP-67 by Dave Munroe in Portland Oregon and appeared in the V8N3P25 (May 1981) PPC Journal. Thanks to Dave wherever he is today!

What's included here... There is a program listing, a detailed set of instructions and descriptions, and a program example.

The H67 is an HP-41/HP-42 program that simulates the CPU, registers and functions of a hypothetical computer that can be programmed in its own machine language. Note: I have re-written the original HP-67 program so that it runs on the HP-41/HP-42. If you really want the original HP-67 program, email me.

What is the use of writing and playing with such a program? Simulators exist for two useful reasons: 1) In the real world, it allows computer programmers to write and test software for a machine that hasn't been built yet, and 2) they allow people who don't usually have the opportunity to work at the machine level to do so.

This simulator will be very helpful to someone trying to learn how computers work at a very low level, i.e., things like given its architecture, what is the best way of implementing subroutines and passing parameters, etc. The ability to single-step execution is a great help.

H67 Description:
1) The basic unit of memory in the simulator is a 3 digit word or cell. Three words of memory are stored in an HP-41/HP-42 memory register.

2) Memory capacity is 50 words.

3) The H67 has 5 CPU registers:

HP-41/42 Memory       H67 Register
    10              rA - Accumulator
    11              rB - Index, Work Register
    12              rC - Work Register
    13              PC - Program Counter
    14              CC - Condition Code
4) Addressing Modes are : 1) Immediate (Operand is part of the instruction itself), 2) Register (Operand is in a register), 3) Direct and 4) Indexed are both described later.

5) Instruction Set. These will be briefly listed here and described in more detail later.

HLT   000     Halt, Display rA
XCA   020     Exchange rC and rA
L     1aa     Load
ST    2aa     Store
A     3aa     Add
S     4aa     Subtract
B     5aa     Branch
BC    6aa,aa  Branch on condition
I/D   7mr     Increment/Decrement register
LBI   8ii     Load rB Immediate
T     9rr     Transfer Register

Memory Organization: Memory is organized into 50 words, with HP- 41/HP-42 register holding three words as a decimal number. So, the number 0.116720020 would actually be three H67 instructions, 116, 720 and 020. Values in a word may range from 000 to 999. Memory is held beginning in the HP-41/HP-42 memory register #016 and flows as shown below:
Register    H67 Words
  16        00  01  02
  17        03  04  05
  18        06  07  08
.... 
  31        45  46  47
  32        48  49

All H67 instructions which access memory (such as Load) can specify either a direct or indexed address.

A value in the range 00..49 is a direct address. Thus a 116 instruction means to load into the rA register the value located at location 16.

A value in the range 50..99 is an indexed address, whose effective address is determined as EA = (AA-50) + rB, that is, the effective address is found by subtracting 50 and adding the contents of rB. Thus, if rB = 2, then 156 would mean to load rA from the value at location 56- 50 + 2 or location 8.

Condition Codes: The condition code (CC) is set after any operation that changes the contents of a register. The CC reflects the contents of that register. If the register is less than 0, the CC is less than 0. If the register is 0, the CC is 0. If the register is greater than 0, the CC is greater than 0. The condition code is used in conjunction with the Branch on Condition instruction.


Detailed Description of the H67 Instruction Set: Each of the descriptions below gives the instructions mnemonic, opcode and functional description.

HLT, 000, Halt and Display rA: Halts and displays the contents of rA. At the time of the halt, the PC will be pointing to the next instruction and the CC will be unchanged from its last setting. If the operator keys in a value and then presses R/S, the H67 will load the entered value into rA, set the CC and then continue execution.

XCA, 020, Exchange rC and rA: Exchanges rC and rA, with the CC set to reflect the new rA contents (the previous rC contents).

L, 1aa, Load rA: Loads the rA register from the effective address aa, which may be direct or indexed.

ST, 2aa, Store rA: Stores the contents of the rA register into memory at the effective address aa, which may be direct or indexed. Note: The CC is not altered.

A, 3aa, Add to rA: Adds the contents of memory at effective address aa to the contents of rA. Address aa may be direct or indexed.

A, 4aa, Subtract from rA: Subtracts the contents of memory at effective address aa from the contents of rA leaving the result in rA. Address aa may be direct or indexed.

B, 5aa, Branch to aa: Branch to the effective address aa and continue execution there. Address aa may be direct or indexed. Note: The CC is not altered.

BC, 6aa,bb, Branch on Condition: This is a two-word instruction. The first word contains the opcode '6' and the address aa. The second word contains the address bb. aa and bb may be direct or indexed, independently of each other. If the CC is less than zero, the H67 branches to the effective address aa. If the CC is equal to zero, the H67 branches to effective address bb. If the CC is greater than zero, execution continues with the next sequential instruction. Note: The CC is not altered.

I or D, 7mr, Increment or Decrement Register: Increments or decrements an H67 register. Fields m and r specify the mode and the register according to the list below:
m = 2 = increment
m = 0 = decrement
r = 0 = rA
r = 1 = rB
r = 2 = rC

LBI, 8ii, Load rB Immediate: Loads rB with the two digit value ii.

T, 9rr, Transfer Register: This instruction consists of the opcode 9 followed by a source register r, followed by the destination register r. The source register contents are transfered to the destination register contents by this instruction. The values for r include:
r = 0 = rA
r = 1 = rB
r = 2 = rC
For example, 921 transfers the contents of rB to rC.


Sample H67 Program: The sample program presented below will compute the result of 6*8 - 3*4. Originally on the HP-67, this took almost 4.5 minutes to run. The HP-41 at normal speed will run this program in about 2 minutes. The HP-42 at normal speed will run this program in about a minute. The HP-42 in 'Fast Mode' only takes about 35 seconds!

I have used the MUL routine provided below to actually write a factorial program using this program. It worked. Just don't ask me how long it took.

Does this listing bring back memories for some of you out there? It does for me. I'm reminded of carrying stackes of punched cards when I took Assembler!

REG LOC CONTENTS   LABEL    OP     ADDRESS   COMMENTS
      ; SAMPLE PROGRAM TO COMPUTE (6*8)-(3*4) AND TO
      ; ILLUSTRATE SUBROUTINES AND PASSING PARAMETERS
      ;
16  00    802      PGM:     LBI     PARMS    ;B=ADDR(PARMS)
    01    513               B       MUL      ;CALL MUL
    02    003      PARMS:   CON     3
17  03    004               CON     4
    04    000      ANS1:    RES     1        ;WILL BE 3*4
    05    807               LBI     .+2      ;NEW PARMS
18  06    513               B       MUL      ;CALL MUL
    07    006      PARMS2:  CON     6        
    08    008               CON     8        
19  09    000      ANS2:    RES     1        ;WILL BE 6*8
    10    902               T       A,C      ;C=A=(6*8)
    11    404               S       ANS1     ;A=(6*8)-(3*4)
20  12    000               HLT              ;DISPLAY A
      ;
      ; MUL - MULTIPLICATION ROUTINE
      ; CALL MUL BY:    LBI  .+2
      ;                 B    MUL
      ;              N  CON  V1
      ;              S  CON  V2
      ;              T  RES  1
      ; WHERE N < S. PRODUCT IS ALSO IN REGISTER A.
      ; EXECUTION RESUMES AT THE INSTRUCTION FOLLOWING
      ; THE PARAMETER LIST
    13    150      MUL:     L       0,B      ;A=MULTIPLIER
    14    020               XCA     
21  15    151               L       1,B      ;MULTIPLICAND
    16    702      MLOOP:   D       C        ;IF CC = 0 THEN DONE
    17    623               BC      ERR;DONE ;ERR - CAN'T BE <0
22  18    021                 
    19    351               A       1,B      ;FORMING PRODUCT
    20    516               B       MLOOP    
23  21    252      DONE:    ST      2,B      ;STORE PRODUCT
    22    553               B       3,B      ;RETURN
    23    000      ERR:     HLT              ;ERROR - HALT
24  24    000               END     PGM

To load this program, store the following values into these HP-41/HP-42 memories. The spaces between the 3 digits are to illustrate the H67 instructions contained.
R16 - 0.802 513 003
R17 - 0.004 000 807
R18 - 0.513 006 008
R19 - 0.000 902 404
R20 - 0.000 150 020
R21 - 0.151 702 623
R22 - 0.021 351 516
R23 - 0.252 553 000
R24 - 0.0

If you actually want to play with this simulator, I suggest you create a separate program that will store the values above into the proper memories to restore the original program. Run that loading program AFTER you have booted the H67.


Running programs on the H67: Once you are located within the H67 program on the HP-41 or HP-42, the following local labels are active.

LBL B - Pressing this key will boot the H67, clearing the program registers and setting up certain constants. This is only necessary the first time you run the program.
LBL D - Pressing this key will put the H67 into single-step mode.
LBL C - Pressing this key will put the H67 back into normal processing mode.
LBL E - Pressing this key will begin program execution. Be patient.

Prior to running the program, be sure to load rA, rB, rC, PC, or CC as needed. Common mistake Don't boot by pressing the B key AFTER you have loaded the program starting in memory 16! You will erase your program!

While running programs in single-step mode (a good way to debug what you have written), the H67 will stop and display the PC of the instruction about to be executed, allowing you to follow the program flow. When it is stopped like this, you may view any registers, etc., as long as you return the value of the PC to the display prior to pressing R/S to continue execution.


Program Listing: The program listing is provided below. Instructions should be self-explanatory, but email if you have any questions.
Line      Instruction
01         LBL "H67"
02         LBL E
03         RCL 13
04         FS? 01
05         STOP
06         XEQ 15
07         RCL 06
08         1
09         +
10         STO 13
11         RDN
12         100
13         STO 07
14         /
15         STO 15
16         FRC
17         RCL 07
18         *
19         GTO IND 15
20         LBL 15
21         XEQ 14
22         STO 06
23         3
24         /
25         INT
26         STO 05
27         RCL 08
28         +
29         STO 04
30         STO 15
31         RCL 06
32         RCL 05
33         3
34         *
35         -
36         STO 00
37         RCL IND 15
38         RCL 09
39         *
40         INT
41         STO 03
42         LASTX
43         FRC
44         RCL 09
45         *
46         INT
47         STO 02
48         LASTX
49         FRC
50         RCL 09
51         *
52         INT
53         STO 01
54         3
55         RCL 00
56         -
57         STO 15
58         FS?C 02
59         GTO 11
60         RCL IND 15
61         RTN
62         LBL 11
63         RCL 07
64         STO IND 15
65         RCL 04
66         STO 15
67         RCL 03
68         RCL 02
69         RCL 01
70         RCL 09
71         /
72         +
73         RCL 09
74         /
75         +
76         RCL 09
77         /
78         STO IND 15
79         RTN
80         LBL 14
81         50
82         X > Y?
83         GTO 14
84         -
85         RCL 11
86         +
87         RTN
88         LBL 14
89         RDN
90         RTN
91         LBL 01
92         XEQ 15
93         STO 10
94         STO 14
95         GTO E
96         LBL 02
97         RCL 10
98         STO 07
99         RDN
100        SF 02
101        XEQ 15
102        GTO E
103        LBL 09
104        10
105        /
106        LASTX
107        STO 07
108        +
109        STO 15
110        RCL IND 15
111        X <> 15
112        FRC
113        10
114        *
115        RCL 07
116        +
117        X <> 15
118        STO IND 15
119        STO 14
120        GTO E
121        LBL 07
122        10
123        /
124        STO 07
125        INT
126        1
127        -
128        RCL 07
129        FRC
130        10
131        *
132        LASTX
133        +
134        STO 15
135        RDN
136        STO+ IND 15
137        RCL IND 15
138        STO 14
139        GTO E
140        LBL 06
141        RCL 14
142        X = 0?
143        GTO 06
144        X > 0?
145        GTO 12
146        RDN
147        XEQ 14
148        GTO 13
149        LBL 06
150        RCL 13
151        XEQ 15
152        XEQ 14
153        GTO 13
154        LBL 12
155        RCL 13
156        1
157        +
158        LBL 13
159        STO 13
160        GTO E
161        LBL 03
162        XEQ 15
163        RCL 10
164        +
165        STO 10
166        STO 14
167        GTO E
168        LBL 08
169        STO 11
170        STO 14
171        GTO E
172        LBL 05
173        XEQ 14
174        STO 13
175        GTO E
176        LBL 04
177        XEQ 15
178        RCL 10
179        X <> Y
180        -
181        STO 10
182        STO 14
183        GTO E
184        LBL 00
185        X NE 0?
186        GTO 00
187        RCL 10
188        STOP
189        STO 10
190        STO 14
191        GTO E
192        LBL 00
193        RCL 12
194        RCL 10
195        STO 12
196        RDN
197        STO 10
198        STO 14
199        GTO E
200        LBL C
201        CF 01
202        GTO E
203        LBL D
204        SF 01
205        GTO E
206        LBL B
207        0.015
208        ENTER
209        0
210        LBL 17
211        STO IND Y
212        ISG Y
213        GTO 17
214        16
215        STO 08
216        1000
217        STO 09
218        CF 01
219        CF 02
220        CLST
221        END
That's it. Enjoy and Learn!