Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7.36 (Machine-Language Programming) Let\'s create a computer called the Simpletr

ID: 3883966 • Letter: 7

Question

7.36 (Machine-Language Programming) Let's create a computer called the Simpletron. As its

name implies, it's a simple machine, but powerful. The Simpletron runs programs written in the

only language it directly understands: Simpletron Machine Language (SML).

The Simpletron contains an accumulator —a special register in which information is put

before the Simpletron uses that information in calculations or examines it in various ways. All the

information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal

number, such as +3364 , -1293 , +0007 and -0001 . 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 . The simulator

will start executing at this location.

Each instruction written in SML occupies one word of the Simpletron's memory (so instructions

are signed four-digit decimal numbers). We shall 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 Simpletron's mem-

ory may contain an instruction, a data value used by a program or an unused (and so undefined) area

of memory. The first two digits of each SML instruction are the operation code specifying the opera-

tion to be performed. SML operation codes are summarized in Fig. 7.33.

The last two digits of an SML instruction are the operand— the address of the memory location

containing the word to which the operation applies. Let's consider several simple SML programs.

Operation code

Meaning

Input/output operations:

final int READ = 10 ;

Read a word from the keyboard into a specific location in memory.

final int WRITE = 11 ;

Write a word from a specific location in memory to the screen.

Load/store operations:

final int LOAD = 20 ;

Load a word from a specific location in memory into the accumulator.

final int STORE = 21 ;

Store a word from the accumulator into a specific location in memory.

Arithmetic operations:

final int ADD = 30 ;

Add a word from a specific location in memory to the word in the

accumulator (leave the result in the accumulator).

final int SUBTRACT = 31 ;

Subtract a word from a specific location in memory from the word in

the accumulator (leave the result in the accumulator).

final int DIVIDE = 32 ;

Divide a word from a specific location in memory into the word in

the accumulator (leave result in the accumulator).

final int MULTIPLY = 33 ;

Multiply a word from a specific location in memory by the word in

the accumulator (leave the result in the accumulator).

Transfer-of-control operations:

final int BRANCH = 40 ;

Branch to a specific location in memory.

final int BRANCHNEG = 41 ;

Branch to a specific location in memory if the accumulator is negative.

final int BRANCHZERO = 42 ;

Branch to a specific location in memory if the accumulator is zero.

final int HALT = 43 ;

Halt. The program has completed its task.

Fig. 7.33 | Simpletron Machine Language (SML) operation codes.

The first SML program (Fig. 7.34) reads two numbers from the keyboard and computes and

displays their sum. The instruction +1007 reads the first number from the keyboard and places it

into location 07 (which has been initialized to 0). Then instruction +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 arith-

metic 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 dis-

plays it (as a signed four-digit decimal number). The halt instruction, +4300 , terminates execution.

Location

Number

Instruction

00

(Read A)

+1007

01

(Read B)

+1008

02

(Load A)

+2007

03

(Add B)

+3008

04

(Store C)

+2109

Fig. 7.34 | SML program that reads two integers and computes their sum. (Part 1 of 2.)

Location

Number

Instruction

05

(Write C)

+1109

06

(Halt)

+4300

07

(Variable A)

+0000

08

(Variable B)

+0000

09

(Result C)

+0000

Fig. 7.34 | SML program that reads two integers and computes their sum. (Part 2 of 2.)

The second SML program (Fig. 7.35) reads two numbers from the keyboard and determines

and displays the larger value. Note the use of the instruction +4107 as a conditional transfer of con-

trol, much the same as Java's if statement.

Location

Number

Instruction

+1009

00

(Read A)

01

+1010

(Read B)

+2009

02

(Load A)

03

+3110

(Subtract B)

+4107

04

(Branch negative to 07)

05

+1109

(Write A)

+4300

06

(Halt)

07

+1110

(Write B)

+4300

08

(Halt)

09

+0000

(Variable A)

+0000

10

(Variable B)

Fig. 7.35 | SML program that reads two integers and determines the larger.

Now write SML programs to accomplish each of the following tasks:

a) Use a sentinel-controlled loop to read 10 positive numbers. Compute and display their

sum.

b) Use a counter-controlled loop to read seven numbers, some positive and some negative,

and compute and display their average.

c) Read a series of numbers, and determine and display the largest number. The first num-

ber read indicates how many numbers should be processed.

Explanation / Answer

Solution for A)

01 +1014 Read A Sentinel i,e 0 value
02 +1015 Read B Increment value
03 +1016 Read C Number to add
04 +2016 Load c value
05 +4103 if value read was negative then again read the value
04 +2017 Load D Sum of numbers, initially 0
05 +3016 Add C D+C, sum numbers
06 +2117 Store D Store new sum
07 +2014 Load A Load sentinel for incrementing
08 +3115 Subtract B Decrement the sentinel
09 +2114 Store A Store new sentinel
10 +4200 Branchzero 11 Branch if the sentinel is 0,
11 +4002 Branch 2 Sentinel is not 0, so go back to 2 and do it again
12 +1116 Write D Print sum of all values
13 +4300 Halt

Solution for B)


01 +1020 Read value A to subtract from counter value
02 +1021 Read B Counter value i.e, 7
03 +2021 Load B , inorder to save this value
04 +2144 store b value in memory 44
05 +1022 Read C numbers to add
06 +2023 Load D sum of numbers, initially 0
07 +3022 Add c i.e D+C, sum
08 +2123 Stored D, new sum
09 +2020 Load A to subtract from counter
10 +3121 subtract B, decrement counter
11 +4200 branchzero 11 branch if counter is 0
12 +4005 branch 3 , to read the value and continue sum
13 +2044 load value of counter from loction 44
14 +3223 divede sum of all values / total number
15 +2148 stores avg value in location 48
16 +1148 displays avg value on screen
17 4300 halt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote