CS201: Architecture and Assembly Language



Similar documents
Base Conversion written by Cathy Saxton

Lecture 8: Binary Multiplication & Division

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Useful Number Systems

Computer Science 281 Binary and Hexadecimal Review

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

=

Systems I: Computer Organization and Architecture

CSI 333 Lecture 1 Number Systems

Chapter 2. Binary Values and Number Systems

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

COMPSCI 210. Binary Fractions. Agenda & Reading

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Lecture 11: Number Systems

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Lecture 2. Binary and Hexadecimal Numbers

Binary Representation

3. Convert a number from one number system to another

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Chapter 4: Computer Codes

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

The string of digits in the binary number system represents the quantity

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Number Systems and Radix Conversion

NUMBER SYSTEMS. William Stallings

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Section 1.4 Place Value Systems of Numeration in Other Bases

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

Chapter 7 Lab - Decimal, Binary, Octal, Hexadecimal Numbering Systems

Numbering Systems. InThisAppendix...

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

NUMBER SYSTEMS. 1.1 Introduction

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Decimal Notations for Fractions Number and Operations Fractions /4.NF

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Binary Numbers. Binary Octal Hexadecimal

2011, The McGraw-Hill Companies, Inc. Chapter 3

Activity 1: Using base ten blocks to model operations on decimals

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

CPEN Digital Logic Design Binary Systems

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Binary, Hexadecimal, Octal, and BCD Numbers

AN617. Fixed Point Routines FIXED POINT ARITHMETIC INTRODUCTION. Thi d t t d ith F M k Design Consultant

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

ARM Cortex-M3 Assembly Language

Lecture #21 April 2, 2004 Relays and Adder/Subtractors

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Number and codes in digital systems

Playing with Numbers

Chapter 5 Instructor's Manual

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

CS321. Introduction to Numerical Methods

1 Description of The Simpletron

ASCII and BCD Arithmetic. Chapter 11 S. Dandamudi

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

What Is Singapore Math?

26 Integers: Multiplication, Division, and Order

Let s put together a Manual Processor

The Euclidean Algorithm

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

MACHINE INSTRUCTIONS AND PROGRAMS

Grade 6 Math Circles. Binary and Beyond

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Chapter 1: Digital Systems and Binary Numbers

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Binary Adders: Half Adders and Full Adders

Number Representation

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

Preliminary Mathematics

3.2 The Factor Theorem and The Remainder Theorem

THUMB Instruction Set

Solution Guide Chapter 14 Mixing Fractions, Decimals, and Percents Together

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Introduction to Fractions

Decimal to Binary Conversion

Solutions of Linear Equations in One Variable

Decimals and other fractions

MICROPROCESSOR AND MICROCOMPUTER BASICS

MIPS Assembly Language Programming CS50 Discussion and Project Book. Daniel J. Ellard

Subnetting Examples. There are three types of subnetting examples I will show in this document:

Session 29 Scientific Notation and Laws of Exponents. If you have ever taken a Chemistry class, you may have encountered the following numbers:

47 Numerator Denominator

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

JobTestPrep's Numeracy Review Decimals & Percentages

Faculty of Engineering Student Number:

CHAPTER 5 Round-off errors

Positional Numbering System

1. The Fly In The Ointment

Linear Programming Notes V Problem Transformations

Stupid Divisibility Tricks

Transcription:

CS201: Architecture and Assembly Language Lecture Three Brendan Burns CS201: Lecture Three p.1/27

Arithmetic for computers Previously we saw how we could represent unsigned numbers in binary and how binary addition is performed. Today we ll flesh out many of the details. Representing Negative Numbers Subtraction, Multiplication and Division CS201: Lecture Three p.2/27

A reminder First a quick reminder on binary, octal and hexidecimal. Binary Octal Hex Decimal Binary Octal Hex Decimal 0000 0 0 0 1000 10 8 8 0001 1 1 1 1001 11 9 9 0010 2 2 2 1010 12 A 10 0011 3 3 3 1011 13 B 11 0100 4 4 4 1100 14 C 12 0101 5 5 5 1101 15 D 13 0110 6 6 6 1110 16 E 14 0111 7 7 7 1111 17 F 15 CS201: Lecture Three p.3/27

Signed Numbers We ve already seen how to represent unsigned numbers. How could we do that? We ll we could just add a sign bit... But that s problematic: Which side? Negative Zero? (Univac had it...) It slows down adding. Can we solve these? Sure! CS201: Lecture Three p.4/27

Two s Compliment In the end, since no representation was really pretty, hardware designers chose the one which made the hardware easiest: 00000000000000000000000000000000 two = 0 ten 00000000000000000000000000000001 two = 1 ten... 01111111111111111111111111111110 two = 2, 147, 483, 646 ten 01111111111111111111111111111111 two = 2, 147, 483, 647 ten 10000000000000000000000000000000 two = 2, 147, 483, 648 ten 10000000000000000000000000000001 two = 2, 147, 483, 647 ten... 11111111111111111111111111111110 two = 2 ten 11111111111111111111111111111111 two = 1 ten CS201: Lecture Three p.5/27

Two s Compliment Cont. This is known as Two s Compliment Notation It does have the problem that there is one negative number without a matching positive number Also adding 1 to 2,147,483,647 gives you -2,147,483,648 But it has a sign bit (the most-significant), and all numbers are computed in the same way (x31 2 31 + x30 2 30 +... + x1 2 1 + x0 2 0 ) CS201: Lecture Three p.6/27

Two s Compliment (Cont.) If you re loading in a byte or half-word, how its loaded varies depending on whether its signed or not. ldrsh r1, [r2] loads a signed half-word ldrsb r1, [r2] loads a signed byte In order to do this, the load performs sign-bit extension 11111110 two 11111111111111111111111111111110 two The same is performed for signed half-words. CS201: Lecture Three p.7/27

Two s Compliment (Cont.) Two s compliment is also convenient because negating a number is as simple as inverting the number and adding one to it: 1 7 = NOT (0111 two ) + 0001 two 1 7 = 1000 two + 0001 two 1 7 = 1001 two 1 7 = 7 ten CS201: Lecture Three p.8/27

Binary Addition Previously we ve seen how to do binary addition, for example: 15 + 23 What s cool is that it works for negatives too! 12 + 10 CS201: Lecture Three p.9/27

Binary Subtraction Binary subtraction proceeds pretty much exactly like addition: 10 3 Alternatively we can negate the number and add. 10 3 = 10 + 3 CS201: Lecture Three p.10/27

Binary Multiplication It turns out that binary multiplication is exactly what you d expect too: 12 * 13 But we need to worry overflowing of our value. Fortunately ARM comes with a special multiply instruction U,SMULLL r0,r1,r2,r3 unsigned/signed multiplies r2 * r3 and stores the result into both r0 lo-word and r1 hi-word. CS201: Lecture Three p.11/27

Multiplication Algorithms So how does this actually work? 1. Set the product to multiplier 2. Is the l.s.b. of the product 0? If so, goto 3 3. Add the multiplicand to the product 4. Shift the product right 1 bit 5. Repeat 32 times CS201: Lecture Three p.12/27

Signed Multiplication Signed multiplication is trickier. An efficient algorithm for performing this is Booth s algorithm: 1. Set the product to the multiplier 2. If the last two bits are 00 or 11 goto 5 3. If the last two bits are 01 add the multiplicand to the left half of the pduct 4. If the last two bits are 10 subtract the multiplicand from the left half od the product 5. Shift the product right by one CS201: Lecture Three p.13/27

Booth s Example 4 ten 6 ten = 24 ten CS201: Lecture Three p.14/27

Why does this work? Consider a b At each step in Booth s algorithm we evaluate (a i 1 a i ), if its zero we do nothing, if its 1 we add b, if its -1 we subtract b. Thus at the end of the algorithm we have: (a 1 a 0 ) b 2 0... (a 30 a 31 ) b 2 3 1 By factoring out b we get b a 0 2 0... a 31 2 3 1 or a b. CS201: Lecture Three p.15/27

Binary Division Binary division proceeds much like binary multiplication 1010101/110 CS201: Lecture Three p.16/27

An algorithm for division The division algorithm is much like multiplication 1. Remainder gets the quotient 2. Shift the remainder to the left 1 bit 3. Subtract the divisor from the left half of the remainder 4. If remainder is < 0 add the divisor to the left half of the remainder and shift the remainder left with a 0 rightmost bit 5. If the remainder 0 shift the remainder to the left with a 1 rightmost bit 6. repeat 32 times CS201: Lecture Three p.17/27

Signed Division It turns out there s no cool algorithm for signed division. The best is to treat them as unsigned and set the sign bit afterwards. The tricky thing is to set the remainder s sign. We do this by noting that if the quotient should be negative and finding the remainder which works. 7 / 2 = 3 r 1 7 / -2 = -3 r +1-7 / 2 = -3 r -1-7 / -2 = 3 r -1 CS201: Lecture Three p.18/27

Dividing in Assembly Of course the ARM chip doesn t have a divide operator, so on the ARM you actually have to manually do out the division in assembly language: You can simply implement the algorithm described above in assembly. Or you can use a simpler algorithm (like repeated subtractions) In either case you ll need branch instructions Branch instructions change the location of the program counter. (we ll see them soon) CS201: Lecture Three p.19/27

Condition Flags Previously I mentioned that the CPU had a status register. It keeps track of the status of various things Two of them are: The carry flag The overflow flag Their necessary to determine the relationship between the true result and the actual result. CS201: Lecture Three p.20/27

The Carry Flag The carry flag indicates if there has been a carry out of the bounds of the result. This only happens when the actual result is not equal to the true result 0 0 1 0 + 1 0 0 1 1 0 1 1 C = 0 0 1 0 0 + 0 1 0 1 1 0 0 1 C = 0 1 1 0 0 + 0 1 0 1 0 0 0 1 C = 1 CS201: Lecture Three p.21/27

The Overflow Flag Well, that worked for unsigned numbers, what about signed ones? The V flag in the status register indicates overflow. Overflow indicates that we have run out of bits to store our result fortunately we can reconstruct it from the flag. CS201: Lecture Three p.22/27

The overflow flag If the signs don t match, we re guaranteed not to have overflow,(why?) so the overflow flag is never set. If the signs are the same, there are four cases: 0 x 1 x 2 x 3 1 0 x 1 x 2 x 3 + 0 y 1 y 2 y 3 0 r 1 r 2 r 3 V = 0 + 0 y 1 y 2 y 3 0 r 1 r 2 r 3 V = 1 CS201: Lecture Three p.23/27

Overflow, Cont. 1 1 x 1 x 2 x 3 + 1 y 1 y 2 y 3 0 r 1 r 2 r 3 V = 1 1 1 1 x 1 x 2 x 3 + 1 y 1 y 2 y 3 1 r 1 r 2 r 3 V = 0 CS201: Lecture Three p.24/27

Subtraction? What about in the case of subtraction? We ve already handled signed subtraction (its just addition) For unsigned subtraction, the meaning of C is reversed. That is if C = 1 the actual result = the true result If C = 0 the actual result neq the true result This is because subtraction is really inverted addition CS201: Lecture Three p.25/27

Other flags The status register has two other flags that are also set by addition/subtraction operations (and multiplication if requested) The Z register is set to 1 if the result of the operation is zero The N register is set to 1 if the hi-bit of the result is one. (e.g. this number is negative if in signed representation!) CS201: Lecture Three p.26/27

Condition Flag Summary Flag Unsigned meaning Signed Meaning N = 0 No meaning Actual 0 N = 1 No meaning Actual < 0 Z=0 Actual 0 Actual 0 Z=1 Actual = 0 Actual = 0 C=0 Actual = True if ADD No Meaning Actual True if SUB No Meaning C=1 Actual True if ADD No Meaning Actual =True if SUB No Meaning V=0 No Meaning Actual = True V=1 No Meaning Actual True CS201: Lecture Three p.27/27