csc 3210 computer organization and programming chapter 1 the computer d.m. rasanjalee himali

41
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Upload: margaret-barber

Post on 13-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

CSC 3210Computer Organization and Programming

Chapter 1THE COMPUTER

D.M. Rasanjalee Himali

Page 2: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Outline Introduction

Calculators Stack Calculators The Use of Registers Programmable Calculators Machine Language Programming

Macros Macros with Arguments Memory Location Conditionals and Branching

The Von Neumann Machine The Stack Machine Load/Store Machines Assemblers

Page 3: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Introduction

Much of Computer Architecture involves the substitution of numeric codes for symbols

Manipulation of symbols is facilitated by a macro processor

In this chapter, the UNIX macro processor m4 is introduced

Page 4: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Calculators

The calculator has a numeric keyboard and a few function keys, +, -, x, /.

It has a single register, the accumulator, into which numbers may be entered or combined with other numbers using the function keys.

The contents of the accumulator are displayed after each entry and operation.

Page 5: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Stack Calculators

Use a simple calculator to evaluate the following expression for x = 10:

precedence in which the parenthesized expressions are evaluated first, as follows: (10-1) = 9 (10-7) = 3 (9*3) = 27 (10- 11) = -1 27/(-1) = -27

A simple calculator provides only ALU, not memory to store intermediate results

y = (x-1) (x-7)(x-11)

Page 6: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Stack Calculator

Memory can be provided for the temporary results of expressions in the form of a stack.

A stack : LIFO data structure in which only the top two stack elements are

accessible. Place Data items: pushing and removing items : popping. No addresses for the memory cells

Operations (+,*,/) remove the top two elements of the stack and then push the result of the arithmetic operation back onto the stack

Page 7: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Stack Calculator Hewlett-Packard calculators are built to perform

arithmetic by using a stack.

10 enter1 –10 enter7 –*10 enter11 – /

Page 8: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Stack Calculators

Page 9: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Use of Registers We need to enter 3.172843 three times for

3.172843 . There must be a better way !! Registers hold constants such as x =

3.172843. These registers are named by number,

starting at 0. Approximately 10 registers are provided.

Page 10: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Using Registers

To Store a Number in Register: Copy from top of stack by typing the number into the

calculator followed by the key sequence sto and then the register name.

To Retrieve a Number from Register: Retrieve to the top of the stack by typing rcl followed

by the register name.

Registers may also be used to hold intermediate results in evaluating expressions.

Page 11: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Using Registers To evaluate the expression above, using the stack and a

register, for x = 3.172843 we might enter the following key sequence:

Page 12: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Programmable Calculators

When the calculator is in program mode, the keystrokes are not executed, a code for each key is stored in a memory

Memory has an address and holds data

Start by storing the keystrokes into memory location zero.

After each keystroke is entered, the memory address is incremented so that the next keystroke will be stored in the next memory location.

Page 13: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

HP-15C Programmable Calculator All keys have three

designations: The principal designation, printed

on the face of the key in white ink, is obtained simply by using the key.

Above the key is a second designation printed in yellow; to obtain this function you must press the yellow f key followed by the function key.

To obtain the designation on the lower face of the key, printed in blue, you must press the blue g key followed by the function key.

To indicate the end of the following program, the rtn key is entered after the blue prefix g

Page 14: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Programmable Calculator

The contents of the calculator’s memory after it has been programmed :

Page 15: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Machine Language

Keyboard sends the appropriate numeric codes to memory.

To perform the expression evaluation above, using machine language program: Using symbol table:

machine language program : 44 0 1 30 45 0 730 20 45 0 1 1 30 10 43 32

Page 16: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Assembly Language Making use of this list, we could translate the program,

with symbols representing the keys.

This program is known as an assembly language program

It is a program with symbols representing numeric values.

Translating an assembly language program into a machine language program involves looking up the symbols and mnemonics in a symbol table and substituting the matching numeric value.

sto1sub rcl7sub mul rcl11sub Divrtn

Page 17: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macros Macro processor m4 – translates symbols into numeric constants

Defining Macros: use the define macro. define takes two arguments: the macro token and the definition. Ex: to define the machine instructions for the calculator:

Save these definitions in a file, called, for example, cal.m along with the program

Run program using m4: %m4 cal.m

Page 18: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macroscal.m

%m4 cal.m

Output

the output is the translated symbols (machine language)

Page 19: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macros with Arguments

Macros may have up to nine arguments

Arguments are specified in the macro definition by $n, where i is a digit between 0 and 9.

If macro name followed immediately by ‘(‘, then arguments are present

Page 20: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macros with Arguments

Ex1: Definition of cat: Call: Output: Whitespaces before arg is ignored

Ex2: (fewer Arguments): Call: Output: unsupplied arguments replaced by nulls

Page 21: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macros with Arguments

Redefine sto to define(sto,’44 $1’) and r as define(rcl, ‘45 $1’)

We can now enter the program as:

This produce the same machine code as before.

Page 22: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Macros with Arguments

The following also produces the identical machine code with everything now defined symbolically.

This is good programming practice, as it makes programs clearer and much easier to understand

Page 23: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Memory Location We can also add the memory address of where in memory our

instructions are stored :

loc (location counter) is the memory address of the instruction being assembled.

First define a symbol, loc, to have the value 0. Each macro definition print the current value of loc and redefine loc to

be loc plus the memory locations needed to store the instruction.

Page 24: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Memory Location

When the macro definitions and program are run through m4, the following text results:

the addresses of the machine instructions

Page 25: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Conditionals and Branching

Problem: Evaluate the expression for values of x, 0 <x 10, in

increments of 1 Is a fair amount of typing just entering in the values of

x.

Solution: Determine when to stop evaluating the expression

(Testing) and change the address of the next instruction to be

executed (Branching)

Page 26: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Conditionals and Branching

In the HP15C calculator we may test if the current value of the top of the stack is zero

If it is not, the next instruction in line is skipped.

Normally, the instruction following the test is a goto instruction, which will transfer control to some other point in the program.

Targets of branches are labels.

Page 27: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Conditionals and Branching Three more macros needed to handle labels and branching:

label: If later evaluated, will have the value of the location of the next instruction

to be executed. ifeq:

the key code to test if current value of the expression evaluation is zero If it is zero, the next instruction executed; otherwise, it is skipped.

gto: corresponds to the gto key has a label as argument when executed, the pc is assigned the value of the argument (location of

the target branch instruction). pse:

To see the values of the expression evaluation use the pause key f pse.

Page 28: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Conditionals and Branching

Page 29: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Conditionals and Branching A:

Initialize the X register

B: Compare value of x to 11 to see if the loop

is to be executed; if it is to be executed, the value of y is

computed and printed

C: Return to calculator mode

D: Finally, value of x is incremented and the

program branches back to the test

Start of program

Loop

Return of program

A

B

C

D

Page 30: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Von Neumann Machine

HP15C Programmable calculator is a small computer that fits the definition of stored program concept proposed by von Neumann.

consists of : An addressable memory (hold instructions and

data), An arithmetic logic unit (execute the

instructions fetched from memory).

The address of the next instruction to be executed was held in a register called the program counter.

Page 31: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Von Neumann Machine

The cycle the von Neumann machine executed:

Page 32: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Stack Machine Does not have registers

Only Stack and ALU

Use memory to place items onto stack

Use load and store operations for moving data between memory and the stack

Must specify memory address Load(<<from-memory-address>>) Store(<<to-memory-address>>)

MAR – memory address register MDR – memory data register IR – instruction register holds fetched instruction

Page 33: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Stack Machine In HP calculators, instructions like sub and mul are

fetched from memory.

These instructions have no operands- they are on stack.

Machine decode instruction fetched from memory

Instruction is executed by removing operands from top of stack, perform operation and storing any result back on to top of stack.

An architecture such as HP calculator is similar to Stack architecture.

Page 34: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Stack Machine

The stack architecture differs from the calculator in that it has no set of registers for holding constants/intermediate results.

Therefore in our equation, we must store x and y as variables in memory

To do this we introduce two instructions :load(get result on to top of stack from given memory address) and store(store results in top of stack to given memory address)

Page 35: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

The Stack Machine ALU uses top two elements on

the stack for all computations

Memory is accessed by first loading an address into the MAR

On a pop instruction, the top of the stack is popped into the MDR, for storing into memory

on a push instruction, the MDR is loaded from memory and then pushed onto the top of the stack.

Stack architecture is simple as it has no registers, only stack and ALU

Page 36: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Accumulator Machine Like a very simple calculator Has one register: accumulator Content of accumulator is

combined with single operand to replace acc value.

Ex: add Acc += operand Finally the result is stored in

memory(load/store) Input to ALU is accumulator No registers or stack

Page 37: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Load/Store Machine Early machines’ memory limited to

few hundred words Access time to all locations was

the same As memory size increased access

time vs. cost issue arose New designs included variable

access times Register file – high speed

memory Use load and store instructions

between registers and memory ALU would function on registers

only Register file replaces the stack of

the stack machine SPARC architecture is a load/store

machine

Page 38: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Assemblers

An assembler is a macro processor specialized for translating symbolic programs into machine language programs (assembling a program).

An assembler effectively reads a file twice, once to determine all the symbol definitions and the second time to apply those definitions and thus translate the

symbolic text into numeric instructions and data.

The assembler does essentially what we have been using m4 to do (translating all symbols into numbers)

Page 39: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Assemblers

The variables can be moved to the end of the program

Assembler:A symbol followed by a colon defines the

symbol to have as its value the current value of the location counter

(m4 define a symbol before it is used.)

Page 40: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Assemblers Label:

An identifier followed by a colon. Labels are the arguments for goto instructions.

We will be using the UNIX assembler as with m4 to write programs for SPARC. In the as assembler, register names are preceded by a % character. The assembler also allows us to terminate lines with comments beginning with an exclamation

point (!). The exclamation point and remaining text on the line are ignored.

label

Page 41: CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali

Assemblers

Our program, written for m4 and as:

If the program is first processed by m4, the symbolic register definitions are processed to yield a program suitable for as: