Numeric/Instruction Representation

17
Lecture Objectives: Numeric/Instruction Representation 1) Define the terms least significant bit and most significant bit. 2) Explain how unsigned integer numbers are represented in memory 3) Understand the limitations of using sign and magnitude to represent signed integer numbers 4) Explain how signed integer numbers are represented in memory using twos complement notation. 5) Convert twos complement numbers into decimal. 6) Convert decimal numbers into twos complement format. 7) Explain the concept of sign extension 8) Convert a binary number into hexadecimal 9) Explain how instructions are represented in memory as machine code

description

Numeric/Instruction Representation. Define the terms least significant bit and most significant bit. Explain how unsigned integer numbers are represented in memory Understand the limitations of using sign and magnitude to represent signed integer numbers - PowerPoint PPT Presentation

Transcript of Numeric/Instruction Representation

Page 1: Numeric/Instruction  Representation

Lecture Objectives:

Numeric/Instruction Representation

1) Define the terms least significant bit and most significant bit.2) Explain how unsigned integer numbers are represented in memory3) Understand the limitations of using sign and magnitude to represent signed

integer numbers4) Explain how signed integer numbers are represented in memory using twos

complement notation.5) Convert twos complement numbers into decimal.6) Convert decimal numbers into twos complement format.7) Explain the concept of sign extension8) Convert a binary number into hexadecimal9) Explain how instructions are represented in memory as machine code

Page 2: Numeric/Instruction  Representation

Numeric values can be represented in any base. People commonly use base 10 (decimal).

Given a decimal (base 10) number, convert it to binary:

• 51ten= ?two

• -7ten= ?two

CS2710 Computer Organization 2

Page 3: Numeric/Instruction  Representation

Given a binary number, convert it to decimal format

• 1 1011two= ?ten

CS2710 Computer Organization 3

Page 4: Numeric/Instruction  Representation

Processors use each bit of a word to represent a binary digit of a numeric value• Consider the integer value 305,419,896

represented with a MIPS 32-bit word:

00010010001101000101011001111000

CS2710 Computer Organization 4

Bit 0(LSB)

Bit 31(MSB)

In this representation, each bit represents the value xi*2i, where iis the bit number, and x is either0 or 1.

Page 5: Numeric/Instruction  Representation

Signed numbers: How do we represent negative values (+/-)?

• First approach– Add a sign bit to the number– 0 indicates positive– 1 indicates negative

• Example:-11ten= ?two

CS2710 Computer Organization 5

Page 6: Numeric/Instruction  Representation

Dual zeros: the problem with sign and magnitude• 1 00000000 == 0 00000000

CS2710 Computer Organization 6

Sign bit Sign bit

Sign bits were used in early computers, but were soon abandoned.No modern computers use sign bits!

Page 7: Numeric/Instruction  Representation

Solution: 2’s complement numbers

CS2710 Computer Organization 7

Leading 0’s mean positive, leading 1’s mean negative

Example for 32 bits:1111 1111 1111 1111 1111 1111 1111 11002

= –1×231 + 1×230 + … + 1×22 +0×21 +0×20

= –2,147,483,648 + 2,147,483,644 = –410

32 bits can represent the values–2,147,483,648 to +2,147,483,647Range: –2n – 1 to +2n – 1 – 1

n 1 n 2 1 0n 1 n 2 1 0x x 2 x 2 x 2 x 2

Page 8: Numeric/Instruction  Representation

Shortcut: Converting a number into 2’s complement1. Convert the absolute value of the number

into a binary number2. Complement the bits (1-> 0, 0->1)3. Add 1 to the value

Example: Convert -51 (decimal) to binary using 2’s complement.

CS2710 Computer Organization 8

Page 9: Numeric/Instruction  Representation

Sign extension• Representing a number using more bits– Preserve the numeric value

• Replicate the sign bit to the left– c.f. unsigned values: extend with 0s

• Examples: 8-bit to 16-bit– +2: 0000 0010 => 0000 0000 0000 0010– –2: 1111 1110 => 1111 1111 1111 1110

CS2710 Computer Organization 9

Page 10: Numeric/Instruction  Representation

Example• Convert -51 to a 16 bit signed binary (base 2)

representation based on the 2’s complement value from before

CS2710 Computer Organization 10

Page 11: Numeric/Instruction  Representation

Converting binary to hex (CE1900 review)

• Hexadecimal – Base 16– Group binary digits into sets of 4– Convert each group into a hex digit

CS2710 Computer Organization 11

0000 0 1000 80001 1 1001 90010 2 1010 A0011 3 1011 B0100 4 1100 C0101 5 1101 D0110 6 1110 E0111 7 1111 F

Page 12: Numeric/Instruction  Representation

Example• Convert the number 10100101 to hex

• Convert the number 1100100 to hex– What decimal number is this?

CS2710 Computer Organization 12

Page 13: Numeric/Instruction  Representation

Representing Instructions

• Instructions are represented in binary– Called machine code

• MIPS instructions (e.g. add $t0, $s1, $s2)– Encoded as 32-bit words– Small number of formats– Regularity in the pattern

• Register numbers$t0 – $t7 are registers 8 – 15$t8 – $t9 are registers 24 – 25$s0 – $s7 are registers 16 – 23§2.5 Representing Instructions in the Computer

Page 14: Numeric/Instruction  Representation

MIPS R[egister]-format Instructions

• Instruction fields– op: operation code (opcode)– rs: first source register number– rt: second source register number– rd: destination register number– shamt: shift amount (00000 for now)– funct: function code (extends opcode)

• For add, $t0, $s1, $s2 (see the green card in the textbook)– op = 0– rs = $s1, register 17– rt = $s2, register 18– rd = $t0, register 8– shamt = 0– funct = 32

op rs rt rd shamt funct6 bits 6 bits5 bits 5 bits 5 bits 5 bits

Page 15: Numeric/Instruction  Representation

MIPS R-format for the add instruction

• For add, $t0, $s1, $s2 – op = 0 (for add)– rs = $s1, register 17– rt = $s2, register 18– rd = $t0, register 8– shamt = 0 (for add)– funct = 32 (for add)

op rs rt rd shamt funct6 bits 6 bits5 bits 5 bits 5 bits 5 bits

0 17 18 8 0 326 bits 6 bits5 bits 5 bits 5 bits 5 bits

000000 10001 10010 01000 00000 1000006 bits 6 bits5 bits 5 bits 5 bits 5 bits

Q1: why 5 bits for register values???

Q2: What is the hex equivalent?

add instruction syntax:add rd, rs, rt(See the green card in the textbook)

Page 16: Numeric/Instruction  Representation

MIPS I[mmediate]-format Instructions

• Instruction fields– op: operation code (opcode)– rs: first source register number– rt: second source register number– Last field: constant value or 16-bit base address offset

• For addi, $t0, $s1, 100 # t0 = s1 + 100– op = 8– rt = $t0, register 8– rs = $s1, register 17– Constant =100

op rs rt Constant or address6 bits 5 bits 5 bits 16 bits

addi instruction syntax:addi rt, rs, value (sign-extended)(See the green card in the textbook)

0010 00 10 001 0 1000 0000 0000 0110 01006 bits 5 bits 5 bits 16 bits

Page 17: Numeric/Instruction  Representation

MIPS I[mmediate]-format Instructions

• Instruction fields– op: operation code (opcode)– rs: first source register number– rt: second source register number– Last field: constant value or 16-bit base address offset

• For lw $t0, 1200($t1) #load value from memory ref’d by t2 into t1– op = 35– rt = $t0, register 8– rs = $t1, register 9– Address offset=1200 (bytes)

op rs rt Constant or address6 bits 5 bits 5 bits 16 bits

lw instruction syntax:lw rt, (offset)rs, (See the green card in the textbook)

1000 11 01 001 0 1000 0000 0100 1011 00006 bits 5 bits 5 bits 16 bits