# CS代考计算机代写 assembler compiler assembly mips Number Representations

Number Representations
There are 10 types of people in this world Those who understand binary and those who don’t

Agenda • Bits, Bytes, and Words
• Numberbasesandbaseconversion – Positional notation
• Binary arithmetic and data representation – Signed numbers
– Arithmetic and overflow
– Packed Decimal, ASCII, Parity…

From Lecture 1: Below Your Program • High-level language program (in C)
swap (int v[], int k) { int temp = v[k]; v[k] = v[k+1]; v[k+1] = temp;
}
• Assembly language program (for MIPS) swap: sll \$2, \$5, 2 add \$2, \$4,\$2
C compiler
lw \$15, lw \$16, sw \$16, sw \$15, jr \$31
• Machine (object) code (for MIPS)
0(\$2) 4(\$2) 0(\$2) 4(\$2)
assembler
000000 00000 00101 0001000010000000
000000 00100 00010 0001000000100000 …

How do people and computers represent numbers?

Why Base 10? Why Base 2?
• Decimal: Base 10, a single number from 0-9
• Binary: Base 2, a single digit is called a bit (binary digit)
• A bit is the smallest unit of information, and can represent… –1/0
– True / False
– Yes / No
– On/Off
– used in a two-state (Boolean) logic
• Can represent anything with a sequence of binary bits, but the bit patterns have no intrinsic meaning by themselves!

Nibbles to Words • Typicallystoreinformationingroups
– a byte is a group of 8 bits • e.g. 01100101
– a nibble/nybble (a small bite) is a group of 4 bits, • e.g. 0110
– a word (MIPS) is a group of 4 bytes, or 32 bits • e.g. 01100101011001010110010101100101
• Leastsignificantbitrightmost

Numbers and Positional Notation
anan-1………a1a0 . a-1a-2……..a-m
N=෌𝑛 𝑖=−𝑚
a𝑖b𝑖 or
N = anbn + an-1bn-1 +……+ a1b + a0 + a-1b-1 + ……. +a-mb-m

• 23810
• 101102
Examples

Common Bases
Name of Base
Base
Digits used
Decimal
10
0,1,2,3,4,5,6,7,8,9
Binary
2
0,1
Octal
8
0,1,2,3,4,5,6,7
16
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
• We often write hex numbers preceded by 0x 10112 =1110 =138 =B16 =0xB

How many bits are needed to represent a decimal number?
• What is the largest decimal number with n digits? 10n – 1
• What is the largest binary number with m digits? 2m – 1
• For base b, the largest number is bk – 1

How many bits are needed to represent a decimal number?
• How many digits necessary for numbers up to one million?

How to Convert from one Base to Another?

Base Conversion – Decimal to Another Base
• Approach1:Makeatable – Divide by bi
– The quotient is the most significant bit – Repeat with the remainder
• Approach2:
– Divide by b
– Remainder of result is least significant bit – Repeat with the quotient

Base Conversion – Decimal to Another Base • Example: What is 52310 in binary?

Base Conversion – Decimal to Another Base • Example: What is 5324110 in hexadecimal?

Base Conversion – Other Base to Decimal • BasicApproach:Directexpansionwithpositionalweights
N = anbn + an-1bn-1 +……+ a1b + a0
• AdvancedApproach:Horner’srule N = anbn + an-1bn-1 +……+ a1b + a0
= (…((anb + an-1)b + an-2)b + … +a1)b + a0

Base Conversion – Other Base to Decimal • Examples: What is 10101012 in Decimal

Base Conversion – Other Base to Decimal • Examples: What is 1AB16 in Decimal?

Base Conversion – Base A to Base B
• Conversion from base a to base b – First convert base a to decimal
– Then convert decimal to base b
• Special cases (easier by grouping) – Binary to hexadecimal and back
• Example: 110101011012
– Binary to octal and back
• Example: 7608 becomes 1111100002

Questions
• What is the largest binary number with n bits?
• Howdowesubtractbinarynumbers?
• Why do programmers always mix up Halloween and Christmas?
• Howshouldwerepresentnegativenumbers?

How to Represent Signed Numbers?

Sign-and-Magnitude • The first approach
• Use the most significant bit to represent the sign +13= 0000 1101
-13 = 1000 1101 • Problems
– Two representations for zero: 0000 0000 and 1000 0000
– Cannot add a positive number and a negative number together

• Invert each bit! +13= 0000 1101 -13 = 1111 0010
One’s Complement
• Problems:
– Still two representations for zero 0000 0000 and 1111 1111 – Answer is off by 1
– Incorrect overflow • What is 16 + (-13)?

• The gold standard
Two’s Complement
• Invert the bits and add one! +13 = 0000 1101
What is –13?
• Unique zero 0000 0000
• MSB represents the sign. – 1 if negative
– 0 if positive
• Negative numbers are defined as -N = Bn – N

Two’s Complement
• Easily implemented in hardware
• Range from -2n-1 to +2n-1 – 1
• The complement of complement of Y is Y, i.e., -(-Y) = Y.

• 16-13=?
Two’s Complement

The Hitchhiker’s Guide to the Galaxy What is the question to life, the universe, and everything?

Binary Arithmetic
Subtraction
Multiplication
0+0=0
0-0=0
0*0=0
0+1=1
0 – 1 = 1 borrow 1
0*1=0
1+0=1
1-0=1
1*0=0
1 + 1 = 0 carry 1
1-1=0
1*1=1
• Rules in base 10 are also valid in any other base
• Subtraction often done using addition and 2’s complement
• Multiplication and division are similar. We will learn in a few lectures

Arithmetic overflow
• Typically use a fixed # of bits to represent numbers!