Abstract

This is the third of three talks How Does a Computer work. This talk will discuss how to talk to the CPu

We are going to discuss types of instructions, methods of creating the instructions, as well as interrupts.

Due to travel restrictions this meeting will be a Virtual meeting over Zoom.

Instruction Set

An instruction set architecture (ISA) is an abstract model of a computer. It is also referred to as architecture or computer architecture. A realization of an ISA, such as a central processing unit (CPU), is called an implementation.

In general, an ISA defines the supported data types, the registers, the hardware support for managing main memory fundamental features (such as the memory consistency, addressing modes, virtual memory, and the input/output model of a family of implementations of the ISA.

Ref: Instruction set architecture

So in a nut shell, each CPU contains an instruction set the define what instructions it can execute. These instructions are hard coded into the CPU as design. Another name for the instructions are Operational Codes.

What is an Instruction Set

Free42
Free42

If I gave you a calculator, like the one pictured above, and asked you to tell me what are the calculations it can perform, you would just read the keys. Alternately you could look at the manual to know what the calculator can do. Well a CPU is basically a calculator, so its instruction set is a list of the things it can do.

Now just as with the calculator shown above, you have to input the numbers before executing a calculation. The same is true with the CPU. With the CPU you need to tell it where to store the result, but on the calculator you just read it from the screen.

Operational Codes (op codes)

Now that we know was an opcode is we can look at instructions for the Motorola 6800 here.

Programming the Computer in Assembly

The following excerpts are from the book 6800 Assembly Language Programming.

For example, this is the 6800 program which adds the contents of memory locations 60 and 61 and places the result in memory location 62:

Binary
10010110
01100000
10011011
01100001
10010111
01100010

This is a machine language, or object, program. If this program were entered into the memory of a 6800-based microcomputer, the microcomputer would be able to execute it directly.

Although the computer handles binary numbers with ease, people do not. People find binary programs long, tiresome, confusing, and meaningless. A programmer may even¬ tually start remembering some of the binary codes, but such effort should be spent more productively.

We can improve the situation somewhat by writing instructions as octal or hexadecimal, rather than binary, numbers. We will use hexadecimal numbers in this book because they are shorter, and because they are the standard for the microprocessor industry.

Hex
96
60
98
61
97
62
Hexadecimal Digit Binary Equivalent Decimal Equivalent
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
A 1010 10
B 1011 11
C 1100 12
D 1101 13
E 1110 14
F 1111 15

Along with the instruction mnemonics, the manufacturer will usually assign names to the CPU’s registers. As with the instruction names, some register names are obvious (e g., A for Accumulator) while others may have only historical significance. Again, we will use the manufacturer’s suggestions simply to promote standardization.

If we use standard 6800 instruction and register mnemonics, as defined by Motorola, our 6800 addition program becomes:

Assembly Code version

LDAA
60 
ADDA
61  
STAA
62

Normally written as

LDAA 60 
ADDA 61  
STAA 62

Transforming assembly code to machine code

We have see how to write simple programs in assembly code. But the computer does not read assembly code, only machine code. To translate from assembly into machine code we use a program called an assembler.

The Assembler is used to translate the program written in Assembly language into machine code. The source program is a input of assembler that contains assembly language instructions. The output generated by assembler is the object code or machine code understandable by the computer. Language Processors: Assembler, Compiler and Interpreter

Programming Computers in High level languages

Now that we have seen how to write a program in assembly code, we can understand why most programmers do not want to write in assembly. You need to define every step, and it is easy to create errors which are hard to find. Given this limitation, programmers sought to create something closer to human language to use in programs.

Computer languages

Lets do a brief history of some of the major programming languages with example code.

FORTRAN Programming Language

In 1957, the first of the major languages appeared in the form of FORTRAN. Its name stands for FORmula TRANslating system. The language was designed at IBM for scientific computing. The components were very simple, and provided the programmer with low-level access to the computers innards. A History of Computer Programming Languages

Example of FORTRAN code. From: Fortran This is as later version, For tram 90

program average

  ! Read in some numbers and take the average
  ! As written, if there are no data points, an average of zero is returned
  ! While this may not be desired behavior, it keeps this example simple

  implicit none

  real, dimension(:), allocatable :: points
  integer                         :: number_of_points=0.0
  real                            :: average_points=0., positive_average=0., negative_average=0.

  write (*,*) "Input number of points to average:"
  read  (*,*) number_of_points

  allocate (points(number_of_points))

  write (*,*) "Enter the points to average:"
  read  (*,*) points

  ! Take the average by summing points and dividing by number_of_points
  if (number_of_points > 0) average_points = sum(points) / number_of_points

  ! Now form average over positive and negative points only
  if (count(points > 0.) > 0) then
 positive_average = sum(points, points > 0.) / count(points > 0.)
  end if

  if (count(points < 0.) > 0) then
 negative_average = sum(points, points < 0.) / count(points < 0.)
  end if

  deallocate (points)

  ! Print result to terminal
  write (*,'(a,g12.4)') 'Average = ', average_points
  write (*,'(a,g12.4)') 'Average of positive points = ', positive_average
  write (*,'(a,g12.4)') 'Average of negative points = ', negative_average

end program average

Lisp Programming Language

In 1958, John McCarthy of MIT created the LISt Processing (or LISP) language. It was designed for Artificial Intelligence (AI) research. Because it was designed for a specialized field, the original release of LISP had a unique syntax: essentially none. Programmers wrote code in parse trees, which are usually a compiler-generated intermediary between higher syntax (such as in C or Java) and lower-level code. A History of Computer Programming Languages

Here are a couple of examples of Lisp from Lisp (programming language)

The basic “Hello world” program:

(print "Hello world")

An iterative factorial version which uses Common Lisp’s loop macro:

(defun factorial (n)
  (loop for i from 1 to n
    for fac = 1 then (* fac i)
    finally (return fac)))

Pascal Programming Language

Pascal was begun in 1968 by Niklaus Wirth. Its development was mainly out of necessity for a good teaching tool. In the beginning, the language designers had no hopes for it to enjoy widespread adoption. Instead, they concentrated on developing good tools for teaching such as a debugger and editing system and support for common early microprocessor machines which were in use in teaching institutions. A History of Computer Programming Languages

Here is an example program taken from Pascal (programming language)

Pascal is a structured programming language, meaning that the flow of control is structured into standard statements, usually without ‘goto’ commands.

while a <> b do  WriteLn('Waiting');

if a > b then WriteLn('Condition met')   {no semicolon allowed!}
else WriteLn('Condition not met');

for i := 1 to 10 do  {no semicolon for single statements allowed!}
WriteLn('Iteration: ', i);

repeat
a := a + 1
until a = 10;

case i of
0 : Write('zero');
1 : Write('one');
2 : Write('two');
3,4,5,6,7,8,9,10: Write('?')
end;

C Programming Language

C was developed in 1972 by Dennis Ritchie while working at Bell Labs in New Jersey. The transition in usage from the first major languages to the major languages of today occurred with the transition between Pascal and C. Its direct ancestors are B and BCPL, but its similarities to Pascal are quite obvious. All of the features of Pascal, including the new ones such as the CASE statement are available in C. C uses pointers extensively and was built to be fast and powerful at the expense of being hard to read. But because it fixed most of the mistakes Pascal had, it won over former-Pascal users quite rapidly. A History of Computer Programming Languages

Here is an example of C code from C programs

Example - C program check if an integer is prime or not

#include <stdio.h>

int main()
{
  int n, c;

  printf("Enter a number\n");
  scanf("%d", &n);

  if (n == 2)
printf("Prime number.\n");
  else
  {
for (c = 2; c <= n - 1; c++)
{
  if (n % c == 0)
    break;
}
if (c != n)
  printf("Not prime.\n");
 else
   printf("Prime number.\n");
  }
  return 0;
}

Example - command line arguments

#include <stdio.h>
int main(int argc, char *argv[])
{
  int c;

  printf("Number of command line arguments passed: %d\n", argc);

  for (c = 0; c < argc; c++)
printf("%d argument is %s\n", c + 1, argv[c]);

  return 0;
}

Computer language History

Graphic flowchart of Computer language History. You will need to magnafy this to view this graphic, it is BIG.

Translating High level Languages

We have seen how high level computer languages have evolved but how do you get from a high level language to machine code. The answer is a compiler. Now there are two types of compilers, one converts the computer code to machine code and stores it is an executable file. This is how languages like C are compiled.

The other type is called an interpreter, which converts the high level language which is then converted and executed on the fly. A commonly interpreted language is Pascal.

So now we understand how to translate a computer program to something a CPU can understand.

CPU control

Lets take a step back to view a block diagram of the Motorola 6800 again. There are some control pins we need to understand.

6800 Block Diagram
6800 Block Diagram

Lets discuss the following pins to understand better how the CPU works.

Reset

The reset pin is used when ever you want the CPU to return to it’s initial state.

RESET - The RESET input is used to reset and start the MPU from a power down condition resulting from a power failure or initial start-up of the processor. This level sensitive input can also be used to reinitialize the machine at any time after start-up.

If a high level is detected in this input, this will signal the MPU to begin the reset sequence. During the reset sequence, the contents of the last two locations (FFFE, FFFF) in memory will be loaded into the Program Counter to point to the beginning of the reset routine. MC6800 Specs

This pin creates an initial state for the processor. Once it is triggered the CPU has a known place to begin computing.

IRQ

Interrupt Request (IRQ) - This level sensitive input requests that an interrupt sequence be generated within the machine. The processor will wait until it completes the current instruction that is being executed before it recognizes the request. At that time, if the interrupt mask bit in the Condition Code Register is not set, the machine will begin an interrupt sequence. The Index Register, Program Counter, Accumulators, and Condition Code Register are stored away on the stack. Next, the MPU will respond to the interrupt request by setting the interrupt mask bit high so that no further interrupts may occur. At the end of the cycle, a 16-bit address will be loaded that points to a vectoring address which is located in memory locations FFF8 and FFF9. An address loaded at these locations causes the MPU to branch to an interrupt routine in memory. MC6800 Specs

So what are interrupts? These are signals to the CPU to stop what it is doing and service the interrupt. A typical interrupt is a key press on the keyboard, or a request from a port.

How does the computer decide what to do

DBE (Data Bus Enable)

When it is desired that another device control the data bus, such as in Direct Memory Access (DMA) applications, DBE should be held low. MC6800 Specs

DMA is used to move blocks of data around in memory. This can be when transferring data to and from a disk drive, or video memory. This is an interesting case, because the CPU is not controlling the ram when these take place.

CISC vs RISC computers


Written by John F. Moore

Last Revised: Mon Apr 6 17:58:39 EDT 2020

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
HTML5 Powered with CSS3 / Styling, and Semantics