24
CMPUT101 Introduction t o Computing (c) Jia You, Vadim Bulitko, Yn gvi Bjornsson 1 An Introduction to An Introduction to System Software and System Software and Virtual Machines Virtual Machines Chapter 6.1-6.3 Chapter 6.1-6.3 Topics: Topics: System Software System Software Assemblers and Assembly Assemblers and Assembly Language Language

CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 1

An Introduction to System An Introduction to System Software and Virtual Software and Virtual

MachinesMachinesChapter 6.1-6.3Chapter 6.1-6.3

Topics:Topics:System SoftwareSystem Software

Assemblers and Assembly LanguageAssemblers and Assembly Language

Page 2: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 2

von Neumannvon Neumann

ArchitectureArchitecture

von Neumannvon Neumann

ArchitectureArchitecture

Page 3: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 3

The Naked MachineThe Naked Machine• Difficult to use:Difficult to use:

Store program in RAMStore program in RAMPut address of first instruction in PC, ...Put address of first instruction in PC, ...

• Difficult to program:Difficult to program:• Machine language instructions look like: Machine language instructions look like: 1010000 ...1010000 ...

Page 4: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 4

User InterfacesUser Interfaces• User interfaces User interfaces

– Hide the details of hardware (users require no in-Hide the details of hardware (users require no in-depth knowledge of hardware), thus, allow easy depth knowledge of hardware), thus, allow easy access to the hardware resources.access to the hardware resources.

• Use all the time in our daily life, e.g.:Use all the time in our daily life, e.g.:– Dashboard in a carDashboard in a car

– Control of a stereo/VCRControl of a stereo/VCR

– Punch keys on a microwavePunch keys on a microwave

Page 5: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 5

System SoftwareSystem Software

• System softwareSystem software provides us with an simpler provides us with an simpler interface to operate and program the computer:interface to operate and program the computer:– Is a collection of programs that manage the resources Is a collection of programs that manage the resources

of the computer, and act as an intermediary between of the computer, and act as an intermediary between the user and the computer.the user and the computer.

– Hide the details of the Von Neumann architectureHide the details of the Von Neumann architecture– Present information in understandable wayPresent information in understandable way– Allow user to access the hardware resources in a Allow user to access the hardware resources in a

simple, safe, and efficient way.simple, safe, and efficient way.

Page 6: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 6

Virtual MachineVirtual Machine• The The servicesservices (interface) provided by the system (interface) provided by the system

software is what the user sees, that environment is software is what the user sees, that environment is called, a called, a virtual machinevirtual machine (or virtual environment). (or virtual environment).

System SoftwareSystem Software

HardwareHardwareHardwareHardware

Virtual machine interfaceVirtual machine interface

Page 7: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 7

Typical System SoftwareTypical System Software• Language translatorsLanguage translators

– Assemblers, compilers.Assemblers, compilers.• Memory managersMemory managers

– Allocate space and load programs into memory.Allocate space and load programs into memory.• File systemsFile systems

– Storage/Retrieval of information from mass-storage devicesStorage/Retrieval of information from mass-storage devices• SchedulerScheduler

– Schedules the order of execution of programs.Schedules the order of execution of programs.• UtilitiesUtilities

– E.g. text editors.E.g. text editors.

Page 8: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 8

Using the MachineUsing the Machine

• We want to write and run a program:We want to write and run a program:– Use a Use a text editortext editor to create the program. to create the program.– Store the file on the Store the file on the file systemfile system..– Use a Use a language translatorlanguage translator (compiler) to translate (compiler) to translate

program into machine code.program into machine code.– Memory managerMemory manager, or loader, allocates space and , or loader, allocates space and

loads program into memory (RAM).loads program into memory (RAM).– SchedulerScheduler, executes the program., executes the program.

• We are interacting with the system software!We are interacting with the system software!

Page 9: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 9

Programming the MachineProgramming the Machine

• Algorithms/Programs must be translated into Algorithms/Programs must be translated into machine-code before they can run on the computer:machine-code before they can run on the computer:

Pseudo-codePseudo-codeProgrammingProgramming

LanguageLanguage

Machine CodeMachine Code

T1T1

T2T2T1:T1: by a programmer by a programmerT2:T2: by a computer program by a computer program

Page 10: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 10

Programming the MachineProgramming the Machine

• Instead of writing in machine code (Instead of writing in machine code (yuck!yuck!) we ) we can write our programs using a more "friendly" can write our programs using a more "friendly" programming language:programming language:– Assembly language (learn now)Assembly language (learn now)– C++ (learn later)C++ (learn later)

• System software provides us with software tools System software provides us with software tools to translate programs into machine code:to translate programs into machine code:– AssemblerAssembler– CompilerCompiler

Page 11: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 11

Assembly LanguageAssembly Language

• Similar instruction as in machine-code, except:Similar instruction as in machine-code, except:– Can use symbolic names for instructions, addressesCan use symbolic names for instructions, addresses– Values can be stated as decimalValues can be stated as decimal– Can use comments Can use comments

• Much simpler to use, for example, instead ofMuch simpler to use, for example, instead of 0001 0000010010010001 000001001001

we can writewe can write LOAD A -- Load value of variable A into registerLOAD A -- Load value of variable A into register

Page 12: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 12

Assembly Instruction Assembly Instruction FormatFormat

• Labels are used to mark the location of:Labels are used to mark the location of:– Instruction we need to JUMP to.Instruction we need to JUMP to.– Memory locations (variables) we want to refer to.Memory locations (variables) we want to refer to.

• Op-code mnemonicsOp-code mnemonics– The instructions in the computer instruction set.The instructions in the computer instruction set.

• Address fieldAddress field– The address the instruction works with, or more The address the instruction works with, or more

typically, a label indicating the address.typically, a label indicating the address.

Label:Label: Op-code mnemonicOp-code mnemonic Address fieldAddress field

Page 13: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 13

Instruction Set for Our Von Instruction Set for Our Von Neumann MachineNeumann Machine

Opcode MnemonicOpcode Mnemonic AddressAddress MeaningMeaningLOAD X CON(X) --> RSTORE X R --> CON(X)CLEAR X 0 --> CON(X)ADD X R + CON(X) --> RINCREMENT X CON(X) + 1 --> CON(X)SUBTRACT X R - CON(X) --> RDECREMENT X CON(X) - 1 --> CON(X)COMPARE X If CON(X) > R then GT = 1 else 0

If CON(X) = R then EQ = 1 else 0

If CON(X) < R then LT = 1 else 0JUMP X Get next instruction from memory location XJUMPGT X Get next instruction from memory loc. X if GT=1JUMPxx X xx = LT / EQ / NEQIN X Input an integer value and store in XOUT X Output, in decimal notation, content of memory loc. XHALT Stop program execution

Page 14: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 14

Additional FormatAdditional Format

• In addition to the aforementioned instructions, In addition to the aforementioned instructions, we use three pseudo instructions (do not we use three pseudo instructions (do not generate any machine-code):generate any machine-code):– .BEGIN.BEGIN indicates beginning of programindicates beginning of program– .END.END indicates end of programindicates end of program– .DATA.DATA reserves memory for a data valuereserves memory for a data value

• Can include comments, by using Can include comments, by using ----..– LOAD A -- This is a comment! LOAD A -- This is a comment!

Page 15: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 15

Typical Assembly Program Typical Assembly Program StructureStructure

.BEGIN.BEGIN -- Beginning of program-- Beginning of program

...... -- Machine instructions-- Machine instructionsLabel:Label:

......HALTHALT -- Stop program-- Stop program

A:A: .DATA.DATA -- Data declaration-- Data declaration......

...... .DATA.DATA.END.END -- End of program-- End of program

Page 16: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 16

Practice Question #1Practice Question #1• Write an assembly program that reads in 2 numbers, adds Write an assembly program that reads in 2 numbers, adds

them together, and outputs their sum (algorithm given below).them together, and outputs their sum (algorithm given below).

Get values for A and BGet values for A and BSet the value of C to (A+B)Set the value of C to (A+B)Print the value of CPrint the value of CStopStop

Page 17: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 17

.BEGIN.BEGINININ AA -- Get values for A and B-- Get values for A and BININ BBLOADLOAD AA -- Set the value of C to (A + B)-- Set the value of C to (A + B)ADDADD BBSTORE STORE CCOUT OUT CC -- Print the value of C-- Print the value of CHALTHALT -- Stop-- Stop

A:A: .DATA.DATA 00 -- Reserving memory for variables-- Reserving memory for variablesB:B: .DATA.DATA 00 -- A, B, and C.-- A, B, and C.C:C: .DATA.DATA 00

.END.END

Page 18: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 18

Practice Question #2Practice Question #2• Write an assembly program that reads in 5 numbers Write an assembly program that reads in 5 numbers

and prints out their sum (algorithm given below):and prints out their sum (algorithm given below):

Set the value of Sum to 0Set the value of Sum to 0Set the value of i to 1Set the value of i to 1While i <= 5 do While i <= 5 do Get a value for NGet a value for N Set the value of Sum to (Sum + N)Set the value of Sum to (Sum + N) Add 1 to iAdd 1 to iEnd of loopEnd of loopPrint the value of Sum Print the value of Sum StopStop

Page 19: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 19

.BEGIN.BEGINCLEARCLEAR SumSum -- Set the value of Sum to 0-- Set the value of Sum to 0LOADLOAD OneOne -- Set the value of i to 1-- Set the value of i to 1STORE STORE ii

Loop:Loop: LOADLOAD FiveFive -- While i <= 5 do-- While i <= 5 doCOMPARECOMPARE iiJUMPGTJUMPGT EndloopEndloopININ NN -- Get the value of N-- Get the value of NLOADLOAD SumSum -- Set Sum to (Sum + N)-- Set Sum to (Sum + N)ADDADD NNSTORE STORE SumSumINCREMENTINCREMENT ii -- Add 1 to i-- Add 1 to iJUMPJUMP LoopLoop -- End of loop-- End of loop

Endloop:Endloop: OUTOUT SumSum -- Print the value of Sum-- Print the value of SumHALTHALT -- Stop-- Stop

Sum:Sum: .DATA.DATA 00 -- Reserve memory for variables.-- Reserve memory for variables.i:i: .DATA.DATA 00N:N: .DATA.DATA 00One:One: .DATA.DATA 11 -- Constant 1-- Constant 1Five:Five: .DATA.DATA 55 -- Constant 5-- Constant 5

.END.END

Page 20: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 20

Practice Question #3Practice Question #3• Write an assembly program that reads in 2 Write an assembly program that reads in 2

numbers, and prints out the larger of the two numbers, and prints out the larger of the two (algorithm given below):(algorithm given below):

Get values for A and BGet values for A and BIf A >= B thenIf A >= B then Print the value of APrint the value of AElseElse Print the value of BPrint the value of BStopStop

Page 21: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 21

.BEGIN.BEGINININ AA -- Get values for A and B-- Get values for A and BININ BBLOADLOAD BBCOMPARECOMPARE AA -- If A >= B then-- If A >= B thenJUMPLTJUMPLT ElseElseOUTOUT AA -- Print the value of A-- Print the value of AJUMPJUMP EndifEndif

Else:Else: OUTOUT BBEndif:Endif: HALTHALTA:A: .DATA.DATA 00 -- Reserve memory for variables.-- Reserve memory for variables.B:B: .DATA.DATA 00

.END.END

Page 22: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 22

TranslationTranslation

• An An assemblerassembler translates assembly programs into translates assembly programs into machine code.machine code.

– Converts symbolic op-codes to binary. Converts symbolic op-codes to binary. • Simply a table-lookup.Simply a table-lookup.

– Converts symbolic addresses to binary. Two passes:Converts symbolic addresses to binary. Two passes:1.1. Establishing bindings between labels and addressesEstablishing bindings between labels and addresses2.2. Convert references to labels to binary according to bindings.Convert references to labels to binary according to bindings.

• The resulting file with the machine code is called an The resulting file with the machine code is called an object fileobject file..

Page 23: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 23

Translation, Build BindingsTranslation, Build BindingsProgram Location Counter BindingsProgram Location Counter Bindings .BEGIN.BEGIN Labels addr’s Labels addr’sLoop: IN X Loop: IN X 0 Loop 00 Loop 0 LOAD X LOAD X 1 Done 51 Done 5 COMPARE Y COMPARE Y 2 X 72 X 7 JUMPLT Done JUMPLT Done 33 JUMP Loop JUMP Loop 44Done : OUT Y Done : OUT Y 55 HALT HALT 66X: .DATA 0 X: .DATA 0 77 .END.END

Page 24: CMPUT101 Introduction to Computing(c) Jia You, Vadim Bulitko, Yngvi Bjornsson1 An Introduction to System Software and Virtual Machines Chapter 6.1-6.3

CMPUT101 Introduction to Computing (c) Jia You, Vadim Bulitko, Yngvi Bjornsson 24

LOADINGLOADING

By a program called By a program called loader loader whichwhich

• reads instructions of an object program into RAMreads instructions of an object program into RAM

• places the address of first instruction to Program places the address of first instruction to Program

Counter (PC) to initiate execution.Counter (PC) to initiate execution.