Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control
-
Upload
hsien-hsin-lee -
Category
Devices & Hardware
-
view
362 -
download
1
Transcript of Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control
![Page 1: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/1.jpg)
ECE2030 Introduction to Computer Engineering
Lecture 19: Program Control
Prof. Hsien-Hsin Sean LeeProf. Hsien-Hsin Sean LeeSchool of Electrical and Computer EngineeringSchool of Electrical and Computer EngineeringGeorgia TechGeorgia Tech
![Page 2: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/2.jpg)
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?
![Page 3: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/3.jpg)
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, arraylb $9, ($8)lb $10, 1($8)add $11, $9, $10sb $11, ($8)addiu $8, $8, 4lh $9, ($8)lhu $10, 2($8)add $11, $9, $10sh $11, ($8)addiu $8, $8, 4lw $9, ($8)lw $10, 4($8)sub $11, $9, $10sw $11, ($8)
PC
![Page 4: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/4.jpg)
4
Program Counter (Little Endian)
main:la $8, arraylb $9, ($8)lb $10, 1($8)add $11, $9, $10sb $11, ($8)addiu $8, $8, 4lh $9, ($8)lhu $10, 2($8)add $11, $9, $10sh $11, ($8)addiu $8, $8, 4lw $9, ($8)lw $10, 4($8)sub $11, $9, $10sw $11, ($8)
0x010x100x080x3c
PC
la $8, array
0x000x000x090x81
PC+4 lb $9, ($8)
0x010x000x0a0x81
PC+8
lb $10, 1($8)
• Each MIPS instruction is 4-byte wide
0x200x580x2a0x010x000x000x0b0xa10x04
add $11,$9,$10
sb $11, ($8)
PC+12
PC+16
PC+20
![Page 5: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/5.jpg)
5
Program Counter Control
32-bit PC RegisterSystem clock +
432
32
32
2kx32
INSTRUCTIONMEMORY
32
Data (i.e. instruction)
Instruction Register
![Page 6: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/6.jpg)
6
Change of Flow Scenarios• Our current defaultdefault modemode
– Program executed in sequentialsequential 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
![Page 7: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/7.jpg)
7
Absolute Instruction Addressing
• The next PC address is given as an absolute value– PC address = <given address>
• Jump class examples– J LABEL – JAL LABEL – JALR $r– JR $r
![Page 8: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/8.jpg)
8
Absolute Addressing Example
• Assume no delay slot• J Label2J Label2
– Encoding: • Label2=0x00400048• J opcode= (000010)2
• Encoding=0x8100012– Temp = <Label2 26-bit
addr>– PC = PC31..28|| Temp || 02
main:la $8, arraylb $9, ($8)lb $10, 1($8)add $11, $9, $10sb $11, ($8)j Label2......
Label2:addiu $8, $8, 4lh $9, ($8)lhu $10, 2($8)
![Page 9: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/9.jpg)
9
Relative Instruction Addressing• An offset relative to the current PC address
is given instead of an absolute address– PC address = <current PC address> +
<offset>• Branch class examples
– bne $src, $dest, LABEL– beq $src, $dest, LABEL– bgez $src, LABEL – Bltz $src, LABEL
– Note that there is no blt instruction, that’s why slt is needed. To facilitate the assembly programming, there is a “blt blt pseudo oppseudo op”” that programmers can use.
![Page 10: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/10.jpg)
10
Relative Addressing Example
• Assume no delay slot• beq $20,$22,L1beq $20,$22,L1
– Encoding=0x12960004 (next slide)– Target=(offset15)14 || offset || 02
– PC = PC + Target
la $8, arraybeq $20, $22, L1lb $10, 1($8)add $11, $9, $10sb $11, ($8)
L1:addiu $8, $8, 4
![Page 11: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/11.jpg)
11
BEQ Encoding (I-Format)
beq $20, $22, L1
Offset Value = (Target addr – beq addr)/4
= # of instructions in-between
including beq instructionrt
rs
0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 00 0
Encoding = 0x12960004
0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 00 031
opcode rs rt
26 25 21 20 1615 0
Offset Value
Branch to L1 if $20==$22
Offset = 4 instructions
![Page 12: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/12.jpg)
12
Relative Addressing Example• The offset can be positive as well as negative
L2:addiu $20, $22, 4lw $24, ($22)lw $23, ($20)beq $24, $23, L2
0 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 10 0
Encoding = 0x1317FFFD
0 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 10 031
opcode rs rt
26 25 21 20 1615 0
Offset Value
Offset = -3-3 instructions
beq $24, $23, L2
![Page 13: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/13.jpg)
13
MIPS Control Code Example• See array.s and prime.s
![Page 14: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/14.jpg)
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
![Page 15: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/15.jpg)
15
Procedural Call Example C code snippetC code snippet
z = pyth(x, y); z = sqrt(z);
int pyth(int x, int y) { return(x2+y2); }
MIPS snippetMIPS 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 $raNote 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 usedafter the procedure call again
![Page 16: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/16.jpg)
16
Procedural Call: Recursion
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
MIPS snippetMIPS snippet
la $t0, x lw $a0, ($t0) jal fact
fact: addi $v0, $zero, 1 blt $a0, $v0, L1 addi $a0, $a0, -1 jal factL1: jr $ra
?$ra ($31) is overwritten and old value is lost
![Page 17: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/17.jpg)
17
Procedural Call: Many Arguments
C code snippetC code snippet
long a[5];
z = foo(a[0], a[1], a[2], a[3], a[4]);
int foo(int t, int u, int v, int w, int x) { return(t+u-v*w+x); }
MIPS snippetMIPS 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
Run out of passing argument registers !
![Page 18: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/18.jpg)
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)
![Page 19: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/19.jpg)
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
![Page 20: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/20.jpg)
20
Stack Push 4 bytes
higheraddr
loweraddr
$sp
jal foo ... ...
foo: subu $sp, $sp, 32
...
addu $sp, $sp, 32 jr $ra
PUSHPUSH
New
Stac
k Fr
ame
New
Stac
k Fr
ame
for f
oo()
for f
oo()
![Page 21: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/21.jpg)
21
Stack Pop4 bytes
higheraddr
loweraddr
$sp
jal foo ... ...
foo: subu $sp, $sp, 32
...
addu $sp, $sp, 32 jr $ra
New
Stac
k Fr
ame
New
Stac
k Fr
ame
for f
oo()
for f
oo()
Curre
nt
Curre
nt
Stac
k St
ack
Fram
eFr
ame
![Page 22: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/22.jpg)
22
Recursive Call
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
MIPS snippetMIPS snippetli $v0, 1j fact
fact:beq $a0, $v0, return
return:jr $ra
![Page 23: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/23.jpg)
23
Recursive Call
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
MIPS snippetMIPS snippetli $v0, 1j fact
fact:beq $a0, $v0, return
jal fact
return:jr $ra
What to do with $a0 and $ra ?
![Page 24: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/24.jpg)
24
Push onto the Stack MIPS snippetMIPS snippet
li $v0, 1j fact
fact:beq $a0, $v0, returnsub $sp, $sp, 8sw $ra, 4($sp)sw $a0, 0($sp)sub $a0, $a0, 1jal fact
return:jr $ra
$sp Return address$a0 (= X)
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
What happens after returning from
the procedure ???
![Page 25: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/25.jpg)
25
Pop from the Stack
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
MIPS snippetMIPS snippetli $v0, 1j fact
fact:beq $a0, $v0, returnsub $sp, $sp, 8sw $ra, 4($sp)sw $a0, 0($sp)sub $a0, $a0, 1jal fact
lw $a0, 0($sp)lw $ra, 4($sp)mult $a0, $v0mflo $v0add $sp, $sp, 8
return:jr $ra$sp
Return address$a0 (= X)
![Page 26: Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Program Control](https://reader036.fdocuments.in/reader036/viewer/2022062823/58759cef1a28ab6d198b5a0f/html5/thumbnails/26.jpg)
26
Recursive call for Factorial
C code snippetC code snippet
z = fact(x);
int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }
MIPS snippetMIPS snippetli $v0, 1j fact
fact:beq $a0, $v0, returnsub $sp, $sp, 8sw $ra, 4($sp)sw $a0, 0($sp)sub $a0, $a0, 1jal fact
lw $a0, 0($sp)lw $ra, 4($sp)mult $a0, $v0mflo $v0add $sp, $sp, 8
return:jr $ra