Phillipa Gill phillipa@cs.toronto.edu SCI 199 Y Tutorial Sept. 14, 2009 Phillipa Gill phillipa@cs.toronto.edu
Von Neumann Architecture OS Programs Data Memory Control Unit Arithmetic Logic Unit Accumulator Input Output
General Purpose Machines As opposed to fixed purpose E.g., your calculator is not a word processor or gaming console Machine can be used to solve problems using programs stored in memory
Example Program: Problem Input: a value X, a list A with length N Output: the position in the list where X resides if X is in the list. 0 otherwise. Example: A = 2,4,6,5,10 if X is 6 this program will return 3
Example Program: Code ANSWER=0 I=1 WHILE ANSWER = 0 and I <= N IF X=A(I) then ANSWER = I I=I+1 ENDWHILE OUTPUT ANSWER
Example Program: Memory 1000 X 1001 N 1002 I 1003 ANSWER 2000 A 2001 … 2000 + N-1
Example Program: Memory (2) Write-constant-into-memory 0,1003 % set ANSWER=0 Write-constant-into-memory 1,1002 % set I=1 Get-memory 1001 %put N into accumulator Subtract-memory 1002 % compute N-I Conditional-jump-on-negative 16 %if N-I was negative Get-memory 2000 % get value of A(I) Subtract-memory 1000 % compute A(I) – X Conditional-jump-on-zero 14 % if A(I)-X=0 Get-memory 6 % load word 6 into memory Add-constant 1 % increment address in word 6 Get-memory 1002 % load I
Example Program: Memory (3) Get-memory 6 % load word 6 into memory Add-constant 1 % increment address in word 6 Get-memory 1002 % load I Add-constant 1 % increment I Jump 6 % go back to start of loop Get-memory 1002 % get value of I Put-memory 1003 %put this value into 1003 (answer) Output 1003 % output answer
Issues Code can modify itself! The above code changes the statement: Get-memory 6 % load word 6 into memory Add-constant 1 % increment address in word 6 The above code changes the statement: Get-memory 2000 into Get-memory 2001 Why is this a bad idea? Recent developments have tried to fix this