Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.

Slides:



Advertisements
Similar presentations
Assembly Language – 1.
Advertisements

Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
 Suppose for a moment that you were asked to perform a task and were given the following list of instructions to perform:
Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
Instruction Set Architecture & Design
TK 2633 Microprocessor & Interfacing
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Level ISA3: Information Representation
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language for Intel-Based Computers, 4th Edition
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Binary Arithmetic Math For Computers.
CPS-304 DIGITAL LOGIC & DESIGN Instructor : Ms. Saba Iqbal.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Numbering systems.
How Computers Work Dr. John P. Abraham Professor UTPA.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
4-1 Chapter 4 - The Instruction Set Architecture Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring.
IT253: Computer Organization
Comp Sci 251 Intro 1 Computer organization and assembly language Wing Huen.
Chapter 1: Basic Concepts
4-1 Chapter 4 - The Instruction Set Architecture Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of.
Computer Systems Organization CS 1428 Foundations of Computer Science.
Microcode Source: Digital Computer Electronics (Malvino and Brown)
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
4-1 Chapter 4 - The Instruction Set Architecture Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
CPU Internal memory I/O interface circuit System bus
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
4-1 Chapter 4 - The Instruction Set Architecture Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
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.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Represents different voltage levels High: 5 Volts Low: 0 Volts At this raw level a digital computer is instructed to carry out instructions.
Computer Organization 1 Instruction Fetch and Execute.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
What is a program? A sequence of steps
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Computer Organization Instructions Language of The Computer (MIPS) 2.
ECE DIGITAL LOGIC LECTURE 2: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/14/2016.
Winter 2016CISC101 - Prof. McLeod1 Today Go over the Python disassembly “experiment” again. Code interpretation vs. code compilation. History and features.
First Foray into Programming (the hard way). A reminder from last lesson: A machine code instruction has two parts:  Op-code  Operand An instruction.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Digital logic COMP214  Lecture 2 Dr. Sarah M.Eljack Chapter 1 1.
1 Chapter 1: Introduction Appendix A: Binary and Hexadecimal Tutorial Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
Number Systems & Binary Arithmetic
Assembly language.
Computer Organization
Number Systems and Binary Arithmetic
Data Representation Binary Numbers Binary Addition
A Closer Look at Instruction Set Architectures
A Closer Look at Instruction Set Architectures
Number System conversions
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CSCI206 - Computer Organization & Programming
CISC101 Reminders Your group name in onQ is your grader’s name.
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Overview of Computer Architecture and Organization
MARIE: An Introduction to a Simple Computer
CISC124 Assignment 1 due this Friday, 7pm.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
8051 ASSEMBLY LANGUAGE PROGRAMMING
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Computer Organization and Assembly Language
CS 111 – Sept. 16 Machine language examples Instruction execution
Presentation transcript:

Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written in a high level language like Python?

CISC101 - Prof. McLeod2 Numeric Representation Binary numbers or “base 2” is a natural representation of numbers to a computer. As a transition, hexadecimal (or “hex”, base 16) numbers are also used. Octal (base 8) numbers are hardly ever used anymore – so I won’t talk about this base. Decimal (base 10) numbers are not naturally represented in computers. Winter 2016

CISC101 - Prof. McLeod3 Reminder – Python Integer Literals From Exercise 1: –Binary literals – prefix the number with 0b –Octal literals – prefix the number with 0o –Hex literals – prefix the number with 0x –Decimal literals – don’t prefix the number with anything! Winter 2016

CISC101 - Prof. McLeod4 Numeric Representation - Cont. In base 2 (digits either 0 or 1): r=2, a binary number: ( ) 2 = 1×2 5 +1×2 4 +0×2 3 +1×2 2 +0×2 1 +1×2 0 +1× ×2 -2 =53.75 (in base 10) “r” is the “radix” or the base of the number Winter 2016

CISC101 - Prof. McLeod5 Numeric Representation - Cont. Hexadecimal Numbers: a base-16 system with 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F: For example: r = 16, a hex number: (B65F) 16 = 11× × × ×16 0 = Winter 2016

CISC101 - Prof. McLeod6 Numeric Representation - Cont. The above series show how you can convert from binary or hex to decimal. How to convert from decimal to one of the other bases? Here is an algorithm that you can use: integral part: divide by r and keep the remainder. decimal part: multiply by r and keep the carry “r” is the base - either 2 or 16 Winter 2016

CISC101 - Prof. McLeod7 Numeric Representation - Cont. For example, convert to binary: So, is Divisor(r)DividendRemainder (quotient) most significant digit least significant digit Winter 2016

CISC101 - Prof. McLeod8 Numeric Representation - Cont. For the “ ” part: So, is is: ( ) 2 Multiplier(r)MultiplicandCarry (product) Winter 2016

CISC101 - Prof. McLeod9 Aside - Roundoff Error Consider 0.1, for example: (0.1) 10 = ( …) 2 What happens to the part of a real number that cannot be stored? It is lost - the number is either truncated or rounded. The “lost part” is called the Roundoff Error. Winter 2016

An Experiment Without worrying about the syntax details for now, enter the following lines at the Python prompt: >>> sum = 0 >>> for i in range(1000) : sum = sum >>> sum Why is the value in sum not what it should be? Winter 2016CISC101 - Prof. McLeod10

Not All Numbers! A computer only has a finite amount of memory to store a number: –Only a portion of most real numbers can be stored. –The majority of real numbers cannot be stored exactly on a computer! –Only numbers that are a sum of powers of 2 can be stored exactly. –Most calculations made with real numbers on a computer can only yield approximate results! CISC101 - Prof. McLeod11Winter 2016

CISC101 - Prof. McLeod12 Numeric Representation - Cont. Converting between binary and hex is much easier - done by “grouping” the digits: For example: (2C6B.F06) 16 = (?) 2 ( 2 C 6 B. F 0 6 ) 16 ( ) 2 Winter 2016

Numeric Representation - Cont. And going from binary to hex: ( ) 2 = (?) 16 ( ) 2 ( 5 9 D. D 8 ) 16 CISC101 - Prof. McLeod13Winter 2016

CISC101 - Prof. McLeod14 Commanding the Processor Suppose we want the processor to carry out the operation: X = A * B + C Assume we have used some other operations to put numbers in three memory locations at locations 1024, 1025 and 1026 in RAM, corresponding to A, B and C. We want the result to go into memory location 1027, corresponding to X. Winter 2016

CISC101 - Prof. McLeod15 X = A * B + C Remember the “von Neumann Cycle”? The operations in the ALU (or “Arithmetic Logic Unit”) part of the CPU would be: 1.Fetch the contents of location 1024 and put it into a register. 2.Fetch the contents of location 1025 and multiply it with the number in the register, storing the result in the register. 3.Fetch the contents of location 1026 and add this value to the contents of the register. 4.Move the contents of the register to memory location Winter 2016

CISC101 - Prof. McLeod16 X = A * B + C, Cont. Naturally, these instructions have to be communicated to the CPU in binary: byte 1 bytes 2, 3 and 4 operand opcode Winter 2016

Aside – Bytes and Bits A byte is 8 bits. A bit is either 0 or 1. So a byte can range from: , to In base 10 this is 0 to 255 In hex this is 0 to FF So one byte can be represented by two hex digits. CISC101 - Prof. McLeod17Winter 2016

CISC101 - Prof. McLeod18 X = A * B + C, Cont What are these instructions in base 10? The operands are the memory locations The opcodes are 16 for “load”, 36 for “multiply”, 35 for “add” and 17 for “store”. The default register is used, so it does not need to be specified. Winter 2016

CISC101 - Prof. McLeod19 Machine Language These 4 byte binary commands are examples of machine language. Normally these commands would be viewed in base 16, or hex: 1.0x x x x Winter 2016

CISC101 - Prof. McLeod20 X = A * B + C, Cont (Pseudo) Assembly language instructions: 1.LOAD A, ACC 2.MULT B, ACC 3.ADD C, ACC 4.STOR ACC, X Each assembly language keyword is immediately translated into the corresponding machine language code by an “interpreter” program called an “Assembler”. Winter 2016

CISC101 - Prof. McLeod21 X = A * B + C, Cont A higher level language goes one step above assembly language. For example, in C, C++, C# or Java, you would write: X = A * B + C; (Python is the same, except no semi-colon.) Each high-level line of code gets translated into many lines of assembly code and each line of assembly code must be translated into binary machine language. Much easier to write high level code, right? Winter 2016

Disassembly Experiment Python has a module called “dis” that can show you the assembly language version of a Python line of code, function, object or an entire module. At the >>> prompt type: >>> import dis >>> a = 10 >>> b = 30 >>> c = 2 >>> dis.dis("z = a + b * c") Winter 2016CISC101 - Prof. McLeod22

Disassembly Experiment, Cont. You see: 1 0 LOAD_NAME 0 (a) 3 LOAD_NAME 1 (b) 6 LOAD_NAME 2 (c) 9 BINARY_MULTIPLY 10 BINARY_ADD 11 STORE_NAME 3 (z) 14 LOAD_CONST 0 (None) 17 RETURN_VALUE Winter 2016CISC101 - Prof. McLeod23

Disassembly Experiment, Cont. One line of Python code required eight lines of assembly! Note that the multiply is carried out before the addition. Why did the interpreter choose that order? In case you are curious, see section in the Python help docs for the meaning of assembly instructions. These ones are explained on the next slide: Winter 2016CISC101 - Prof. McLeod24

Disassembly Experiment, Cont. LOAD_NAME pushes a value onto the stack from a variable name. BINARY_MULTIPLY multiplies the two numbers on top of the stack and puts the result on top of the stack. BINARY_ADD adds the two numbers on top of the stack and puts the result back on the stack. STORE_NAME takes the value on the top of the stack and stores it into a variable name. LOAD_CONST puts a zero on top of the stack, to clear it. RETURN_VALUE returns the value on top of the stack (zero in this case) as a result of the evaluation of the expression. Winter 2016CISC101 - Prof. McLeod25

Disassembly Experiment, Cont. The “stack” is used to temporarily hold values and the results of calculations. How the stack changes after each instruction: top: Winter 2016CISC101 - Prof. McLeod a a b a b c a z b * ca + b * c

Aside – What’s a Stack? A “LIFO” – “Last In, First Out” type of data structure. (Like a plate dispenser in a cafeteria!) You “push” values on to the top of the stack and “pop” them off. Winter 2016CISC101 - Prof. McLeod27