Upload
kenneth-martin
View
217
Download
2
Tags:
Embed Size (px)
Citation preview
Student Computer Simulation
Barbara Ericson
Georgia Tech
Sept 2005
Operating System - Organizer• Keep track of executing
programs– Give them time with the CPU– A program gets a slice of time
with the CPU• Keep track of memory
– Decide when to move some data to disk (virtual memory)
• Keep track of disk space– Decide where to store stuff
• Interface with the user– Accept input via keyboard and
mouse• Keep track of devices
– USB drives, cameras, etc
Try it Out
• Type ctrl-alt-delete (press all keys at the
same time)– To bring up the task
manager– Click on each of the
tabs• To see the applications
running• To see the processes
that are running• To see how much
memory is used
CPU – Calculator Plus More
• Add, subtract, multiply, divide numbers
• Compare numbers– Characters are encoded as
numbers• Jump to a new instruction
based on the result of a comparison
• Can only work with one program at a time!– Keeps a program counter
which is the address of the currently executing instruction
Memory – Whiteboard
• Holds the currently executing programs and their data
• Clears when the power is turned off
• Much faster than disk• Has addresses for data
based on bytes• Can get data from an
address• Can put data into an
address
Disk – Storage Facility
• Stores data and programs
• Doesn’t clear when the power is turned off
• Much slower than memory
• Much cheaper than memory
Program - Recipe
• Set of instructions to the computer
• Carried out by the CPU
• Accomplishes some task
Computer Startup
• The power is turned on– The Basic Input/Output System (BIOS)
• Loads from a memory chip (ROM) and executes• Initializes the hardware (keyboard, disk drive, mouse, etc)
– Then loads and executes the operating system
• The Operating System waits for user input• The user starts a program
– By double clicking on an icon or file– Or by click on Start->Program->Some Program
Program Execution
• The operating system creates a process for the program and reads it into memory
• It gives it a time slice of the CPU
• It saves the current state of the CPU when it gives another program a time slice of the CPU– Context switch
• It can restore the state of the CPU
Student Computer Simulation
– Operating System – need 1• Keep a list of programs to be run• Tell CPU to run a program (set program counter to memory
address)• Tell CPU to switch to another program
– Central Processing Unit – need 1• Keep track of the contents of Registers A, B, and C and the
program counter• Be able to save current state to memory
– Memory – need 1• Have a list of values for addresses (on board)
– Disk – need 1• Hold original programs and byte code versions
Simulation
• Give the person playing the disk the two programs and the assembler version of the programs
• Have the operating system ask for program 1 to be loaded into memory at address 0
• Have the disk give a copy of the assembler version of program 1 to the memory– And assign an address to the start of it (0)
• Have the operating system tell the CPU to start executing from the memory address of the first program– Have the CPU update the program counter to 0– Ask the memory for the instruction at this address– Execute the instruction and increment the program counter
Simulation – page 2• Have the operating system ask for program 2 to be loaded into
memory at address 64• Have the disk give a copy of program 2 to the memory
– And assign an address to the start of it (64)• Have the operating system tell the CPU to save the current state to
the memory• Ask memory to save the values of the registers and program
counter to 200• Have the operating system tell the CPU to start executing from the
memory address of the second program (64)– Have the CPU update the program counter to 64– Ask the memory for the instruction at this address– Execute the first two instructions and increment the program counter
• Keep doing this till both programs have finished– When a program finishes tell the CPU and it can remove the program
from the list of programs to run
Program 1
// how many pizza slices?
int numPeople = 30;
int numSlicesPerPerson = 2;
int totalSlices = numPeople * numSlicesPerPerson;
int numPizzas = totalSlices / 10;
Compiling
• Computers can’t execute source code– They need to execute bytecodes
• Instructions for the machine the code is running on– 1 might be add A and B and store the result in C
• We compile Java source code Name.java into Name.class– bytecodes for a virtual machine
• Not any particular machine
• Then we execute Java class files– Runs a Java Virtual Machine to interpret the bytecodes– Or compile to native bytecodes using a just-in-time compiler
Assembler
• Low level programming language– Names for instructions instead of numbers
• ADD instead of 1• LOADA instead of 20
• Use an assembler to convert to the bytecodes for a machine
Example AssemblerLOADA mem - Load register A from memory address LOADB mem - Load register B from memory address CONB con - Load a constant value into register B SAVEB mem - Save register B to memory address SAVEC mem - Save register C to memory address ADD - Add A and B and store the result in C SUB - Subtract A and B and store the result in C MUL - Multiply A and B and store the result in C DIV - Divide A and B and store the result in C COM - Compare A and B and store the result in test JUMP addr - Jump to an address JEQ addr - Jump, if equal, to address JNEQ addr - Jump, if not equal, to address JG addr - Jump, if greater than, to address JGE addr - Jump, if greater than or equal, to address JL addr - Jump, if less than, to address JLE addr - Jump, if less than or equal, to address STOP - Stop execution
Program 1 Assembler
0 CONB 30 // put 30 in register B
1 SAVEB 128 // int numPeople = 30;
2 CONB 2 // put 2 in register B
3 SAVEB 132 // int numSlicesPerPerson = 2;
4 LOADA 128 // get value in address 128
5 MUL // multiply a and b and store result in c
6 SAVEC 136 // int totalSlices = numPeople * numSlicesPerPerson;
7 LOADA 136
8 CONB 10
9 DIV // divide a and b and store result in c
10 SAVEC 140 int numPizzas = totalSlices / 10;
11 STOP
Program 2
• // calculate if we have enough material
• int width = 30;
• int length = 50;
• int total = width * height;
Program 2 Assembler
0 CONB 301 SAVEB 160 // (int width = 30)2 CONB 503 SAVEB 164 // (int height = 50)4 LOADA 160 // load width5 LOADB 164 // load height6 MUL7 SAVEC 168 // (int total = width * height)8 STOP