HUMBOLDT-UNIVERSITÄT ZU BERLIN
INSTITUT FÜR INFORMATIK
Lecture 5
DATA AND INSTRUCTION FORMATS,
ADDRESSING METHODS
AND MACHINE PROGRAMMING
CONCEPTS (2)
Sommersemester 2002
Leitung: Prof. Dr. Miroslaw Malek
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)
• Compiler/assembler and operating system in combination with
computer architecture (instruction set plus I/O capabilities)
are decisive factors with respect to computer performance
• The most important advantages of knowing an assembly
langage:
– understanding how a computer really works – learning how to debug programs
0
EXAMPLE OF AN INSTRUCTION EXECUTION
Autoincrement mode 010 (210): EA=[PC]=[R7] and increment PC=R7 General format
Op-code src dstn
15 12 11 6 5 0
4-bit operation field
6-bit source address field
6-bit destination address field
R3 32 R3 49
PC = R7 i PC = R7 i + 4
i i + 2 i + 4
add 2 R7 R3
17
i i + 2 i + 4
add R7 R3
17
0 2
Main memory address
A = 1150 B = 1152
317 - 193 1200 1202 1204 1206 1208 1210 1212 MOV ADD MOV 6 - 54 - 56 988 HALT
C = 2200
R7 0 R0
6 R7 0 R0
0 R6 6 R7
Assembly language version of program
MOV A, R0 ADD B, R0 MOV R0, C
HALT
124
EXAMPLE OF A SHORT PROGRAM EXECUTION
1) R0 = 317 2) R0 = 124
Mode “6“ - Relative index addressing
EA=X + [PC]=X + [R7 ]
EA = X + PC = -54 + 1204 = 1150
(a) G en er al for mat 6 bit
Opcode
Example of an stw (storeword) instruction execution
(Power PC)
Autoincrement mode stw
stw rS, d(rA) , where rS (Source) and rA (Destination) are registers and d is an offset The contents of rS are stored into the word in memory addressed by EA, where EA=(rA)+d.
Binary representation of mnemonic stw: 100100 Decimal representation of mnemonic stw: 36
5 bit
Source register
5 bit
I/O BUS
CPU
10 8Op./s
TTYIN TTYOUT
CIN COUT
Keyboard Monitor
e.g., 10 - 100 Characters/sec. TERMINAL
I/O PROGRAMMING (I)
Big Problem: Matching
Speeds
TTYIN 8 bit buffer register associated with a keyboard TTYOUT 8 bit buffer register associated with a monitor CIN, COUT control flags
CIN = 1 informs CPU that a valid character is in TTYIN
COUT= 1 informs CPU that it can transfer a valid character to TTYOUT
I/O PROGRAMMING (II)
MOV #LOC,R0 Initialize pointer register R0 to contain the address of
the first location of the area in main memory where the characters are to be loaded.
READ: TSTB KBSTATUS Wait for a character to be entered into the keyboard BPL READ buffer register TTYIN.
MOVB TTYIN,@R0 Transfer the character from into the main memory
(this clears CIN to 0).
ECHO-BACK: TSTB PRSTATUS Wait for the monitor to become ready.
BPL ECHOBACK
MOVB @R0,TTYOUT Move the character just read the monitor buffer
register TTYOUT for printing (this clears COUT to 0).
CMPB (R0)+,#CR Check to see if the character just read is "carriage
BNE READ return” (CR). If it is not CR, branch back and read HALT another character, otherwise stop. The pointer
TYPE OF ARCHITECTURES
• 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
A STACK IN THE MAIN MEMORY
0
-28 17 739
43
.
. .
. .
.
.
Ri
Current Top Element
The Stack
Bottom Element
M – 1 Stack
R1
Stack
(a) Initial contents of stack (b) After push from NEWITEM
c) After pop into TOPITEM
d) After executing ADD (R1)+, @R1
EXAMPLES OF STACK OPERATIONS
-28 17 739 43 19 Newitem Topitem
MOV NEWITEM,- (R1) (push)
MOV (R1)+,TOPITEM (pop)
Notice (a) => (b), (a) => (c), (a) => (d)
. . . . R1 -28 17 739 43 19 Topitem Newitem . .
19 R1 17
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 MACHINES (2)
• Binary arithmetic and logic operations:
– operands: top 2 items on stack
– operands are removed from stack
– result is placed on top of stack
• Unary arithmetic and logic operations:
– operand: top item on the stack
– operand is replaced by result of operation
• Data move operations:
– push: place memory data on top of stack
STACK MACHINES - SAMPLE PROGRAM
• We evaluate our favorite expression(y
←
ax
2+ 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 x
2mult
; tos: a x
2push
b
; tos: a x
2b
push
x
; tos: a x
2b x
mult
; tos: a x
2bx
push
c
; tos: a x
2bx c
add
; tos: a x
2bx+c
add
; tos: a x
2+bx+c
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.
• Frequently used operands are placed in registers (under
program control)
GPR MACHINES - SAMPLE PROGRAM
• Again, we evaluate (y
←
ax
2+bx+c) on a hypothetical machine with
16 registers, R0 to R15, and 2 operand register-register ALU
operations
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 <- ax
2mult
R1, R3 ;
R3 <- bx
add
R2, R3 ;
R3 <- ax
2+ bx
add
R3, R4 ;
R4 <- ax
2+ bx + c
GPR MACHINES - SAMPLE MACHINES
• VAX
• IBM 360, IBM 370, PC-RT, RISC 6000
• PowerPC, MOTOROLA 68000
• CDC AND CRAY
Program base (PB) Program counter (PC) Program limit (PL) Program
segment Data limit (DL)
Data base (DB)
Data segment Data area Stack filled by previous procedures Empty stack locations Stack marker Stack pointer (SP)
Stack limit (SL) Increasing memory addresses Stack filled by current procedure
CALLING STACK (I)
200 Call_subroutine SUB SUB “first instruction” 201 “next instruction”
Branch (LINK)
The Call_subroutine instruction places the address 201 into memory location LINK
Memory location
Calling Program Subroutine
CALLING STACK (II)
200 Call_subroutine SUB SUB “first instruction” 201 “next instruction”
Return
The Call_subroutine instruction The return instruction pushes the address 201 pops the top of the stack onto the stack. into the PC.
Memory location
Calling Program Subroutine
NESTED SUBROUTINE CALLS ON A STACK (I)
old value of R5 R5
[R6] = 1050 topvalue . . .
Processorstack
a) Initially
NESTED SUBROUTINE CALLS ON A STACK (II)
2004 R5
[R6] = 1048 old value of R5
topvalue .
. .
b) After execution of JSR R5, SUB1
2006 R5
[R6] = 1042 2254 [R1] main [R0] main old value of R5
topvalue . . .
NESTED SUBROUTINE CALLS ON A STACK (III)
2006 R5
[R6] = 1040 [R0] sub1 2254 [R1] main [R0] main old value of R5
topvalue . . .
d) After execution of MOV R0, -(R6) in SUB 2
2006 R5
[R6] = 1044 [R1] main [R0] main old value of R5
topvalue . . .
NESTED SUBROUTINE CALLS ON A STACK (IV)
old value of R5 R5
[R6] = 1050 topvalue . . .
2006
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 R0and R1
RTS R5
Subroutine
3000 SUB2: MOV R0,-(R6) Save [R0]sub1on the processor stack.
……..
ADD R0,R1 Send a result to SUB1 through R1. MOV (R6)+,R0 Restore R0.
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