NB. Machine Language Simulator NB. Keith Smillie NB. Department of Computing Science NB. University of Alberta NB. Edmonton, Alberta T6G 2E8 NB. September 2003 NB. NB. This program is a machine language simulator to illustrate programming NB. for a one-address decimal computer with an order code of 15 instructions. NB. Instructions have the format "opxxx", where "op" is the two-digit order NB. code and "xxx" is the three-digit address. In addition to the main memory NB. of up to 1000 words with 0-origin indexing, there is an accumulator A for NB. arithmetic, an instruction register IR for storing the current instruction, NB. and a counter register CR for storing the address of the next instruction. NB. The instructions may be described briefly as follows where parentheses NB. indicate the contents of the enclosed memory location: NB. NB. 00000 Halt Halt NB. 01xxx Add A <- (A) + (xxx) NB. 02xxx Subtract A <- (A) - (xxx) NB. 03xxx Multiply A <- (A) * (xxx) NB. 04xxx Divide A <- (A) / (xxx) NB. 05xxx Get A <- (xxx) NB. 06xxx Store xxx <- (A) NB. 07xxx Transfer Next instr. from xxx NB. 08xxx Tr. -ve Next instr. from xxx if (A) < 0 NB. 09xxx Tr. zero Next instr. from xxx if (A) = 0 NB. 10xxx Tr. zero or pos. Next instr. from xxx if (A) >= 0 NB. 11000 Input A <- No. on tape NB. 12000 Print1 Print (A) NB. 13000 Print2 Print Carr. Ret. NB. 14xxx Return xxx <- (CR) + 1 NB. NB. The following example shows a simple program to input two numbers NB. and compute and print their sum: NB. NB. Ex0=: 11000 6006 11000 1006 12000 0 NB. Tape0=: 2.5 6.8 NB. Clear '' NB. 0 Load Ex0 NB. 0 Run Tape0 (or 0 Run 2.5 6.8) NB. PR NB. 9.3 NB. NB. The program may be displayed by the statement "0 Display Ex0" and is as NB. shown with annotations added: NB. 000 11000 Read 1st number NB. 001 06006 Store NB. 002 11000 Read 2nd number NB. 003 01006 Add NB. 004 12000 Print NB. 005 00000 Halt NB. NB. Programming notes: NB. 1. The memory size is controlled by the global variable "MemorySize" NB. which has a default value of 200 and may have a maximum value of 1000. NB. 2. Instructions may be added to or removed from the list of NB. instructions by appropriate changes to the global variable NB. "InstrNames" and the verb "comp". NB. 3. The verb "Display" does not correctly display program constants NB. which are not non-negative integers. MemorySize=: 200 InstrNames=: 'hlt';'add';'sub';'mpy';'div';'get';'str';'jmp';'trn';'trz';'tpz';'inp';'pr1';'pr2';'rtn' comp=: 3 : 0 whilst. -. HALT do. IR=:CR{M NB. Get instruction CR=: >:CR NB. Increment counter 'OP ADDR'=: 100 1000 #: IR NB. Decode instruction select. > OP { InstrNames NB. Execute instruction case. 'hlt' do. if. 0 < #BR do. PR=: >PR,
PR end. [ HALT=: 1 case. 'add' do. A=: A + ADDR } M case. 'sub' do. A=: A - ADDR } M case. 'mpy' do. A=: A * ADDR } M case. 'div' do. A=: A % ADDR } M case. 'get' do. A=: ADDR } M case. 'str' do. M=: A ADDR } M case. 'jmp' do. CR=: ADDR case. 'trn' do. if. A < 0 do. CR=: ADDR end. case. 'trz' do. if. A = 0 do. CR=: ADDR end. case. 'tpz' do. if. (A > 0) +. (A = 0) do. CR=: ADDR end. case. 'inp' do. TAPE=: }. TAPE [ A=: {. TAPE case. 'pr1' do. BR=: BR, A case. 'pr2' do. BR=: i. 0 [ PR=: PR,
: CR) ADDR } M end. end. empty '' ) NB. Display program with addresses Display=: (3: fmt [ + [: i. [: # ]) ,.[: _8&{."1 5: fmt ] fmt=: [:}."1[:": (10"_^[)+([:,.]) NB. Phrases Chap. 13 NB. Clear memory Clear=: 3 : 0 empty M=: (MemorySize <. 1000)$0 ) NB. Load program Load=: 3 : 0 : empty M=: y. (x.+i.#y.) } M ) NB. Run program Run=: 4 : 0 CR=: x. NB. Address of first instruction TAPE=: y. NB. Data tape A=: IR=: 0 NB. Registers PR=: BR=: i. 0 NB. Printer HALT=: 0 NB. Run indicator comp '' ) NB. ***** Sample programs ***** NB. Sum of two numbers NB. Clear '' NB. 0 Load Ex0 NB. 0 Run Tape0 (or 0 Run 2.5 6.8) Ex0=: 11000 6006 11000 1006 12000 0 Tape0=: 2.5 6.8 NB. Sum of a list NB. Clear '' NB. 10 Load Ex1 NB. 10 Run Tape1 Ex1=: 5021 2021 6021 11000 8018 1021 6021 7013 5021 12000 0 Tape1=: 4.59 3.55 3.17 1.99 1 5.5 1.36 _1 NB. Table of squares NB. Clear '' NB. 0 Load Ex2 NB. 0 Run Tape2 Ex2=: 11000 6025 5016 6026 5026 12000 3026 12000 13000 5026 Ex2=: Ex2, 1016 6026 5025 2026 10004 0 1 Tape2=: 5 NB. Table of squares and square roots NB. Clear '' NB. 0 Load Ex3a NB. 50 Load Ex3b NB. 0 Run Tape3 Ex3a=: 11000 6025 5020 6026 5026 12000 3026 12000 5026 14069 Ex3a=: Ex3a, 7050 12000 13000 5026 1020 6026 5025 2026 10004 0 1 Ex3b=: 6072 4070 6073 5072 4073 1073 4070 6074 2073 10063 Ex3b=: Ex3b, 6075 2075 2075 2071 8068 5074 6073 7053 5074 7000 2 0.00001 Tape3=: 5