Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 4 –Binary Arithmetic These are lecture notes to accompany the book SPARC.

Slides:



Advertisements
Similar presentations
B261 Systems Architecture
Advertisements

Integer Arithmetic: Multiply, Divide, and Bitwise Operations
EECC250 - Shaaban #1 Lec # 2 Winter Addressing Modes  Addressing modes are concerned with the way data is accessed  Addressing can be.
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
EET 1131 Unit 7 Arithmetic Operations and Circuits
HEXADECIMAL NUMBERS Code
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Integer Multiplication and Division
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
COE 308: Computer Architecture (T041) Dr. Marwan Abu-Amara Integer & Floating-Point Arithmetic (Appendix A, Computer Architecture: A Quantitative Approach,
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
IT Systems Number Operations EN230-1 Justin Champion C208 –
FIGURES FOR CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
The M68HC11 Basic Instruction Set Basic Arithmetic Instructions
Arithmetic for Computers
Lecture 5.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
The 8051 Microcontroller and Embedded Systems
Computer Architecture Lecture 3: Logical circuits, computer arithmetics Piotr Bilski.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
Basic Arithmetic (adding and subtracting)
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC Architecture Dr. Anu Bourgeois 1.
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
CPS120: Introduction to Computer Science Computer Math: Signed Numbers.
Basic Arithmetic (adding and subtracting)
Lecture 6: Multiply, Shift, and Divide
Operations on Bits Arithmetic Operations Logic Operations
Natawut NupairojAssembly Language1 Arithmetic Operations.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Integer Multiplication and Division
Computer Math CPS120 Introduction to Computer Science Lecture 4.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
1 2.1 Introduction A bit is the most basic unit of information in a computer. –It is a state of “on” or “off” in a digital circuit. –Sometimes these states.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 8 – Machine Instructions These are lecture notes to accompany the book.
Arithmetic Operations
Addition, Subtraction, Logic Operations and ALU Design
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
©2010 Cengage Learning SLIDES FOR CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit.
ECE DIGITAL LOGIC LECTURE 3: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/19/2016.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
1 CS 151 : Digital Design Chapter 4: Arithmetic Functions and Circuits 4-3 : Binary Subtraction.
ECE DIGITAL LOGIC LECTURE 15: COMBINATIONAL CIRCUITS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2015, 10/20/2015.
Integer Operations Computer Organization and Assembly Language: Module 5.
Chapter 8 Computer Arithmetic. 8.1 Unsigned Notation Non-negative notation  It treats every number as either zero or a positive value  Range: 0 to 2.
Computer Math CPS120 Introduction to Computer Science Lecture 7.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Chapter 8 – Machine Instructions
Floating Point Representations
Integer Multiplication and Division
Chapter 3 – Digital Logic and Binary Numbers
ECE/CS 552: Arithmetic and Logic
MISP Assembly.
CS 235 Computer Organization & Assembly Language
CSC 3210 Computer Organization and Programming
Computer Architecture & Operations I
Digital Logic & Design Lecture 02.
CS/COE0447 Computer Organization & Assembly Language
CS/COE0447 Computer Organization & Assembly Language
Presentation transcript:

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 4 –Binary Arithmetic These are lecture notes to accompany the book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition, By Michael Weeks

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Arithmetic Arithmetic involves –addition –subtraction –multiplication –division People use base 10 Computers use base 2 Base 2 is actually easier than base 10

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Addition From right to left, we add each pair of digits We write the sum, and add the carry to the next column on the left Sum Carry Sum Carry

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary Sums and Carries abSumabCarry XORAND

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary Addition Example mov 0x45, %r1 mov 0xE7, %r2 add %r1, %r2, %r3 What values are in the registers after this? 0x45= xE7= => 0x12C so r1=0x45, r2=0xE7, r3=0x12C

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Half Adder / Full Adder A half adder –For adding 2 bits –Gives “carry out” and “sum” –1 AND and 1 XOR gate A full adder –For adding 2 bits plus a “carry in” –Gives “carry out” and “sum” –2 ANDs, 2 XORs, and 1 OR You’ll see more of this in the Computer Architecture class

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Figure 4.2 – Full Adder

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Modulus Arithmetic “Modulus arithmetic considers only numbers in the range 0 <= n < M, where M is the modulus.” [page 117] Example: a car odometer if a car has miles on it, and you drive another mile, the odometer will then read 00000

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Modulus Arithmetic Computers do this SPARC has 32-bit registers Each register can store an integer number –between 0 and 2 n -1 –where n=32 for SPARC if you have a value of 2 n -1 in a register, and you add 1 to this, the register will then hold a value of 0

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Subtraction Let r be the base, and n the number of digits a – b = a + (r n – 1 – b) + 1 since the result is modulus r n, adding r n does not affect the result –Imagine if your car’s odometer read and you drove 100,000 miles, it would then read again. no borrowing is needed once (r n – 1 – b) is found, subtraction can be done with addition

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Complement Arithmetic r n -1-b is called the –“nine’s complement” if r=10 –“one’s complement” if r=2 r n -1-b+1 is called the radix complement –“ten’s complement” if r=10 –“two’s complement” if r=2 Any number where the most significant digit >= (r/2) is considered negative means –128 when r=2, n=8 84 means –16 when r=10, n=2 [see page 120]

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Two’s Complement In binary, finding the one’s complement and the two’s complement are easy One’s complement: –Replace every 0 with a 1, –and replace every 1 with a 0 Two’s complement: –Find the one’s complement, –and add 1

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Two’s Complement Example What is –16 (decimal) in binary (r=2) ? We’ll assume n=8 16 = in binary one’s complement two’s complement add 1

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Two’s Complement Numbers four-bit eight-bit

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Number Ranges A signed number has the range -2 n-1 to 2 n-1 –1 An unsigned number has the range 0 to 2 n -1 What is the range of a SPARC register? -2,147,483,648 to 2,147,483,647 (signed) 0 to 4,294,967,295 (unsigned)

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Addition and Subtraction “The two’s complement system is an interpretation of numbers in registers; the hardware always performs binary addition.” [page 122] To subtract, find the 2’s complement of the 2 nd operand, and add There is no need for a hardware subtractor

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Shift Operations A registers’ contents can be shifted –left shift is like multiplying by 2 –right shift is like dividing by 2 Logical shift –copies 0 into most significant bit(s) Arithmetic shift –Copies the sign bit into most significant bit (otherwise, negatives could become positives)

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Shift Instructions Shift right logical (srl) 0 Shift right arithmetic (sra) Shift left logical (sll) Shift left arithmetic is not provided 0

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Shift Instructions Shift right logical (srl) srlreg rs1, reg_or_imm, reg rd Shift right arithmetic (sra) srareg rs1, reg_or_imm, reg rd Shift left logical (sll) sllreg rs1, reg_or_imm, reg rd The number of shifts is the low 5 bits of the reg_or_imm, so largest shift is 31

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Branching Conditions Branching is based on the following flags: N(negative) the most significant bit of the result not used with unsigned numbers Z(zero) set when all bits of the result are 0 V(overflow) when result is too big for the register C(carry) set when an addition has carry out, or when subtraction does not have carry out

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Signed Branches Assembler Signed Arithmetic MnemonicBranchesCondition Codes blbranch on less(N xor V) = 1 blebranch on less or equalZ or (N xor V) = 1 bebranch on equalZ = 1 bnebranch on not equalZ = 0 bgebranch on greater or equal(N xor V) = 0 bgbranch on greaterZ or (N xor V) = 0

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Unsigned Arithmetic “Hardware operations on signed and unsigned numbers are identical.” [page 124] The carry flag (C) can be used instead of the overflow flag (V)

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Unsigned Branches Assembler Signed Arithmetic MnemonicBranchesCondition Codes blubranch on lessC = 1 bleubranch on less or equalZ or C = 1 bebranch on equalZ = 1 (same as signed branch) bnebranch on not equalZ = 0 (same as signed branch) bgeubranch on greater or equalC = 0 bgubranch on greaterZ = 0 and C = 0

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Condition Code Branches Assembler Signed Arithmetic MnemonicBranchesCondition Codes bnegbranch on negativeN = 1 bposbranch on positiveN = 0 bzbranch on zero setZ = 1 (same as be) bnzbranch on zero not setZ = 0 (same as bne) bvsbranch on overflow setV = 0 bvcbranch on overflow clearV = 1 bcsbranch on carry setC = 1 (same as blu) bccbranch on carry clearC = 0 (same as bgeu)

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Numeric Labels “The assembler allows single-digit labels to appear many times in a single source file.” [page 137] addcc%lo_r, %lo_r, %lo_r bcc1f! find label ‘1’ forward add%hi_r, %hi_r, %hi_r bset1, %hi_r 1: The letter “b” or “f” must be appended to the digit, for backward or forward direction. The closest label in that direction will be the branch target, if the branch is taken. These are good for labels used in control structures.

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Extended Precision What if we need to work with data that are more than 32 bits wide? We can use extended precision instructions, with the carry addxreg rs1, reg_or_imm, reg rd addxccreg rs1, reg_or_imm, reg rd these mean: reg rd =reg rs1 + reg_or_imm + C

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Extended Precision (Sub) We can use extended precision instructions, with the carry subxreg rs1, reg_or_imm, reg rd subxccreg rs1, reg_or_imm, reg rd these mean: reg rd =reg rs1 - reg_or_imm - C Data larger than 32 bits must be stored in 2 (or more) registers. That is, add (or sub) the low part first, then use the extended precision addx (or subx) for the high part. This is just like doing arithmetic on paper, where we work with one digit at a time.

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Summary of Chapter 4 sum and carry modulus arithmetic –one’s complement –two’s complement signed and unsigned arithmetic condition codes shifting numeric labels extended precision

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Summary of Chapter 4 bl label also: ble, be, bne, bge, bg, blu, bleu, be, bne, bgeu, bgu, bneg, bpos, bz, bnz, bvs, bvc, bcs, bcc srlreg rs1, reg_or_imm, reg rd srareg rs1, reg_or_imm, reg rd sllreg rs1, reg_or_imm, reg rd addxreg rs1, reg_or_imm, reg rd addxccreg rs1, reg_or_imm, reg rd subxreg rs1, reg_or_imm, reg rd subxccreg rs1, reg_or_imm, reg rd