ECE232: Hardware Organization and Design Lecture 4: Logic Operations - - PowerPoint PPT Presentation

ece232 hardware organization and design
SMART_READER_LITE
LIVE PREVIEW

ECE232: Hardware Organization and Design Lecture 4: Logic Operations - - PowerPoint PPT Presentation

ECE232: Hardware Organization and Design Lecture 4: Logic Operations and Introduction to Conditionals Adapted from Computer Organization and Design , Patterson & Hennessy, UCB Overview Previously examined arithmetic operations R and


slide-1
SLIDE 1

Adapted from Computer Organization and Design, Patterson & Hennessy, UCB

ECE232: Hardware Organization and Design

Lecture 4: Logic Operations and Introduction to Conditionals

slide-2
SLIDE 2

ECE232: Logic Operations + Intro to Conditionals 2

Overview

  • Previously examined arithmetic operations
  • R and I instructions formats
  • Microprocessors also require logic operations
  • NAND, OR, AND, etc
  • Operations performed in the ALU
  • Similar to logic operations
  • Big picture
  • Logic and arithmetic operations are fundamental to

computer operation

  • Introduction to conditional operations
  • How computers make choices
slide-3
SLIDE 3

ECE232: Logic Operations + Intro to Conditionals 3

Typical Operations (little change since 1960)

Data Movement Load (from memory) Store (to memory) register-to-register move input (from I/O device)

  • utput (to I/O device)

push, pop (to/from stack) Arithmetic integer (binary + decimal) or FP Add, Subtract, Multiply, Divide Logical not, and, or, set, clear Shift shift left/right, rotate left/right Control (Jump/Branch) unconditional, conditional Subroutine Linkage call, return Interrupt trap, return Graphics (MMX) parallel subword ops (e.g., 4-16 bit add)

slide-4
SLIDE 4

ECE232: Logic Operations + Intro to Conditionals 4

MIPS Logical Instructions

Instruction Example Meaning Comment and and $1,$2,$3 $1 = $2 & $3 3 reg. operands; Logical AND

  • r
  • r $1,$2,$3

$1 = $2 | $3 3 reg. operands; Logical OR xor xor $1,$2,$3 $1 = $2 $3 3 operands; Logical XOR and immediate andi $1,$2,10 $1 = $2 & 10 Logical AND reg,constant

  • r immediate
  • ri $1,$2,10

$1 = $2 | 10 Logical OR reg, constant xor immediate xori $1, $2,10 $1 = $2  10 Logical XOR reg, constant shift left logical sll $1,$2,10 $1 = $2 << 10 Shift left by constant shift right logical srl $1,$2,10 $1 = $2 >> 10 Shift right by constant

slide-5
SLIDE 5

ECE232: Logic Operations + Intro to Conditionals 5

Logical Operations

  • Instructions for bitwise manipulation

Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR | |

  • r, ori

Bitwise NOT ~ ~ nor

  • Useful for extracting and inserting groups of bits in a word
slide-6
SLIDE 6

ECE232: Logic Operations + Intro to Conditionals 6

Shift Operations

  • shamt: how many positions to shift
  • Shift left logical
  • Shift left and fill with 0 bits
  • sll by i bits multiplies by 2i
  • Shift right logical
  • Shift right and fill with 0 bits
  • srl by i bits divides by 2i (unsigned only)
  • p

rs rt rd shamt funct

6 bits 6 bits 5 bits 5 bits 5 bits 5 bits

slide-7
SLIDE 7

ECE232: Logic Operations + Intro to Conditionals 7

AND Operations

  • Useful to mask bits in a word
  • Select some bits, clear others to 0

and $t0, $t1, $t2

0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t2 $t1 0000 0000 0000 0000 0000 1100 0000 0000 $t0

slide-8
SLIDE 8

ECE232: Logic Operations + Intro to Conditionals 8

OR Operations

  • Useful to include bits in a word
  • Set some bits to 1, leave others unchanged
  • r $t0, $t1, $t2

0000 0000 0000 0000 0000 1101 1100 0000 0000 0000 0000 0000 0011 1100 0000 0000 $t2 $t1 0000 0000 0000 0000 0011 1101 1100 0000 $t0

slide-9
SLIDE 9

ECE232: Logic Operations + Intro to Conditionals 9

NOT Operations

  • Useful to invert bits in a word
  • Change 0 to 1, and 1 to 0
  • MIPS has NOR 3-operand instruction
  • a NOR b == NOT ( a OR b )

nor $t0, $t1, $zero

0000 0000 0000 0000 0011 1100 0000 0000 $t1 1111 1111 1111 1111 1100 0011 1111 1111 $t0

Register 0: always read as zero

slide-10
SLIDE 10

ECE232: Logic Operations + Intro to Conditionals 10

MIPS Registers and Usage

Name

Register number Usage

$zero

the constant value 0

$at

1 reserved for assembler

$v0-$v1

2-3 values for results and expression evaluation

$a0-$a3

4-7 arguments

$t0-$t7

8-15 temporary registers

$s0-$s7

16-23 saved registers

$t8-$t9

24-25 more temporary registers

$k0-$k1

26-27 reserved for Operating System kernel

$gp

28 global pointer

$sp

29 stack pointer

$fp

30 frame pointer

$ra

31 return address

slide-11
SLIDE 11

ECE232: Logic Operations + Intro to Conditionals 11

Conditional Operations

  • Branch to a labeled instruction if a condition is true
  • Otherwise, continue sequentially
  • beq rs, rt, L1
  • if (rs == rt) branch to instruction labeled L1;
  • bne rs, rt, L1
  • if (rs != rt) branch to instruction labeled L1;
  • j L1
  • unconditional jump to instruction labeled L1
slide-12
SLIDE 12

ECE232: Logic Operations + Intro to Conditionals 12

MIPS Conditional Branch Instructions

  • Conditional branches allow decision making

beq R1, R2, LABEL if R1==R2 goto LABEL bne R3, R4, LABEL if R3!=R4 goto LABEL beq $s1, $s2, 25 if ($s1==$s2) PC = PC + 4 + 4*25 else PC = PC + 4

  • Example

C Code if (i==j) goto L1; f = g + h; L1: f = f - i;

  • Assembly

beq $s3, $s4, L1 add $s0, $s1, $s2 L1: sub $s0, $s0, $s3

{

Address of next sequential instruction Offset in bytes

slide-13
SLIDE 13

ECE232: Logic Operations + Intro to Conditionals 13

Binary Representation - Branch

  • Branch instructions use I-Format
  • ffset is added to PC when branch is taken

beq r0, r1, offset has the effect: if (r0==r1) pc = pc + 4 + (offset << 2) else pc = pc + 4;

  • Offset is specified in instruction words (why?)
  • What is the range of the branch target addresses?
  • p

rs rt

  • ffset

6 bits 5 bits 5 bits 16 bits

Conversion to byte offset

slide-14
SLIDE 14

ECE232: Logic Operations + Intro to Conditionals 14

Binary Representation - Jump

  • Jump Instruction uses J-Format (op=2)
  • What happens during execution?

PC = PC[31:28] : (IR[25:0] << 2)

  • p

address 6 bits 26 bits

Conversion to byte offset Concatenate upper 4 bits

  • f PC to form complete

32-bit address

slide-15
SLIDE 15

ECE232: Logic Operations + Intro to Conditionals 15

if statement

if ( condition ) { statements } # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, if_end_label # MIPS code for the statements if_end_label:

slide-16
SLIDE 16

ECE232: Logic Operations + Intro to Conditionals 16

Compiling If Statements

  • C code:

if (i==j) f = g+h; else f = g-h;

  • f, g, … in $s0, $s1, …
  • Compiled MIPS code:

bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: …

Assembler calculates addresses

slide-17
SLIDE 17

ECE232: Logic Operations + Intro to Conditionals 17

if else statement

if ( condition ) { if-statements } else { else-statements } # MIPS code for the condition expression

#(if condition satisfied set $t0=1)

beq $t0, $zero, else_label # MIPS code for the if-statements j if_end_label else_label: # MIPS code for the else-statements if_end_label:

slide-18
SLIDE 18

ECE232: Logic Operations + Intro to Conditionals 18

Summary

  • We have now covered the major “operation” instructions
  • Most programs require lots of arithmetic and logic operations
  • Can use registers or immediate operands
  • Be sure to keep instruction formats in mind
  • Control operations
  • Modify the flow of the program
  • What’s next?
  • Control operations – programs require decisions
  • Implementing control
  • Implementing methods/subroutines