CSE 243: Introduction To Computer Architecture and Hardware/Software Interface
CSE 243: Introduction To Computer Architecture and Hardware/Software Interface
PC contains the address of the memory location from which the next instruction is to be fetched.
When the instruction completes its fetch/execution cycle, the contents of the PC point to the next instruction. Thus, a sequence of instructions can be executed to complete a task.
Simple task:
Add two numbers stored in memory locations A and B. Store the result of the addition in memory location C.
Move A, R0 (Move the contents of location A to register R0) Add B, R0 (Add the contents of location B to register R0) Move R0, C (Move the contents of register R0 to location C)
2
4
8
Move R0, C
Instructions are executed one at a time in order of increasing addresses. Straight line sequencing
3
Branch>0 LOOP:
Checks if the count in register R1 is 0 (Branch > 0) If it is 0, then store the sum in register R0 at memory location SUM (Move R0, SUM). If not, then get the next number, and repeat (go to LOOP). Go to is specified implicitly.
Note that the instruction (Branch > 0 LOOP) has no explicit reference to register R1.
Flags are grouped together in a special purpose register called condition code register or status register.
If the result of Decrement R1 is 0, then flag Z is set. Branch> 0, tests the Z flag. If Z is 1, then the sum is stored. Else the next number is added.
7
Otherwise the PC is incremented in a normal way, and the next sequential instruction is fetched and executed.
Conditional branch instructions use condition code flags to check if the various conditions are satisfied.
Move N, R1 Move #NUM1, R2 (Initialize R2 with address of NUM1) Clear R0 LOOP Add (R2), R0 (Indirect addressing) Add #4, R2 (Increment R2 to point to the next number) Decrement R1 Branch>0 LOOP Move R3, SUM
9
10
Stacks
A stack is a list of data elements, usually words or bytes with the accessing restriction that elements can be added or removed at one end of the stack.
End from which elements are added and removed is called the top of the stack. Other end is called the bottom of the stack.
Push - placing a new item on the stack. Pop - Removing the top item from the stack.
11
Stacks (contd..)
Data stored in the memory of a computer can be organized as a stack.
When new elements are pushed on to the stack they are placed in successively lower address locations.
A processor register called as Stack Pointer (SP) is used to keep track of the address of the element that is at the top at any given time.
12
Stacks (contd..)
0 4
Processor with 65536 bytes of memory. Byte addressable memory. Word length is 4 bytes. First element of the stack is at BOTTOM. SP points to the element at the top.
1 2 739
SP
Push operation can be implemented as: Subtract #4, SP Move A, (SP) Pop operation can be implemented as: Move (SP), B Add #4, SP Push with autodecrement: Move A, -(SP) Pop with autoincrement: Move (SP)+, A
13
BOTTOM
43
65535
Subroutines
In a program subtasks that are repeated on different data values are usually implemented as subroutines. When a program requires the use of a subroutine, it branches to the subroutine.
Branching to the subroutine is called as calling the subroutine. Instruction that performs this branch operation is Call.
After a subroutine completes execution, the calling program continues with executing the instruction immediately after the instruction that called the subroutine.
Subroutine is said to return to the program. Instruction that performs this is called Return.
14
Subroutines (contd..)
Recall that when an instruction is being executed, the PC holds the address of the next instruction to be executed.
This is the address to which the subroutine must return. This addressed must be saved by the Call instruction.
Way in which a processor makes it possible to call and return from a subroutine is called subroutine linkage method. The return address could be stored in a register called as Link register Call instruction:
Stores the contents of the PC in a link register. Branches to the subroutine.
Return instruction:
15
Subroutines (contd..)
Memory location Calling program Memory location Subroutine SUB Calling
200 204
1000
first instruction
Return
1000
PC
204
program calls a subroutine, whose first instruction is at address 100. The Call instruction is at address 200. While the Call instruction is being executed, the PC points to the next instruction at address 204. Call instructions stores address 204 in the Link register, and loads 1000 into the PC. Return instruction loads back the address 204 from the link register into the PC.
Link Call
204 Return
16
Push the return addresses onto a stack as they are generated by subroutine calls. Pop the return addresses from the stack as they are needed to execute return instructions.
17
Assembly language
Recall that information is stored in a computer in a binary form, in a patterns of 0s and 1s. Such patterns are awkward when preparing programs. Symbolic names are used to represent patterns. So far we have used normal words such as Move, Add, Branch,
to represent corresponding binary patterns.
When we write programs for a specific computer, the normal words need to be replaced by acronyms called mnemonics. E.g., MOV, ADD, INC A complete set of symbolic names and rules for their use constitute a programming language, referred to as the assembly language.
18
Each mnemonic represents the binary pattern, or OP code for the operation performed by the instruction. Assembly language must also have a way to indicate the addressing mode being used for operand addresses. Sometimes the addressing mode is indicated in the OP code mnemonic. E.g., ADDI may be a mnemonic to indicate an addition operation
with an immediate operand.
19
The statements which provide additional information to the assembler to translate source program into an object program are called assembler directives or commands.
20
128
132
Move
R0,SUM
What is the numeric value assigned to SUM? What is the address of the data NUM1 through NUM100? What is the address of the memory location represented by the label LOOP? How to place a data value into a memory location?
NUM 100604
21
Operation
SUM N NUM1
START
LOOP
EQU ORIGIN DA TAW ORD RESER VE ORIGIN MO VE MO VE CLR ADD ADD DEC BGTZ MOVE RETURN END
200 204 100 400 100 N,R1 #NUM1,R2 R0 (R2),R0 #4,R2 R1 LOOP R0,SUM STAR T
EQU: Value of SUM is 200. ORIGIN: Place the datablock at 204. DATAWORD: Place the value 100 at 204 Assign it label N. N EQU 100 RESERVE: Memory block of 400 words is to be reserved for data. Associate NUM1 with address 208 ORIGIN: Instructions of the object program to be loaded in memory starting at 100. RETURN: Terminate program execution. END: End of the program source text
22
May indicate the address of an instruction to be executed. May indicate the address of a data item.
How does the assembler determine the values that represent names?
Value of a name may be specified by EQU directive. A name may be defined in the Label field of another instruction, value represented by the name is determined by the location of that instruction in the object program.
E.g., BGTZ LOOP, the value of LOOP is the address of the instruction ADD (R2) R0
23
When a name appears second time, it is replaced with its value from the table.
What if a name appears before it is given a value, for example, branch to an address that hasnt been seen yet (forward branch)?
Assembler can scan through the source code twice. First pass to build the symbol table. Second pass to substitute names with numerical values. Two pass assembler.
24
25
: 8 bits. : 4 bits to specify a register 3 bits to specify the addressing mode. Destination operand : 4 bits to specify a register. 3 bits to specify the addressing mode. Other information : 10 bits to specify other information such as index value. Opcode Source operand
26
Opcode : 8 bits. Source operand : 3 bits to specify the addressing mode. Destination operand : 4 bits to specify a register. 3 bits to specify the addressing mode.
Leaves us with 14 bits to specify the address of the memory location. Insufficient to give a complete 32 bit address in the instruction. Include second word as a part of this instruction, leading to a two-word instruction.
27
OP code
Source
Dest
Other info
Second word specifies the address of a memory location. Second word may also be used to specify an immediate operand.
Complex instructions can be implemented using multiple words. Complex Instruction Set Computer (CISC) refers to processors using instruction sets of this type.
28
ADD R1, R2 can be specified. ADD LOC, R2 cannot be specified. Use indirect addressing mode: ADD (R3), R2
R3 serves as a pointer to memory location LOC. Relative addressing mode.
29
Three-operand instruction
OP code Ri Rj Rk Other info
30