3.0 SIM3 WITH ADDITIONAL ACCUMULATOR INSTRUCTIONS 3.1 INTRODUCTION The SIM3 machine language simulator adds instructions that use or modify the contents of the accumulator. IN and OUT instructions allow four digits numbers to be read or printed. CLR, INC, DEC, and NEG instructions perform the obvious functions of setting to zero, incrementing, decrementing, or negating the accumulator. The SHFTL (SHiFT Left) instruction multiplies the number in the accumulator by 10, while SHFTR divides the accumulator by 10. As shown below, these accumulator instructions have the format 8n00, where "8" indicates an instruction in the accumulator group, and n specifies a particular accumulator instruction. #define ACCSET 8 /* accumulator instructions */ /* accumulator operation codes (format 8n00, where n is as follows) */ #define IN 0 /* input a 4-digit number into acc */ #define OUT 1 /* output the 4-digit number from acc */ #define CLR 2 /* clear the acc (acc = 0) */ #define INC 3 /* increment (add 1) to the acc */ #define DEC 4 /* decrement (subtract 1) from the acc */ #define NEG 5 /* negate the number in acc */ #define SHFTL 6 /* shift acc left (acc = acc * 10) */ #define SHFTR 7 /* shift acc right (acc = acc / 10) */ 3.2 SAMPLE PROGRAMS The file readv1.sim3 defines a machine language program that reads two data items and outputs the sum. In the file, the "data" follows the machine language program with one data item (and no comments) on each line. readv1.sim3 Read 2 integers and print the sum */ 000 8000 start: in read(a); 001 2009 st a 002 8000 in read(b); 003 2010 st b 004 1009 ld a sum = a + b; 005 3010 add b 006 2011 st sum 007 8100 out print(sum); 008 0000 halt exit(0); 009 0000 a: .word int a; 010 0000 b: .word int b; 011 0000 sum: .word int sum; 000 .end start 10 20 Running this program generates the following output. cis-lclient02:~>./a.out < readv1.sim3 readv1.sim3 Read 2 integers and print the sum */ 000 8000 start: in read(a); 001 2009 st a 002 8000 in read(b); 003 2010 st b 004 1009 ld a sum = a + b; 005 3010 add b 006 2011 st sum 007 8100 out print(sum); 008 0000 halt exit(0); 009 0000 a: .word int a; 010 0000 b: .word int b; 011 0000 sum: .word int sum; 000 .end start Starting execution of SIM program at address 000 cnt = 1, ip = 000, inst = 8000, acc = 0000 Input a 4-digit number - 10 cnt = 2, ip = 001, inst = 2009, acc = 0010 cnt = 3, ip = 002, inst = 8000, acc = 0010 Input a 4-digit number - 20 cnt = 4, ip = 003, inst = 2010, acc = 0020 cnt = 5, ip = 004, inst = 1009, acc = 0020 cnt = 6, ip = 005, inst = 3010, acc = 0010 cnt = 7, ip = 006, inst = 2011, acc = 0030 cnt = 8, ip = 007, inst = 8001, acc = 0030 output from program - 0030 cnt = 9, ip = 008, inst = 0000, acc = 0030 Processor executed HALT instruction cnt = 0009, ip = 0009, inst = 0000, acc = 0030 cis-lclient02:~> The program in the file readv2.sum3 reads n integers and prints the sum. The first in statement inputs a count of the number of data items (n). A down-counting loop is used to input the n data items and sum them. With the data items shown (3, 10, 20, 30), the program will output "60" when it is executed. readv2.sim3 Read n integers and print the sum */ 000 8000 start: in read(n); 001 2016 st n 002 8200 clr sum = 0; 003 2017 st sum 004 1016 loop: ld n for (; n>0; n--) 005 7200 skne { 006 6013 jmp done 007 8400 dec 008 2016 st n 009 8000 in sum = sum + read(); 010 3017 add sum 011 2017 st sum 012 6004 jmp loop } 013 1017 done: ld sum print(sum); 014 8100 out 015 0000 halt exit(0); 016 0000 n: .word int n; 017 0000 sum: .word int sum; 000 .end start 3 10 20 30 3.3 PROBLEMS AND PROGRAMS 1. Rewrite the readandsum() program using an up-counting loop. 2. Write a machine language program to input n numbers and output the largest number. 3. Write a machine language program to input n numbers and output them in reverse order. (If the input data is "3, 10, 20, 30", the output should be "30, 20, 10".) This is quite difficult because it requires modifying instructions. 3.4 SIM2 PRACTICE QUIX Each of the following SIM3 programs begins execution at address 0 and will print one or more numbers before it halts. What 4-digit numbers are printed? 1. printed value = ________ 000 8000 start: in 001 8700 shr 002 8600 shl 003 8100 out 004 0000 halt 000 .end start 9999 2. printed value = ________ 000 8000 start: in 001 8300 loop: inc 002 8500 neg 003 8100 out 005 0000 halt 000 .end start 0 3. printed value = ________ 000 8000 start: in 001 2008 st b 002 1007 ld a 003 4008 sub b 004 8300 inc 005 8100 out 006 0000 halt 007 9999 a: .word 9999 008 0000 b: .word 0 000 .end start 10 4. Hand assemble the following program to create a machine language program that begins execution at address 000. _ _ _ _ _ _ _ start: in _ _ _ _ _ _ _ st b _ _ _ _ _ _ _ ld a _ _ _ _ _ _ _ sub b _ _ _ _ _ _ _ inc _ _ _ _ _ _ _ out _ _ _ _ _ _ _ halt _ _ _ _ _ _ _ a: .word 9999 _ _ _ _ _ _ _ b: .word 0 _ _ _ .end start