Announcements
ECE 15B COMPUTER ORGANIZATION
• Homework #1 Due tomorrow (Friday 4/10) at 5:00 PM • Homework #2 will be posted by Saturday and is due next Friday (4/17) • Quiz #1 moved to Tuesday 4/21 from Thursday 4/23 • Project #1 will be assigned next week
Lecture 4
Arithmetic/Data Transfer April 9, 2009
Dr. Rahul Singh Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
2
Lecture 4: Arithmetic/Data
Review: Abstract View of the Implementation
Overview • Addition and Subtraction • Multiply and Divide • Memory Access
Control
A 32 Rw Ra Rb 32 32 32-bit Registers B
Clk
PC
Address
Clk
ALU
Next Address
Ideal Instruction Instruction Control Signals Conditions Memory Rd Rs Rt 5 5 5 Instruction Data Data 32 Address Ideal Out Data Data Memory In
32
Clk
Datapath Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
3
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
4
1
Review: Assembly Language
Review: Assembly Variables - Registers
• Basic job of a CPU: execute lots of instructions • Instructions are the primitive operations that the CPU may execute. • Different CPUs implement different sets of instructions. The set of instructions a particular CPU implements is an Instruction Set Architecture (ISA)
• Unlike HLL like C or Java, assembly cannot use variables • Why not? Keep Hardware Simple
• Assembly Operands are registers • limited number of special locations built directly into the hardware • operations can only be performed on these!
• Examples: Intel 80x86 (Pentium 4), IBM/Motorola PowerPC (Macintosh), MIPS, Intel IA64, ... Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
5
• Benefit: Since registers are directly in hardware, they are very fast
(faster than 1 billionth of a second) Lecture 4: Arithmetic/Data Transfer Instructions
Review: Assembly Variables
• Instruction Syntax: [label:] Op-code [oper. 1], [oper. 2], [oper. 3] [#comment] (0)
(2)
(3)
(4)
(5)
1) operation name 2,3,4) operands
5) Comments
0) label field is optional, will discuss later.
• for arithmetic and logic instruction
2) operand getting result (“destination”)
3) 1st operand for operation (“source1”)
4) 2nd operand for operation (“source2”)
$8 - $15 $t0 - $t7 (correspond to temporary variables) • Later will explain other 16 register names April 9, 2009
(1)
• where:
$0, $1, $2, … $30, $31 $16 - $23 $s0 - $s7 (correspond to C variables)
Electrical and Computer Engineering University of California, Santa Barbara
6
April 9, 2009
Review: MIPS Syntax
• Registers are numbered from 0 to 31 • Each register can be referred to by number or name • Number references:
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
• Syntax is rigid: • 1 operator, 3 operands • Why? Keep hardware simple via regularity 7
Lecture 3: Arithmetic Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 7, 2009
8
2
Review: Addition & Subtraction of integers • Addition in Assembly
• How do we do this?
• Example:
add
$s0, $s1, $s2
f = (g + h) - (i + j);
(in MIPS)
• Use intermediate temporary register
• Equivalent to: a = b + c (in C) • Where MIPS registers $s0, $s1, $s2 are associated with C variables a, b, c
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
• Subtraction in Assembly • Example:
sub
Review: Addition & Subtraction of Integers
$s3, $s4, $s5
# temp = g + h
# temp = i + j
# f = (g+h) - (i+j)
• where MIPS registers $s0,$s1,$s2, and $s3 are associated with C variables f, g, h, and i
(in MIPS)
• Equivalent to: d = e - f (in C) • Where MIPS registers $s3, $s4, $s5 are associated with C variables d, e, f Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
9
April 9, 2009
Review: Immediates
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
10
Integer Multiplication • Paper and pencil example (unsigned):
• Immediates are numerical constants. • They appear often in code, so there are special instructions for them. • Add Immediate:
addi $s0, $s1, 10
Lecture 4: Arithmetic/Data Transfer Instructions
Multiplicand Multiplier
# f = g + 10 (in C)
• where MIPS registers $s0 and $s1 are associated with C variables f and g
• Syntax similar to add instruction, except that last argument is a number instead of a register.
1000 8 x 1001 x 9 1000 0000x 0000xx 1000xxx 1001000 =1*26 + 1*23=72
• m bits x n bits = m + n bit product Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
11
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
12
3
Multiplication
Multiplication • Example: • in C: a = b * c;
• In MIPS, we multiply registers, so: • 32-bit value x 32-bit value = 64-bit value
• in MIPS:
• Syntax of Multiplication (signed): mult register1, register2
• let b be $s2 • let c be $s3 • let a be $s0 and $s1 (since it may be up to 64 bits)
mult $s2,$s3
mfhi $s0
mflo $s1
# b*c
# move upper half of
# product into $s0"
# move lower half of"
# product into $s1 • Note: Often, we only care about the lower half of the product.
• Multiplies 32-bit values in those registers & puts 64-bit product in special result regs: • puts product upper half in hi, lower half in lo • hi and lo are 2 registers separate from the 32 general purpose registers
• To move from hi, lo to another register use:
mfhi register
mflo register Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
13
Lecture 4: Arithmetic/Data Transfer Instructions
Integer Division
April 9, 2009
14
Division • Syntax of Division (signed): div register1, register2
• Paper and pencil example (unsigned): • Decimal digits limited to only 0 or 1
• Divides 32-bit register 1 by 32-bit register 2 (divisor):
Quotient Divisor
Electrical and Computer Engineering University of California, Santa Barbara
• • • •
Dividend
Register 1 is dividend Register 2 is divisor puts remainder of division in hi, puts quotient in lo
• Implements C division (/) and modulo (%), example:
a = c / d;
b = c % d;
• In MIPS: a->$s0; b->$s1; c->$s2; d->$s3
Remainder Dividend = Quotient x Divisor + Remainder Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
15
div $s2,$s3
mflo $s0
mfhi $s1 Lecture 4: Arithmetic/Data Transfer Instructions
# lo=c/d, hi =c%d
# get quotient
# get remainder Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
16
4
Assembly Operands: Memory
Anatomy: 5 components of any Computer
• C variables map into registers • What about large data structures like arrays?
• If operands are in memory • Registers are in the datapath of the processor
• memory contains such data structures • 1 of 5 components of a computer
• But MIPS arithmetic instructions only operate on registers
Computer Processor
• never directly on memory.
Control (“brain”)
• Data transfer instructions • transfer data between registers and memory:
Datapath Registers
• Memory to register • Register to memory Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
17
Data Transfer: Memory to Register
Output
These are “data transfer” instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
18
• two values are required:
• specify this by number (0 - 31) or symbolic name ($s0, …, $t0, …)
• Memory address: (not as simple) • Think of memory as a single one-dimensional array • We can address it simply by supplying a pointer to a memory location or address. • Other times, will want be able to offset from this pointer.
Remember: Load FROM memory April 9, 2009
Input
• To specify a memory address
• Register:
Electrical and Computer Engineering University of California, Santa Barbara
Lecture 4: Arithmetic/Data Transfer Instructions
Devices
Data Transfer: Memory to Register
• To transfer a word of data, we need to specify two things:
Lecture 4: Arithmetic/Data Transfer Instructions
Memory
• Must transfer them to the processor to operate on them • And then transfer back to memory when done
19
• Register containing a pointer to memory • Numerical offset (in bytes)
• The desired memory address is the sum of these two values. • Example:
8($t0) • specifies the memory address pointed to by the value in $t0 plus 8 bytes Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
20
5
Data Transfer: Memory to Register
Data Transfer: Memory to Register
• MIPS Load Instruction Syntax:
• Example:
lw $t0, 12($s0)
lw register#, offset(register#)
(1) (2) (3) (4)
• Adds 12 bytes to the the pointer in $s0
• Then loads into register $t0 the value in the addressed memory location
1) operation name
2) register that will receive value
3) numerical offset in bytes
4) register containing pointer to memory
• Notes: • $s0 is the base register • 12 is the offset • Offset is generally used in accessing elements of an array or structure • Base register points to beginning of array or structure
• lw - meaning Load Word • 32 bits or one word are loaded at a time Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
21
Lecture 4: Arithmetic/Data Transfer Instructions
Data Transfer: Register to Memory
22
• That value type can be: • (signed) int • unsigned int • pointer (memory address), etc…
• 32 bits or one word are loaded at a time
• If you write:
add $t2,$t1,$t0
Data
Flow
• then $t0 and $t1 better contain values
• Instruction first calculates a memory address
• If you write
lw $t2, 0($t0)
• Adds 16 bytes to the the pointer in $s0
• Then stores value in register $t0 into the addressed memory location • Remember: Store INTO Memory Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
• Key Concept: A register can hold any 32-bit value.
• sw - meaning Store Word
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
Pointers vs. Values
• Want to store value from a register into memory • Store and Load instruction syntaxes are identical
sw register#, offset(register#) • Example:
sw $t0, 16($s0)
• Instruction first calculates a memory address
• where:
Lecture 4: Arithmetic/Data Transfer Instructions
Data Flow
April 9, 2009
• then $t0 better contain a pointer
Donʼt mix these up! 23
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
24
6
Addressing: Byte vs. word
Compilation with Memory • What offset in lw to select A[8] in C?
• Every word in memory has an address • Similar to an index in an array
• (4 bytes/word) x (8 words) = (32 bytes) to select A[8]
• Early computers numbered words like C numbers elements of an array:
• Compile by hand using registers: g = h + A[8]; (in C)
Memory[ 0 ], Memory[ 1 ], Memory[ 2 ], …
• where: • g: $s1 • h: $s2 • base address of A: $s3
Called the “address” of a word
• Computers needed to access bytes (8-bits) as well as words (1 word = 4 bytes), • Today machines address memory as bytes • Hence 32-bit (4 byte) word addresses differ by 4
• 1st transfer from memory to register:
lw
add
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
#Load value in A[8] into $t0
# $s1 = h+A[8]
• Add 32 to $s3 to select A[8], put into $t0 • Next add it to h and place in g
Memory[ 0 ], Memory[ 4 ], Memory[ 8 ], … Lecture 4: Arithmetic/Data Transfer Instructions
$t0, 32($s3)
$s1, $s2, $t0
25
Notes about Memory
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
26
More Notes about Memory: Alignment
• Pitfall: Forgetting that sequential word addresses in machines with byte addressing do not differ by 1.
• MIPS requires that all words start at byte addresses that are multiples of 4 bytes 0
• Many an assembly language programmer has toiled over errors made by assuming that the address of the next word can be found by incrementing the address in a register by 1 instead of by the word size in bytes. • To be word aligned, remember that for both lw and sw, the sum of the base address and the offset must be a multiple of 4
Aligned Not Aligned
1
2
3
Last hex digit of address is:
0, 4, 8, or Chex 1, 5, 9, or Dhex 2, 6, A, or Ehex 3, 7, B, or Fhex
Alignment: objects must fall on address that is multiple of their size. Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
27
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
28
7
Role of Registers vs. Memory
Conclusion • In MIPS Assembly Language:
• What if more variables than registers? • Compiler tries to keep most frequently used variables in registers • Less common in memory: spilling
• • • •
• Why not keep all variables in memory? • Smaller is faster:
registers are faster than memory • Registers more versatile:
• Memory is byte-addressable, but lw and sw access one word at a time. • A pointer (used by lw and sw) is just a memory address, so we can add to it or subtract from it (using offset).
• MIPS arithmetic instructions can read 2 registers, operate on them, and write to 1 register per instruction • MIPS data transfer only read or write 1 operand per instruction, and no operation Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
Registers replace C variables One Instruction (simple operation) per line Simpler is Better Smaller is Faster
April 9, 2009
29
April 9, 2009
31
Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
April 9, 2009
30
Review • Instructions so far:
add, addi,
sub
mult, div
mfhi, mflo
lw, sw
• Registers so far: • C Variables: $s0 - $s7 • Temporary Variables: $t0 - $t9 • Zero: $zero Lecture 4: Arithmetic/Data Transfer Instructions
Electrical and Computer Engineering University of California, Santa Barbara
8