chapter 2 machine language. machine language the only language a computer can understand directly....

41
Chapter 2 Machine Language

Upload: forrest-bailes

Post on 22-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Chapter 2

Machine Language

Page 2: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Machine language

• The only language a computer can understand directly.

• Each type of computer has its own unique machine language.

• We will study the machine language of the H1 computer.

• This is a simulated computer.

Exercise: Find examples of different machine languages and the computers they run on.

Page 3: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Computer Architecture

• A computer’s machine language and its corresponding assembly language are often seen as the expression of the computer’s architecture because the instructions in these languages determine the physical layout of the computer CPU.

Page 4: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

H1• Simplified model of a modern computer

• Easy to learn

• Simulated by the sim program in the H1 Software Package. sim runs on DOS, Windows, X86 Linux, Sun Sparc Solaris, and Macintosh OS X.

Exercise: Download and install the H1 package from• www.course.com• Select Computer Science• Go to Page 2 and Find Prof. Dos Reis’ book• Select Student Downloads

Page 5: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Central Processing Unit (CPU) of H1

• Heart of H1: central processing unit (CPU).

• The CPU contains computational circuits, control circuits, and registers.

• A register is a special memory area that can hold one 16-bit number.

• The design or arrangement of circuits of the CPU are determined by the instructions it needs to understand.

Exercise: Recall adder design of Chapter 1

Page 6: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Registers on H1• Program counter (pc)

– contains the address of the next instruction to be executed

• Accumulator (ac)– contains the result of the most recent

calculation

• Stack pointer (sp)– points to the area of memory being used by

most recently called function

Page 7: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 8: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Clock

• Generates stream of pulses that determines the speed at which the CPU performs its operation.

• One Hertz is 1 pulse per second.

• One megaHertz is 1 million pulses per second.

• One gigaHertz is 1 billion pulses per second.

How fast are computers today?

Page 9: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

one clock tick

Everything that happens in the CPU happens with the clockkeeping time.

+ve voltage

-ve or 0 voltage

Exercise: What is the name usedto describe the above wave?

Page 10: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Machine language instructions

• Binary numbers

• Tell the CPU what to do

• The only kind of instruction the CPU can understand

• The CPU does NOT understand C++ or Java instructions. Instructions in these languages have to be translated to machine language (compiled).

Page 11: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Main memory

• Holds machine instructions, data, and results.

• CPU gets data and machine instructions from main memory.

• CPU places results into main memory.code

stack

dataheap

low addr

high addr

Page 12: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Data and instructions are movedbetween the CPU and mainmemory on the data bus.

The data bus is a ribbon of parallel wires; bits travel in parallel along adjacent wires

16-bit data bus

bits

Page 13: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Why computers are fast

• The CPU and main memory are all electronic. They have NO moving parts to slow them down.

• As long as main memory can provide the CPU with machine instructions and data fast enough, the CPU can perform computations at an enormous rate.

Exercise: How fast was the first IBM PC? How much faster are current PCs?

Page 14: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Organization of main memory• An indexed array of memory slots• Each slot is one word (16 bits) on the H1. On

most computers, each slot is one byte (8 bits). (word-addressable vs byte-addressable)

• Each slot has an index called its address.• Addresses are sequential integers starting

from 0. • What is in a memory slot is called its contents.

Page 15: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

H1 has 4kwords of memory

contents

Ex: How many bytes?

Page 16: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Be sure to distinguish between the address of a main memory slot and

its contents.

The contents is the binary number in the slot. The address is the

number that identifies or locates that slot.

Page 17: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

I/O devices

• Input devices provide main memory and/or the CPU with data from the outside world.

• Output devices provide information to the outside world from main memory or the CPU.

• Some devices perform both input and output (e.g., a disk drive).

• “I/O” refers to input, output, and input/output devices.

Page 18: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 19: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Location of OS in main memory

Page 20: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Load machine instructionOpcode

0000 000000000100

address (12 bits)

0004 in hex

This instruction loads the ac register from location 4 of main memory.

ac mem

Page 21: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 22: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Some opcodes

• 0000 load ac register from memory

• 0001 store ac register into memory

• 0010 add memory contents to ac register

• 1111111111111111 halt

Page 23: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Add machine instruction

Opcode

0010 000000000101

address (only one argument!!)

2005 in hex

ac = ac + mem

implicit argument

Page 24: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 25: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Store machine instruction

Opcode

0001 000000000110

address

1006 in hex

ac mem

Page 26: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 27: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Halt machine instruction

Opcode (16 bits)

1111111111111111

FFFF in hex

Page 28: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Machine language program

Page 29: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Important reminder

• We use hex numbers as a shorthand representation of binary numbers.

• All the numbers in the computer are in binary.

• The next slide shows what our simple machine language program really looks like as it sits in memory.

Page 30: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language
Page 31: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

sim

• sim is a program that makes your computer act like H1.

• sim runs on DOS, Windows, Sun Sparc Solaris, X86 Linux, and Macintosh OS X.

• sim is better for our purposes than a real H1: With sim we can easily make changes to H1.

• sim has a powerful debugger.

Page 32: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Some commands for sim’s debugger

Page 33: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

When a register changes its contents, sim’s debugger shows its before and after values.

Page 34: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Mnemonics

• A mnemonic is an easy-to-remember representation of an opcode.

• ld mnemonic for the load opcode

• st mnemonic for the store opcode

• add mnemonic for the add opcode

• halt mnemonic for the halt opcode

Page 35: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Debugger’s prompt provides a lot of information

just hit<enter>

Page 36: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

The next slide shows how to use the debugger to enter and run our simple machine language program.

The bold type corresponds to user input.

Page 37: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

enter command – e0

Page 38: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Creating a log file

Page 39: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

sim does not prompt for arguments if they are provided on the command line when sim is first invoked.

Page 40: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Getting help on sim

Enter one of the following:

sim /h sim –h sim /? sim -?

Avoid ‘?’ on Sun, Linux, and OS X

Page 41: Chapter 2 Machine Language. Machine language The only language a computer can understand directly. Each type of computer has its own unique machine language

Getting help on sim’s debugger

Enter one of the following:

h ?

when the debugger is active.