Question: Especially problem(C).THX. Not just copied the answer from it has already have. 7.27 (Machine-Language Programming) Let's create a computer we'll call the Simpletron. As its


Especially problem(C).THX. Not just copied the answer from it has already have.
7.27 (Machine-Language Programming) Let's create a computer we'll call the Simpletron. As its name implies, it's a simple machine, but as we'll soon see, it's a powerful one as well. The Simpletron runs programs written in the only language it directly understandsthat is, Simpletron Machine Language, or SML for short. The Simpletron contains an accumulatora "special register" in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number such as +3364. - 1293, +0007.-0001 and so on. The Simpletron is equipped with a 100- word memory, and these words are referenced by their location numbers 00, 01, ..., 99. Before running an SML program, we must load or place the program into memory. The first instruction (or statement) of every SML program is always placed in location 00. Each instruction written in SML occupies one word of the Simpletron's memory, so instruc- tions are signed four-digit decimal numbers. We assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Sim- pletron's memory may contain either an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the opera- 358 Chapter 7 C Pointers tion code, which specifies the operation to be performed. SML operation codes are summarized in Fig. 7.32 Operation code Meaning Input/output operations: #define READ 10 Read a word from the terminal into a specific location in memory Write a word from a specific location in memory to the termi- nal. #define WRITE 11 I sad store operations #define LOAD 20 Load a word from a specific location in memory into the accumulator. Store a word from the accumulator into a specific location in memory #define STORE 21 Arishmetic operations: #define ADD 30 #define SUBTRACT 31 Add a word from a specific location in memory to the word in the accumulator (leave result in accumulator). Subtract a word from a specific location in memory from the word in the accumulator (leave result in accumulator). Divide a word from a specific location in memory into the word in the accumulator (leave result in accumulator). Multiply a word from a specific location in memory by the word in the accumulator (leave result in accumulator). #define DIVIDE 32 #define MULTIPLY 33 Transfer of control operations: #define BRANCH 40 #define BRANCHNEG 41 Branch to a specific location in memory. Branch to a specific location in memory if the accumulator is negative. Branch to a specific location memory if the accumulator is #define BRANCHZERO 42 zero. #define HALT 43 Halt-i.e., the program has completed its task. Fig. 7.32 Simpletron Machine Language (SML) operation codes. The last two digits of an SML instruction are the operand, which is the address of the memory location containing the word to which the operation applies. Now let's consider several simple SML programs. The following SML program reads two numbers from the keyboard, and computes and prints their sum. The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to zero). Then +1008 reads the next number into location 08. The load instruction, +2007, puts the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator. The store instruction, +2109, places the result back into memory location 09, from which the write instruction, +1109, takes the number and prints it (as a signed four-digit decimal number). The bal instruction, +4300, terminates execution. Example Location Number Instruction +1007 00 01 +1008 02 +2007 03 +3008 04 05 +2109 +1109 +4300 (Read A (Read B) (Load A) (Add B) (Store C) (Write C) (Halt) (Variable A) (Variable B) (Result C) 06 07 +0000 +0000 08 09 +0000 The following SML program reads two numbers from the keyboard, and determines and prints the larger value. Note the use of the instruction +4107 as a conditional transfer of control, much the same as C's if statement. Example 2 Location Number Instruction 00 +1009 01 +1010 02 +2009 03 +3110 04 +4107 05 (Read A (Read B) (Load A) (Subtract B (Branch negative to 07) (Write A) (Halt) (Write B) (Halt) (Variable A) (Variable B) +1109 +4300 +1110 06 07 08 +4300 09 +0000 10 +0000 Now write SML programs to accomplish each of the following tasks. a) Use a sentinel-controlled loop to read positive integers and compute and print their sum. b) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute and print their average. c) Read a series of numbers and determine and print the largest number. The first number read indicates how many numbers should be processed. The last two digits of an SML.imaruction are the end, which is the address of the memory location containing the word to which the operation applies. Now les consider several simple SML. programs. The following SML program rade two be from the keyboard and comptes and prints their wum. The instruction 100 reads the fint number from the keyboard and places it into location 07 (which has been initiated to me. The 2008 as the best umibet into locations The ind instruction. -2007, puts the fint number into the accumulator and the old instruction +3008. add the second number to the number is the SML antimetri Intre hair onder in the arm The intactic 220 places the resulback into memory location 09, from which the amie instruction, 120, takes the number and prinesit (an a signed four-digit decimal number). The instruction-300. termine ceci Example 1 Location 00 01 03 04 05 06 07 08 09 Number Instruction -1007 (Resad ) -2008 ( RB (Land) - 3008 GAR 2109 Wrio 300 -0000 -6000 Hariable B) 0000 (Radio The following SML program reads two numbers from the keyboard, and determines and prints the larget value. Note the wie of the instructie 4207 as a conditional trasfer of control. much the same as Cif statement Example 2 Location 00 03 02 03 04 os 06 07 Number Instruction 2009 (Read A -1010 Rund (do -3110 4102 Branch wie 07) 1109 CHA Wha (Habe) 0000 Marie A 0000 09 10 Now write SML. programs to accomplish each of the following tasks 1) Use a sentinel-controlled loop to read positive interns and compute and print their b) Use a counter-controlled loop to read even bets, some positive and some negative and compute and print their average Read a series of numbers and determine and print the largest number. The first number read indicates how many numbers should be processed
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
