Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC.

Slides:



Advertisements
Similar presentations
RAM (cont.) 220 bytes of RAM (1 Mega-byte) 20 bits of address Address
Advertisements

Chapter 2 Data Manipulation Dr. Farzana Rahman Assistant Professor Department of Computer Science James Madison University 1 Some sldes are adapted from.
 Suppose for a moment that you were asked to perform a task and were given the following list of instructions to perform:
Instruction Set Architecture
Processor System Architecture
Our First Real System.
TK 2633 Microprocessor & Interfacing
Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.
1 Foundations of Software Design Lecture 3: How Computers Work Marti Hearst Fall 2002.
Traffic Light Behavior IF A=1 AND B=0 Car Sensors B A.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Programming November 13, Administrivia From Thu Nov 8 12:05: Date: Thu, 8 Nov :04: (EST) From:
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Computer Processing CSCE 110 J. Michael Moore.
Midterm Review October 23, 2001.
Chapter 10-Arithmetic-logic units
Chapter 4 Processor Technology and Architecture. Chapter goals Describe CPU instruction and execution cycles Explain how primitive CPU instructions are.
Chapter 2.2 Machine Language.
Henry Hexmoor1 Chapter 10- Control units We introduced the basic structure of a control unit, and translated assembly instructions into a binary representation.
EET 2261 Unit 2 HCS12 Architecture
Counters and Registers
Assembly & Machine Languages
Lecture 18 Last Lecture Today’s Topic Instruction formats
Lecture 3. Diff b/w RAM and Registers Registers are used to hold data immediately applicable to the operation at hand Registers are used to hold data.
1 Machine Language Alex Ostrovsky. 2 Introduction Hierarchy of computer languages: 1. Application-Specific Language (Matlab compiler) 2. High-Level Programming.
CS1Q Computer Systems Lecture 9 Simon Gay. Lecture 9CS1Q Computer Systems - Simon Gay2 Addition We want to be able to do arithmetic on computers and therefore.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Finite State Machines (FSMs) and RAMs and inner workings of CPUs COS 116, Spring 2010 Guest: Szymon Rusinkiewicz.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Finite State Machines (FSMs) and RAMs and CPUs COS 116, Spring 2011 Sanjeev Arora.
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
CS 111 – Sept. 15 Chapter 2 – Manipulating data by performing instructions “What is going on in the CPU?” Commitment: –Please read through section 2.3.
Important Concepts  Parts of the CPU  Arithmetic/Logic Unit  Control Unit  Registers  Program Counter  Instruction Register  Fetch/Decode/Execute.
1 i206: Lecture 4: The CPU, Instruction Sets, and How Computers Work Marti Hearst Spring 2012.
Data Manipulation CSC Overview  Computer Architecture  Machine Language  Computer Architecture  Machine Language.
A summary of TOY. 4 Main Components Data Processor Control Processor Memory Input/Output Device.
Register Transfer Languages (RTL)
Arithmetic-logic units1 An arithmetic-logic unit, or ALU, performs many different arithmetic and logic operations. The ALU is the “heart” of a processor—you.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
Computers. Light A Review: State Machine IF A=1 AND B=0 Always IF A=0 AND B=1 Otherwise Light B Otherwise Always.
What is a program? A sequence of steps
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
CMSC 104, Lecture 061 Stored Programs A look at how programs are executed.
Chapter 2 Data Manipulation © 2007 Pearson Addison-Wesley. All rights reserved.
Control units In the last lecture, we introduced the basic structure of a control unit, and translated our assembly instructions into a binary representation.
Computer Organization 1
Instruction Sets: Characteristics and Functions  Software and Hardware interface Machine Instruction Characteristics Types of Operands Types of Operations.
The Universal Machine (UM) Implementing the UM Noah Mendelsohn Tufts University Web:
Logic Gates Dr.Ahmed Bayoumi Dr.Shady Elmashad. Objectives  Identify the basic gates and describe the behavior of each  Combine basic gates into circuits.
CPU (Central Processing Unit). The CPU is the brain of the computer. Sometimes referred to simply as the processor or central processor, the CPU is where.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Lecture 07 – October 11, 2001 Programming our toy machine Review of hardware Storing images and sounds Compression Administrivia –send questions to
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Dr.Ahmed Bayoumi Dr.Shady Elmashad
Assembly Language Programming of 8085
A Closer Look at Instruction Set Architectures: Expanding Opcodes
Starter Read the Feedback Click on Add new Feedback Open Realsmart
Processor (I).
The Processor and Machine Language
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
ECEG-3202 Computer Architecture and Organization
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Branch instructions We’ll implement branch instructions for the eight different conditions shown here. Bits 11-9 of the opcode field will indicate the.
ECEG-3202 Computer Architecture and Organization
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
Control units In the last lecture, we introduced the basic structure of a control unit, and translated our assembly instructions into a binary representation.
Instructions in Machine Language
Review: The whole processor
Presentation transcript:

Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC (Mac) DragonBall (Palm Pilot) StrongARM/MIPS (WinCE) Many Others (specialized or general-purpose) They represent instructions differently in their assembly/machine languages (even common ones) Let’s look instructions for a simple example CPU

An Example CPU We’ll look at a “toy” CPU’s Machine Language Our CPU has: 8 Registers – each holds 16 bits (2 bytes)

Our Example CPU We’ll look at a toy CPU’s Machine Language: Our CPU has: 8 Registers – each holds 16 bits (2 bytes) Our RAM: Reads and writes (loads and stores) in blocks of 16 bits (2 bytes) Has 2 8 = 256 addresses Total Memory: 256*2 = 512 bytes

Writing it down We are going to be looking at lots of 16-bit sequences. E.g It can be tiring/confusing to read so many bits. So we use Hexadecimal representation of data and instructions

Recall Hexadecimal ABCDEF89ABCDEF

Hex Shorthand e.g ABCDEF89ABCDEF

Hex Shorthand e.g ABCDEF89ABCDEF C A 5

Machine “Core” Everything is in binary (hex) Registers R0: 0000 R1: 0CA8 R2: A9DB R3: 0705 R4: 1011 R5: 90A0 R6: 0807 R7: 00A0 00: : : C: : B106B200B … F8: FC: Memory

Representing Instructions Machine instructions will also be represented by 16 bits. We’ll divide those bits up into groups: The first 4 bits are called the Op-Code: Tells what type of instruction it is. How many possibilities does 4 bits give us? 6 C A 5

Instructions 16 possible Op-Codes: We’ll only look at a few in detail… 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left

Halt Instruction Opcode 0 : Halt This just tells the machine to stop. Other 12 bits are ignored. So: All have same effect. 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left F F F 0 9 A C

Data Instructions Opcode B : Load Direct / Address Sets a Register to a fixed Specified Value Encoding: Effect: Register A becomes the specified value 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left B regA value (8 bits)

Arithmetic/Logic Instructions Opcode 1 : Add Adds contents of two registers together and puts result in third register Encoding: Effect: Register A becomes Register B + Register C 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 1 regA regB regC

Adding Numbers Simple Program to calculate = 21 = 15 (hex) 10: Load R0  01 (always 1) 11: Load R2  00 (running total) 12: Load R1  01 (current number) 13: Add R2  R2 + R1 (R2=1) 14: Add R1  R1 + R0 (R1=2) 15: Add R2  R2 + R1 (R2=3) 16: Add R1  R1 + R0 (R1=3) 17: Add R2  R2 + R1 (R2=6) 18: Add R1  R1 + R0 (R1=4) 19: Add R2  R2 + R1 (R2=A) 1A: Add R1  R1 + R0 (R1=5) 1B: Add R2  R2 + R1 (R2=F) 1C: Add R1  R1 + R0 (R1=6) 1D: Add R2  R2 + R1 (R2=15) 1E: halt Algorithm: Initialize the ‘current number’ to 1. Keep incrementing the ‘current number’ by 1 until it reaches 6, and keep adding it to a running total

Arithmetic/Logic Instructions Opcode 2 : Subtract Similar to Add… Encoding: Effect: Register A gets the value of Register B - Register C 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 2 regA regB regC

Control Instructions Opcode 6 : Jump if Positive Jump to a different place in memory if Register is > 0 Encoding: Effect: If Register A > 0, Go To Instruction at specified address (I.e. change IP to address) 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 6 regA address (8 bits)

How to Simplify the Program Wrote the same instruction pairs 6 times What if we were adding from 1 to 15000? Can we reduce this? Solution: Loops Implemented with jump (or branch) instructions Conditionally change IP to jump back to earlier point in program

Adding Numbers Revisited Use a Loop, with a simple jump instruction, to calculate … + N 10: Load R1  0006 (N is 6) 11: Load R2  0000 (running total) 12: Load R0  0001 (always 1) 13: Add R2  R2 + R1 (add in N) 14: Sub R1  R1 - R0 (N = N-1) 15: Jump to 13 if (R1>0) (If N isn’t 0 yet, go back) 16: halt (N is now 0, and R2 = 1+2+…+N) Notice: Decrements, instead of incrementing, current number

Advanced Control Instructions Opcode 7 : Jump and count (backwards) Jump to a different place in memory if Register value is > 0, AND decrement Register by 1 Encoding: Effect: If Register A > 0, Go To Instruction at specified address and set A = A - 1 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 7 regA address (8 bits)

Adding Numbers Revisited Alternate Loop using Jump & Count for … + N 10: Load R1  0006 (N) 11: Load R2  0000 (running total) 12: Add R2  R2 + R1 (add in N) 13: If (R1>0), Jump to 12 (If N isn’t 0 yet, and decrease R1 Let N=N-1, and go back) 14: halt (N is now 0, and R2 = 1+2+…+N) No need for R0, which stored increment/decrement ‘size’ of 1, and no need for subtract instruction: these are incorporated in Jump&Count instruction

Memory Instructions Opcode 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load data from RAM at specified address into Register A 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA address (8 bits)

Memory Instructions Opcode A : Store (into memory) Store Register into Memory Encoding: Effect: Store contents of Register A into memory at specified address 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left A regA address (8 bits)

Memory Instructions: Indirect Opcode 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load from RAM at address [B+C] into Register A 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA regB regC

How can CPU tell these apart? Sneaky trick: CPU has 8 Registers, so only need 3 bits to specify Register A Use extra bit to tell which type of LOAD Memory Instructions: Indirect 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA regB regC 9 regA address (8 bits)

This is called a HACK! Hacks are terrible! (not all) Generally should be avoided. Sadly, they are everywhere. Some people get perverse pleasure out of hacks. Hacks are a major source of bugs! Hacks 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left

A Virus! Copies itself on top of other programs! 10: Load R1  0006 (Length of Virus) 11: Load R2  001F (Memory Location to copy self To -1) 12: Load R3  000F (Memory Location to copy self From -1) 13: Load R0  Address[R3+R1] (Load last instruction from source) 14: Store Address[R2+R1]  R0 (Store to last instr in destination) 15: If (R1>0), Jump to 13 (If haven’t copied whole virus, and decrease R1 decrease R1, and go back) 16: halt (Virus has replicated)... ______________________________________________________________ 20:.... (Program about to be destroyed.) 21:

We have looked at a typical Machine Language and how instructions are represented. Halt Instruction Data Instructions Arithmetic/Logic Instructions Control Instructions Memory Instructions Summary

We saw a few simple examples of programs you can write in Assembly Language. You now have a pretty solid understanding of how computers really work ! But you don’t want to write programs in Assembly Language Modern programming. Writing programs in high- level languages. Start with traffic light example, in software rather than in hardware Summary (cont.)

Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that your rules are complete Translate the rules into the computer language –Build structures to hold your data –Build tools to manipulate the structures Make sure that the program does what the rules say

Elements of Programming We looked at machine language –Single instructions that tell the hardware what to do –Primitive Arithmetic, simple branching, communication with memory We built state machines –States using memory –Transitions modeling tasks –A “hardwired” program

Elements of Programming We’ve seen –Truth tables –Logic gates –States and transitions in a state machine –Machine language Now, higher level programming language

To build a computer program Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that your rules are complete Translate the rules into the computer language –Build structures to hold your data –Build tools to manipulate the structures Make sure that the program does what the rules say

Figuring out the rules For traffic lights: –We stored data that told us the current color of lights –We read input from sensors –We had rules that told us whether to change state –We had rules that told us how to change state

Light A Traffic Light Behavior IF A=1 AND B=0 Always IF A=0 AND B=1 Otherwise Light B Otherwise Always

Turn Memory, Inputs and Outputs Into Variables Store data to tell current color of lights –Dim LightA, LightB as Integer 0 for red, 1 for yellow, 2 for green Read input from sensors –Dim SensorA, SensorB as Integer tell if cars are waiting

Turn Rules Into Statements Decide whether to change state –If LightA = 0 And LightB = 2 And SensorA = 1 And SensorB = 0 Then here we want to specify that the colors change –If LightA = 2 And LightB= 0 And SensorA = 0 And SensorB = 1 Then again, we want to specify that the colors change

Build shell of program Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) If LightA = 0 and LightB = 2 And SensorA = 1 And SensorB = 0 Then ChangeGreenToYellow(LightB) ChangeYellowToRed(LightB) ChangeRedToGreen(LightA)

Some Rules Statements have to be in blocks How does the computer know that the instructions are If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) And not … If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB)

Some Rules Statements have to be in blocks If LightA = 2 And Light B = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) End If