cda 3101 fall 2013 introduction to computer organization procedure support & number...
TRANSCRIPT
![Page 1: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/1.jpg)
CDA 3101 Fall 2013
Introduction to Computer Organization
Procedure Support
& Number Representations
11,13 September 2013
![Page 2: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/2.jpg)
Review• 3 formats of MIPS instructions in binary:
– Op field determines format
• Operands– Registers: $0 to $31 mapped onto: $zero, $at, $v_, $a_, $s_, $t_, $gp, $sp, $fp, $ra
– Memory : Mem[0], Mem[4], … , Mem[4294967292]• Index is the “address” (Array index => Memory index)
• Stored program concept (instructions are numbers!)
op immediaters rt
op functrs rt shamtrd
op destination address
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
RI
J
![Page 3: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/3.jpg)
Overview
• Memory layout
• C functions
• MIPS support (instructions) for procedures
• The stack
• Procedure Conventions
• Manual Compilation
• Conclusion
![Page 4: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/4.jpg)
Memory Layout
Stack segment
Data segment
Text segment
Dynamic data
Static data
0xFFFFFFFF
0x7FFFFFFF
0x10000000
0x00400000
0x00000000
(2GB)
(4MB)
$gp (0x10008000)
0x10010000
![Page 5: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/5.jpg)
Data Segment
lw $v0, 0x0020($gp)
0xFFFFFFFF
0x10000000
$gp (0x10008000)
0x10010000
2 instructions (lui and lw) are needed to access this memory
(64KB) lui $s0, 0x1001 # s0 = 0x10010000lw $v0, 0x8020($s0) # 0x8020 = (-7fe0)16
0x10008020
![Page 6: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/6.jpg)
C Functions / Procedures
main() {int i, j, k;
…
i = fact(j,k);
…
}
int fact (int mcand, int mlier) {int product;
product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product;
}
What information must compiler keep track of?
![Page 7: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/7.jpg)
Procedure Call Bookkeeping
• Problems– Procedure address– Return address– Arguments– Return value– Local variables
• Register conventionsLabels
$ra
$a0, $a1, $a2, $a3
$v0, $v1
$s0, $s1, …, $s7
• Dynamic nature of procedures• Procedure call frames
• Arguments, save registers, local variables
![Page 8: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/8.jpg)
Procedure Call Convention• Software rules for using registers
Name Register Number Usage Preserved on call
$zero 0 the constant value 0 n.a.$at 1 reserved for the assembler n.a.
$v0-$v1 2-3 expr. evaluation and function result no$a0-$a3 4-7 arguments (procedures/functions) yes$t0-$t7 8-15 temporaries no$s0-$s7 16-23 saved yes$t8-$t9 24-25 more temporaries no$k0-$k1 26-27 reserved for the operating system n.a.
$gp 28 global pointer yes$sp 29 stack pointer yes$fp 30 frame pointer yes$ra 31 return address yes
![Page 9: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/9.jpg)
ProcedureOne
The Stack
0xFFFFFFFF
0x7FFFFFFF
main
ProcedureTwo
Stack Frames
![Page 10: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/10.jpg)
ProcedureOne’s (callee) frame
Main’s (caller) frame
Stack Frames
argument 5argument 6
local variables
saved registers
$fp, $ra, S-registers
$fp
$sp
Saved VAT registers
$a0
$a3
$a2
$a1
$v0
$v1
![Page 11: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/11.jpg)
Caller / Callee Conventions• Immediately before the caller invokes the callee
– Pass arguments ($a0 - $a3). Extra args: push on the stack– Save caller-saved registers ($a0 - $a3; $t0 - $t9)– Execute jal (jumps and links to callee; saves return addr)
• Just before the callee starts executing– Allocate memory for the frame ($sp = $sp – fsize)– Save callee-saved registers ($s0-$s7; $fp; $ra)– $fp = $sp + (fsize – 4)
• Immediately before the callee returns to caller– Place the returned value in register $v0– Restore all callee-saved registers– Pop the stack frame ($sp = $sp + fsize); restore $fp– Return by jumping to the address in $ra
![Page 12: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/12.jpg)
Procedure Support
address1000 add $a0,$s0,$zero # $a0 = x1004 add $a1,$s1,$zero # $a1 = y 1008 addi $ra,$zero,1016 # $ra=10161012 j sum # jump to sum1016 ...
2000 sum: add $v0,$a0,$a12004 jr $ra # jump to 1016
main( ) {…
s = sum (a, b); …}
int sum(int x, int y) {return x + y;
}
![Page 13: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/13.jpg)
Jump and Link Instruction• Single instruction to jump and save return address
– jal: jump and link (Make the common case fast)– J Format Instruction: jal label– Should be called laj
1. (link): save address of next instruction into $ra2. (jump): jump to label
1000 add $a0,$s0,$zero # $a0 = x 1004 add $a1,$s1,$zero # $a1 = y 1008 jal sum # $ra = 1012; jump to sum 1012 ... 2000 sum: add $v0,$a0,$a1 2004 jr $ra # jump to 1012
![Page 14: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/14.jpg)
Nested Proceduresint sumSquare(int x, int y) {
return mult(x,x) + y;
}sumSquare:
subi $sp,$sp,12 # space on stack sw $ra,$ 8($sp) # save ret addr
sw $a0,$ 0($sp) # save x sw $a1,$ 4($sp) # save y
addi $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $ra,$ 8($sp) # get ret addr lw $a0,$ 0($sp) # restore x
lw $a1,$ 4($sp) # restore y add $vo,$v0,$a1 # mult()+y addi $sp,$sp,12 # free stack space
jr $ra
![Page 15: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/15.jpg)
Example (1/2)main( ) {
int f; f = fact (10); printf (“Fact(10) = %d\n”, f); }
int fact ( int n) { if (n < 1)
return (1); else
return (n * fact(n-1);}
Old $raOld $fp
Old $a0Old $raOld $fp
Old $a0Old $raOld $fp
Old $a0Old $raOld $fp
main
fact (10)
fact (8)
fact (9)
![Page 16: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/16.jpg)
Example (2/2)main: subu $sp, $sp, 32
sw $ra, 20($sp)sw $fp, 16($sp)addiu $fp, $sp, 28li $v0, 4la $a0, strsyscallli $a0, 10jal factaddu $a0, $v0, $zero li $v0, 1syscalllw $ra, 20($sp) lw $fp, 16($sp)addiu $sp, $sp, 32jr $ra
fact: subu $sp, $sp, 32sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28sw $a0, 0($fp) lw $v0, 0($fp) bgtz $v0, L2li $v0, 1j L1
L2: lw $v1, 0($fp)subu $v0, $v1, 1move $a0, $v0jal factlw $v1, 0($fp) mul $v0, $v0, $v1
L1: lw $ra, 20($sp) lw $fp, 16($sp) addiu $sp, $sp, 32jr $ra
![Page 17: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/17.jpg)
Summary• Caller / callee conventions
– Rights and responsibilities– Callee uses VAT registers freely– Caller uses S registers without fear of being overwritten
• Instruction support: jal label and jr $ra• Use stack to save anything you need. Remember to
leave it the way you found it• Register conventions
– Purpose and limits of the usage– Follow the rules even if you are writing all the code
yourself
![Page 18: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/18.jpg)
New Topic – Number Systems
CompilerOperating
System(Linux, Win)
Application (browser)
Digital Logic
Circuit Design
Instruction Set Architecture (ISA)
Datapath & Control
Transistors
Hardware
Software
Assembler
Memory I/O System
![Page 19: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/19.jpg)
The Arithmetic and Logic Unit
0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111
...
Memory
program & data
I / O
Datapath
Control
load/store
add $t0, $s1, $s2 # $s1 + $s2and $t0, $s1, $s2 # $s1 AND $s2lw $t0, 100($s1) # $s1 + 100beq $s1, $s2, L # $s1 = = $s2
![Page 20: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/20.jpg)
Computer Arithmetic• Computer arithmetic vs. Mathematical theory
– Fixed-precision numbers• Not closed with respect to +, -, *, /
– Overflow– Underflow– Undefined
• Laws of normal algebra do not always hold in computers– a + (b - c) = (a + b) - c– a * (b – c) = a * b – a * c
– Binary numbers• Base: 2• Digits: 0 and 1
• Decimal number = Σ di * 2i (d31 d30 d29 . . . d2 d1 d0) i = 0
n-1
-231 231-1 Expressible Integers
( 1101 1010 1101 )2
( B A D )16
![Page 21: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/21.jpg)
Data Representation
Bits can represent anything:• Characters
– 26 letters => 5 bits– Upper/lower case + punctuation => 7 bits (in 8)– Rest of the world’s languages => 16 bits (unicode)
• Unsigned numbers (0, 1, …, 2n-1)• Logical values
– 0 -> False, 1 => True
• Colors• Locations / addresses / commands• But n bits can only represent 2n distinct objects
![Page 22: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/22.jpg)
Negative Numbers
• We discussed unsigned numbers – What about the sign?• Naive solution: define leftmost bit to be sign
– 0 means +, 1 means - => sign bit
– Remainder of bits can be numerical value of number
• This representation called sign and magnitude• MIPS uses 32-bit integers (16-bit immediates/displacements)
+1ten would be:
0000 0000 0000 0000 0000 0000 0000 0001
- 1ten would be:
1000 0000 0000 0000 0000 0000 0000 0001
![Page 23: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/23.jpg)
Problems with Sign and Magnitude
1. Arithmetic circuit more complicated– Special steps depending whether signs are the
same or different (e.g., -x . -y = xy = x * y)
2. Two zero representations– 0x00000000 = +0ten
– 0x80000000 = -0ten
– Programming implications (+0 == -0)
• Sign and magnitude was abandoned because of confusion over zero
![Page 24: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/24.jpg)
Let’s Try: One’s Complement
• Complement the bits to get the negative• Example: 710 = 001112 -710 = 110002
• Positive numbers have leading 0s, negative numbers have leadings 1s.
00000 00001 01111...
111111111010000 ...
• Still two zeros (oops.. )– 0x00000000 = +0ten
– 0xFFFFFFFF = -0ten
• Arithmetic not too hard
![Page 25: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/25.jpg)
Better: Two’s Complement
• Positive numbers start with 0• Negative numbers are the inverse of positive + ONE• Example
110 = 000000012
-110 = 111111102 + 12 = 111111112
710 = 000001112
-710 = 111110002 + 12 = 111110012
• Positive numbers can have infinitely many leading 0’s• Negative numbers can also have infinitely many leading
1’s
![Page 26: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/26.jpg)
Two’s Complement Number Line
• 2 n-1 non-negatives • 2 n-1 negatives• one zero• 2 n-1-1 positives• comparison• Overflow due to
circulant domain
00000001
00101111
1110
1000 01111001
0 12
-1-2
-7 -87
0011
0100
0101
01101010
1011
1100
11013
4
5
6
-3
-4
-5
-6
0-8 -6 -4 -2 2 4 6 8
![Page 27: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/27.jpg)
Example: Two’s Complement
0000 ... 0000 0000 0000 0000two =
0ten
0000 ... 0000 0000 0000 0001two =
1ten
0000 ... 0000 0000 0000 0010two =
2ten
. . .0111 ... 1111 1111 1111 1101two =
2,147,483,645ten
0111 ... 1111 1111 1111 1110two =
2,147,483,646ten
0111 ... 1111 1111 1111 1111two =
2,147,483,647ten
1000 ... 0000 0000 0000 0000two =
–2,147,483,648ten
1000 ... 0000 0000 0000 0001two =
–2,147,483,647ten
1000 ... 0000 0000 0000 0010two =
–2,147,483,646ten
. . . 1111 ... 1111 1111 1111 1101two =
–3ten
1111 ... 1111 1111 1111 1110two =
–2ten
1111 ... 1111 1111 1111 1111two =
–1ten
![Page 28: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/28.jpg)
Two’s Complement How-To
• Can represent positive and negative numbers by first bit (MSB) as –231 position, then positive 2n:d31 x -231 + d30 x 230 + ... + d2 x 22 + d1 x 21 + d0 x 20
• Example1111 1111 1111 1111 1111 1111 1111 1100two
= 1x-231 +1x230 +1x229+... +1x22+0x21+0x20
= -231 + 230 + 229 + ... + 22 + 0 + 0
= -2,147,483,648ten + 2,147,483,644ten
= -4ten
• Note! Must specify width to find MSB => 32bits is used in MIPS, so d31 is MSB
![Page 29: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/29.jpg)
Two’s Complement Negation• Invert (every 0 to 1 and every 1 to 0), then
add 1 to the result– Sum of number and its one’s complement must be
111...111two = -1ten
– Let x’ denote the inverted representation of x– Then x + x’ = -1 x + x’ + 1 = 0 x’ + 1 = -x
• Example: x = -4 to +4 to -4 x=-4 : 1111 1111 1111 1111 1111 1111 1111 1100two
x’ : 0000 0000 0000 0000 0000 0000 0000 0011two
x’ + 1: 0000 0000 0000 0000 0000 0000 0000 0100two
invert: 1111 1111 1111 1111 1111 1111 1111 1011two
add 1 : 1111 1111 1111 1111 1111 1111 1111 1100two
![Page 30: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/30.jpg)
Signed vs. Unsigned Comparisons
• X = 1111 1111 1111 1111 1111 1111 1111 1100two
• Y = 0011 1011 1001 1010 1000 1010 0000 0000two
Ambiguity:• Is X > Y?
– Unsigned: YES
– Signed: NO
• Converting to decimal to check– Signed comparison:
-4ten < 1,000,000,000ten?
– Unsigned comparison: -4,294,967,292ten < 1,000,000,000ten
![Page 31: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/31.jpg)
2’s Compl. Sign Extension• Problem: Convert 2’s complement number using
n bits to more than n bits• Solution: Replicate the most significant bit (sign
bit) of smaller to fill new bits–2’s comp. positive number has infinite 0s to the left–2’s comp. negative number has infinite 1s to the left–Bit representation hides leading bits; sign extension restores some of the infinite number of leading bits–16-bit -4ten to 32-bit:
1111 1111 1111 1100two
1111 1111 1111 1111 1111 1111 1111 1100two
16-bit
32-bit
MSB
![Page 32: CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062517/56649f385503460f94c55127/html5/thumbnails/32.jpg)
Conclusion• We represent objects in computers as bit patterns:
n bits =>2n distinct patterns• Decimal for humans, binary for computers• 2’s complement universal in computing: cannot
avoid, so we will learn• Computer operations on the representation are
abstraction of real operations on the real thing• Overflow:
- Numbers infinite- Computer finite
• Enjoy: Weekend!