320 likes | 333 Views
CDA 3101 Spring 2016 Introduction to Computer Organization. Procedure Support & Number Representations 28 January 2016. Review. 3 formats of MIPS instructions in binary: Op field determines format Operands
E N D
CDA 3101 Spring 2016Introduction to Computer Organization Procedure Support & Number Representations 28 January 2016
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!) 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits R op rs rt rd shamt funct I op rs rt immediate J op destination address
Overview • Memory layout • C functions • MIPS support (instructions) for procedures • The stack • Procedure Conventions • Manual Compilation • Conclusion
Memory Layout 0x00000000 (4MB) 0x00400000 Text segment 0x10000000 Static data $gp (0x10008000) Data segment Dynamic data 0x10010000 Stack segment 0x7FFFFFFF (2GB) 0xFFFFFFFF
Data Segment 0x10000000 $gp (0x10008000) (64KB) lui $s0, 0x1001 # s0 = 0x10010000 lw $v0, 0x8020($s0) # 0x8020 = (-7fe0)16 0x10008020 lw $v0, 0x0020($gp) 0x10010000 2 instructions (lui and lw) are needed to access this memory 0xFFFFFFFF
C Functions / Procedures int fact (int mcand, int mlier) {int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product; } main() {int i, j, k; … i = fact(j,k); … } What information must compiler keep track of?
Procedure Call Bookkeeping • Register conventions Labels $ra $a0, $a1, $a2, $a3 $v0, $v1 $s0, $s1, …, $s7 • Problems • Procedure address • Return address • Arguments • Return value • Local variables • Dynamic nature of procedures • Procedure call frames • Arguments, save registers, local variables
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 Procedure Call Convention • Software rules for using registers
Stack Frames The Stack ProcedureTwo ProcedureOne main 0x7FFFFFFF 0xFFFFFFFF
$sp $a0 $a1 $a2 ProcedureOne’s (callee) frame $a3 saved registers $fp, $ra, S-registers $v0 $v1 $fp Main’s (caller) frame Stack Frames local variables argument 5 argument 6 Saved VAT registers
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
main( ) {… s = sum (a, b); … } int sum(int x, int y) {return x + y; } Procedure Support address1000 add $a0,$s0,$zero # $a0 = x1004 add $a1,$s1,$zero # $a1 = y1008 addi $ra,$zero,1016 # $ra=10161012 j sum # jump to sum1016 ... 2000 sum: add $v0,$a0,$a12004jr $ra # jump to 1016
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 • (link): save address of next instruction into $ra • (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 2004jr $ra # jump to 1012
Nested Procedures int sumSquare(int x, int y) { return mult(x,x) + y; } sumSquare:subi $sp,$sp,12 # space on stack sw $ra,$ 8($sp) # save ret addrsw $a0,$ 0($sp) # save x sw $a1,$ 4($sp) # save yaddi$a1,$a0,$zero # mult(x,x) jal mult # call mult lw $ra,$ 8($sp) # get ret addrlw $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
Example (1/2) main( ) {int f; f = fact (10); printf (“Fact(10) = %d\n”, f); } Old $a0 Old $ra Old $fp fact (8) Old $a0 Old $ra Old $fp fact (9) int fact ( int n) { if (n < 1) return (1); else return (n * fact(n-1); } Old $a0 Old $ra Old $fp fact (10) Old $ra Old $fp main
Example (2/2) main: subu $sp, $sp, 32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 li $v0, 4 la $a0, str syscall li $a0, 10 jal fact addu $a0, $v0, $zero li $v0, 1 syscall lw $ra, 20($sp) lw $fp, 16($sp) addiu $sp, $sp, 32 jr $ra fact: subu $sp, $sp, 32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 sw $a0, 0($fp) lw $v0, 0($fp) bgtz $v0, L2 li $v0, 1 j L1 L2: lw $v1, 0($fp) subu $v0, $v1, 1 move $a0, $v0 jal fact lw $v1, 0($fp) mul $v0, $v0, $v1 L1: lw $ra, 20($sp) lw $fp, 16($sp) addiu $sp, $sp, 32 jr $ra
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
New Topic – Number Systems Application (browser) Operating Software Compiler System (Linux, Win) Assembler Instruction Set Architecture (ISA) Datapath & Control Memory I/O System Digital Logic Hardware Circuit Design Transistors
The Arithmetic and Logic Unit Datapath Memory 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 ... load/store program & data Control I / O add $t0, $s1, $s2 # $s1 + $s2 and $t0, $s1, $s2 # $s1 AND $s2 lw $t0, 100($s1) # $s1 + 100 beq $s1, $s2, L # $s1 = = $s2
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) Expressible Integers -231 231-1 ( 1101 1010 1101 )2 ( B A D )16 n-1 i = 0
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
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
Problems with Sign and Magnitude • Arithmetic circuit more complicated • Special steps depending whether signs are the same or different (e.g., -x . -y = xy = x * y) • Two zero representations • 0x00000000 = +0ten • 0x80000000 = -0ten • Programming implications (+0 == -0) • Sign and magnitude was abandoned because of confusion over zero
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. 00001 01111 00000 ... 10000 ... 11110 11111 • Still two zeros (oops.. ) • 0x00000000 = +0ten • 0xFFFFFFFF = -0ten • Arithmetic not too hard
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
Two’s Complement Number Line 0000 • 2 n-1 non-negatives • 2 n-1 negatives • one zero • 2 n-1-1 positives • comparison • Overflow due to circulant domain 0001 1111 0010 1110 0 -1 1 -2 2 0011 1101 3 -3 1100 4 0100 -4 5 -5 0101 1011 6 -6 7 -7 0110 1010 -8 1001 0111 1000 -8 -6 -4 -2 0 2 4 6 8
Example: Two’s Complement 0000 ... 0000 0000 0000 0000two = 0ten0000 ... 0000 0000 0000 0001two = 1ten0000 ... 0000 0000 0000 0010two = 2ten. . .0111 ... 1111 1111 1111 1101two = 2,147,483,645ten0111 ... 1111 1111 1111 1110two = 2,147,483,646ten0111 ... 1111 1111 1111 1111two = 2,147,483,647ten1000 ... 0000 0000 0000 0000two = –2,147,483,648ten1000 ... 0000 0000 0000 0001two = –2,147,483,647ten1000 ... 0000 0000 0000 0010two = –2,147,483,646ten. . . 1111 ... 1111 1111 1111 1101two = –3ten1111 ... 1111 1111 1111 1110two = –2ten1111 ... 1111 1111 1111 1111two = –1ten
Two’s Complement How-To • Can represent positive and negative numbers by first bit (MSB) as –231 position, then positive 2n: d31x -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
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 -4x=-4 : 1111 1111 1111 1111 1111 1111 1111 1100twox’: 0000 0000 0000 0000 0000 0000 0000 0011twox’+ 1: 0000 0000 0000 0000 0000 0000 0000 0100twoinvert: 1111 1111 1111 1111 1111 1111 1111 1011twoadd 1: 1111 1111 1111 1111 1111 1111 1111 1100two
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
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 MSB 16-bit 32-bit
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!