Docsity

Introduction to Looping, Testing, and Branching in Assembly Language Programming, Lecture notes of Computer science

Toggle sidebar
bg1
Inside the CPU
• how does the CPU work?
– what operations can it perform?
– how does it perform them? on what kind of data?
– where are instructions and data stored?
• some short, boring programs to illustrate the basics
• a toy machine to try the programs
– a program that simulates the toy machine
– so we can run programs written for the toy machine
• computer architecture: real machines
• caching: making things seem faster than they are
• how chips are made
• Moore's law
• equivalence of all computers
– von Neumann, Turing
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Introduction to Looping, Testing, and Branching in Assembly Language Programming and more Lecture notes Computer science in PDF only on Docsity!

Inside the CPU

  • how does the CPU work?
    • what operations can it perform?
    • how does it perform them? on what kind of data?
    • where are instructions and data stored?
  • some short, boring programs to illustrate the basics
  • a toy machine to try the programs
    • a program that simulates the toy machine
    • so we can run programs written for the toy machine
  • computer architecture: real machines
  • caching: making things seem faster than they are
  • how chips are made
  • Moore's law
  • equivalence of all computers
    • von Neumann, Turing

Block diagram of computer

  • CPU can perform a small set of basic operations - arithmetic: add, subtract, multiply, divide, … - memory access: fetch data from memory, store results back in memory - decision making: compare numbers, letters, …, and decide what to do next according to result - control the rest of the machine
  • operates by performing sequences of very simple

operations very fast

CPU (processor) Memory (RAM) Hard disk mouse (^) keyboard display (and many others) Bus CD/ DVD network /wireless

Toy computer block diagram (non-artist's conception)

keyboard Memory (RAM) accumulator display LOAD STORE ADD

GET

PRINT

CPU

arithmetic, logic, control

A program to print a number

GET get a number from keyboard into accumulator

PRINT print the number that's in the accumulator

STOP

  • convert these instructions into numbers
  • put them into RAM starting at first location
  • tell CPU to start processing instructions at first location
  • CPU fetches GET, decodes it, executes it
  • CPU fetches PRINT, decodes it, executes it
  • CPU fetches STOP, decodes it, executes it

Looping and testing and branching

  • we need a way to re-use instructions
  • add a new instruction to CPU's repertoire:
    • GOTO take next instruction from a specified RAM location instead of just using next location
  • this lets us repeat a sequence of instructions indefinitely
  • how do we stop the repetition?
  • add another new instruction to CPU's repertoire:
    • IFZERO if accumulator value is zero, go to specified location instead of using next location
  • these two instructions let us write programs that repeat instructions until a specified condition becomes true
  • the CPU can change the course of a computation according to the results of previous computations

Add up a lot of numbers and print the sum

Start GET get a number from keyboard IFZERO Show if number was zero, go to "Show" ADD Sum add Sum so far to new number STORE Sum store it back in Sum so far GOTO Start go back to "Start" to get the next number Show LOAD Sum load sum into accumulator PRINT print result STOP Sum 0 initial value set to 0 before program runs (by assembler)

A simulator for the toy computer

  • simulator (a program) reads a program written for the toy computer
  • simulates what the toy computer would do
  • toy machine's instruction repertoire: get read a number from the keyboard into accumulator print print contents of accumulator load Val load accumulator with Val (which is unchanged) store Lab store contents of accumulator into location labeled Lab add Val add Val to accumulator sub Val subtract Val from accumulator goto Lab go to instruction labeled Lab ifpos Lab go to instruction labeled Lab if accumulator positive (>= 0) ifzero Lab go to instruction labeled Lab if accumulator is zero stop stop execution Num initialize this memory location to numeric value Num (once, before simulation starts) if Val is a name like Sum, it refers to a memory location with that label; if Val is a number like 17, that value is used literally

Summary

  • each memory location holds an instruction or a data value (or part)
  • instructions are encoded numerically (so they look the same as data)
    • e.g., GET = 1, PRINT = 2, LOAD = 3, STORE = 4, …
  • can't tell whether a specific memory location holds an instruction or a data value (except by context) - everything looks like numbers
  • CPU operates by a simple cycle
    • FETCH: get the next instruction from memory
    • DECODE: figure out what it does
    • EXECUTE: do the operation move operands between memory and accumulator, do arithmetic, etc.
    • go back to FETCH

Computer architecture

  • what instructions does the CPU provide?
    • CPU design involves complicated tradeoffs among functionality, speed, complexity, programmability, power consumption, …
    • Intel and ARM are unrelated, totally incompatible Intel: lot more instructions, many of which do complex operations e.g., add two memory locations and store result in a third ARM: fewer instructions that do simpler things, but faster e.g., load, add, store to achieve same result
  • how is the CPU connected to the RAM and rest of machine?
    • memory is the real bottleneck; RAM is slow (25-50 nsec to fetch) modern computers use a hierarchy of memories (caches) so that frequently used information is accessible to CPU without going to memory
  • what tricks do designers play to make it go faster?
    • overlap fetch, decode, and execute so several instructions are in various stages of completion (pipeline)
    • do several instructions in parallel
    • do instructions out of order to avoid waiting
    • multiple "cores" (CPUs) in one package to compute in parallel
  • speed comparisons are hard, not very meaningful

Caching: making things seem faster than they are

  • cache: a small very fast memory for recently-used information
    • loads a block of info around the requested info
  • CPU looks in the cache first, before looking in main memory
    • separate caches for instructions and data
  • CPU chip usually includes multiple levels of cache
    • faster caches are smaller
  • caching works because recently-used info is more likely to be used again soon - therefore more likely to be in the cache already
  • cache usually loads nearby information at the same time
    • nearby information is more likely to be used soon
    • therefore more likely to be in the cache when needed
  • this kind of caching is invisible to users
    • except that machine runs faster than it would without caching

Caching is a much more general idea

  • things work more efficiently if what we need is close
  • if we use something now
    • we will likely use it again soon (time locality)
    • or we will likely use something nearby soon (space locality)
  • other caches in computers:
    • CPU registers
    • cache(s) in CPU
    • RAM as a cache for disk or network or …
    • disk as a cache for network
    • network caches as a cache for faraway networks
    • caches at servers
  • some are automatic (in hardware), some are controlled by software, some you have some control over

Other kinds of computers

  • not all computers are PCs or Macs
  • "supercomputers"
    • usually large number of fairly standard processors
    • extra instructions for well-structured data
  • "distributed" computing
    • sharing computers and computation by network
    • e.g., web servers
  • embedded computers
    • phones, games, music players, ...
    • cars, planes, weapons, ...
  • each represents some set of tradeoffs among cost, computing power, size, speed, reliability, ...

Moore's Law (1965, Gordon Moore, founder & former CEO of Intel)

  • computing power (roughly, number of transistors on a chip)
    • doubles about every 18 months
    • and has done so since ~
  • consequences
    • cheaper, faster, smaller, less power consumption per unit
    • ubiquitous computers and computing
  • limits to growth
    • fabrication plants now cost $2-4B; most are outside US
    • line widths are nearing fundamental limits
    • complexity is increasing
  • maybe some other technology will come along
    • atomic level; quantum computing
    • optical
    • biological: DNA computing

Turing machines

  • **Alan Turing ***
  • showed that a simple model of a computer was universal
    • now called a Turing machine
  • all computers have the same computational power
    • i.e., they can compute the same things
    • though they may vary enormously in speed, memory, etc.
  • equivalence proven / demonstrated by simulation
    • any machine can simulate any other
    • a "universal Turing machine" can simulate any other Turing machine
  • see also
    • Turing test
    • Turing award