From Calculator to Computer - Computer Engineering Group

Download Report

Transcript From Calculator to Computer - Computer Engineering Group

Review Chpt 2 and 3

Five components of a computer






Input
Output
Memory
Arithmetic & Logic Unit (ALU)
Control
Two types of information

Data and instruction
1
Think of a Calculator

Components in a calculator





Input
Output
Memory
ALU
One type of information

Data
2
Memory in Calculator

One register R, which is implicit



Its content is shown at the LCD panel
Arithmetic operations are performed on R
One memory M, which is explicit



Its content is not shown.
Arithmetic operation on M is limited
M is additional storage in case R is not enough


Copy data from R to M is done by MS
Copy data from M to R is done by MR
3
Memory in Computer (MIPS)

More registers



32 integer registers: $0 to $31
32 floating-point registers: $f0 to $f31
More memory




4 Giga Byte (GB) memory, organized as 1 Gita
Word
A word is 32 bits (b), a Byte (B) is 8 bits
A unique address is designated for each memory
byte
1K=1024=210, 1M=1024K=220, 1G=1024M=230
4
Examples


Which one can store more information:
a 128MB memory card or a half Gb
memory chip?
How many times more data can a 40GB
hard disk store than a 256KB memory?
5
Memory Address
3
2
1
0
00000000
00000004
00000008
...
FFFFFFF8
FFFFFFFC
6
Example: Memory content
chara:
msg1:
msg2:
value:
.word 0x00000061
.asciiz “ab01cd”
.ascii “ab”
.word 15
3
00000000
00000004
00000008
0000000C
00000010
...
00
31
61
??
00
# assume starting mem 0
2
00
30
00
??
00
1
00
62
64
??
00
0
61
61
63
62
0F
7
Data and Instruction



Memory contains data and instruction
The content itself does not tell if it is
data or instruction
Each instruction takes one word, and
instruction address must be a word
address
8
Data Encoding

ASCII code (American Standard Code
for Information Interchange)

Total 128 characters, including






digits (0-9)
alphabets (a-z, A-Z)
math (+, -, *, /, >, <, =, (, ), {, }, [, ], %)
punctuations (!, ,, ., ?, :, ;, ‘, “, `, ,)
special (@, #, $, ^, &, ~, _, |, \)
control (back space, tab, line return, null)
9
Data Encoding


Unicode (universal encoding)
Capable of expressing most languages
in the world
Text
ASCII
UTF-8
UTF-16
big
62 69 67
62 69 67
0062 0069 0067
gro
67 72 6f ??
67 72 6f c3 9f
0067 0072 006f 00df
10
Data Encoding




Fixed point integer
Single precision floating point
Double precision floating point
Question: Which format is most efficient in
expressing a number, say 12345678? How
about 1234.5678?



ASCII character string
Fixed point
Floating point
11
Example on Numbers

What is the following hexadecimal?

8D280000 as binary:


8D280000 as integer (in decimal):


-(2^27+2^26+2^24+2^21)
8D280000 as floating-point (in decimal):



1000 1101 0010 1000 0000 0000 0000 0000
1000 1101 0010 1000 0000 0000 0000 0000
(-1) 2^{26-127}*(1+0.025+0.0625)=
8D280000 as instruction:


1000 1101 0010 1000 0000 0000 0000 0000
lw $8 0($9)
12
Word v.s. Byte


Memory can be addressed either as words,
each 32 bits, or as bytes, each 8 bits
Whether an address is word address or byte
address depends on the instruction
lw $a1, 0($zero)
lw $a1, 2($a2)
lbu $a1, -7($a2)
#
#
#
#
#
$a1 = 00 00 00 61
assume $a2=6
$a1 = 61 00 64 63
assume $a2=12
$a1 = 00 00 00 62
13
ALU

In a calculator, ALU performs operation
between R and the new number typed
on key pad
12+ enter 12 into R and operation is +
3= enter 3, and perform addition with R

In a computer, ALU performs more
complex operations between registers
14
Example

Convert temperature from Fahrenheit in $f0
to Celsius: C=(5/9)*(F-32)


Assume 5.0 is in $f16, 9.0 is in $f17, and 32.0 is in
$f18
div.s $f16, $f16, $f17
# .s is single precision
sub.s $f0, $f0, $f18
mul.s $f0, $f0, $f16
Puzzle: How to swap values in $a1 and $a2
without using additional register?
15
Fixed Point Mult/Div

Assume F temperature is in $a0, store C
temperature result in $a1
addi
addi
mult
mflo
addi
div
mflo
$a1, $a0, -32
# $a1 = F-32
$t0, $zero, 5
# $t0 = 5
$a1, $t0
# mult first reduces error
$a1
# $a1 = (F-32)*5
$t0, $zero, 9
# $t0 = 9
$a1, $t0
$a1
16
IEEE 754 Standard Exception

Normal case


e=1 to 254: (-1)S * 2e-127 * 1.M
Special cases:




Exponent=0, mantissa=0: 0
Exponent=0, mantissa0: denormalized
(-1)S * 2-127 * 0.M
Exponent=255, mantissa=0: Infty
Exponent=255, mantissa 0: NaN
17
Control


In a calculator, human controls the operation
at each step
In a computer, control follows from the
instruction




1. Program Counter (PC) points to the first
instruction to be executed
2. Execute the instruction pointed by PC
3. PC=PC+4, go to 2
If there is branch instruction, PC is modified
18
Exercise

Count the number of 1’s in $a0 and store
result in $s0
or $s0, $zero, $zero
addi $t0, $zero, 1
loop: beq $t0, $zero, exit
and $t1, $a0, $t0
sll $t0, $t0, 1
beq $t1, $zero, loop
addi $s0, $s0, 1
j loop
exit:
#
#
#
#
#
$s0=0
$t0=00000001
if $t0=0, exit
check one bit
shift $t0 left 1 bit
# found a 1
19
Five Address Modes (p. 101)


Address Mode tells where to find the data or
branch address
Immediate addressing



It is rather “immediate” than “addressing” since
the data is included in the instruction
For example, addi $a1, $a1, 100
Register addressing


Data address is register number
For example, add $a1, $a1, $a0
20
Five Addressing Mode (cont’d)

Base (or displacement) addressing


Data address is base (register content)
plus displacement
For example, lw $a1, 12($a0)
21
Five Addressing Mode (cont’d)


The last two modes are for branch only.
Address is instruction address.
PC-relative addressing



Branch address is PC+4+offset*4
For example, beq $a1, $zero, 100
Pseudo-direct addressing


Branch address is 26 bits in instruction
concatenated with top 4 bits of PC
For example, j 10000
22
Addressing Mode Example

C program, assume i and k correspond to $s3 and
$s5, and base address of array a is in $s6:
while (a[i] == k)
i = i + 1;

MIPS assembly code
loop:
sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, exit
addi $s3, $s3, 1
j loop
#
#
#
#
#
#
$t1 = i*4
$t1 = addr of a[i]
$t0 = a[i]
if a[i] != k, exit
i = i+1
go to loop
23
Addressing Mode Example
loop:
sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, exit
addi $s3, $s3, 1
j loop
#
#
#
#
#
#
$t1 = i*4
$t1 = addr of a[i]
$t0 = a[i]
if a[i] != k, exit
i = i+1
go to loop
exit
FFFF0004
FFFF0008
FFFF000C
FFFF0010
FFFF0014
FFFF0018
0
0
19
9
2
0
0
9
22
9
0
32
35
9
8
0
5
8
21
2
8
19
19
1
2
1111 1111 1111 0000 0000 0000 01
24
Homework 2, due Friday


2.21, 2.36
3.27, 3.42
25
Exercise 2.21


Write a subroutine convert to convert an ASCII
decimal string to an integer. You can expect register
$a0 to hold the address of a null-terminated string
containing some combination of the digits 0 through
9. Your program should compute the integer value
equivalent to this string of digits, then place the
number in register $v0. If a non-digit character,
including “-” or “.”, appears anywhere in the string,
your program should stop with the value –1 in
register $v0 .
For example, if register $a0 points to a sequence of
three character 50ten , 52ten , 0ten (the null-terminated
string “24”), then when the program stops, register
$v0 should contain the value 24ten .
26
Exercise 2.36
Consider the following fragment of C code:
for (i=0; i<=100; i=i+1)
a[i] = b[i] +c;
Assume that a and b are arrays of words and
the base address of a is in $a0 and the base
address of b is in $a1 . Register $t0 is
associated with variable i and register $s0
with c.
 Write the code for MIPS. How many
instructions are executed during the running
of this code? How many memory data
27

Chapter 4 Performance

What is performance?



Response time: The time between start and
completion of the task. (unit is time, say sec)
Throughput: the total amount of work done at a
given time. (unit is jobs/sec)
Do the following changes to a computer
system increase/decrease performance?


Replacing the processor by a faster one
Adding additional processors to a multi-processor
system, such as google search engine
28