Beruflich Dokumente
Kultur Dokumente
Lecture 5
Sommersemester 2002
www.informatik.hu-berlin.de/rok/ca
CA - V - D&IF(2) - 1
COMPUTER PROGRAMMING CONCEPTS
• Choice of a language:
– machine language (uses 0‘s and 1‘s or hex, inconvenient and error
prone)
– assembly language (machine dependent but efficient)
– high-level language (convenient and universal)
R3 32 R3 49
PC = R7 i PC = R7 i+4
Autoincrement mode 010 (210): EA=[PC]=[R7] and increment PC=R7
i add 2 R7 0 R3 i add 2 R7 0 R3
i+2 17 i+2 17
i+4 i+4
CA - V - D&IF(2) - 4
Example of an stw (storeword) instruction execution
(Power PC)
6 bit 5 bit 5 bit
Opcode Source register Source- Offset
address A
CA - V - D&IF(2) - 5
I/O PROGRAMMING (I)
I/O BUS
TERMINAL
Big Problem:
CPU
TTYIN TTYOUT
8 Matching
10 Op./s CIN COUT Speeds
Keyboard Monitor
MOVB @R0,TTYOUT Move the character just read the monitor buffer
register TTYOUT for printing (this clears COUT to
0).
• STACK ARCHITECTURES
• REGISTER-REGISTER ARCHITECTURES
• REGISTER-STORAGE ARCHITECTURES
• STORAGE-STORAGE ARCHITECTURES
• TAGGED ARCHITECTURES
• TRADEOFFS AMONG STACK, REGISTER AND STORAGE
ARCHITECTURES
– PERFORMANCE
– EFFICIENCY
– DESIGN COMPLEXITY
– EASE OF PROGRAMMING
– EASE OF PARAMETER PASSING AND SUBROUTINE CALLS
– RECURSION
CA - V - D&IF(2) - 8
A STACK IN THE MAIN MEMORY
0
Stack
.
Pointer
Register .
. Current
Ri -28 Top
17 Element
739
The Stack .
.
Bottom
43 Element
.
.
M–1
CA - V - D&IF(2) - 9
EXAMPLES OF STACK OPERATIONS
MOV NEWITEM,- (R1) (push)
MOV (R1)+,TOPITEM (pop)
R1 -28 R1 19 R1 17 R1 -11
739 17 .
. Stack . .
. 739 . .
. . . .
. . . .
43 43 43 43
(a) Initial contents of stack (b) After push from c) After pop d) After executing
NEWITEM into TOPITEM ADD (R1)+, @R1
Notice (a) => (b), (a) => (c), (a) => (d)
CA - V - D&IF(2) - 10
STACK MACHINES (1)
• Most instructions manipulate the top few data items (mostly top
2) of a pushdown stack
• Additional instructions are provided to move data between
memory and top of stack
• Top few items of the stack (2 to 8 items or more) are kept in the
CPU
• Instructions manipulate the top of the stack implicitly
• Ideal for evaluating expressions (stack holds intermediate
results)
• Were thought to be a good match for high level languages
• Awkward:
– stack becomes very slow if it grows beyond CPU local storage
CA - V - D&IF(2) - 11
STACK MACHINES (2)
• Binary arithmetic and logic operations:
CA - V - D&IF(2) - 12
STACK MACHINES - SAMPLE PROGRAM
• We evaluate our favorite expression(y ← ax2+ bx+c) ; we use a
hypothetical assembly language (as usual)
push a ; tos: a
push x ; tos: a x
dup ; tos: a x x
mult ; tos: a x2
mult ; tos: a x2
push b ; tos: a x2 b
push x ; tos: a x2 b x
mult ; tos: a x2 bx
push c ; tos: a x2 bx c
add ; tos: a x2 bx+c
add ; tos: a x2+bx+c
pop y ; y <- a x2+bx+c
CA - V - D&IF(2) - 13
GENERAL PURPOSE REGISTER MACHINES
• With stack machines, only the top two elements of the stack are
directly available to instructions. In general purpose register
machines, the CPU storage is organized as a set of registers
which are equally available to the instructions.
CA - V - D&IF(2) - 14
GPR MACHINES - SAMPLE PROGRAM
load x, R1 ; R1 <- x
load a, R2 ; R2 <- a
load b, R3 ; R3 <- b
load c, R4 ; R4 <- c
mult R1, R2 ; R2 <- ax
mult R1, R2 ; R2 <- ax2
mult R1, R3 ; R3 <- bx
add R2, R3 ; R3 <- ax2 + bx
add R3, R4 ; R4 <- ax2 + bx + c
store R4, y ; y <- ax2 + bx + c
CA - V - D&IF(2) - 15
GPR MACHINES - SAMPLE MACHINES
• VAX
CA - V - D&IF(2) - 16
EXAMPLE OF PROGRAM AND DATA
SEGMENT ORGANIZATION MC68000
Program
Data segment
segment Data limit (DL)
Program base
(PB)
Data
area
Program Data base (DB)
counter (PC) Stack
filled by
previous
procedures
Program Stack marker
limit (PL) Stack filled
by current
Stack pointer procedure
(SP)
Increasing
memory Empty
stack
addresses
locations
Stack limit (SL)
CA - V - D&IF(2) - 17
CALLING STACK (I)
CA - V - D&IF(2) - 18
CALLING STACK (II)
b) Using a stack
CA - V - D&IF(2) - 19
NESTED SUBROUTINE CALLS ON A STACK (I)
R5 old value of R5
a) Initially
CA - V - D&IF(2) - 20
NESTED SUBROUTINE CALLS ON A STACK (II)
R5 2004 R5 2006
b) After execution of JSR R5, SUB1 c) After execution of JSR R7, SUB2
CA - V - D&IF(2) - 21
NESTED SUBROUTINE CALLS ON A STACK (III)
R5 2006 R5 2006
R5 old value of R5
2006
CA - V - D&IF(2) - 23
NESTED SUBROUTINE CALLS ON A STACK ii
Main program …
2000 JSR R5,SUB1 Call SUB1, passing addresses PARAM and ANSWER
2004 .WORD PARAM through memory locations.
2006 .WORD ANSWER
2008 next instruction
…….
Subroutine
2200 SUB1: MOV R0,-(R6) Save [R0]main and [R1]main on the processor stack.
MOV R1,-(R6) Load parameter into R0.
MOV @(R5)+,R0
…….
MOV LOC,R1 Place a parameter into R1 and call SUB2.
2250 JSR R7,SUB2
2254 next instruction
…….
MOV RESULT,@(R5)+ Send [RESULT] as a “result“ into location
ANSWER in the main program.
MOV (R6)+,R1 Restore original contents of
MOV (R6)+,R0 registers R0 and R1
RTS R5
Subroutine
3000 SUB2: MOV R0,-(R6) Save [R0]sub1 on the processor stack.
……..
ADD R0,R1 Send a result to SUB1 through R1.
MOV (R6)+,R0 Restore R0.
RTS R7
CA - V - D&IF(2) - 24
EXAMPLE PROGRAM FOR THE CALCULATION OF
n FACTORIAL ON PowerPC
lbz r1, 15(r0) Load the number n from MEM[15+ r0] into register r1
clz r2
add 1 Load 1 into register r2, which holds the result
fac: cmpwi crf0, r1, 2 Compare r1 to 2; crf0 is condition register field 0, which
is the same as less than, in other words: if r1<2 the
bit in crf0 is set to 1
blt end Conditional jump to a label end, if bit in crf0 is 1
mullw r2, r1, r2 Multiply r2, by r1, the result is stored into r2
subi r1, r1, 1 Decrement r1 by 1
b fac Jump to a label fac
end: blr Branch to linkregister
The result is stored in register r2.
CA - V - D&IF(2) - 25