cs 300 – lecture 11 intro to computer architecture / assembly language quiz wrapup strings and...
Post on 20-Dec-2015
221 views
TRANSCRIPT
CS 300 – Lecture 11
Intro to Computer Architecture
/ Assembly Language
Quiz Wrapup
Strings and Characters and More
C Programming
We're going to start doing some C programming. We'll write C code and run it on the MIPS simulator as well as your laptops.
Does everyone plan to run C on their own machine or will we need lab computers with C compilers?
Please install the MIPS simulator from your textbook CD or by googling "SPIM"
We'll also need the gcc cross compiler – I haven't tried to install this yet.
A Quick Quiz Tour
Computers are commercially successful strictly due to their speed / cost: T/F?
Supercomputers go so fast because they are able to do many things at once: T/F?
Benchmarking
When you run standard benchmarks to see how fast a computer is, which aspects of the system are being tested? a) The raw speed of the processor b) The quality of the compiler that generates the code c) The ability of the programmer to write a fast benchmark d) The quality of the operating system as it manages the memory and page table
Array Code
register $a0 = a register $a1 = ia) a[4] lw $t0, 16($a0)b) a[i-1] sll $t1, $a1, 2 add $t1, $a0, $t1 lw $t0, -4($t1)c a[4*i] sll $t1, $a1, 4 add $t1, $a0, $t1 lw $t0, 0($t1)
Be A Compiler!
int f(int x, int y) {
return(x+y-10); }
f: add $v0, $a0, $a1
addi $v0, $v0, -10
jr $ra
Logical Operations
a) To clear (set to 0) a field you use the and instructionb) To extract a field and place it in a register by itself, you need to use a shift instruction to move the field to the right of the word and then an and to clear out other fields.c) To set a field to all 1 bits, an "or" instruction would be used.d) If a field contains a signed integer, when it is extracted into a register the upper bits ofthe register need to contain the sign bit of the field.
Bogus MIPS Instructions
a) add $s1, $s2, 4($s3) - no memory ref!
b) addi $t1, $t2, FFFFFF - to big
c) lw $t1, $s1($s2) - offset must be constant
d) sw $a1, 3($zero) – not word aligned
And Now for the Pentium …
_fact:pushl %ebpmovl %esp, %ebpsubl $8, %espcmpl $0, 8(%ebp)jne L2movl $1, -4(%ebp)jmp L1
%ebp = $fp esp = $sp
From "gcc –S"
See 2.16 for more
And Now for the Pentium …
L2: movl 8(%ebp), %eaxdecl %eaxmovl %eax, (%esp)call _factimull 8(%ebp), %eaxmovl %eax, -4(%ebp)
L1: movl -4(%ebp), %eaxleaveret
More About $fp
Why have both $sp and $fp?* Stack use varies within a function (why?). Using $fp, we always know where locals are* $fp allows for the creation of chained environments. This is needed in Java for nested objects.* $fp can accept incoming parameters from the stack top
Note that you often don't need $fp at all.
Bytes and Characters
Programs that manipulate text have always had to choose a representation for text.It used to be common to encode characters in 6 bits – that allowed for A-Z, 0-9, and most common punctuation. Note that the univac word was 36 and the CDC word was 60!Then they invented lower case letters.This led to a 7 bit character set: ASCII
The ASCII Character Set
This was the character set of choice for MANY years. The first 32 characters were "control" or non-printing. This is where the "control" key on your computer comes from. Cool control chars: tab, bell, linefeed, carriage return, vtab, null, ETX, EOT, backspace, escape…
Many of these are still around although they now do things unrelated to the original ASCII intentions.
ASCII
The "top bit" of an ASCII byte was unused – this was later used to extend ASCII to a host of 8-bit character sets. These are generally european and Latin-1 is considered the standard 8 bit character set.It was originally assumed that 8 bits would always be enough to encode a character of text – computers are built around an 8 bit byte so that string manipulation is simplified
Strings
It's bad enough we don't know how to represent characters – we also don't have a standard representation of strings.* Array of char (Java) – length is part of the string* Null terminated pointer-based representation* Immutable strings* Hardware represented stringsBefore we can talk about turning string-based functions into code we need to know which representation is being used.
The C String
C adopted a convention of null terminated strings. These are notoriously prone to errors but very easy to use.Types: char – a single character (often 8 bits) char * - a pointer to a character (a string)The char * type is directly supported by MIPS and other machines with byte addressable memories.
C Pointers and Arrays
The C language freely interchanges the notions of pointers and arrays.
char x[] -- x is an array of char
char *x -- x is a pointer to an array
x[3] -- The 4th character in x
*(x+3) -- The 4th character in x
Variables are (essentially) 1 element arrays since you can make a pointer to them.
Get used to this!
A String Example
void strcpy(char *to, char *from)
{ while (( *to++ = *from++) != '\0');}
I'm purposely deviating from the textbook string copy to show:
* A different piece of C that does the same thing
* A more efficient piece of code (no stack needed)
Coding strcpy
strcpy: lb $t0, 0($a1)
sb $t0, 0($a0)
addi $a1, $a1, 1
addi $a0, $a0, 1
bne $t0, $zero, strcpy
jr $ra