strings in mips. chapter 2 — instructions: language of the computer — 2 character data...

8
Strings in MIPS

Upload: matthew-greene

Post on 05-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

Strings in MIPS

Page 2: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

Chapter 2 — Instructions: Language of the Computer — 2

Character Data

• Byte-encoded character sets– ASCII: 128 characters• 95 graphic, 33 control

– Latin-1: 256 characters• ASCII, +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 Comm

unicating with People

Page 3: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,
Page 4: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

How NOT to do Strings in MIPS

• Should we try to output a string by putting ASCII values into $a0?

• This is not correct. • Just as in C, you output a string by passing the

MEMORY ADDRESS of the beginning of a sequence of characters (bytes).

• Similarly, if you do a syscall 8 (read_string), the contents of the string read in are not in $a0.

• How could, say, a 256 byte string fit into a 4 byte quantity?

Page 5: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

A look at syscodes

Page 6: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

Hello World

# This is a simple program to print hello world

.data greet: .asciiz "Hello world\n".text

main:li $v0, 4

la $a0, greetsyscall # print the stringjr $ra # return from main

Page 7: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

Another example .data theString:

.space 64 .text main: li $v0, 8 la $a0, theString li $a1, 64 syscall li $v0,4 syscall

Page 8: Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,

• If you run this program and type this in:Hello!