Introduction
3smlsrcQ1_1.txt Q1_2.txt Q1_3.txt
Requirement
Lakehead University
CS 1431 Programming Language II
Practical Assignment No. 1
Instructor: Dr. Jinan FIAIDHI
Q1: (Machine-Language Programming) Let us create a computer we will call the Simpletron. As its
name implies, it is a simple machine, but, as we will soon see, it is a powerful one as well. The
Simpletron runs programs written in the only language it directly understands, that 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, etc .
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; thus, instructions are signed four-digit decimal
numbers. 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 memory may contain 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 operation code that specifies the operation to be performed.
SML operation codes are shown in Fig. 1.
Figure 1. Simpletron Machine Language (SML) operation codes.
Operation code Meaning
Input/output operations
#define READ 10 Read a word from the keyboard into a specific location in memory.
#define WRITE 11 Write a word from a specific location in memory to the screen.
Load and store operations
#define LOAD 20 Load a word from a specific location in memory into the accumulator.
#define STORE 21 Store a word from the accumulator into a specific location in memory.
Arithmetic operations
#define ADD 30 Add a word from a specific location in memory to the word in
the accumulator (leave result in accumulator).
#define SUBTRACT 31 Subtract a word from a specific location in memory from the
word in the accumulator (leave result in accumulator).
#define DIVIDE 32 Divide a word from a specific location in memory into the
word in the accumulator (leave result in accumulator).
#define MULTIPLY 33 M ultiply a word from a specific location in memory by the
word in the accumulator (leave result in accumulator).
Transfer-of-control operations
#define BRANCH 40 Branch to a specific location in memory.
#define BRANCHNEG 41 Branch to a specific location in memory if the accumulator is negative.
#define BRANCHZERO 42 Branch to a specific location in memory if the accumulator is zero.
#define HALT 43 Halt the program has completed its task.
The last two digits of an SML instruction are the operand and the address of the memory location
containing the word to which the operation applies.
Now let us consider two simple SML programs. The first SML program (Fig. 2) 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). Instruction
+1008 reads the next number into location 08 . The load instruction, +2007 , places (copies) 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 (copies) the result back into memory location 09 . Then the write
instruction, +1109 , takes the number and prints it (as a signed four-digit decimal number). The halt
instruction, +4300 , terminates execution.
Figure 2 SML Example 1.
Location Number Instruction
00 +1007 (Read A)
01 +1008 (Read B)
02 +2007 (Load A)
03 +3008 (Add B)
04 +2109 (Store C)
05 +1109 (Write C)
06 +4300 (Halt)
07 +0000 (Variable A)
08 +0000 (Variable B)
09 +0000 (Result C)
The SML program in Fig. 3 reads two numbers from the keyboard, then 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.
Figure 3. SML Example 2.
Location Number Instruction
00 +1009 (Read A)
01 +1010 (Read B)
02 +2009 (Load A)
03 +3110 (Subtract B)
04 +4107 (Branch negative to 07 )
05 1109 (Write A)
06 +4300 (Halt)
07 +1110 (Write B)
08 +4300 (Halt)
09 +0000 (Variable A)
10 +0000 (Variable B)
Now write SML programs to accomplish each of the following tasks:
a. Use a sentinel-controlled loop to read positive numbe†䵰䱲⁴†⡷㱮†⽮㹥൧‼⤊㰮⽥㸠ൡਠ㩴⁴⡲♯㭬╯♰㭥ⱡ†⥯㭥㰠⽩㹴൩੶㩤†⁶㵥†⁰⁴㭥㰠⼠㹰൲੩㩴⬼㵢†㭩㱥⽦㸠൮ੵ㨠††㱩⁴⼠㹮൵੭㩲⁴†⁹㱳⽯㹵൬㱳⽯㹢൬⁵⁹†⁵††‾ⱥ⁴㱲⁒⽡㹴൨⁷†⁰⁴⁴† ⡦ⱲⱯ㱬⼠㹴൨⁙††⁰⁴⁓⥰⁴‾Ɑ⁰㱴†⽹㹯൵ਠ䙤⁵ㄠ㱣⽬㹬൹ਠ††‾†⁷㱯†⽓㹩൭ੰ⸾㰍⽬㹣൯੭䙥ㅭ䅥⁴⸍㰊⽡㹳ਠ䕮䝴䥥䕹㩲㰠⽧㹲ൡ੭どはの〠㰼†⼯㸾ਊ〠ぷ㱯
⼠㹡൴ਠ†とづ〠〼㱢 ⼾㸍ഊ੬䍵〠ち㱮⼠㹱൵ひに㰠
⼮㸠഼䵲䔠䴯㩙㱯⁴⽨㹥൮ਠぴ⁹ㅰ㈠⁴㍨㐠⁷㕯㙤†㝦㡲†㥴㱨†⽬㹯ൣぴ⭯の〮〠〼⭲〠〯〾」 ⭔べばづ〠⁴⭨づ〠びづどのづぬ†⬭〹〹〹〹‹⬠ぴは〠び⁴⭯ば〠づの⁴⭥ひどので†⬼ぢひ〠〯㰾⁹⽯㹵൲ਠㅰひ⭧ひちね〮†⬼ぢひ〠〯‾⬍《すはふ⬠ばひはで⭡ね〠びとぬつ〠び⭭ふぬちぴ⬠ぴとづ〠⁓⭩ねばぬづ⁴⭲はの〦ひ㱳⼻㹳ഠ੭㉥ね⭲べ〠ぷど⁴⭨〠ち〠び⭮でぬづ〭ぢびっひ⭰ぴづつ〬†⬱〰〰〭づ⭥ねづのぴ†⭡ひひちべ‼⭢ひ〠〯〾⬊ねづねは㱲⼠㹎൯㌠ち⭳ふねづ〠⁴⭨ちぴ〠ぴ⭥〠びどね⁵⭬ちぴはひ†⭩び〠ひふ⭮どので〬†⭡のつ〠ぬ〠ふび〠⭸ちねどの⬠ぴとづ〠㱤⽯㹧ഠ㑳〠⁷⭥〠づのぴ⭲〠ぴとづ‼⭢ひ〠〯〾⬊ばひはで⭡ね〠はて†⭅へちねば⭥〠〲〠は⬠け〱〺〼⭲〠〯〾」 ⬰〰〠〿〠㰫‰⼹㸼ൢੲ㔠〯‾⬍《〰〱〠‿⬠〫〱〰〱‰⬼ぢひ〠〯‾⬍《〰〲〠‿⬠〫〲〰〰‹⬼ぢひ〠〯‾⬍《〰〳〠‿⬠〫〳〱〱‰⬼ぢひ〠〯‾⬍《〰〴〠㰿‴⼱㸰ഷ਼㙢ひ†⬯〾」《〰‵⬠〿〠〫〱‱⬰〹〼ぢひ†⬯〾」《〰‶⬠〿〠〫〴″⬰〰〼ぢひ†⬯〾」《〰‷⬠〿〠〫〱‱⬱〰〼ぢひ†⬯〾」《〰㰸†⼫㸴ളਰ㜰〼⭲〠〯〾」 ⬰〹〠〿〠⬰〰〰〰〼⭲〠〯〾」 ⬱〰〠〿〠⬰〰〰〰〼⭲〠〯〾」 ⬱〱〠〿〠⬹〹〹〹〹‼⭢ひ〠〯〾㰍⽯㹧൲㡭〠⭯ちつどの⬠っはねば⭥ぴづつ〠‼⭢ひ〠〯〾⬊ぐひはで⭡ね〠づへ⭣ふぴどは⬠ぢづでど⭳〠〼ぢひ†⬯〾」《ぎ㱯†⽴㹨ൡੴ㤠ぴ⭥〠のふね⭥ひび〠ぴ⬠ぴとづ〠⭩でとぴ〠⭦〠づちっ⬠〿〠どの†とづ〠ば⭥っづつど⭧〠つどち⭯で〠ひづ㱰⽥㹮൴ਠ䱴†⁌ ⁴☠㬠††⭮ㅯぷ〠㥢†ひ〠䅤㱡⽹㸠൭Ⱐ ⁎†††⁌⁅⁵䍴㰠⁴⽨㸠൴੨⁴⡲♵㬠╩☠㭩Ɐ †⥩㭫㱥⼠㹣൯੮䅴⁵⡬㽹⤠⁵†††⁹ 㰠⽴㹲൯੬‾⁴⁴⁴†⁴䕥†㰼†⼯㸾ਊ⁴†⁴†っ㥯㱡⼠㹴൨䄠Ɫ†䅴㱹⽩㹮൧ਠ
†‼ ⁶†⁰†㱤⽴㹥ഠੴⱲ⁹⁴⁵⁰⁔㩮㱤⼠㹴൨⬠⭲㭴㰠⼠㹤൩੧⁴⁴‼† ⸠⁵⁴⠠Ᵽ⁴㰠⁴⽨㹥൲ਬ†⥥⁸‼ ††㰠⽬㹥ਠ⁴⁴††‼⡦⸠Ɐ††⤠⁰䅥⁴㱮⽥㸠ബਠ…ⁱ※†⁴‼†Ⱐ⁗†㱧⼠㹥൸⠠㑥で⥳⁴⁴‼㰠⼊㹦൯੬㴠 㭓㱔⁓⼺㸼ൢੲ☠㬼 ♢㬊‼㰠⼊㹯൰⁴⡩†㴯㴾《⥰㱥⽤㸠രਰ 㵎㭳㰠⽱㹵൯䅷Ⱳ※⁸†‱†䵡䱴㱢 ⼾㸍ഊ†ㅡ†⁴⁴⁴䵯䱮†⁴†⁴⁴†㱮⽵㹣൴੩⸊㱴⽥㸠൰⁴†䑲⁵⁴ⰾ㱳⁵⽣㹴൩੯ⱥ⁴※‾♥㭯⁴†㥮㥳㥴㥲⁵㱩†⽲㹥൧੩⭳㥴㥥㥲㤠†‼ ⁴⁴※ Ɱ㱒⽳㹴ੲ†ⰻ‼‾†⁴⁴††⸠㰨⽳㹵൳ਠ䑡† Ᵽ††ⱓ⁍‾㰠⽴㹣൨ਠⱨ Ⰰ ⠀⸀⸀Ⰰ 㰀 ⼀㸀ഀ ⬀㤀㤀㤀㤀 ⴀ 㤀㤀㤀㤀 ⤀ ⸀ 㰀 ⼀㸀ഀ ⸀ Ⰰ 㰀 ⼀㸀ഀ 㰀 ⼀㸀ഀ䄀 㰀 ⼀㸀ഀ 㰀 ⼀㸀ഀ ⸀ 㰀 ⼀㸀ഀ ⸀㰀 ⼀㸀ഀ㨀 䐀 䨀 ㈀㌀㰀 ⼀㸀ഀ㰀 ⼀㸀ഀ☀㬀㰀⼀㸀