ECE2030 Introduction to Computer Engineering Lecture 17: Memory and Programmable Logic Devices - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

ECE2030 Introduction to Computer Engineering Lecture 17: Memory and Programmable Logic Devices

Description:

Are there times we need to change the flow of a program? ... Use $v0 ($2) for passing the result back to the caller. 15. Procedural Call Example ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 27
Provided by: profhsienh
Category:

less

Transcript and Presenter's Notes

Title: ECE2030 Introduction to Computer Engineering Lecture 17: Memory and Programmable Logic Devices


1
ECE2030 Introduction to Computer
EngineeringLecture 19 Program Control
Prof. Hsien-Hsin Sean Lee School of Electrical
and Computer Engineering Georgia Tech
2
Program Execution on Processors
  • How a program is executed on a processor?
  • How instructions ordering are maintained?
  • Are there times we need to change the flow of a
    program?
  • How to handle change of flow of a program?

3
Program Counter
  • How a sequence of instructions are fetched and
    executed by a computer?
  • Program Counter (PC)
  • A special register
  • Provide the logical ordering of a program
  • Point to the address of the instruction to be
    executed

main la 8, array lb 9, (8) lb 10,
1(8) add 11, 9, 10 sb 11, (8) addiu 8,
8, 4 lh 9, (8) lhu 10, 2(8) add 11, 9,
10 sh 11, (8) addiu 8, 8, 4 lw 9,
(8) lw 10, 4(8) sub 11, 9, 10 sw 11,
(8)
4
Program Counter (Little Endian)
PC
0x01
main la 8, array lb 9, (8) lb 10,
1(8) add 11, 9, 10 sb 11, (8) addiu 8,
8, 4 lh 9, (8) lhu 10, 2(8) add 11, 9,
10 sh 11, (8) addiu 8, 8, 4 lw 9,
(8) lw 10, 4(8) sub 11, 9, 10 sw 11,
(8)
0x10
la 8, array
0x08
0x3c
  • Each MIPS instruction is 4-byte wide

5
Program Counter Control
32

32-bit PC Register
System clock
32
4
6
Change of Flow Scenarios
  • Our current default mode
  • Program executed in sequential order
  • When a program will break the sequential
    property?
  • Subroutine Call and Return
  • Conditional Jump (with Test/Compare)
  • Unconditional Jump
  • MIPS R3000/4000 uses
  • Unconditional absolute instruction addressing
  • Conditional relative instruction addressing

7
Absolute Instruction Addressing
  • The next PC address is given as an absolute value
  • PC address ltgiven addressgt
  • Jump class examples
  • J LABEL
  • JAL LABEL
  • JALR r
  • JR r

8
Absolute Addressing Example
main la 8, array lb 9, (8) lb 10,
1(8) add 11, 9, 10 sb 11, (8) j
Label2 ... ... Label2 addiu 8, 8, 4 lh 9,
(8) lhu 10, 2(8)
  • Assume no delay slot
  • J Label2
  • Encoding
  • Label20x00400048
  • J opcode (000010)2
  • Encoding0x8100012
  • Temp ltLabel2 26-bit addrgt
  • PC PC31..28 Temp 02

9
Relative Instruction Addressing
  • An offset relative to the current PC address is
    given instead of an absolute address
  • PC address ltcurrent PC addressgt ltoffsetgt
  • Branch class examples
  • bne src, dest, LABEL
  • beq src, dest, LABEL
  • bgez src, LABEL
  • Bltz src, LABEL
  • Note that there is no blt instruction, thats why
    slt is needed. To facilitate the assembly
    programming, there is a blt pseudo op that
    programmers can use.

10
Relative Addressing Example
la 8, array beq 20, 22, L1 lb 10,
1(8) add 11, 9, 10 sb 11, (8) L1 addiu
8, 8, 4
  • Assume no delay slot
  • beq 20,22,L1
  • Encoding0x12960004 (next slide)
  • Target(offset15)14 offset 02
  • PC PC Target

11
BEQ Encoding (I-Format)
Offset Value (Target addr beq addr)/4 of
instructions in-between including beq
instruction
rs
beq 20, 22, L1
rt
Branch to L1 if 2022
Offset 4 instructions
Encoding 0x12960004
12
Relative Addressing Example
  • The offset can be positive as well as negative

L2 addiu 20, 22, 4 lw 24, (22) lw
23, (20) beq 24, 23, L2
Offset -3 instructions
beq 24, 23, L2
Encoding 0x1317FFFD
13
MIPS Control Code Example
  • See array.s and prime.s

14
Procedural Calls in MIPS
  • MIPS uses jal or jalr to perform procedure calls
    (assume no branch delay slot)
  • jal LABEL r31 PC 4
  • jal rd, rs rd PC 4
  • Return address is automatically saved
  • When return from procedure
  • jr 31 or jr d
  • Note that 31 is aliased to ra
  • Call convention
  • Use a0 to a3 (4 to 7) for the first 4
    arguments
  • Use v0 (2) for passing the result back to the
    caller

15
Procedural Call Example
C code snippet z pyth(x, y) z
sqrt(z) int pyth(int x, int y)
return(x2y2)
MIPS snippet la t0, x la t1, y lw a0,
(t0) lw a1, (t1) jal pyth add a0, v0,
zero jal sqrt la t2, z sw v0, (t2)
pyth mult a0, a0 mflo t0 mult a1, a1
mflo t1 add v0, t0, t1 jr ra
sqrt . jr ra
Note that by software convention, t0 to t7 are
called caller-saved registers, i.e. the caller
is responsible to back them up before procedure
call if they are to be used after the procedure
call again
16
Procedural Call Recursion
MIPS snippet la t0, x lw a0, (t0) jal
fact fact addi v0, zero, 1 blt a0, v0,
L1 addi a0, a0, -1 jal fact L1 jr ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
?
ra (31) is overwritten and old value is lost
17
Procedural Call Many Arguments
MIPS snippet la t0, a lw a0, 0(t0) lw
a1, 4(t0) lw a2, 8(t0) lw a3,
12(t0) lw ???, 16(t0) foo ... ... jr
ra
C code snippet long a5 z foo(a0,
a1, a2, a3, a4) int foo(int
t, int u, int v, int w, int
x) return(tu-vwx)
Run out of passing argument registers !
18
Stack
  • The solution to address the prior 2 problems
  • a LIFO (Last-In First-Out) memory
  • A scratch memory space
  • Typically grow downward (i.e. push to lower
    address)

19
Stack
  • Stack Frame
  • Base pointed by a special register sp (29)
  • Each procedure has its own stack frame (if stack
    space is allocated)
  • Push
  • Allocate a new stack frame when entering a
    procedure
  • subu sp, sp, 32
  • What to store?
  • Return address
  • Additional passing arguments
  • Local variables
  • Pop
  • Deallocate the stack frame when return from
    procedure
  • addu sp, sp, 32

20
Stack Push
4 bytes
jal foo ... ... foo subu sp, sp, 32
... addu sp, sp, 32 jr ra
higher addr
PUSH
New Stack Frame for foo()
lower addr
21
Stack Pop
4 bytes
jal foo ... ... foo subu sp, sp, 32
... addu sp, sp, 32 jr ra
higher addr
Current Stack Frame
New Stack Frame for foo()
lower addr
22
Recursive Call
MIPS snippet li v0, 1 j fact fact beq
a0, v0, return return jr ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
23
Recursive Call
MIPS snippet li v0, 1 j fact fact beq
a0, v0, return jal fact
return jr ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
What to do with a0 and ra ?
24
Push onto the Stack
MIPS snippet li v0, 1 j fact fact beq
a0, v0, return sub sp, sp, 8 sw ra,
4(sp) sw a0, 0(sp) sub a0, a0, 1 jal
fact return jr ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
What happens after returning from the
procedure ???
Return address
a0 ( X)
25
Pop from the Stack
MIPS snippet li v0, 1 j fact fact beq
a0, v0, return sub sp, sp, 8 sw ra,
4(sp) sw a0, 0(sp) sub a0, a0, 1 jal
fact lw a0, 0(sp) lw ra, 4(sp) mult
a0, v0 mflo v0 add sp, sp, 8 return jr
ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
Return address
a0 ( X)
26
Recursive call for Factorial
MIPS snippet li v0, 1 j fact fact beq
a0, v0, return sub sp, sp, 8 sw ra,
4(sp) sw a0, 0(sp) sub a0, a0, 1 jal
fact lw a0, 0(sp) lw ra, 4(sp) mult
a0, v0 mflo v0 add sp, sp, 8 return jr
ra
C code snippet z fact(x) int fact(int
n) if (n lt 1) return(1) else
return(n fact(n-1))
Write a Comment
User Comments (0)
About PowerShow.com