Dr. ChuckCartledgeDr. ChuckCartledgeDr. ChuckCartledge ...
Transcript of Dr. ChuckCartledgeDr. ChuckCartledgeDr. ChuckCartledge ...
1/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
CSC-205 Computer OrganizationLecture #008
Chapter 6, section 5,Chapter 1, section 5
Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge
15 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 201515 July 2015
2/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Table of contents I
1 Miscellanea
2 6.5
3 Fun with Fibonacci
4 Break
5 1.5
6 Exam
7 Conclusion
8 References
3/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Corrections and additions since last lecture.
Return chapter 5 homework
Return remaining tests
4/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
What is it and why should I care??
Different types of memory allocation:
Static – you know in advance how much you need and youwant to keep stuff around
Allocated by the compiler/translator
Dynamic – you don’t know how much you’ll need and yourneeds may change as the program executes
Allocated by the applicationReleased by the application
Released dynamically allocated memory can cause memory tobecome fragmented and unusable.
Released dynamically allocated memory can be “cleaned up”via garbage collection techniques.
Allocation and consolidation of dynamic memory usuallyhandled by the Operating System.
5/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating global pointers
A new C++ token, the asterisk “*”means the variable points to a memorylocation. It “strips” away one layer ofinformation hiding.
Create three pointers and givethose pointers names.
“new” dynamically sets aside anumber of bytes that the pointer“points to”
Need to tell the compiler to usethe value of the pointer vice thelocation of the pointer (that’swhat all those asterisks are about)
Assembler code uses “indirectaddressing” to get to the actualvalue of things (0x0012 and0x0018 for example) Pages 304 – 305.
6/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating local pointers
Very similar in structure totranslating global pointers.
The variables “live” on thestack.
Pointers point to locationsin the stack.
Pages 309 – 311
7/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating structures (ideas)
What is a structure??
It is a group of dataelements brought togetherunder the same name.
Data elements can be ofdifferent types and sizes
Data elements can be in anyorder
The structure name refers tothe entire group
Structure data elementname refers to the elementwithin the structure
In many ways, like predefined types on steroids.
8/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating structures (example)
Load the offset into theINDEX register (0x0008)
The INPUT a character intothe base memory locationplus the offset in the INDEXregister (0x000B)
Same sort of process tooutput the data (load offsetinto INDEX, output relativeto base address)
Pages 313 – 315.
9/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating linked list structures (ideas)
What is a linked list?? And why do Icare??
A linked list is an ordered set ofdata elements, each containing alink to its successor
What is a ordered list?? Acollection of elements where youcan say one follows (or proceeds)another.
What is a link?? A pointer (ourfriend from before) to the another(usually the next one) element.
The link from the last element tothe next one is usually NULL.
Somehow you have to have apointer to the first item in the list.
10/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Dynamic memory allocation
Translating linked list structures (example)
We define a structure thatwe will used to link things.
We create a couple orpointers for the current andnext structure
We create a structure(0x0021), update thestructure’s pointers(0x0018), enter a value,store it into a structure(0x002A based on the baseof the structure and anoffset), and repeat
Pages 317 – 319 (programprob0637.cpp).
11/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Hystorical perspective (no that isn’t a typo)
Leonardo Bonacci (c. 1170 – c. 1250)
Considered to be the most talentedWestern mathematician of the MiddleAges.
Most notable work Liber Abaci
(1202) introduced:
Arabic numbersPlace valuesPromoted his ideas byapplying them tobookkeeping
Image from [2].
Posed a problem having to do with rabbits.
12/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Hystorical perspective (no that isn’t a typo)
Fibonacci’s rabbits (the background)
The growth of an idealized rabbit population, assuming that:
1 A newly born pair of rabbits, one male, one female, are put ina field;
2 Rabbits are able to mate at the age of one month so that atthe end of its second month a female can produce anotherpair of rabbits;
3 Rabbits never die and a mating pair always produces one newpair (one male, one female) every month from the secondmonth.
13/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Hystorical perspective (no that isn’t a typo)
Fibonacci’s rabbits (the puzzle)
How many pairs will there be in one year?
1 At the end of the first month, they mate, but there is still only1 pair.
2 At the end of the second month the female produces a newpair, so now there are 2 pairs of rabbits in the field.
3 At the end of the third month, the original female produces asecond pair, making 3 pairs in all in the field.
4 At the end of the fourth month, the original female hasproduced yet another new pair, the female born two monthsago produces her first pair also, making 5 pairs.
At the end of the nth month, the number of pairs of rabbits isequal to the number of new pairs (which is the number of pairs inmonth n - 2) plus the number of pairs alive last month (n - 1).
14/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Hystorical perspective (no that isn’t a typo)
Fibonacci’s rabbits (math)
The Fibonacci series:
Fn = Fn−1+Fn−2
It starts:
Traditional: F1 = F2 = 1Modern: F0 = 0,F1 = 1
Negative Fibonacci numberexist as well
Fn−2 = Fn−Fn−1
F−n = (−1)n+1Fn
15/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Hystorical perspective (no that isn’t a typo)
Fibonacci numbers (solutions)
More than one way to skin a cat(number 49 with duct tape isexciting):
For loops
Recursive function
Function (direct look up)
State machine
A process queue is left forconjecture.
Image from [1].
Sample programs are attached to this pdf.
16/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Break time.
Take about 10 minutes.
17/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
So far we have only looked at integers, the countingnumbers.
What about “real” or “floating point”numbers?? How do we represent thevalue 5.375??
For integers:
n = ∑positionsi=1 vali ∗base
(i−1)
For floating point:
n = ∑positionsi=1 vali ∗base
(i−1−m)
m is the number of bits to theright of the decimal point
If m = 3 then 5.37510 = 101.0112
Like so many other things, we change the definition of the bits.
18/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
So how does this work??
We’ll pick it apart. n = ∑cellSizei=1 vali ∗base
(i−1−m) base = 2,m = 3
How do we represent the decimal point in binary??
19/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
Another mechanical way to convert to binary.
Repeatedly divide theinteger portion by 2 andrecord the remainder
Repeatedly multiply thefractional portion by 2 andrecord the overflow
6.585937510 = 110.100101112
Stop converting when the integer and fractional part are 0.
20/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
What happens with “messy” numbers??
As an exercise, convert1.2(10) to binary.
.2
0 .40 .81 .61 .20 .40 .81 .61 .2...
...
Some fractions can not be represented by a fixed length binarynumber.
21/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
Our friend, scientific notation.
A short hand way of writingnumbers. Makes things likemultiplication and division oflarge numbers trivial. A couple ofexamples.
−328.4 =−3.284∗102
Three parts:1 Sign (- in this case)2 Coefficient (3.284 in this
case), sometimes calledthe significand
3 Exponent (2 in this case)4 Base (10 in this case)
value = SignCoefficient ∗
BaseExponent
Writing numbers in scientific notation is called “normalized” form.
22/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
Remember the question about where do we store thedecimal point??
When dealing with numbers in the normalized form, everyoneknows where the decimal is, so no one has to store it.
And because everyone “knows” that the most significant bithas to be 1, no has to store it.
Hidden and excess bits are conventions that allow more bits to beused where they are needed.
23/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
Special Values
The limitation on the cell size(i.e., the number of bits we canuse) places a limit on the rangeof values we can represent.
We’ve seen how ADDingcan overflow a cell.
Normalization limits thesmallest coefficient that wecan have greater than 0.
Operations using values inrange can result in valuesthat can not be represented.
Sign bit, 7 bit coeff, 5 bit expon.
All of these errors can to caught by the CPU and software.
24/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
There are too many possibilities. We needstandardization.
The IEEE 754 Floating PointStandard to the rescue.
Each CPU manufacturer hadtheir own floating pointrepresentation.
Each felt their’s was thebest.
Application software on onemachine may not runcorrectly on anothermachine.
25/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Real and floating point numbers
IEEE 754 Special Values and Notes
Predefined special values by thespecification:
Positive and negativeinfinity: sign ⇒ 0/1, expon.⇒ all 1s, coeff ⇒ all 0s
Not a Number (NaN): sign⇒ 0/1, expon. ⇒ all 1s,coeff ⇒ anything except all0s
Precision:2−149
≈ 1.17549∗10−38 or2−1074
≈ 4.94066∗10−324
Rounding: nearest, even, 0,+/- infinity
26/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Steps to succeed on the exam.
General guidelines and directions
Pay attention to theexamples
Understand the homework
Work questions that haveanswers
Ask questions
27/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Steps to succeed on the exam.
Things of real interest in chapter 5
Assembly language:
Understanding figures 1 and2
What do thepseudo-operators do
How does the von Neumanncycle work
What does it mean totranslate from HOL6 toISA3 or ASL5
28/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
Steps to succeed on the exam.
Things of real interest in chapter 6
Compiling to the Assembly level
Understanding the stack and theheap
Understanding the addressingmodes (and there are several ofthem)
What purpose does the stack andthe heap serve relative tovariables and functions
How are C++ statementstranslated into Pep/8
How are values passed betweencallers and callees
How are indexes into arrayshandled
What types of memory allocationare there and how are theydifferent
29/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
What have we covered?
Chapter 6, section 5Different ways to have funwith FibonacciRepeat of Chapter 1, section5Preps for the exam coveringchapters 5 and 6
Next week:
Exam covering chapters 5 and 6“Fun” with von Neumann
30/30
Miscellanea 6.5 Fun with Fibonacci Break 1.5 Exam Conclusion References
References I
[1] Hanson Schmidt-Cornelius, Recursion for runaways,http://livecode.com/recursion-for-runaways/, 2014.
[2] Wikipedia Staff, Fibonacci,https://en.wikipedia.org/wiki/Fibonacci, 2015.