Programming Model 3
Programming Model 3
Introduction
Objectives
At the end of this lab you should be able to:
Explain how common program variables are stored
Distinguish between different types of high-level program
statements
Understand low-level code corresponding to program
statements
Explain how program subroutines work
Use the simulator to create user interrupts
Basic Theory
High-level language (HLL) programs are made of variables holding
data values and multiple program statements as algorithms. These
statements often control the flow of program execution under certain
conditions. Calls to subroutines and interrupts all change the sequential
flow of a program execution without which feature programs would not
do any useful work.
Simulator Details
This section includes some basic information on the simulator, which
should enable the students to use the simulator. The tutor(s) will be
available to help anyone experiencing difficulty in using the simulator.
The simulator for this lab is an application running on a PC and is
composed of multiple windows.
1
Image 1 - Main simulator window
2
Image 3 - The main compiler window
3
program Test1
var IntVar integer
var BoolVar boolean
var StrVar1 string (5)
var StrVar2 string(20)
IntVar = 6
BoolVar = true
StrVar1 = "Hello"
StrVar2 = "And again"
end
Program Test2
n = 0
i = n + 1
p = i * 3
writeln (“ n=”, n, ” i=”, i, ” p=”, p)
end
Compile the above program. Now observe the code generated in the
PROGRAM CODE window. You don’t need to analyse it in detail.
However, count the number of jump instructions (i.e. those that start
with a letter ‘J’) and note this down. Can you tell what kind of program
statements this program is using?
Program Test3
n = 0
if n < 5 then
p = p + 1
end if
end
4
Compile the above program. Now observe the code generated. How
many jump instructions are there? What do you think is the purpose of
the jump instruction in this code? What kind of a statement is an ‘if’
statement?
program Test4
p = 1
for n = 1 to 10
p = p * 2
next
end
Compile the above program. Now observe the code generated. How
many jump instructions are there? What do you think is the purpose of
each of the jump instructions in this code? What kind of a statement is
a ‘for’ statement? Can you think of another statement of this kind (you
can give an example from any programming language you are familiar
with)?
Program Test5
sub One
writeln(“I am sub One”)
end sub
sub Two
call One
writeln(“I am sub Two”)
end sub
call Two
End
5
note of the PROGRAM STACK frame contents after executing a CAL
instruction or a RET instruction. Keep executing instructions until you
reach the HLT instruction.
program Test6
sub Any
n = 0
end sub
do
loop
end
Slow down the CPU simulation (e.g. a little above half way on the
sliding scale). Trigger the interrupt and observe the PROGRAM
STACK contents. You can click on the STOP button as soon as you
see numbers appearing on this stack so that you have time to look at
its contents. What do you observe?
There are two main types of interrupts: vectored and polled. Which
type is the above interrupt? Explain.