1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002
2 Homework 5: 1 a[0] = a[0] + a[1] - a[2]; lw $a0, __($t0) lw $a1, __($t0) lw $a2, __($t0) add $t1,, sub $t2, $t1, sw $t2, __($t0) Diagram assumes Big-Endian
3 Homework 5: 2 foo(int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; return; }
4 From representation to value 1000
5 Today Review: unsigned numbers Signed numbers MIPS instructions for signed and unsigned instructions Character encoding Logical operations
6 Unsigned binary arithmetic Biggest number? Smallest number?
7 Signed binary arithmetic (two’s complement) Sign bit Biggest number? Smallest number?
8 Signed binary to decimal
9 Shortcut To negate a number –Invert each bit –Add 1 Practice –1001 0110 0111 (7 10 ), so the original was -7 –1111 –1000
10 Relative sizes Which 4-bit number is bigger: 1000 or 0111? –Signed comparison –Unsigned comparison
11 Sign extension The number The number Extend the sign bit all the way to the left.
12 A closer look at load-byte (lb) load byte signed (lb) –Treat the byte as a signed number –Sign extend it to word length –lb $t0, 400($zero) load byte unsigned (lbu) –Treat the byte as an unsigned number –Zero extend it to word length –lbu $t0, 400($zero)
13 MIPS instructions Signed numbers –set less than (slt) –set less than immediate (slti) –load byte (lb) Unsigned numbers –set less than unsigned (sltu) –set less than immediate unsigned (sltiu) –load byte unsigned (lbu) a0 = two a1 = two slt $t0, $a0, $a1 # signed comparison sltu $t1, $a0, $a1 # unsigned comparison
14 How do we add signed numbers? Just like we always have! signed unsigned
15 What about overflow? signed unsigned
16 Definition of overflow When a carry bit flows into the sign bit This can only happen with signed arithmetic Machine raises an exception
17 MIPS instructions (2) Signed numbers –set less than (slt) –set less than immediate (slti) –load byte (lb) –add –add immediate (addi) –sub –subtract immediate (subi) Unsigned numbers –set less than unsigned (sltu) –set less than immediate unsigned (sltiu) –load byte unsigned (lbu) –add unsigned (addu) –add immediate unsigned (addiu) –subtract unsigned (subu) –subtract immediate unsigned (subiu)
18 Pseudo-instructions Definition: Instructions converted by the assembler into real machine instructions Examples: –Load immediate: li $r1, 5 is replaced by: –Subtract immediate: subi $r1, $r1, 5 is replaced by:
19 Differences between signed and unsigned instructions Sign extension vs. Zero extension –lb/lbu Whether to signal overflow/underflow –add/addu, addi/addiu, sub/subu How to compare numbers –slt/sltu, slti/sltiu
20 Remainder of today Logical operations –Bitwise operations –Shifting
21 Bitwise operations Perform the operation on each bit position Practice –Initial values $t0 = $t1 = –Problems not $t2, $t0 and $t2, $t0, $t1 or $t2, $t0, $t1
22 Shifting Assume $t0 = Shift left logical (sll) –sll $t1, $t0, 2 –result: Shift right logical (srl) –srl $t1, $t0, 2 –result: Note: Zeroes are shifted in