Updated 080908 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Updated 080908

Description:

Fetch the contents of memory location 137 and store it in a register in the ALU ... assembly language it is also possible to assign labels. to memory locations. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 22
Provided by: adria9
Category:
Tags: updated

less

Transcript and Presenter's Notes

Title: Updated 080908


1
Introduction
2
Electronic Numerical Integrator and Computer
(ENIAC)
100s of calculations per second Modern PCs are
over 10,000 faster (and much smaller!)
3
von Neumann Computing Model
Memory
Central Processing Unit (CPU)
Arithmetic Logic Unit (ALU)
Control Unit
accumulator
Input
Output
4
Software Interface
User
Application Software
Operating System
Hardware
  • PC
  • Macinstosh
  • SUN
  • Windows XP
  • Windows Vista
  • Linux
  • compilers
  • word processors
  • web browers
  • students
  • engineers
  • scientists

5
Adding two numbers together
  • Suppose the two numbers we want to add are stored
    in
  • memory locations 137 and 138, and we want to
    store the
  • result in memory location 139.
  • The following instructions need to be executed by
    the CPU
  • Fetch the contents of memory location 137 and
    store it in a register in the ALU
  • Fetch the contents of memory location 138 and add
    it to the contents of the register
  • Store the contents of the register in memory
    location 139

6
Adding two numbers together
  • The machine language code to do this is
  • 00110101 10001001 00000001
  • 10010101 10001010 00000001
  • 01000010 00000001 10001011

opcodes
operands
7
Adding two numbers together
  • The assembly language code to do this is
  • LOAD 137, R1
  • ADD 138, R1
  • STORE R1, 139
  • With assembly language it is also possible to
    assign labels
  • to memory locations. If A is a label for
    location 137, B is
  • a label for location 138, and C is a label for
    location 139, the
  • assembly language code can be written as
  • LOAD A, R1
  • ADD B, R1
  • STORE R1, C

8
Adding two numbers together
  • High-level languages were developed to simplify
    programming. To add two numbers in C, the
    programmer uses the expression
  • c a b
  • Similar expressions are used in other programming
    languages
  • FORTRAN
  • C A B
  • APL
  • C ? A B
  • COBOL
  • ADD A TO B GIVING C.

9
Program Compilation/Linking/Execution
Input data
C language program
Machine language program
Program output
Compile
Link/load
Execute
10
Problem Solving and Computers
  • Computers
  • Fast
  • Repeatable
  • Problems
  • Repetitive tasks
  • Iterative solutions
  • Simulations

11
Repetitive Tasks
  • Compute the straight-line distance between two
    points in a plane.
  • include ltiostreamgt
  • include ltcmathgt
  • using namespace std
  • int main()
  • double x1, y1, x2, y2
  • double side1, side2, distance
  • cout ltlt "Enter x and y value for point 1 "
  • cin gtgt x1 gtgt y1
  • cout ltlt "Enter x and y value for point 2 "
  • cin gtgt x2 gtgt y2
  • side1 x2 - x1

12
Iterative problem
  • Assume we are given a function f(x) and we would
    like to see where this function intersects the
    line y x. Stated another way, we are trying to
    determine where x f(x).
  • Fixed point theorem
  • If f(x) ? K lt 1 for all x ? a,b, then the
    fixed point iteration pn f(pn-1) will converge
    to the fixed point x f(x).
  • The fixed point theorem basically states that we
    can use the fixed point iteration to determine x
    f(x) under a certain condition. The condition
    is that the absolute value of the first
    derivative of f(x), in the range a,b, is less
    than some constant K, which is less than 1.

13
Iterative problem
  • Fixed point iteration
  • Start with a point p0 in the range a,b
  • Determine the point pn f(pn-1). Point pn will
    be closer to the value x f(x) than point pn-1.
  • Repeat step 2 until we have approximated x close
    enough
  • (e.g. if pi pi-1 lt threshold).

14
Iterative problem
  • Example Let f(x) e-x in the range 0, 1.
    Start with p0 0.5.
  • p1 e-p0 e-0.5 0.60653
  • p2 e-p1 e-0.60653 0.54524
  • p3 e-p2 e-0.54524 0.57970
  • p4 e-p3 e-0.57970 0.56007
  • 21. p21 e-p20 e-0.567142 0.567143
  • 22. p22 e-p21 e-0.567143 0.567143
  • This process converges to lim n?? pn 0.567143

15
Iterative problem
  • Graphically we can see this process. It is
    obvious that this iterative process is indeed
    converging to the fixed point where the line y
    x crosses the function f(x).

16
Iterative problem
Performing this by hand is easy but very tedious
however, a computer can easily accomplish this
task.
  • include ltiostreamgt
  • include ltcmathgt
  • using namespace std
  • const double THRESHOLD 0.5e-6
  • int main()
  • double f
  • double oldp, p 0.5
  • int n 0
  • do
  • f exp(-p)
  • cout ltlt "p(" ltlt n ltlt ") " ltlt p
  • ltlt " and f(p(" ltlt n ltlt ")) " ltlt f ltlt
    endl
  • oldp p
  • p exp(-p) // compute p(n) f(p(n-1))

17
Iterative problem
  • p(0) 0.5 and f(p(0)) 0.606531
  • p(1) 0.606531 and f(p(1)) 0.545239
  • p(2) 0.545239 and f(p(2)) 0.579703
  • p(3) 0.579703 and f(p(3)) 0.560065
  • p(4) 0.560065 and f(p(4)) 0.571172
  • p(5) 0.571172 and f(p(5)) 0.564863
  • p(6) 0.564863 and f(p(6)) 0.568438
  • p(7) 0.568438 and f(p(7)) 0.566409
  • p(8) 0.566409 and f(p(8)) 0.56756
  • p(9) 0.56756 and f(p(9)) 0.566907
  • p(10) 0.566907 and f(p(10)) 0.567277
  • p(11) 0.567277 and f(p(11)) 0.567067
  • p(12) 0.567067 and f(p(12)) 0.567186
  • p(13) 0.567186 and f(p(13)) 0.567119
  • p(14) 0.567119 and f(p(14)) 0.567157
  • p(15) 0.567157 and f(p(15)) 0.567135
  • p(16) 0.567135 and f(p(16)) 0.567148
  • p(17) 0.567148 and f(p(17)) 0.567141
  • p(18) 0.567141 and f(p(18)) 0.567145

18
Simulation problem
  • Sic Bo is a game of chance that uses three dice.
    The simplest bet is to predict a face that will
    appear on at least one of the dice. If your
    prediction is correct you receive your bet amount
    for each die that has your prediction. If your
    prediction is wrong you lose your bet.
  • For example, if you bet 1 on three
  • No die shows a three, you lose your 1
  • One die shows a three, you get back your 1
  • Two dice show a three, you get your 1 back plus
    1
  • All three dice show a three, you get your 1 back
    plus 2
  • It can be shown that your winnings per game is
    (some thought is required here)

19
Simulation problem
While this solution can be derived, it can be
approximated by simulating a number of games. The
simulation of many games is very quick when using
a computer.
  • include ltiostreamgt
  • include ltcstdlibgt
  • include ltctimegt
  • using namespace std
  • const int NUM_GAMES 100000
  • int main()
  • int dice1, dice2, dice3
  • int predict
  • int winnings 0
  • int i
  • // initialize the random generator using the
    current time
  • srand(time(NULL))

20
Simulation problem
  • for (i 0 i lt NUM_GAMES i)
  • // rand() returns a number between 0 and
    RAND_MAX
  • predict rand()6 1
  • dice1 rand()6 1
  • dice2 rand()6 1
  • dice3 rand()6 1
  • winnings-- // minus one for betting
  • if ( (predict dice1) ) // plus one for
    every correct prediction
  • winnings
  • if ( (predict dice2) )
  • winnings
  • if ( (predict dice3) )
  • winnings

21
Simulation problem
Running this program a few times yields the
following results.
  • On average you will win -50094/100000 -0.50094
    per game
  • On average you will win -50180/100000 -0.5018
    per game
  • On average you will win -49861/100000 -0.49861
    per game
  • On average you will win -49986/100000 -0.49986
    per game
  • On average you will win -50187/100000 -0.50187
    per game
Write a Comment
User Comments (0)
About PowerShow.com