Chapter 4 binary arithmetic l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

Chapter 4 –Binary Arithmetic PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on
  • Presentation posted in: General

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, 2000. By Michael Weeks. Arithmetic. Arithmetic involves addition subtraction multiplication division

Download Presentation

Chapter 4 –Binary Arithmetic

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 4 binary arithmetic l.jpg

Chapter 4 –Binary Arithmetic

These are lecture notes to accompany the book SPARC Architecture, Assembly Language Programming, and C,

by Richard P. Paul, 2nd edition, 2000.

By Michael Weeks

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Arithmetic l.jpg

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 l.jpg

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

011

+001

Sum

Carry

198

+264

Sum

Carry

4

0

6

1

2

1

1

0

0

1

0

1

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Binary sums and carries l.jpg

Binary Sums and Carries

abSumabCarry

000000

011010

101100

110111

XOR

AND

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Binary addition example l.jpg

Binary Addition Example

mov 0x45, %r1

mov 0xE7, %r2

add %r1, %r2, %r3

  • What values are in the registers after this?

0x45= 0 0100 0101

0xE7=+ 0 1110 0111

1 0010 1100

=> 0x12C

so r1=0x45, r2=0xE7, r3=0x12C

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Half adder full adder l.jpg

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 l.jpg

Figure 4.2 – Full Adder

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Modulus arithmetic l.jpg

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 99999 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 arithmetic9 l.jpg

Modulus Arithmetic

  • Computers do this

  • SPARC has 32-bit registers

  • Each register can store an integer number

    • between 0 and 2n-1

    • where n=32 for SPARC

  • if you have a value of 2n-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 l.jpg

Subtraction

  • Let r be the base,

    and n the number of digits

    a – b = a + (rn – 1 – b) + 1

  • since the result is modulus rn, adding rn does not affect the result

    • Imagine if your car’s odometer read 54321 and you drove 100,000 miles, it would then read 54321 again.

  • no borrowing is needed

  • once (rn – 1 – b) is found, subtraction can be done with addition

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Complement arithmetic l.jpg

Complement Arithmetic

  • rn-1-b is called the

    • “nine’s complement” if r=10

    • “one’s complement” if r=2

  • rn-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

    10000000 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 l.jpg

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 l.jpg

Two’s Complement Example

  • What is –16 (decimal) in binary (r=2) ?

  • We’ll assume n=8

16 = 0001 0000 in binary

1110 1111 one’s complement

+ 0000 0001 add 1

1111 0000 two’s complement

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Two s complement numbers l.jpg

Two’s Complement Numbers

  • 01117

  • 01106

  • 01015

  • 01004

  • 00113

  • 00102

  • 00011

  • 00000

  • 1111-1

  • 1110-2

  • 1101-3

  • 1100-4

  • 1011-5

  • 1010-6

  • 1001-7

  • 1000-8

0111 1111 127

0111 1110 126

....

0000 0100 4

0000 0011 3

0000 0010 2

0000 0001 1

0000 0000 0

1111 1111 -1

1111 1110 -2

1111 1101 -3

1111 1100 -4

1111 1011 -5

....

1000 0001 -127

1000 0000 -128

four-bit

eight-bit

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Number ranges l.jpg

Number Ranges

  • A signed number has the range

    -2n-1 to 2n-1 –1

  • An unsigned number has the range

    0 to 2n -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 l.jpg

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 2nd operand, and add

  • There is no need for a hardware subtractor

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Multiplication and division l.jpg

Multiplication and Division

  • These operations can be done with additions and subtractions

  • There is no instruction for multiplication (nor division) in the SPARC architecture

  • We will use the “call .mul” and “call .div”

  • We will skip sections 4.10 and 4.11 (but do read about numeric labels on page 137)

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Shift operations l.jpg

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 l.jpg

Shift Instructions

  • Shift right logical (srl)

    0

  • Shift right arithmetic (sra)

  • Shift left logical (sll)

  • Shift left arithmetic is not provided,

    • it would be the same as shift left logical

0

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Shift instructions20 l.jpg

Shift Instructions

  • Shift right logical (srl)

    srlregrs1, reg_or_imm, regrd

  • Shift right arithmetic (sra)

    sraregrs1, reg_or_imm, regrd

  • Shift left logical (sll)

    sllregrs1, reg_or_imm, regrd

  • 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 l.jpg

Branching Conditions

  • Branching is based on the following flags:

    N(negative)

    the most significant bit of the result

    not used with unsigned numbers

    V(overflow)

    when result is too big for the register

    Z(zero)

    set when all bits of the result are 0

    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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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

    • addxregrs1, reg_or_imm, regrd

    • addxccregrs1, reg_or_imm, regrd

    • these mean:

    • regrd =regrs1 + reg_or_imm + C

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


Extended precision sub l.jpg

Extended Precision (Sub)

  • We can use extended precision instructions, with the carry

    • subxregrs1, reg_or_imm, regrd

    • subxccregrs1, reg_or_imm, regrd

  • these mean:

    • regrd =regrs1 - 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 l.jpg

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 430 l.jpg

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

srlregrs1, reg_or_imm, regrd

sraregrs1, reg_or_imm, regrd

sllregrs1, reg_or_imm, regrd

addxregrs1, reg_or_imm, regrd

addxccregrs1, reg_or_imm, regrd

subxregrs1, reg_or_imm, regrd

subxccregrs1, reg_or_imm, regrd

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C


  • Login