CPS3340 COMPUTER ARCHITECTURE Lecture 12: Instructor: Ashraf Yaseen

Download Report

Transcript CPS3340 COMPUTER ARCHITECTURE Lecture 12: Instructor: Ashraf Yaseen

1
CPS3340
COMPUTER ARCHITECTURE
Fall Semester, 2013
Lecture 12: Character Data
Instructor: Ashraf Yaseen
10/22/2013
DEPARTMENT OF MATH & COMPUTER SCIENCE
CENTRAL STATE UNIVERSITY, WILBERFORCE, OH
Review
2

Last Class


This Class


Procedures
Handling Character Data
Next Class

Starting and Loading a Program

Linking

Dynamic Linking
3

Byte-encoded character sets
 ASCII:
 95
128 characters
graphic, 33 control
 Latin-1:
 ASCII,

256 characters
+96 more graphic characters
Unicode: 32-bit character set
 Used
in Java, C++ wide characters, …
 Most of the world’s alphabets, plus symbols
 UTF-8, UTF-16: variable-length encodings
§2.9 Communicating with People
Character Data
ASCII
4
Unicode
5
Byte Operation
6

Load Byte (lb)
 Load
a byte from memory
 Place it in the rightmost 8 bits of a register
 Example
lb $t0, 0($sp)

Store Byte (sb)
 Take
a byte from the rightmost 8 bits of a register
 Write it to the memory
 Example
sb $t0, 0($sp)
Byte/Halfword Operations
7


Could use bitwise operations
MIPS byte/halfword load/store
 String
processing is a common case
lb rt, offset(rs)
lh rt, offset(rs)
 Sign extend to 32 bits in rt
lbu rt, offset(rs)
lhu rt, offset(rs)
 Zero extend to 32 bits in rt
sb rt, offset(rs)
sh rt, offset(rs)
 Store just rightmost byte/halfword
String Copy Example
8

C code (naïve):
 Null-terminated
string
void strcpy (char x[], char y[])
{
int i;
i = 0;
while ((x[i]=y[i])!='\0')
i += 1;
}
 Addresses
i
in $s0
of x, y in $a0, $a1
String Copy Example
9

MIPS code:
strcpy:
addi
sw
add
L1: add
lbu
add
sb
beq
addi
j
L2: lw
addi
jr
$sp,
$s0,
$s0,
$t1,
$t2,
$t3,
$t2,
$t2,
$s0,
L1
$s0,
$sp,
$ra
$sp, -4
0($sp)
$zero, $zero
$s0, $a1
0($t1)
$s0, $a0
0($t3)
$zero, L2
$s0, 1
0($sp)
$sp, 4
#
#
#
#
#
#
#
#
#
#
#
#
#
adjust stack for 1 item
save $s0
i = 0
addr of y[i] in $t1
$t2 = y[i]
addr of x[i] in $t3
x[i] = y[i]
exit loop if y[i] == 0
i = i + 1
next iteration of loop
restore saved $s0
pop 1 item from stack
and return
Summary
10

Handling Character Data
What I want you to do
11


Review Chapter 2
Prepare for Midterm II