{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-MIPS-arithmetic

# 08-MIPS-arithmetic - MIPS arithmetic Today well review all...

This preview shows pages 1–8. Sign up to view the full content.

February 17, 2003 ©2001-2003 Howard Huang 1 MIPS arithmetic Today we’ll review all the important ideas of arithmetic from CS231. — Unsigned and signed number representations. — Addition and subtraction with two’s complement numbers. — Overflow detection. These issues are important in understanding MIPS arithmetic instructions. Finally, we review the construction of an ALU that will appear in our CPU designs in the next few weeks.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
February 17, 2003 MIPS arithmetic 2 Unsigned numbers We can store unsigned numbers as their binary equivalents. Bit position i has a decimal weight 2 i , so the decimal value v of an n -bit unsigned binary number a n –1 a n –2 ...a 1 a 0 can be calculated as below. This is just the sum of each digit times the weight of its position. 1 0 1 1 0 1 2 = (2 5 •1) + (2 4 •0) + (2 3 •1) + (2 2 •1) + (2 1 •0) + (2 0 •1) = 45 10 The smallest and largest possible numbers are 0 and 2 n –1. In MIPS, the largest unsigned number is 2 32 –1, or about 4.3 billion. = = 1 0 2 n i i i a v
February 17, 2003 MIPS arithmetic 3 Hexadecimal notation Hexadecimal is frequently used as a shorthand for binary numbers, because one hex digit corresponds to four bits. Converting between binary and hex is easy with a table. B E E F 16 = 1011 1110 1110 1111 2 In SPIM and many high-level programming languages, the prefix 0x denotes a hexadecimal constant, as in 0xBEEF . F 1111 E 1110 D 1101 C 1100 B 1011 A 1010 9 1001 8 1000 7 0111 6 0110 5 0101 4 0100 3 0011 2 0010 1 0001 0 0000 Hex Binary M o o .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
February 17, 2003 MIPS arithmetic 4 Working with unsigned numbers Addition can be done just like in decimal. Multiplication and integer division by powers of 2 can be done using left and right “logical” shifts. 1 0 1 1 0 1 × 10 = 1 0 1 1 0 1 0 45 × 2 = 90 1 0 1 1 0 1 / 10 = 0 1 0 1 1 0 45 / 2 = 22 68 0 0 1 0 0 0 1 23 + 1 1 1 0 1 0 + 45 1 0 1 1 0 1 1 1 1 1 1
February 17, 2003 MIPS arithmetic 5 Logical shifts in MIPS MIPS has sll (shift left logical) and srl (shift right logical) instructions. — A constant specifies the number of bits to shift, from 0 to 31. — 0s are shifted into the right or left sides, respectively. For example, assume that \$t0 contains 0xFEEDBEEF. sll \$t1, \$t0, 4 # \$t1 contains 0xEEDBEEF 0 srl \$t2, \$t0, 12 # \$t2 contains 0x 000 FEEDB Shifts are actually R-type instructions, not I-type! The constant is stored in the 5-bit “shamt” field that we hadn’t seen up until now. 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits func shamt rd rt rs op

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
February 17, 2003 MIPS arithmetic 6 Unsigned overflow One recurring issue in computer arithmetic is dealing with finite amounts of storage, such as 32-bit MIPS registers. Overflow occurs when the result of an operation is too large to be stored. There are many examples of unsigned n -bit overflows. — When there is a carry out of position n –1 in an addition. — When a n –1 is 1 in a multiplication by two. The 6-bit addition and multiplication operations on page 4 both result in overflow, since the correct answers require 7 bits.
February 17, 2003 MIPS arithmetic 7 Signed two’s-complement numbers Signed numbers are represented in two’s complement format.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}