1.2.3. Introduction To Programming
1.2.3. Introduction To Programming
www.pmt.education
Specification:
1.2.3 a)
● Procedural programming language techniques:
○ Program flow
○ Variables and constants
○ Procedures and functions
○ Arithmetic, Boolean and assignment operators
○ String handling
○ File handling
1.2.3 b)
● Assembly language
○ Following and writing simple LMC programs
www.pmt.education
Procedural programming language techniques
Program Flow
Structured programming is a popular subsection of procedural programming in which the
program flow is given by three main programming structures:
- Sequence
Code is executed line-by-line, from top to bottom.
- Selection
A certain block of code is run if a specific condition is met, using IF, ELSE IF
and ELSE statements.
- Iteration
A block of code is executed a certain number of times or while a condition is
met. Iteration uses FOR, WHILE or REPEAT UNTIL loops.
Constants are also named locations in memory, but the value of a constant cannot be
edited by the program during execution. Constants are used for values that do not need to
be changed, such as the value of pi or the current rate of VAT. Declaring a value as a
www.pmt.education
constant also prevents it from being accidentally changed. When writing code, constants
are often capitalised, as shown below:
PI = 3.14159
VAT = 20
function isEven(number):
if number MOD 2 = 0:
return True
else:
return False
end function
**
is used for exponentiation which is when a number is raised to a power.
2**4 gives 16.
DIV or // calculates the whole number of times a number goes into another. This is
called integer division.
50 DIV 7 g ives 7.
MOD o
r % is used to find the remainder when a number is divided by another.
50 MOD 7 g ives 1.
www.pmt.education
Relational operators are used to make comparisons between two values and produce a
result of either True or False. These include >, < and = alongside greater than and equal to
and less than and equal to signs which are denoted as >= and <= respectively.
One additional operator is the ‘not equal to’ operator which is often used as part of
conditional statements, as shown below:
if result != keyword:
Print ‘not found’
== i
s used to check whether one value is identical to another.
AND will return true only if all of the values being compared are true. OR will return true if
at least one of the values forming the statement returns True.
String handling
There are various operations that can be performed on
strings and that you need to be aware of.
File handling
www.pmt.education
In addition to manipulating strings, you need to be able to use pseudocode to handle files.
To close a file:
myFile.close()
Assembly Language
Assembly language is the next level up from machine code and is part of a family of low
level languages. This is converted to machine code using an assembler when it is
executed.
Assembly language uses mnemonics rather than binary, which makes it easier to use than
direct machine code. Each mnemonic is represented by a numeric code. However, the
commands used by assembly language are processor-specific as they directly interact
with the CPU’s special purpose registers. This allows for direct interaction with hardware
so is useful in embedded systems. Typically, each instruction in assembly language is
equivalent to almost one line of machine code.
Below is a list of the mnemonics you need to be aware of and be able to use:
ADD Add Add the value at the given memory address to the
value in the Accumulator
www.pmt.education
from the value in the Accumulator
LDA Load Load the value at the given memory address into the
Accumulator
INP Input Allows the user to input a value which will be held in
the Accumulator
HLT Halt Stops the program at that line, preventing the rest of
the code from executing.
Below is an example of an LMC program which returns the remainder, called the modulus,
when num1is divided by num2
.
INP
STA num1
INP
STA num2
LDA num1
positive STA num1 // branches to the ‘positive’ flag,
SUB num2 subtracting num2 while the result
BRP positive of num1 minus num2 is positive
LDA num1
OUT
HLT
num1 DAT
num2 DAT
www.pmt.education
www.pmt.education