/* * File: addtwo.asm * ----------------- * This program adds two integers. */ INPUT 50 /* Get an integer, put it in memory location 50 */ INPUT 51 /* Get an integer, put it in memory location 51 */ LOAD 50 /* Put the first integer in the accumulator */ ADD 51 /* Add the second integer to the accumulator */ STORE 52 /* Store the result in memory location 52 */ OUTPUT 52 /* Report the result */ HALT /* * File: addlist.asm * ----------------- * This program adds a list of integers. The user signals the * end of the input by entering the value 0. */ start: LOAD zero STORE total loop: INPUT n LOAD n JZERO done LOAD total ADD n STORE total JUMP loop done: OUTPUT total HALT /* Constants */ zero: 0 /* Variables */ total: 0 n: 0 /* * File: countdown.asm * ------------------- * This program counts backwards from 10 to 0 */ start: LOAD #10 loop: STORE i OUTPUT i SUB #1 JNEG done JUMP loop done: HALT i: 0 /* * File: fibonacci.asm * ------------------- * This program writes out the first n Fibonacci numbers. */ start: INPUT n /* n = readInt(" / "); */ LOAD #0 /* t1 = 0; */ STORE t1 STORE i /* i = 0; */ LOAD #1 STORE t2 /* t2 = 1; */ loop: LOAD n /* if (i == n) */ SUB i JZERO done /* exit the loop */ OUTPUT t1 /* println(t1); */ LOAD t1 /* t3 = t1 + t2; */ ADD t2 STORE t3 LOAD t2 /* t1 = t2; */ STORE t1 LOAD t3 /* t2 = t3; */ STORE t2 LOAD i /* i++ */ ADD #1 STORE i JUMP loop /* go back to start of loop */ done: HALT n: 0 i: 0 t1: 0 t2: 0 t3: 0