Lec #8: Analysis of Algorithms (Part 2) + Assembly Language

Lec #8: Analysis of Algorithms (Part 2) + Assembly Language

Lec #8: Analysis of Algorithms (part 2) + Assembly Language 1 Analysis of Algorithms (part 2) (45 min) ◮ Quick review from last time ◮ Generating best and worst case expressions for insertion sort. ◮ Take away message (for now) 2 Assembly Language (45 min) ◮ What is a computer? ◮ NIC and NAS ◮ Examples Quick review of Algorithms ◮ Computational problem/algorithms/instances ◮ Running examples: ◮ Finding a maximum element in an array of n integers ◮ Finding the index of string (if it exists) in a sorted array of n strings ◮ Sorting an array of n numbers from least to greatest. ◮ “Finding the visible buildings in a row” ◮ Correctness/efficiency ◮ general cost: T(n) ◮ best case cost: B(n) ◮ worst case cost: W(n) Analyzing Insertion Sort Let us look at the analysis of insertion sort in Cormen, et. al. Best and Worst Time Complexity for Insertion Sort B(n)= W(n)= Big-O Let T (n) and f (n) be positive functions. When we write T (n)= O(f (n)) we say that T (n) is ’ordo’ f (n). This means there exists the positive constants c and n0 such that T (n) ≤ c · f (n) for all n > n0. Let’s do an example: T (n)= .5(n2) + 200(n + 6) ◮ is T (n)= O(n)? (No!) ◮ is T (n)= O(n2)? (Yes!) ◮ is T (n)= O(n3)? (Yes!) Let’s show why exactly! Big-Ω Let T (n) and f (n) be positive functions. When we write T (n)=Ω(f (n)) we say that T (n) is ’omega’ f (n). This means there exists the positive constants c and n0 such that T (n) ≥ c · f (n) for all n > n0. Big-Θ When we write T (n)= θ(f (n)) we say that T (n) is ’theta’ f (n) which means T (n)= O(f (n)) and T (n)=Ω(f (n)) Monotonicity A function f (n) is monotonically increasing if n ≤ m implies f (n) ≤ f (m). We are almost always analyzing cost functions that are monotonically increasing. Take away message ◮ Can apply asymptotic analysis to closed formulas, best or worst case algorithm runs, any algorithm run and even problems. ◮ Doing actual cost calculation for algorithm can be tedious (but in certain cases pays off) ◮ Not all algorithms have a function f (n) where T (n)=Θ(f (n)) ◮ We have not seen algorithms where T (n) has log terms yet, but we will. (Time permitting, a discussion of Binary Search). What is a computer? ◮ the Analytic Engine (Babbage, 1837) ◮ Z3 (Zuse, 1941) ◮ Manchester Baby (1948) CPU + Memory NIC ◮ a simulator (nic) ◮ big endian ◮ 2-byte words, 4-bit bytes ◮ 16 2-byte registers ◮ a fixed 4-byte instruction format ◮ words align on 2-byte boundaries ◮ instructions align on 4-byte boundaries ◮ integers in two’s complement format ◮ an assembler (nas) Assembly Language Instruction Set for NIC Instr Code Operands Description halt 0 000 Halt execution. load 1 rxy Load register r with the bit pattern found in the memory cell whose address is xy. loadc 2 rxy Load register r with bit pattern xy. loadr 3 0st Load register s with the bit pattern found in the memory cell whose address is stored in register t. store 4 rxy Store the bit pattern found in register r in the memory cell whose address is xy. storer 5 0st Store the bit pattern found in register s in the memory cell whose address is stored in register t. move 6 0st Copies the bit pattern found in register s to register t. Instruction Set for NIC (part 2) add 7 rst Add the integers in registers s and t and place the result in register r. addc 8 rxy Add the integer xy to register r. mul 9 rst Multiply the integers in registers s and t and place the result in register r. sub a rst Subtract the integer in register t from the integer in register s and place the result in register r. shift b rst Shift the bit pattern in register s the number of positions given by the integer stored in t and place the result in register r. Positive integers shift to the right, negative to the left. and c rst Do a bit-wise logical and on the patterns in registers s and t and place the result in register r. or d rst Do a bit-wise logical or on the patterns in registers s and t and place the result in register r. xor e rst Do a bit-wise logical xor on the patterns in registers s and t and place the result in register r. Instruction Set for NIC (part 3) jump f 0xy Jump to the instruction in memory cell xy. jumpe f rxy Jump to the instruction in the memory cell at address xy if the bit pattern in register r is equal to the bit pattern in register r0. jumpn f rxy+01b Jump to the instruction in the memory cell at address xy if the bit pattern in register r is not equal to the bit pattern in register r0. jumpl f rxy+10b Jump to the instruction in the memory cell at address xy if the integer in register r is less than the integer in register r0. jumple f rxy+11b Jump to the instruction in the memory cell at address xy if the integer in register r is less than or equal to the integer in register r0. noop f 001 Do nothing. An Example Assembly code (in loop.as) // Writes ff, fe, ..., 2, 1, 0 to memory at address n. word n loadc r0 -1 loadc r1 -1 Loop: store r1 n // write r1 to memory address n addc r1 -1 // r1-- jumpn r1 Loop // if r1 != -1 goto Loop n is set to the address immediately at the end of address space of the program. Binary representation (in loop.bi) 1f1f1f1f20ff21ff411c81fff1090000f00000 Let’s grind this example! Contact (Sagan, 1996): Operating somewhere in the Vega System Assembly Code // beats out the primes on register 9 loadc r3 n loadc r0 0 word n loadr r1 r3 Oloop: noop repeat: loadc r2 2 Loop: addc r1 -1 // r1-- store r2 n loadc r9 0 loadc r2 3 loadc r9 -1 // Thump! store r2 n+2 loadc r9 0 loadc r2 5 jumpn r1 Loop // if r1 != -1 goto Loop store r2 n+4 loadc r2 7 noop store r2 n+6 noop loadc r2 11 noop store r2 n+8 noop loadc r2 13 noop store r2 n+10 noop loadc r2 17 store r2 n+12 addc r3 2 loadc r2 19 loadr r1 r3 store r2 n+14 jumpn r1 Oloop loadc r2 23 jump repeat store r2 n+16 ... Darn only the first 18 primes... we need the first 26. could we do better?.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us