1 of 43

�Chapter-3�Programming the Basic Computer �

  • Introduction,
  • Machine Language,
  • Assembly Language and Assembler,
  • Program loops,
  • Programming Arithmetic and
  • logic operations,
  • Subroutines,
  • I-O Programming.

2 of 43

Introduction

  • A total computer system includes both hardware and software . hardware consists of the physical components and all associated equipment.
  • Software refers to the programs that are written for the computer.
  • It is possible to be familiar with various aspects of computer software without being concerned with details of how the computer hardware operates.
  • It is also possible to design parts of the hardware without a knowledge of its software capabilities.

3 of 43

4 of 43

Machine Language

  • A program is a list of instructions or statements for directing the computer to perform a required data-processing task.
  • There are various types of programming languages that one may write for a computer,

…….but the computer can execute programs only when they are represented internally in binary for programs written in any other language must be translated to the binary representations before they can be executed by the computer.

5 of 43

  1. Binary code. This is a sequence of instructions and operands in binary that list the exact representation of instructions as they appear in computer memory.

2. Octal or hexadecimal code. This is an equivalent translation of the binary code to octal or hexadecimal representation.

6 of 43

3. Symbolic code. The user employs symbols (letters, numerals, or special characters) for the operation part, the address part, and other parts of the instruction code.

Each symbolic instruction can be translated into one binary coded instruction.

This translation is done by a special program called an assembler.

Because an assembler translates the sym bois, this type of symbolic program is referred to as an assembly language program.

7 of 43

  • 4. High-level programming languages . These are special languages developed to reflect the procedures used in the solution of a problem rather than be concerned with the computer hardware behavior. An example of a high-level programming language is Fortran.
  • The program is written in a sequence of statements in a form that people prefer to think in when solving a problem. However, each statement must be translated into a sequence of binary instructions before the program can be executed in a computer.
  • The program that translates a high-level language program to binary is called a compiler.

8 of 43

Assembly Language

  • A programming language is defined by a set of rules.
  • Users must conform with all format rules of the language if they want their programs to be translated correctly.
  • Almost every commercial computer has its own particular assembly language.
  • The rules for writing assembly language programs are documented and published in manuals which are usually available from the computer manufacturer.

9 of 43

Rules of the Language

1. The label field may be empty or it may specify a symbolic address.

2. The instruction field specifies a machine instruction or a pseudo instruction.

3. The comment field may be empty or it may include a comment.

10 of 43

  • A symbolic address consists of one, two, or three, but not more than three alphanumeric characters. The first character must be a letter; the next two may be letters or numerals. The symbol can be chosen arbitrarily by the programmer.
  • The instruction field in an assembly language program may specify one of the following items:

1. A memory-reference instruction (MRI)

2. A register-reference or input-output instruction (non-MRI)

3. A pseudo instruction with or without an operand

11 of 43

  • The following is an illustration of the symbols that may be placed in the instruction field of a program.

  • C L A n o n - M R I
  • A D D O P R d i re c t a d d r e s s M R I
  • A D D P T R I i n d i re c t a d d r e s s M R I

12 of 43

Pseudo instruction

  • A pseudo instruction is not a machine instruction but rather an instruction to the assembler giving information about some phase of the translation.

13 of 43

14 of 43

The Assembler

  • An assembler is a program that accepts a symbolic language program and produces its binary machine language equivalent.
  • The input symbolic program is called the source program and the resulting binary program is called the object program.
  • The assembler is a program that operates on character strings and produces an equivalent binary interpretation.

15 of 43

Representation of Symbolic Program in Memory

  • Prior to starting the assembly process, the symbolic program must be stored in memory. The user types the symbolic program on a terminal.
  • A loader program is used to input the characters of the symbolic program into memory.
  • Since the program consists of symbols, its representation in memory must use an alphanumeric character code.

16 of 43

  • In the basic computer, each character is represented by an 8-bit code. The high-order bit is always 0 and the other seven bits are as specified by ASCII.
  • Each character is assigned two hexadecimal digits which can be easily converted to their equivalent 8-bit code.

17 of 43

line of code

  • A line of code is stored in consecutive memory locations with two characters in each location. Two characters can be stored in each word since a memory word has a capacity of 16 bits. A label symbol is terminated with a comma.

18 of 43

Example : PL3, LDA SUB I

19 of 43

First Pass

  • A two-pass assembler scans the entire symbolic program twice.
  • During the first pass, it generates a table that correlates all user-defined address symbols with their binary equivalent value.
  • The binary translation is done during the second pass.
  • To keep track of the location of instructions, the assembler uses a memory location counter (LC) word called a location counter (abbreviated LC).

20 of 43

21 of 43

22 of 43

Second Pass

  • Machine instructions are translated during the second pass by means of tablelookup procedures.
  • A table-lookup procedure is a search of table entries to determine whether a specific item matches one of the items stored in the table.
  • The assembler uses four tables. Any symbol that is encountered in the program must be available as an entry in one of these tables; otherwise, the symbol cannot be interpreted.

23 of 43

  • Four Tables:
  • 1. Pseudoinstruction table.
  • 2. MRI table.
  • 3. Non-MRI table.
  • 4. Address symbol table.

  • The entries of the pseudoinstruction table are the four symbols ORG, END, DEC, and HEX. Each entry refers the assembler to a subroutine that processes the pseudoinstruction when encountered in the program.

24 of 43

25 of 43

  • One important task of an assembler is to check for possible errors in the symbolic program. This is called error diagnostics.

  • One such error may be an invalid machine code symbol which is detected by its being absent in the MRl and non-MRI tables.

26 of 43

Program Loops

  • A program loop is a sequence of instructions that are executed many times, each time with a different set of data. Program loops are specified in Fortran by a DO statement.
  • The following is an example of a Fortran program that forms the sum of 100 integer numbers.

  • Statement number 3 is executed 100 times, each time with a different operand A(J) for J = 1, 2, . . . , 100

27 of 43

28 of 43

Programming Arithmetic�and Logic Operations

  • The number of instructions available in a computer may be a few hundred in a large system or a few dozen in a small one.
  • Some computers perform a given operation with one machine instruction; others may require a large number of machine instructions to perform the same operation.
  • As an illustration, consider the four basic arithmetic operations.
  • Some computers have machine instructions to add, subtract, multiply, and divide.

29 of 43

Multiplication Program

  • We now develop a program for multiplying two numbers.
  • To simplify the program, we neglect the sign bit and assume positive numbers.
  • We also assume that the two binary numbers have no more than eight significant bits so their product cannot exceed the word capacity of 16 bits.
  • It is possible to modify the program to take care of the signs or use 16-bit numbers.
  • However, the product may be up to 31 bits in length and will occupy two words of memory.

30 of 43

  • The multiplication process consists of checking the bits of the multiplier Y and adding the multiplicand X as many times as there are 1' s in Y.
  • the computer can add only two numbers at a time, we reserve a memory location, denoted by P, to store intermediate sums.
  • As shown in the numerical example under P, the partial product starts with zero.
  • The multiplicand X is added to the content of P for each bit of the multiplier Y that is 1.
  • The value of X is shifted left after checking each bit of the multiplier. The final value in P forms the product.
  • The numerical example has numbers with four significant bits. When multiplied, the product contains eight significant bits.
  • The computer can use numbers with eight significant bits to produce a product of up to 16 bits.

31 of 43

32 of 43

Flow Chart Description

  • The program has a loop that is traversed eight times, once for each significant bit of the multiplier. Initially, location X holds the multiplicand and location Y holds the multiplier.
  • A counter CTR is set to -8 and location P is cleared to zero.
  • The multiplier bit can be checked if it is transferred to the E register.
  • This is done by clearing E, loading the value of Y into the AC, circulating right E and AC and storing the shifted number back into location Y. This bit stored in E is the low-order bit of the multiplier.
  • We now check the value of E . If it is 1, the multiplicand X is added to the partial product P. If it is 0, the partial product does not change.
  • We then shift the value of X once to the left by loading it into the AC and circulating left E and AC . The loop is repeated eight times by incrementing location CTR and checking when it reaches zero.
  • When the counter reaches zero, the program exits from the loop with the product stored in location P.

33 of 43

34 of 43

Double-Precision Addition

35 of 43

Logic Operations

  • The basic computer has three machine instructions that perform logic operations:
  • AND, CMA, and CLA. The LDA instruction may be considered as a logic operation that transfers a logic operand into the AC.

36 of 43

Shift Operations

  • The circular-shift operations are machine instructions in the basic computer.
  • The other shifts of interest are the logical shifts and arithmetic shifts.
  • These two shifts can be programmed with a small number of instructions.
  • Shift-right operation : CLE, CIR
  • Shift-left operation : CLE, CIL

37 of 43

Subroutines

  • Frequently, the same piece of code must be written over again in many different parts of a program.
  • Instead of repeating the code every time it is needed, there is an obvious advantage if the common instructions are written only once.
  • A set of common instructions that can be used in a program many times is called a subroutine.

38 of 43

  • A subroutine consists of a self-contained sequence of instructions that carries out a given task.
  • In the basic computer, the link between the main program and a subroutine is the BSA instruction (branch and save return address).

39 of 43

40 of 43

41 of 43

Input-Output Programming

  • Users of the computer write programs with symbols that are defined by the programming language employed.
  • The symbols are strings of characters and each character is assigned an 8-bit code so that it can be stored in computer memory.
  • A binary-coded character enters the computer when an INP (input) instruction is executed.
  • A binary-coded character is transferred to the output device when an OUT (output) instruction is executed.
  • The output device detects the binary code and types the corresponding character.

42 of 43

43 of 43

Program Interrupt

  • The running time of input and output programs is made up primarily of the time spent by the computer in waiting for the external device to set its flag.