Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sequential Prog. Lec 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

FOUNDATION OF SEQUENTIAL

PROGRAMMING

The relationships between H/L languages and the Computer


Architecture that underlies their implementation: basic machine
architecture, assembles specification and translation of P/L
Block Structured Languages, parameter passing mechanisms.
Definition of Sequential
Programming
 Sequential programs are procedural driven programs which only use a
single process at a time. They are single-threaded and do not run
processes concurrently.
 These programs can be written in machine language, assembly,
higher-level languages... whatever. Since each is an abstraction of a
lower level to which we can build a compiler, all of them will give us
the same final result.
 For this course, we are not interested in what those programs do, but
simply in how they do it.
Introduction & Character Encodings
Abstraction is the process of removing or hiding irrelevant details.
Everything is just a sequence of bits (binary digits). There are two
possible values for a bit, and those values can have arbitrary labels such
as:
Up / down.
Yes / no.
1 / 0.
-/I
Pass / fail.
Suppose we have four projector screens, each representing a bit of up/
down, depending on if the screen has been pulled down or left up
(ignoring states between up and down). These screens are up or down
independently. There are sixteen possible combinations:

Screen 1 Screen 2 Screen 3 Screen 4


Up (1) Down (0) Up (1) Down (0)

Down (0) Up (1) Down (0) Up (1)


. . . .
. . . .
Data Representation
Single bit cannot do anything so they are usually put into group of k bits.
Negatives are represented as unsigned 2k greater. For example - 2 = -2
+22 = 2
In computer, we type 7 + 3 (4 bit arithmetic). Well, in this case,
all the calculations are done by modulo 2k. For 7- 6,
is equivalent to 7 + (-6) = 1.

0111(7)
+0011(3)
1010
Programs can be written in machine language, assembly, higher-level
languages... whatever. Since each is an abstraction of a lower level to
which we can build a compiler, all of them will give us the same final
result.
A computer sees a string such as "10000011" as a sequence of bits,
without regards to any sort of encoding. This could bebinary ,
hexadecimal, decimal ASCII
, ... pretty much everything, but the computer
can not know which.
Negative Numbers With n bits, can be represented 2n numbers in binary.
To represent negative numbers we can use the sign and magnitude sign
and magnitude: use the first bit as an indicator of sign and the remaining
bits as magnitude. method, though this is subject to some weirdness. We
will end up with both a positive and a negative zero and will need
specialized hardware for both addition and subtraction.
The Decimal Positional Numbering System
Ten figures: 0 1 2 3 4 5 6 7 8 9
7 x 102 + 3 x 101 + 0 x 100 = 7301
0

9 x 102 + 9 x 101 + 0 x 100 = 9901


0
Conversion of negative number
We can convert a number into two’s complement form by the
following method: for instance -6
-6 can be converted by taking the absolute value in binary (0110)
, then flipping all of its bits (1001) and adding 1 (1010).
 Also this method can be used backwards: subtract 1 from
(0110), (0100) , flip the bits, (1011) and add a negative number
-1. then (1010)
Hexadecimal
0; 1; 2;3;.. ; 9; A;B;C;D;E; F are numbers to represent Hex.
Bit
Byte is a group of 8 bits (k = 8, 2k = 28 = 256 combinations). In this case,
unsigned is between 0 and 255; signed is between -128 and 127; ASCII
codes are characters corresponding to numbers between 0 and 127.
byte meaning represents unsigned 2's complement Hex ASCII
01100001 97 97 61 a
Hexadecimal
Hexadecimal (hex) is base 16. It has sixteen case-insensitive digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f.
Why is hex useful? It makes conversions easy. We group bits into
sequences of four:

00111010
3A
American Standard Code for Information Interchange (ASCII)
ASCII was invented to communicate text. ASCII can represent characters such as A-Z,
a-z, 0-9, and control characters like ();!. Since ASCII uses 7 bits, 27 = 128 characters can
be represented with ASCII. As a consequence of that, ASCII is basically only for Roman,
unaccented characters, although many people have created their own variations of ASCII
with different characters
ASCII is a set of meanings for 7-bit sequences.
Bits ASCII Interpretation
0001010 LF (line feed)
1000111 G
1100111 g
0111000 8
In the last case, 0111000 represents the character `8', not the unsigned big- or little-endian
number 8.
Extended ASCII

Extended ASCII bytes (32 bits); unsigned is between 0 and


232 - 1; 2's complement is between -231 and 231 – 1 and 8
bytes (64 bits)
Hexadecimal Decimal Oct Binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
A 10 12 1010
B 11 13 1011
C 12 14 1100
D 13 15 1101
E 14 16 1110
F 15 17 1111
Conversion of a number
Conversions are made easy especially when the sequences of bits are
lengthy:
101110011100111011100110000011
2E73B983
Alternatively, subtract 2n if and only if the first bit is non-zero (to go from
two’s complement to decimal).
Stored Program Computers
Stored program computers are also known as the Von Neumann architecture.
They group bits into standard-sized sequences.
In modern times, standard-sized sequences of bits are: Byte. A byte is 8-bits (256
possible values). Example: 00111010.
Words.
A word is only guaranteed to be more than a byte." Words are often 16-bits (216 =
65,536 possible values), 32-bits (232 ≡ 4 x 109), or 64-bits (264 ≡ 1019).
For example
A 64-bit CPU" means it's a CPU that uses 64-bit words.
Storage Devices
A. Register
Register are typically a finite number of fixed-sized sequence of bits, bits
can be input in register and modify.
Calculators typically have 2-3 registers for recalling numbers and
maintaining state.
There are a couple of downsides to registers. They're expensive to build,
that is why there is a finite number of them. They're also difficult to keep
track of
B. Random Access Memory (RAM)
RAM is essentially a physical array that has address lines, data lines, and
control lines. Data is fed into RAM using electrical lines. Data will remain in
RAM until overwritten.
If you want to place a happy face character at address 100, you set the
address lines to 100, the data lines to 10001110 (which is the Unicode
representation of a happy face), and give the control lines a kick.
RAM could be implemented in several different ways. It could even be
created with a cathode ray tube. The core method is synonymous with RAM,
however. It involves a magnetic core and the data remains magnetized
after the magnet is removed.
Bits are read by toggling the state (toggling the magnetic poles) and seeing
if it was easier to toggle than expected (similar to unlocking an already-
unlocked door), and then toggling back after. Magnetic cores usage in no
longer done
C. Capacitive memory (also known as dynamic RAM or DRAM)

DRAM is still used today. It involves an insulator and two conductive


plates, one of which is more negatively-charged than the other. The
electrons will remain in their state even when the poles are removed.
There is a problem, however. Insulators are not perfect electrons will
eventually make their way through the insulator. In order to alleviate this,
the charge have to be refresh often (every second, for instance).
D. Switches
 Are typically used only for registers and cache. They produce more
heat, but
are much faster.
Hardware Components

Figure 2.1: Computer System Components

FUNDAMENTALS OF INFORMATION SYSTEMS, THIRD EDITION 22


Parts of a Processor
 A processor is comprised of a control unit, which controls the
execution of the program and keeps track of the next instruction to
execute. It can interpret strings of binary as command instructions and
controls the signals to and from any external devices. It contains a
timing device for synchronised operations.
 The processor also contains an arithmetic logic unit, which is
responsible for arithmetic and logic operations.
 The program counter (PC) is a 32-bit register which contains the
address of the next instruction to be executed. The Instruction Register
(IR) contains the current instruction address.
 We also have General Purpose Registers thirty-two zero-indexed
registers which are 32-bits in size though they are somewhat slow.
Range of Registers
Registers range from $0 − $31. Register $0 always contains the value 0 and register
$31 always contains the value of the return address.
Register $30 contains the address of the top of the stack.
How a Program is Executed
 The OS invokes the ”loader” to load the program into memory
starting at some given memory address, virtually always zero.
 The loader places the starting address in the PC: ”PC → 0x00”.
 We then fetch, decode, and execute each instruction in order. In
effect:
while(1) {
fetch // IR <- MEM[PC]
increment // PC <- PC + 4
decode
execute
}
MIPS Programming

Three human readable :


1.
binary (ASCII 0’s and 1’s)
2.
hexadecimal text
3.
assembly language
C MIPS
int x,y,z; $2 is x, $7 is y and $21 is z
unsigned a, b, c; $4 is a, $8 is b and $19 is c
float w; no direct MIPS implementation
x = y + z; add $2 $7 $21
a = b + c; add $4 $8 $19
y = z; add $7 $0 $21
a = b ∗ c; multu $8 $19, mflo∗ $4
x = 42; lis $42, .word 42
if(t) α; translate(α)
if (t)x = y + z; ∗
if (t) α; else β ∗∗
while (t) α; ∗∗∗
for (α;β;γ)δ; α while (β) { δ;γ}
do { α } while (t) ∗∗∗∗
MIPS Commands
MIPS commands are lines of binary which designate the function to be
executed and the registers involved. For example, in the ADD
command we have
000000 sssss ttttt ddddd 00000
100000
function code $s $t $d padding
opcode
which will give us $d = $s + $t.
The subtract command is similar, but uses the opcode 100010.
$d = $s - $t
100010
Another useful command is the jump command jr $s. This command
basically executes PC = $s and thus jumps to that address.
Note that this is necessary for exiting our program, since the address
for returning our program is initially stored in register $31.
Explanation of MIPS reference sheet ;

1. the first 6 0s means it is a simple instruction

2. the five s and five t and etc. means a register

1. 0 is always equal to 0
Here is one example for jr31.hex :
; 0000 0011 1110 0000 0000 0000 1000
;03e0008
.word 0x03e0008
Conti..
LIS is another useful command; the LIS command will run
$d = MEM[PC] PC = PC + 4
Which can be used to assign a register to any value of your choice.
Assembly Language
There is a one-to-one correspondence between each assembly and machine instruction. The
assembly can be converted directly into machine language with an assembler.
An assembler will read in a file and scan through it, recognizing each line to be a discrete
instruction. It will remove comments and divide the instructions into words and tokens. This
first step is referred to as scanning
scanning: taking the sequence of characters and outputting a sequence of tokens
This is also referred to as lexical analysis or tokenization.
There are multiple types of tokens, for example: opcode (ID), register (REG), comma. These
tokens will also be associated with what we refer to as a lexene
lexene: the string which was recognized as a given token
For example, the lexene of a REG token may be $29.
Loading

A loader is a program that places a file into RAM. For N instructions, we do:
For (int i = 0; i < N; i++) {
MEM[i] = file[i];
}
Note that we can’t necessarily assume that we begin at memory address zero. That is
where MERL (MIPS Executable Relocatable Locatable code) comes into play.
We can add additional information to our file at either the beginning (beq $0, $0, n; ...;
begin:) or the end (jr $31; ...). Often we use both: the beginning of our code contains the
address of the relocation table at the end of our code.
In effect, a relocator will read in the first n bytes of code, follow those to memory
addresses, and add α to them (after the code has already been compiled by assuming
an initial pc of zero).
For example, if a program beginning with example: .word example is meant to be run
beginning at memory address 0x8, this first line would be first compiled as 0000 0000
then relocated as 0000 1000.
Relocator
A relocator uses the following algorithm (note that the relocation table follows the sample format .word
1; .word x; .word 1; .word y...
i = alpha + MEM[alpha + 8]
end = alpha + MEM[alpha + 4]
while i < end:
if MEM[i] == 1:
MEM[alpha + MEM[i + 4]] = MEM[alpha + MEM[i + 4]] + alpha i += or,
in words
1. i is the address of the start of the relocation table
2. end is the address of the end of the relocation table
3. check if MEM[i] has value 1
4. get the original address of a location to be relocated (MEM[i + 4]) and overwrite it with the location
after the offset (MEM[i + 4] + alpha)
Advantages of Assembly Language
An understanding of assembly language provides knowledge of:
Interface of programs with OS, processor and BIOS;
Representation of data in memory and other external devices;
How processor accesses and executes instruction;
How instructions access and process data;
How a program accesses external devices.
Other advantages of using assembly language are:
It requires less memory and execution time;
It allows hardware-specific complex jobs in an easier way;
It is suitable for time-critical jobs;
It is most suitable for writing interrupt service routines and other memory resident programs.
Addressing Data in Memory

The process through which the processor controls the execution of


instructions is referred as the fetch-decode-execute cycle or the execution
cycle. It consists of three continuous steps:
Fetching the instruction from memory
Decoding or identifying the instruction
Executing the instruction
The processor may access one or more bytes of memory at a time. Let us
consider a hexadecimal number 0725H. This number will require two bytes
of memory. The high-order byte or most significant byte is 07 and the low-
order byte is 25.
The processor stores data in reverse-byte sequence, i.e., the low-order
byte is stored in low memory address and high-order byte in high
memory address. So, if processor brings the value 0725H from
register to memory, it will transfer 25 first to the lower memory
address and 07 to the next memory address
X : memory address
When the processor gets the numeric data from memory to register, it again reverses the
bytes. There are two kinds of memory addresses:
 An absolute address - a direct reference of specific location.
 The segment address (or offset) - starting address of a memory segment with the
offset value.

You might also like