Title: Replacement for Chapter 7
1Replacement for Chapter 7
- Low-level Languages for Super Simple CPU (SSC
Machine)
2Ref Lab Manual Laboratories 5 and 7
- We will skip Chapter 7 in the text and instead
use the machine from the lab sessions. - The instruction set for the machine in Chapter 7
is just a bit more complicated than we want to
pursue at this time. - Today it is not common to actually program much
in machine or assembly language, but
understanding these two types of languages help
you understand what happens with high level
languages such as C, C, and Java.
3VIRTUAL MACHINES
- The machine we "built" is not the machine you
"see" when you sit and start typing on the
keyboard. - The hardware at the lowest level consists of
- binary representations
- data instructions
- various circuits built from gates
- full adder test for equality
- S-L latch decoder
- multiplexor
- units built from circuits and buses
- memory
- central processor control unit
- ALU
- I/0 system
- secondary storage units
4INSTRUCTIONS AND PROGRAMS
- Each computer model has its own machine language.
- The machine instruction format is designed by the
computer designer. - The format chosen for an instruction determines
- the number of operations directly supported in
hardware - (called hardwired instructions)
- and
- the size of the addressing space i.e. the
amount of memory that can be supported
5Our Computer SSC(As introduced in the lab)
- It is a 1-address machine.
- This means each instruction holds only one
operand for an operation and if another operand
is needed, it is a register, the accumulator (or
ACC). - Thus an instruction like ADD X obtains one
operand value from X and one operand value from
ACC. - There are other machines which are 2-address
machines, 3-address machines, and ones that mix
all of the different possibilities (called,
appropriately, variable format machines.)
6Examples of Other Formats
- A 2-address machine would have an instruction
such as ADD X Y which would add X to Y and store
the result in X (or possibly Y) - A 3-address machine would have an instruction
such as ADD X Y Z which would X to Y and store
the result in Z. - 1-address machines generally require a few more
instructions to accomplish an operation as the
ACC has to be loaded and then stored. - Being able to utilize the ACC effectively leads
to more efficient programs i.e. avoid
unnecessary loads and stores.
7Our Computer(As introduced in the lab)
- The format chosen for the SSCs instruction is
- opcode 4 bits
- operand 12 bits
- The opcode is the operation code and is a code
that specifies the instruction being supported. - The hardware designer lists the operations to be
supported and then assigns an opcode to each. - The operand usually specifies the location of the
operand to be used. Consequently, it is normally
an address. (An exception is immediate mode
instructions.) - Naturally, as you saw, all instructions are just
a sequence of bits.
8What Impact Does the Instruction Format Have on
the Architecture?
- Our computer has only 4 bits for the opcode. So,
what is the maximum number of instructions it can
have? - 24 16 instructions i.e. not too many
- Our computer has only 12 bits for the address.
Data is located by addresses so this limits the
size of our memory. How many bytes can be
addressed on this machine and, therefore, what is
the maximum memory size? - 212 22210 4K -i.e. not too many
- Note that addresses are always positive so no
sign bit is needed.
9What Impact Does the Instruction Format Have on
the Architecture?
- Note that the lab simulator doesnt provide you
16 instructions nor does it provide you with 4K
of memory. However,... - You will find the 11 instructions and 16 memory
locations will allow you to to obtain a taste for
machine language programming. - This is much simpler than a real machine, but
working with the simulator gives you the basic
idea of how machines operate at the machine
language level.
10Immediate Mode vs Direct Addressing Mode
- Our computer supports both these modes
- Immediate mode is when the actual data is stored
in the instruction instead of the address. (LDI
load immediate mode) - Direct address mode is when the address of the
actual data is stored in the instruction. - Real machines often have other modes which allow
the operand(s) to be used in various ways.
11Opcodes for Our Computer
- binary mnemonic short explanation____________
- 0001 ADD add ACC to operand
- 0010 SUB subtract operand from ACC
- 0011 LOD load operand into ACC1
- 0100 LDI load operand immediate into ACC1
- 0101 STO store ACC into operand1
- 0110 INP input value and store in ACC
- 0111 OUT output value from ACC
- 1000 JMP jump to instruction
- 1001 JNG jump to instruction if ACC lt 0
- 1010 JZR jump to instruction if ACC 0
- 1111 STP stop the computer
- 1The short explanation is stated a bit
differently than on pg 59 in your text. All are
direct mode except for LDI.
12Be Sure to Remember For Most of These, the
Operand is an Address Which Must Be Interpreted
Properly
- 0001 ADD add ACC to operand
- This means to add the value stored in ACC to the
value stored at the address given by the operand. - Example Assume the instruction is
- 0001 0000 0000 0000 1111
- the ACC is
- and address 15 (which is address 1111) is holding
-
- Then after the instruction executes, the ACC will
hold -
13Direct Mode Addressing
- Each of the following instructions operands act
like the one for ADD - 0001 ADD add ACC to operand
- 0010 SUB subtract operand from ACC
- 0011 LOD load operand into ACC
- 0101 STO store ACC into operand
- Consequently, the operand is an address where the
data to be used is stored or an address where the
data is to be stored.
14Immediate Mode Addressing
- For the instruction
- 0101 LDI Load immediate into ACC
- The operand holds the actual number.
- Example Assume the instruction is
- 0101 0000 0000 0000 1111
- Then after the instruction executes, the ACC will
hold the number shown in the operand, not the
number at address 15.
15
15Operands for Jump Instructions
- These hold the address of the next instruction to
be executed. - For our machine, there are 3 jump instructions.
- One is an unconditional jump, JMP, i.e. the
jump is always taken. - The other two are conditional jumps.
- JNG - jump if the ACC value is less than 0
- JZR jump if the ACC value is 0
- When a conditional jump is encountered, the ALU
performs the test and sets the condition codes
appropriately. - It the condition is satisfied, the PC is reset to
the value of the operand.
16The Remaining Three Instructions
- STP, INP, and OUT do not use operands.
- Thus, the number 0 is placed as the operand for
these. - Notice that if you wanted to save space on some
instructions and use a variable length
instruction, these three would be possible
candidates for a smaller instruction length. - That is why some machines use variable length
instructions, but you would need some way of
specifying how long an instruction is.
17What is the SSC Machine Missing in Instructions?
- Note that only integers are represented, not
floating point numbers, logicals, or characters. - Consequently, the add, subtract, input, output,
and conditional test instructions are only for
integers. Moreover, there are no multiply nor
divide instructions. - But, if you can add you can multiply and if you
can subtract you can divide how? (see classroom
notes) - After representing floating point numbers,
logicals, and characters, arithmetic operations,
input, output, and conditional test instructions
would be needed to handle all these kinds of
data. - So, a real machine would need far more than 16
opcodes and more than 4 bits would be needed for
the opcode in an instruction.
18RISC vs CISC
- Over time, instruction sets for real machines
became more and more complex. - These CISC (complex instruction set computers)
such as the IBM 360/370 often had over 200
instructions. - CISCs were acceptable because instruction
execution was fast and memory access was slow. - In 1987, SUN introduced a RISC (reduced
instruction set computer) with significantly
fewer instructions and it was found to be faster
than the CISCs because memory access times had
been reduced significantly.
19Need for More Addresses
- Clearly we want larger memories than 4K bytes.
- How do we obtain memory sizes of 1G today?
- Note that an instruction format that allows 30
bits for an address can support 230 1G of
memory. - That is why a specific model of a computer has a
maximum size of memory that can be supported. - An opinion It is always wise to max out the size
of memory when you buy a machine. An ample amount
of memory allows many programs to run faster.
20Top 10 Instructions Typically Used
Integer Average Total Executed
Rank
Instruction
22
load
1
20
conditional branch
2
16
compare
3
12
store
4
8
add
5
6
and
6
5
sub
7
4
move register-register
8
1
call
9
1
return
10
96
Total
As you can see, these are similar to the ones in
our Super Simple CPU
21Machine Language Programming
- A computer only understands its machine
language, the instructions built into the
hardware of a particular machine. - The electronics of the CPU embody the instruction
set in its design. - Each machine language instruction does only one
very low level task. - These low level tasks can combined and sequenced
by a machine language programmer who directs the
computer what to do to solve a problem by
designing a machine language program, loading the
program into the memory of the computer, and
setting the PC to the address at the start of the
program. - Note that SSC assumes the PC is always set to 0
initially.
22Machine Language Programming
- Even simple tasks can be fairly complicated in
machine language. Consider the GCD (greatest
common divisor) program in your lab
23MACHINE LANGUAGE PROGRAMMING
- Although humans CAN program in a machine
language, there are some difficulties if we only
had machine language programming available to us. - 1) Writing and reading binary numbers is error
prone and difficult. (Hexadecimal notation helps,
but it doesn't eliminate the problems). - 2) Remembering opcodes as binary numbers is
unintuitive, to say the least! - 3) Converting data and addresses to binary form
is generally not viewed as fun.
244) Numeric addresses make it difficult to modify
programs
Note the addresses need to change! 0 LOD 6 1
ADD 5 2 SUB 7 3 STO 6 4 STP 5 4 6 8 7
6
Example (written in base 10 and using mnemonics
for the opcodes) 0 LOD 5 1 ADD 4 2 STO
6 3 STP 4 4 5 8 6 6
Now we decide to do another operation.
Oops, there is no output! This, at least is an
easy change.
254) Numeric addresses make it difficult to modify
programs
Example (written in base 10 and using mnemonics
for the opcodes) 0 LOD 5 1 ADD 4 2 STO
6 3 STP 4 4 5 8 6 6
Note the addresses need to change! 0 LOD 6 1
ADD 5 2 SUB 7 3 STO 6 out 4 STP 5
4 6 8 7 6
Now we decide to do include a SUBTRACT
Oops, there is no output! This, at least is an
easy change.
26Early in the history of computing, machine
language programmers realized that when they
programmed, they would
- Use mnemonics for the instructions.
- Use identifying names for the addresses they
needed. - Tag address locations with a symbolic address
name. - Use decimal notation
- Use some mnemonic such as dat to specify data.
Note each data has to have a tag so it can be
identified.
27 - For example, a mnemonic opcode
- inp
- add x
- out
- stp decimal numbers
- x dat 8
- mnemonic signaling data follows
- tag or label for a symbolic address
28Then they would hand translate the shorthand code
they used into machine code
- inp 0110 0000 0000 0000
- add x 0001 0000 0000 0100
- out 0111 0000 0000 0000
- stp 1111 0000 0000 0000
- x dat 8 0000 0000 0000 1000
- Someone finally thought Why should I do all
this work? Why not write a program (in machine
language, of course as that was all there was)
and have the machine do the translation? - Thus, assembly language programming was born.
- The translating program which translates from
assembly language to machine language is called
an assembler.
29Assembly Language Programming
- Now some of our objections to machine language
programming disappear - The mnemonics are fairly easy to remember.
- We dont have to deal with binary numbers.
- The use of symbolic names for addresses enables
us to change a program easily. -
INP ADD X OUT STP X DAT 8
INP ADD X SUB Y OUT STP X DAT 8
Y DAT 4
30Assembly Language Programming
- Still, only low level actions are performed as
assembly code is just like machine code, but in
an easier to read form for humans. - There is essentially a 1-1 correspondence between
machine language lines in a program and assembly
language lines in a program. - Like machine language, assembly language is
unique to a machine model, but technically
different mnemonics can be used for an
instruction so more than one assembly language
can be designed for a machine. - i.e. LOAD instead of LOD (or even something
like PutACC.
31Assembly Language Programming
- Lets write some program segments that could be
part of a larger program, just to see what types
of instructions can be built with assembly code. - The question we are exploring here is, What kind
of actions can be performed by a computer? - It is important that you understand the next
slides as they will form the basis for all
statements about what a computer can do
32Now We Can Write And Modify Programs Without
Concern About Binary Numbers and Keeping Track of
Actual Addresses
lod x add y sub z out
sto w ... stp x dat 5 y dat 8 z
dat 4 w dat 0
If we use the symbol ? for store, we are asking
for the computation of w ? 5 8 4 The x, y,
and z are tagging memory locations holding
numbers that are constant i.e. they are the
same every time the program runs.
What kind of actions can a computer perform? 1)
Arithmetic calculations certainly.
Lets compute 5 8 - 4, output the
total, and store it somewhere for use later in
the program.
33What kind of actions can a computer perform?
inp sto x inp sto y inp
sto z lod x add y sub z
out sto w ... stp x dat 0 y
dat 0 z dat 0 w dat 0
We are asking for the computation of w ? x y
z The x, y, and z are tagging memory locations
holding numbers that can vary for each program
run.
1) Arithmetic calculations certainly.
To make this more general, lets input 3 numbers,
x, y, and z, compute x y - z, output the total,
and store it somewhere for use later in the
program.
34Note That a Program Is Not Unique
- If we allow Y to be input first, instead of A we
can use B
inp sto x inp sto y inp
sto z lod x add y sub z
out sto w ... stp x dat 0 y
dat 0 z dat 0 w dat 0
inp sto y inp sto z inp
add y sub z out sto w
... stp y dat 0 z dat 0 w dat 0
B
A
35What kind of actions can a computer perform?
inp jng then add b
sto b out jmp end then
add five sto a out end
... a dat 0 b dat 6 five
dat 5
2) Selection of different tasks to do by
branching.
Input a value for A. If A lt 0, add 5 to it and
output the new value for A. Else, add A to B and
output the new value for B.
We can write this as if a lt 0 then a ? a
5 output a else b ? a b
output b
36Another Way to Look at This
if a lt 0 then a ? a 5 output a else
b ? a b output b
Now you can see why this is called a branching
construct.
Input A
true
A ? A 5
false
A lt 0?
B ? A B
output A
output B
next instruction
37Branching Constructs
- What is accomplished by branching constructs?
- Based on the truth or falsity of a condition
expressed as a boolean expression, different
paths can be taken through the code. - Thus, if you were wanting to do some processing
on the records of all students at Mount Allison
and the processing was different for each class
(First year, Second year, etc.) the student was
in, a branching construct would be useful. - if (person is First year) then do procedure
1 - else if (person is a Second year) then do
procedure 2 - else if (person is a Third year) then do
procedure 3. -
38What kind of actions can a computer perform?
There is no JPO jump positive. Note that a gt 0
is false if a lt 0 is true or a 0 is true.
2) Branching constructs
I have been introducing pseudocode, a simple way
to specify a command in English-like language.
Lets continue to use that.
inp jng else
jzr else add five out
jmp end else out end
... five dat 5
What if we want? if a gt 0 then a ? a 5
output a else output a
39What kind of actions can a computer perform?
- Although more work is involved, it should be
clear that instructions such as - if a lt b and c ? d then
- x ? a b c d
- output x
- else
- x ? -a b c d
- output x
- can be coded.
- Realize
- x lt y if x y lt 0.
- x ? y if x y lt 0 or x y
40What kind of actions can a computer perform?
Looping One of the advantages of storing
instructions in the memory of the computer is we
can execute loops. A loop is a section of code
that is executed repeatedly until a condition
either becomes true or becomes false. Well look
at only one looping construct here the while
loop. Format while (condition) do code to
execute endwhile
41While Loop
while (condition) do body i.e.code to
execute endwhile
When using this construct, the condition must
change inside the body or an infinite loop
executes. Other looping constructs exist in
various computer languages. They all allow code
to be executed repeatedly. Lets see how a while
loop can be built in the SSCs assembly language.
42While Loop
What if we change a ? -4 to a ? 1? The loop
never is entered as a lt 0 is false so there is no
output. What if we keep the a ? -4, but change a
? a 1 to a ? a - 1? We then have an
infinite loop i.e. it never stops as a lt 0 is
always true.
What would the output be?
- b? 0
- a ? -4
- while a lt 0 do
- b ? b a
- output b
- a ? a 1
- output a
- endwhile
-
-4 -3 -7 -2 -9 -1 -10 0
When working with while loops, you must avoid
creating infinite loops.
43Write the WHILE Loop in Assembly CodeExcept
Input a and b
- input b inp
- input a sto b
- while a gt 0 do inp
- b ? b a sto a
- output b loop jng end get out
if a lt 0 - a ? a - 1 jzr end /get
out if a 0 - output a lod b
- endwhile add a b ?
b a - out
- lod a
- sub one
a ? a - 1 - out
- jmp loop
go back to test a in ACC - end ltnext instructiongt
-
- a dat 0
- b dat 0
- one dat 1
44input b input a
while a gt 0 do b ? b a
output b a ? a - 1
output a
endwhile
Try writing this with different conditions other
than a gt 0. What if the test condition is a gt 0
and b gt 0 or a gt 0 or b gt0? Should we require
b to change in these cases?
45What kind of actions can a computer perform?
- We have seen it can
- Perform arithmetic calculations on integers.
(Although SSC supports only addition and
subtraction, with some effort multiplication and
division can be performed.) - Store in memory cells the value obtained from a
calculation. - Perform relational operations such as checking if
numbers are equal, not equal, or in a
relationship such as lt, gt, ?, or ?. - Perform logical operations such as AND, OR, and
NOT. - Characters are just integers so it should be
clear they can be stored and manipulated. (What
would a lt b mean?) - We have seen how to represent floating point
numbers. It should be clear, those can also be
stored an manipulated. - The above type of operations are collectively
called arithmetic/logic operations.
46What kind of actions can a computer perform?
- We have seen it can do
- Arithmetic/logic operations
- Branching operations.
- Looping with a WHILE loop.
- Input
- Output
- Although these seem to be very few operations,
there is a theorem in theoretical computer that
proves that these operations are sufficient to
represent ANY algorithm i.e. to program any set
of instructions the computer is to perform!
47NOTE HOW POWERFUL THIS STATEMENT IS There is a
theorem in theoretical computer science (Boehm
Jacopini, (1966), Communications of the ACM
9(366-371) that proves that these 5 operations
are sufficient to represent ANY
algorithm! Algorithms are used to do everything
you see on a computer!
- Do word processing.
- Fly probes to the planets.
- Run the international telephone switching
system. - Create CAT scan images.
- Process your pay checks.
- Run computer games.
- Etc. anything you can think of