+ All Categories
Home > Documents > 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

Date post: 18-Jan-2018
Category:
Upload: bryan-goodwin
View: 216 times
Download: 0 times
Share this document with a friend
Description:
3 Homework 5: 2 foo(int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; return; }
22
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002
Transcript
Page 1: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

1

Signed ArithmeticLogical Operations

Ellen SpertusMCS 111

October 1, 2002

Page 2: 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)

byte offset contents 0 00000000 1 00000000 2 00000000 3 11110010 4 00000000 5 00000000 6 00000000 7 00000110 8 00000000 9 00000000

10 00000000 11 00001110

Diagram assumes Big-Endian

Page 3: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

3

Homework 5: 2foo(int i, int j) { int temp;

temp = a[i];

a[i] = a[j];

a[j] = temp;

return;}

Page 4: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

4

From representation to value

1000

Page 5: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

5

Today

• Review: unsigned numbers• Signed numbers• MIPS instructions for signed and

unsigned instructions• Character encoding• Logical operations

Page 6: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

6

Unsigned binary arithmetic

10012= 1x2 3 + 0x2 2 + 0x2 1 + 1x2 0

Eights (2 3) columnFours (2 2) columnTwos (2 1) columnOnes (2 0) column

• Biggest number?• Smallest number?

Page 7: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

7

Signed binary arithmetic (two’s complement)

10012= 1x-23 + 0x22 + 0x21 + 1x20

Minus Eights (-23) columnFours (22) columnTwos (21) columnOnes (20) column

• Sign bit• Biggest number?• Smallest number?

Page 8: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

8

Signed binary to decimal

-8 4 2 1 1 0 0 1 = 1 1 1 1 = 0 1 1 1 = 1 0 0 0 =

Page 9: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

9

Shortcut

• To negate a number– Invert each bit– Add 1

• Practice– 1001 0110 0111 (710), so the original was

-7– 1111– 1000

Page 10: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

10

Relative sizes

Which 4-bit number is bigger: 1000 or 0111?

– Signed comparison

– Unsigned comparison

Page 11: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

11

Sign extension

The number 710

0111 000001110000000000000111

The number -110

1111 111111111111111111111111

Extend the sign bit all the way to the left.

Page 12: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

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)

400 10000001 404 408 412 416

Page 13: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

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 = 11111111111111111111111111111111two

a1 = 00000000000000000000000000000000two

slt $t0, $a0, $a1 # signed comparisonsltu $t1, $a0, $a1 # unsigned comparison

Page 14: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

14

How do we add signed numbers?

Just like we always have!

signed unsigned -128 64 32 16 8 4 2 1

1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1

Page 15: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

15

What about overflow?

signed unsigned -128 64 32 16 8 4 2 1

0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1

Page 16: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

16

Definition of overflow

• When a carry bit flows into the sign bit• This can only happen with signed

arithmetic• Machine raises an exception

Page 17: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

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)

Page 18: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

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:

Page 19: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

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

Page 20: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

20

Remainder of today

• Logical operations– Bitwise operations– Shifting

Page 21: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

21

Bitwise operations• Perform the operation on each bit

position• Practice

– Initial values• $t0 = ..00000111• $t1 = ..00000001

– Problems• not $t2, $t0• and $t2, $t0, $t1• or $t2, $t0, $t1

Page 22: 1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.

22

Shifting

• Assume $t0 = 0..00001100• Shift left logical (sll)

– sll $t1, $t0, 2– result:

• Shift right logical (srl)– srl $t1, $t0, 2– result:

• Note: Zeroes are shifted in


Recommended