Chapter 2
Chapter 2
• The binary code that is understood by the microprocessor is called machine code
and consists of streams of binary bits.
• They are fed from the RAM or ROM memory chips in blocks of 8, 16, 32 or 64
depending on the microprocessor in use.
• To us the binary stream is total gibberish.
• Typing in streams of ones and zeros is so boring that we will make many
mistakes, particularly when we remember that a real program may be ten
thousand times longer than this.
• Can you imagine typing in half a million bits, finding the program does not run
correctly and then settling down to look for the mistakes?
• Another problem is that the programmer must be aware of the internal structure
of the microprocessor.
• How else could you know which register to use, or even which registers exist?
• So you master all this and then change to another microprocessor and then
what? The whole learning process has to start again – new instructions, new
registers, and new coding requirements. It’s all too horrible.
• The difficulties with machine code hardly mattered in the early days of the
microprocessor.
• Everyone who programmed them were fanatics and loved the complexity and
there were few serious jobs for the microprocessor to do.
• This first program language was called a ‘low-level’ language to differentiate it
from our own verbal communication language which was called a high-level
language.
• Machine code was later referred to as the ‘First generation’ language
2. Assembly language, the second generation language
• Assembly language was designed to do the same work as machine code but be much
easier to use.
• It replaced all the ones and zeros with letters that were easier to remember but it is
still a low-level language.
• It uses the code called a mnemonic.
Example
• SLA E; shift to the left, the contents of register E.
• LD B 25H; load the B register with 25H.
• INC H
• LD C 48H; load 48H to register C
• ADD A,15H; add 15H to the number A contents
All these mnemonic code converted to machine code by program called an Assembler.
• The program allows us to type in the assembly code, called the source code, and
converts it to machine code referred to as object code.
• Assembly and machine code are not portable. This means that they are designed
to be used on a particular microprocessor and are generally not able to be used
on another type.
• They also require the programmer to have knowledge of the internal layout or
architecture of the microprocessor.
• Assembly languages have two overriding advantages in the hands of a
competent programmer (note the ‘competent’).
• A program written in Assembly is faster and is more compact, i.e. it takes less
memory space to store it.
• Machine code and assembly languages are called procedural languages.
• This means that the program instructs the microprocessor to complete the first
instruction, then start the next, then the next and so on until it has finished the
job. This is just like a recipe.
• Assembly language is low level language.
3. High-level languages
1. Third-generation languages
• They were designed to improve the readability by using English words which
would make it easier to understand and to sort out any faults (bugs) in the
program.
• The process of removing bugs is called debugging.
• These languages are called ‘high-level’ and are all procedural.
Compilers
• In Fortran, or any high-level language, we use a compiler to produce the machine
code.
• The compiler will also carry out the useful extras like error and syntax checking
that we met with the assemblers.
• Compilers and assemblers are both software that is, they are programs designed
to do a specific job.
Fortran source code
• Fortran consists of numbered statements called program lines and are used to
tell the system the order in which the instructions are to be carried out.
For example, to load a number and find the square root of it may look like this:
1 Read (4) P
2 A = SQRT(P)
Basic source code
Basic is very readable
• It uses line numbering as in Fortran and is generally readable.
• Here is an example. Test out its claim to readability by guessing the outcome.
10 input A,B
20 Let C = A * B
30 Print C
40 End
• The using of line numbers that increase in steps of five or ten instead of ones. The
advantage is that any forgotten instruction can be added later by giving it a new line
number.
Example, if we remembered that we wanted to divide the value of C/2 then print this
result as well, we could add a couple of extra lines:
• 10 input A, B
• 20 Let C = A * B
• 30 Print C
• 32 Let D = C/2
• 35 Print D
2. Fourth-generation languages
Condition may be true or false, where non-zero value regarded as true & zero value regarded as false.
If condition are satisfy true, then a single or block of statement executed otherwise another single or block
of statement is executed.
if (condition)
{
Statement1;
}
else
{
Statement2;
}
Else statement cannot be used without if or no multiple else statement are allowed within one if statement. It
If….else LADDER
• In this type of nesting there is an if else statement in every else part except the
last part.
• If condition is false control pass to block where condition is again checked with
its if statement. Syntax is :-
if (condition)
Statement1;
else
if (condition)
statement2;
else if (condition)
statement3;
else
statement4;
This process continue until there is no if statement in the last block.
if one of the condition is satisfy the condition other nested “else if” would not
executed.
But it has disadvantage over if else statement that, in if else statement
whenever the condition is true, other condition are not checked.
While in this case, all condition are checked.
Loops in C
Loop:-it is a block of statement that performs set of instructions. In loops Repeating
particular portion of the program either a specified number of time or until a
particular no of condition is being satisfied.
There are three types of loops in c
1.While loop
2.do while loop
3.for loop
Whileloop
Syntax:-
/* wap to print 5 times welcome to C” */
#include<stdio.h>
void main()
{
int p=1;
While(p<=5)
{
printf(“Welcome to C\n”);
P=p+1;
}
}
The test condition may be any expression .when we want to do something a fixed no of
times but not known about the number of iteration, in a program then while loop is used.
do while loop
• This (do while loop) statement is also used for looping. The body of this loop may contain single
statement or block of statement. The syntax for writing this statement is:
Example:-
#include<stdio.h>
void main()
int X=4;
do { Printf(“%d”,X);
X=X+1;
}
whie(X<=10);
for loop
• In a program, for loop is generally used when number of iteration are known in
advance.
• The body of the loop can be single statement or multiple statements.
• Its syntax for writing is: Syntax:-
Example:-
void main()
{
int i;
for(i=1;i<10;i++) //(initialized counter; test counter; update counter)
{
Printf(“ %d ”, i);
}
} Output:-1 2 3 4 5 6 7 8 9
Nesting of loop
• When a loop written inside the body of another loop then, it is known as nesting of
loop.
• Any type of loop can be nested in any type such as while, do while, for. For example
nesting of for loop can be represented as :
void main()
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<5; j++)
printf(“%d %d”, i, j);
Break statement(break)
• Sometimes it becomes necessary to come out of the loop even before loop condition becomes
false then break statement is used. Break statement is used inside loop and switch statements.
Example :
void main()
{
int j=0;
for(;j<6;j++)
if(j==4)
break;
} Output: 0 1 2 3
Continue statement (key word continue)
• Continue statement is used for continuing next iteration of loop after skipping
some statement of loop.
• When it encountered control automatically passes through the beginning of the
loop. It is usually associated with the if statement. It is useful when we want to
continue the program without executing any part of the program.
Example:-
void main()
{
int n;
for(n=2; n<=9; n++)
{
if(n==4) continue;
printf(“%d”, n);
}
}
Printf(“out of loop”);
} Output: 2 3 5 6 7 8 9 out of loop
Character set
• A character denotes any alphabet, digit or special symbol used to represent
information.
• Valid alphabets, numbers and special symbols allowed in C are
ARRAY
• Array is the collection of similar data types or collection of similar entity stored in contiguous
memory location.
• Array of character is a string.
• Each data item of an array is called an element. And each element is unique and located in
separated memory location.
• Each of elements of an array share a variable but each element having different index no.
known as subscript.
• An array can be a single dimensional or multi-dimensional and number of subscripts
determines its dimension.
• And number of subscript is always starts with zero.
DECLARATION OF AN ARRAY :
• Its syntax is :
Data type array name [size];
int arr[100];
int mark[100];
int a[5]={10,20,30,100,5}
Registers and memories
• The logic gates in a microprocessor, are usually grouped together into circuits,
called flip-flops, each one being able to store a single binary digit
A flip-flop or bistable
• A flip-flop or bistable is a circuit that can store a single binary bit – either 0 or 1.
One useful characteristic of the flip-flop is that it can only have an output of 0 or
1. It cannot hover somewhere in between.
A flip-flop – the basic building block of a microprocessor
A register
• A register is just a collection of flip-flops. A flip-flop can only store one bit so to
handle 32 bits at a time we would need 32 flip-flops and would refer to this as a
32-bit register.
• The register has two distinct groups of connections: the data bits 0 to 7 and the
control signals.
• The data connections or data lines carry the binary levels in or out of the register.
• The number of data lines determines the size of the register so a 64-bit register
would have 64 data connections.
What are registers for?
• Registers are storage areas inside the microprocessor.
• Almost the whole of the microprocessor is made of registers.
• They store the data that is going to be used, they store the instructions that are
to be used and they store the results obtained.
• Nearly all registers involve tri-state buffers to control the direction of data flow.
• The information is then safely stored until it is next required.
An 8-bit register
Enable. This is a simple on/off switch for the register.
The line over the top of the word indicates that it is ‘on’ when this line is ‘low’ or at
logic zero Nearly all control lines are active low.
The benefit of having the enable line is that we are able to disconnect a register
without doing any physical uncoupling of links etc.
And what about the clock signal?
• This is just an input to tell the flip-flop that it is time to read the input level.
• All microprocessor operations are carefully timed by clock pulses to ensure that
the system operates in the correct sequence.
• The clock signal is usually a positive-going voltage pulse.
• This pulse can be used to switch two circuits at different times by designing one
circuit to respond to an increasing voltage and the other to use a decreasing
voltage.
Read/write. The terms ‘read’ and ‘write’ are used to describe the direction of data
movement.
• We ‘write’ data into a register then ‘read’ the data to recover it.
• The flip-flop included a separate line for reading the data and another for
writing.
• Now, while this was OK with a single flip-flop, a 64-bit register would require 128
lines just to carry the data in and out.
The sequence is:
1 The read/write line is taken to logic 0 to allow the register to receive data from
an external source.
2 The enable control switches ON the tri-state buffers at the input to each flip-flop.
3 The data is written to each flip-flop and then the enable control puts the register
to sleep until the next time it is needed.
How long can it be stored?
• It will be stored until the power supplies are removed – either by an equipment
fault or, more usually, by the system being switched off.
• The data does not deteriorate in storage.
Memories
• The function of a memory is to store information – almost the same as we said for
the register.
• Generally, a register lives within the microprocessor and stores small quantities of
data for immediate use and it can do useful little tricks like shift and rotate.
• A memory is designed for bulk storage of data but that is all it can do – no tricks
this time.
• Well, almost no tricks – some types can remember the data even when the power
is switched off.
• The ability to remember data after the power is switched off is the dividing line
• If it loses its data when the power is switched off, then we call the memory RAM
or volatile memory.
• If it can hold on to the data without power, we call it ROM or non-volatile
memory (volatile means ‘able to evaporate’).
RAM. It should be called read/write memory or RWM but it is so difficult to get
something to change once it is established
• The memory comes in an integrated circuit looking like a small microprocessor
and is usually called a memory chip.
• Inside, there are a large number of registers, hundreds, thousands, millions
depending on the size of the memory.
Static RAM
• These are constructed of flip-flops.
• The problem with the flip-flop is that it draws current all the time. Therefore, it
tends to get rather warm and, on a single chip, the components cannot be
packed together very tightly.
• The benefit is that they are very fast and are used where speed of access is
important.
• Static RAM is often called SRAM.
Dynamic RAM
• These store the information in capacitors, which are small components that
store an electrical charge in the form of static electricity.
• But the electricity stored in each capacitor leaks away because of the imperfect
insulation. So, after a little while the charge has to be replaced otherwise the
DRAM will be empty and all the stored information will be lost.
• This replacing is called ‘refreshing’ and has to be performed at intervals of about
2 ms by a DRAM control circuit.
• Less heat is being generated internally and we can pack more memory into a
given space & high packing density.
Three types of ROM
• All ROMs are used to store information on a more-or-less permanent basis.
• In use, the ROM can be read but new information cannot be stored in it.
• In other words, we cannot write to it.
Masked ROM
• A masked ROM is manufactured to our specification and cannot be changed.
• We must be very sure that the information is correct before it is made otherwise
it all goes in the waste bin and the person responsible is looking for a new job.
• The initial cost is necessarily high due to the expense of the tooling required.
• It is only worthwhile if at least a few thousand identical chips are required.
Programmable ROM (PROM)
• This chip is supplied with all the data held at zero by means of small internal
fuses.
• When one of the fuses is blown, the associated bit changes from 0 to 1.
• To blow the fuses a piece of programming equipment is needed.
• Once the fuse is blown, it cannot be repaired so if you make a mistake, the chip is
wasted.
• The PROM is useful for low volume production because the initial costs are much
lower than the masked ROM but you do have to program them yourself.
Erasable programmable ROM (EPROM)
• As the name would suggest, this chip allows us to program it, then change our mind
and try again.
• To erase the data there are two methods – ultraviolet light or electrical voltage
pulses.
• EPROMs are ideal for prototyping since it is so easy to change the data to make
The UVEPROM
• The chip is bombarded with ultraviolet light via a transparent window on the
chip.
• We pop the chip in, close the lid and switch on the timer. After a few minutes,
the data is erased. When erased, all the data output is set to 1.
• We then put the chip into an EPROM programmer, We can feed in the new data
and within a couple of minutes, we have finished the process.
• They can be erased and reprogrammed about 700 times before they become
increasingly reluctant to erase and their life is over.
• Once programmed, the data is safe for about seven years.
Electrically erasable programmable ROM (EEPROM)
• This chip uses electrical voltage pulses as inputs to clear the previous data and is
then reprogrammed in the same way as the UVEPROM.
• It has the added advantage that individual parts of the data can be
reprogrammed without deleting everything first as is the case with the
UVEPROM.
• Their disadvantage is that they are slow to program and have a limited number
of reprogramming cycles.
Buses
• These conductors are in groups, since many will be going to the same place for
much the same purpose.
• For example, an 8-bit microprocessor normally uses 8 connectors to carry the
data between the microprocessor and the memory.
• It would make diagrams very complicated if each wire were to be shown
separately so we group them together and refer to them as the data bus.
• A bus is therefore a collection of conductors providing a similar function.
• In a microprocessor-based system we have three main buses: the data bus, the
address bus and a control bus.
• The data bus is a two-way bus carrying data around the system.
• Information going into the microprocessor and results coming out.
• The address bus carries addresses and is a one-way bus from the microprocessor
to the memory or other devices.
• The control bus is rather different from the other two.