410 likes | 592 Views
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
E N D
CSC 3210Computer 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
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
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.
y = (x-1) (x-7) (x-11) 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
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
Stack Calculator • Hewlett-Packard calculators are built to perform arithmetic by using a stack. 10 enter 1 – 10 enter 7 – * 10 enter 11 – /
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.
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.
Using Registers • To evaluate the expression above, using the stack and a register, for x = 3.172843 we might enter the following key sequence:
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.
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 yellowf 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
Programmable Calculator The contents of the calculator’s memory after it has been programmed :
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
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. sto 1 sub rcl 7 sub mul rcl 1 1 sub Div rtn
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
Macros cal.m Output %m4 cal.m the output is the translated symbols (machine language)
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
Macros with Arguments • Ex1: • Definition of cat: • Call: • Output: • Whitespaces before arg is ignored • Ex2: (fewer Arguments): • Call: • Output: • unsupplied arguments replaced by nulls
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.
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
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.
Memory Location • When the macro definitions and program are run through m4, the following text results: the addresses of the machine instructions
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)
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.
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.
Return of program C Start of program A Loop B D 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
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.
The Von Neumann Machine • The cycle the von Neumann machine executed:
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
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.
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)
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
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
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
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)
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.)
label 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.
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: