Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 300 – Lecture 11 Intro to Computer Architecture / Assembly Language Quiz Wrapup Strings and Characters and More.

Similar presentations


Presentation on theme: "CS 300 – Lecture 11 Intro to Computer Architecture / Assembly Language Quiz Wrapup Strings and Characters and More."— Presentation transcript:

1 CS 300 – Lecture 11 Intro to Computer Architecture / Assembly Language Quiz Wrapup Strings and Characters and More

2 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.

3 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?

4 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

5 DeMorgan's Law

6 Simplify! XYZ + XY’Z + XX’ = XYZ + XY'Z + 0 = XYZ + XY'Z = XZ(Y+Y') = XZ1 = XZ

7 A State Machine 01 X=1 X=0 S XS' 0 0 0 11 1 01 1 0 S SX' + S'X

8 Array Code register $a0 = a register $a1 = i a) 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)

9 Be A Compiler! int f(int x, int y) { return(x+y-10); } f: add $v0, $a0, $a1 addi $v0, $v0, -10 jr $ra

10 Logical Operations a) To clear (set to 0) a field you use the and instruction b) 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 of the register need to contain the sign bit of the field.

11 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

12 And Now for the Pentium … _fact: pushl%ebp movl%esp, %ebp subl$8, %esp cmpl$0, 8(%ebp) jneL2 movl$1, -4(%ebp) jmpL1 %ebp = $fp esp = $sp From "gcc –S" See 2.16 for more

13 And Now for the Pentium … L2:movl8(%ebp), %eax decl%eax movl%eax, (%esp) call_fact imull8(%ebp), %eax movl%eax, -4(%ebp) L1:movl-4(%ebp), %eax leave ret

14 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.

15 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

16 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.

17 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

18 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 strings Before we can talk about turning string-based functions into code we need to know which representation is being used.

19 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.

20 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 4 th character in x *(x+3) -- The 4 th character in x Variables are (essentially) 1 element arrays since you can make a pointer to them. Get used to this!

21 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)

22 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

23 Java Strings A Java String is much different: * It contains a length – no "out of bounds" issues * It must work with the GC – extra info is needed to make this happen * Java throws exceptions when errors occur You can't "look inside" the Java version in any nice way. That's why we use C


Download ppt "CS 300 – Lecture 11 Intro to Computer Architecture / Assembly Language Quiz Wrapup Strings and Characters and More."

Similar presentations


Ads by Google