Presentation is loading. Please wait.

Presentation is loading. Please wait.

CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013.

Similar presentations


Presentation on theme: "CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013."— Presentation transcript:

1 CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013

2 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!) opimmediatersrt opfunctrsrtshamtrd opdestination address 6 bits5 bits 6 bits R I J

3 Overview Memory layout C functions MIPS support (instructions) for procedures The stack Procedure Conventions Manual Compilation Conclusion

4 Memory Layout Stack segment Data segment Text segment Dynamic data Static data 0xFFFFFFFF 0x7FFFFFFF 0x10000000 0x00400000 0x00000000 (2GB) (4MB) $gp (0x10008000) 0x10010000

5 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 = 0x10010000 lw $v0, 0x8020($s0) # 0x8020 = (-7fe0) 16 0x10008020

6 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?

7 Procedure Call Bookkeeping Problems –Procedure address –Return address –Arguments –Return value –Local variables Register conventions Labels $ra $a0, $a1, $a2, $a3 $v0, $v1 $s0, $s1, …, $s7 Dynamic nature of procedures Procedure call frames Arguments, save registers, local variables

8 Procedure Call Convention Software rules for using registers NameRegister NumberUsagePreserved on call $zero 0the constant value 0n.a. $at 1reserved for the assemblern.a. $v0-$v1 2-3expr. evaluation and function resultno $a0-$a3 4-7arguments (procedures/functions)yes $t0-$t7 8-15temporariesno $s0-$s7 16-23savedyes $t8-$t9 24-25more temporariesno $k0-$k1 26-27reserved for the operating systemn.a. $gp 28global pointeryes $sp 29stack pointeryes $fp 30frame pointeryes $ra 31return addressyes

9 ProcedureOne The Stack 0xFFFFFFFF 0x7FFFFFFF main ProcedureTwo Stack Frames

10 ProcedureOne’s (callee) frame Main’s (caller) frame Stack Frames argument 5 argument 6 local variables saved registers $fp, $ra, S-registers $fp $sp Saved VAT registers $a0 $a3 $a2 $a1 $v0 $v1

11 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

12 Procedure Support address 1000 add $a0,$s0,$zero # $a0 = x 1004 add $a1,$s1,$zero # $a1 = y 1008 addi $ra,$zero,1016 # $ra=1016 1012 j sum # jump to sum 1016... 2000 sum: add $v0,$a0,$a1 2004 jr $ra # jump to 1016 main( ) { … s = sum (a, b); … } int sum(int x, int y) { return x + y; }

13 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 $ra 2.(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

14 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 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

15 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 $ra Old $fp Old $a0 Old $ra Old $fp Old $a0 Old $ra Old $fp Old $a0 Old $ra Old $fp main fact (10) fact (8) fact (9)

16 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 jalfact 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 jL1 L2:lw$v1, 0($fp) subu$v0, $v1, 1 move$a0, $v0 jalfact lw$v1, 0($fp) mul$v0, $v0, $v1 L1:lw$ra, 20($sp) lw$fp, 16($sp) addiu$sp, $sp, 32 jr$ra

17 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

18 New Topic – Number Systems Compiler Operating System (Linux, Win) Application (browser) Digital Logic Circuit Design Instruction Set Architecture (ISA) Datapath & Control Transistors Hardware Software Assembler MemoryI/O System

19 The Arithmetic and Logic Unit 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... Memory program & data I / O Datapath Control load/store 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

20 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 = Σ d i * 2 i (d 31 d 30 d 29... d 2 d 1 d 0 ) i = 0 n-1 -2 31 2 31 -1 Expressible Integers ( 1101 1010 1101 ) 2 ( B A D ) 16

21 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, …, 2 n-1 ) Logical values –0 -> False, 1 => True Colors Locations / addresses / commands But n bits can only represent 2 n distinct objects

22 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 ) +1 ten would be: 0000 0000 0000 0000 0000 0000 0000 0001 - 1 ten would be: 1000 0000 0000 0000 0000 0000 0000 0001

23 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 = +0 ten – 0x80000000 = -0 ten –Programming implications (+0 == -0) Sign and magnitude was abandoned because of confusion over zero

24 Let’s Try : One’s Complement Complement the bits to get the negative Example: 7 10 = 00111 2 -7 10 = 11000 2 Positive numbers have leading 0s, negative numbers have leadings 1s. 00000 0000101111... 111111111010000... Still two zeros ( oops..  ) – 0x00000000 = +0 ten – 0xFFFFFFFF = -0 ten Arithmetic not too hard

25 Better : Two’s Complement Positive numbers start with 0 Negative numbers are the inverse of positive + ONE Example 1 10 = 00000001 2 -1 10 = 11111110 2 + 1 2 = 11111111 2 7 10 = 00000111 2 -7 10 = 11111000 2 + 1 2 = 11111001 2 Positive numbers can have infinitely many leading 0’s Negative numbers can also have infinitely many leading 1’s

26 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 0000 0001 0010 1111 1110 1000 01111001 0 1 2 -2 -7 -8 7 0011 0100 0101 0110 1010 1011 1100 1101 3 4 5 6 -3 -4 -5 -6 0-8-6-4-22468

27 Example: Two’s Complement 0000... 0000 0000 0000 0000 two = 0 ten 0000... 0000 0000 0000 0001 two = 1 ten 0000... 0000 0000 0000 0010 two = 2 ten... 0111... 1111 1111 1111 1101 two = 2,147,483,645 ten 0111... 1111 1111 1111 1110 two = 2,147,483,646 ten 0111... 1111 1111 1111 1111 two = 2,147,483,647 ten 1000... 0000 0000 0000 0000 two = –2,147,483,648 ten 1000... 0000 0000 0000 0001 two = –2,147,483,647 ten 1000... 0000 0000 0000 0010 two = –2,147,483,646 ten... 1111... 1111 1111 1111 1101 two =–3 ten 1111... 1111 1111 1111 1110 two =–2 ten 1111... 1111 1111 1111 1111 two =–1 ten

28 Two’s Complement How-To Can represent positive and negative numbers by first bit (MSB) as –2 31 position, then positive 2 n : d 31 x -2 31 + d 30 x 2 30 +... + d 2 x 2 2 + d 1 x 2 1 + d 0 x 2 0 Example 1111 1111 1111 1111 1111 1111 1111 1100 two = 1x-2 31 +1x2 30 +1x2 29 +... +1x2 2 +0x2 1 +0x2 0 = -2 31 + 2 30 + 2 29 +... + 2 2 + 0 + 0 = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten Note! Must specify width to find MSB => 32bits is used in MIPS, so d 31 is MSB

29 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...111 two = -1 ten –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 1100 two x ’ : 0000 0000 0000 0000 0000 0000 0000 0011 two x ’ + 1: 0000 0000 0000 0000 0000 0000 0000 0100 two invert: 1111 1111 1111 1111 1111 1111 1111 1011 two add 1 : 1111 1111 1111 1111 1111 1111 1111 1100 two

30 Signed vs. Unsigned Comparisons X = 1111 1111 1111 1111 1111 1111 1111 1100 two Y = 0011 1011 1001 1010 1000 1010 0000 0000 two Ambiguity: Is X > Y? –Unsigned: YES –Signed: NO Converting to decimal to check –Signed comparison: -4 ten < 1,000,000,000 ten? –Unsigned comparison: -4,294,967,292 ten < 1,000,000,000 ten

31 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 -4 ten to 32-bit: 1111 1111 1111 1100 two 1111 1111 1111 1111 1111 1111 1111 1100 two 16-bit 32-bit MSB

32 Conclusion We represent objects in computers as bit patterns: n bits =>2 n 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!


Download ppt "CDA 3101 Fall 2013 Introduction to Computer Organization Procedure Support & Number Representations 11,13 September 2013."

Similar presentations


Ads by Google