THE SIMPLE COMPUTER (SIM) 2.0 SIM2 WITH SKIP AND JUMP INSTRUCATIONS 2.1 INTRODUCTION The SIM1 simulator can only implement "straight line" code without loops or conditional statements. SIM2 adds seven instructions: a jump (jmp) instruction that allows transfer to an instruction located anywhere in memory and a family of six skip instructions which can conditionally skip over the following instruction (which is typically a jmp instruction). For example, the following code sets variable a (in memory cell 100) equal to the minimum of variables b (in 101) and c (in 102). 000 1101 ld b a = b; ; 001 2100 st a 002 4102 sub c if (b - c < 0) 003 8500 sklt { 004 7007 jmp done 005 1102 ld c a = c; 006 2100 st a } 007 ... done: ... The first two instructions set a equal to b. Since the accumulator still contains the value of b, the sub c instruction leaves b - c in the accumulator. If the accumulator is greater than or equal to zero, the sklt (SKip if accumulator Less Than zero) does not skip, and the jmp (JuMP) instruction is executed. If the accumulator is less than zero, the sklt instruction in address 003 causes the jmp instruction in 004 to be skipped, and the instructions in address 005 and 006 are executed, setting a equal to c. The code above is the typically use of the skip and jump instructions. If you want to jump somewhere when some condition is true, you precede the jump instruction with a skip instruction that skips the jump instruction when the condition is false. The jmp instruction has the format 6xxx where xxx is the value the jmp instruction places in the instruction pointer ip. The various skip instruction have the format 7n00, where n specifies the particular skip instruction. The new opcodes are described at the beginning of the SIM2 program using additional #define statements. #define JMP 6 /* jump (branch) instruction */ #define SKIPSET 7 /* skip instructions */ /* skip operation codes (format 7n00 where n is as follows) */ #define SKIP 0 /* unconditional skip */ #define SEQ 1 /* skip if acc equals 0 */ #define SNE 2 /* skip if acc not equal 0 */ #define SGT 3 /* skip if acc greater than 0 */ #define SGE 4 /* skip if acc greater or equal to 0 */ #define SLT 5 /* skip if acc less than 0 */ #define SLE 6 /* skip if acc less than or equal to 0 */ 2.2 SIGNED AND UNSIGNED NUMBERS As shown in the table below, SIM numbers can be interpreted as either unsigned numbers (with a range from 0000 to 9999) or as signed numbers (with a range from -5000 to 4999). value in unsigned signed memory interpretation interpretation 0000 0 0 0001 1 1 0002 2 2 0003 3 3 ... ... ... 4998 4998 4998 4999 4999 4999 5000 5000 -5000 5001 5001 -4999 5002 5002 -4998 ... ... ... 9997 9997 -3 9998 9998 -2 9999 9999 -1 To see the logic behind the signed interpretation, consider a new car with an odometer reading of 000000. As you drove the car from the auto showroom, the odometer would register 000001, 000002, 000003, etc. to record the total mileage driven. But consider what would happen if you backed the car out of the showroom and drove home backwards. The odometer (or at least an old-style mechanical odometer) would register 999999, 999998, 999997, etc. to record the "negative" distance the car was driven. In this case, an odometer reading of 999999 represents -1, 999998 represents -2, 999997 represents -3, etc. The rules for adding (and subtracting) signed numbers are identical to the rules for adding (and subtracting) unsigned numbers, so the SIM computer only needs a single ADD (and SUB) instruction. The skip instructions selected by the programmer determine the interpretation being used for a particular number. For unsigned numbers, only SKEQ and SKNE (which, for unsigned numbers, means greater than zero) are used, For signed numbers, all of the skip instructions can be used (but positive numbers are limited to the range 0 to 4999). 2.3 SAMPLE PROGRAM LOOPV1.SIM2 The file loopv1.sim2 contains a SIM2 program to sum the integers from 1 to 4. The machine language program uses a down-counting loop (4, 3, 2, 1, 0) because it uses fewer instructions than an up-counting loop. If the machine language program in loopv1.sim2 is executed, the following results are produced. ? cis-lclient02:~>gcc sim2.c cis-lclient02:~>./a.out < loopv1.sim2 #loopv1.sim2 - Sum the integers from 1 to 4 using a loop 000 5000 start: lda 0 sum = 0; 001 2015 st sum 002 5004 lda 4 for (cnt=4; cnt>0; cnt--) 003 2016 st cnt { 004 5001 lda 1 005 2017 st one 006 1015 loop: ls sum sum = sum + cnt; 007 3016 add cnt 008 2015 st sum 009 1016 ld cnt 010 4017 sub one 011 2016 st cnt 012 7100 skeq 013 6006 jmp loop } 014 0000 halt exit(0) 015 0000 sum: .word int cnt; 016 0000 cnt: .word int sum; 017 0000 one: .word 000 .end start Starting execution of SIM program at address 000 cnt = 1, ip = 000, inst = 5000, acc = 0000 cnt = 2, ip = 001, inst = 2015, acc = 0000 cnt = 3, ip = 002, inst = 5004, acc = 0000 cnt = 4, ip = 003, inst = 2016, acc = 0004 cnt = 5, ip = 004, inst = 5001, acc = 0004 cnt = 6, ip = 005, inst = 2017, acc = 0001 cnt = 7, ip = 006, inst = 1015, acc = 0001 cnt = 8, ip = 007, inst = 3016, acc = 0000 cnt = 9, ip = 008, inst = 2015, acc = 0004 cnt = 10, ip = 009, inst = 1016, acc = 0004 cnt = 11, ip = 010, inst = 4017, acc = 0004 cnt = 12, ip = 011, inst = 2016, acc = 0003 cnt = 13, ip = 012, inst = 7001, acc = 0003 ... cnt = 30, ip = 013, inst = 6006, acc = 0001 cnt = 31, ip = 006, inst = 1015, acc = 0001 cnt = 32, ip = 007, inst = 3016, acc = 0009 cnt = 33, ip = 008, inst = 2015, acc = 0010 cnt = 34, ip = 009, inst = 1016, acc = 0010 cnt = 35, ip = 010, inst = 4017, acc = 0001 cnt = 36, ip = 011, inst = 2016, acc = 0000 cnt = 37, ip = 012, inst = 7001, acc = 0000 cnt = 38, ip = 014, inst = 0000, acc = 0000 Processor executed HALT instruction cnt = 38, ip = 015, inst = 0000, acc = 0000 cis-lclient02:~> The line generated when cnt was equal to 33, namely: cnt = 33, ip = 008, inst = 2015, acc = 0010 shows the sum of 4, 3, 2, and 1, namely 0010, being stored in address 015 (sum). 2.4 SIM2 QUESTIONS AND PROBLEMS 1. Fill in the missing values in the following table. value in unsigned signed memory interpretation interpretation 0010 _ _ _ _ _ _ _ _ _ 0200 _ _ _ _ _ _ _ _ _ 4000 _ _ _ _ _ _ _ _ _ 5000 _ _ _ _ _ _ _ _ _ 6000 _ _ _ _ _ _ _ _ _ 7000 _ _ _ _ _ _ _ _ _ 9000 _ _ _ _ _ _ _ _ _ 9900 _ _ _ _ _ _ _ _ 9990 _ _ _ _ _ _ _ _ 9999 _ _ _ _ _ _ _ _ 2. Modify loopv1.sim2 to compute the sum of the numbers from 1 to 50. Explain the answer computed by the program. 3. Modify loopv1.sim2 to use an up-counting loop to sum the numbers from 1 to 10. 2.5 SIM2 PRACTICE QUIX Each of the following SIM2 programs begins execution at address 0. What will be in the accumulator when each of the programs halts? 1. acc = __ __ __ __ 000 5001 start: lda 1 001 7100 skeq 002 5002 lda 2 003 0000 halt 000 .end start 2. acc = __ __ __ __ 000 5001 start: lda 1 001 7200 skne 002 5002 lda 2 003 0000 halt 000 .end start 3. acc = __ __ __ __ 000 1006 start: ld n 001 4007 sub one 002 2006 st n 003 7200 skne 004 6000 jmp start 005 0000 halt 006 0010 n: .word 10 007 0001 one: .word 1 000 .end start 4. acc = __ __ __ __ 000 1006 start: ld n 001 4007 sub one 002 2006 st n 003 7100 skeq 004 6000 jmp start 005 0000 halt 006 0010 n: .word 10 007 0001 one: .word 1 000 .end start 5. acc = __ __ __ __ 000 5001 start: lda 1 001 2008 st n 002 6005 jmp e 003 3008 c: add n 004 7200 skne 005 6003 e: jmp c 006 3008 add n 007 0000 halt 008 0000 n: .word 0 000 .end start