14
Students Offering Support CS 241 Exam-AID Package Fall 2008 – Midterm “Raising Marks. Raising Roofs.” Contents Review of Important Concepts Interpreting Bits ..................................................... 1 MIPs Architecture.................................................. 2 Programming in MIPs ............................................. 3 Formal Languages ................................................... 4 State Machines ....................................................... 5 Exam-Style Questions Basic MIPs Programs ............................................. 6 Assemblers ............................................................. 6 Recognizing Languages............................................ 7 www.waterloosos.com [email protected]

08F CS 241FallOct30

Embed Size (px)

DESCRIPTION

cs

Citation preview

Page 1: 08F CS 241FallOct30

Students Offering Support CS 241 Exam-AID Package

Fall 2008 – Midterm

“Raising Marks. Raising Roofs.”

Contents

Review of Important Concepts Interpreting Bits .....................................................1 MIPs Architecture..................................................2 Programming in MIPs .............................................3 Formal Languages ...................................................4 State Machines .......................................................5 Exam-Style Questions Basic MIPs Programs .............................................6 Assemblers .............................................................6 Recognizing Languages............................................7

www.waterloosos.com [email protected]

Page 2: 08F CS 241FallOct30

1

Interpreting Bits Bit A bit is a single binary digit, one or zero. Byte Eight bits! Word A word is the amount of memory read at one time by the CPU. Right now, most computers use 32-bit (4-byte) words. Some new ones use “64 bit architectures” which means they have 8 byte words. Pointers are one word long, so the word size determines the maximum amount of memory your CPU can reference. Unsigned Integers Each bit represents a power of two, with 0

2 at the right. For example,

22164221202121201011043210

=++=!+!+!+!+!=

With n bits, we can store any number from zero to 12 !n .

Signed (Two’s Complement) Integers Each bit represents the same power of two, but the highest (leftmost) bit is negative:

104220212120101103210

!=!+="!"+"+"+"= 16214

64220212120001103210

=+=!"!+!+!+!=4

20

With n bits, we can store any number from 12

!!

n to 121!

!n . To change the sign of a two’s complement integer, flip all the bits and then add one. Hexadecimal Numbers In hexadecimal notation, each digit represents the coefficient of a power of 16. We can have a coefficient as high as 15, so we use 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f as our digits (capital letters are also fine). Hexadecimal numbers are often prefixed with “0x” to avoid confusing them with binary or decimal numbers.

473215162161516216ff2x01010

=+=!+!=!+!=

Page 3: 08F CS 241FallOct30

2

MIPs Architecture Instruction A single MIPs command, used to perform a simple task like adding two numbers, loading a number from memory, or comparing two numbers. CPU Interprets and executes MIPs instructions. You learn all about how this works in CS 251! Register A very fast piece of memory that stores one word (i.e. 32 bits). MIPs has 32 registers, plus a few special ones like the program counter, low, and high. Values must be placed in registers before the CPU can do anything to them.

• $0 always holds the value zero • $30: pointer to the top of the stack • $31: return address for the program • PC: points to the current instruction, incremented by four each time • Low: the 32 low bits of a multiplication or the result of a division (mflo) • High: the 32 high bits of a multiplication or the remainder of a division (mfhi)

RAM Random access memory is the main memory of a computer, where your program is loaded and the stack is stored. Stack The stack is used to store the current state of a program when calling a procedure or otherwise switching contexts. You can push registers’ values onto the top of the stack before calling a procedure and then load them back afterwards to allow the procedure to change register values without breaking the rest of your program. Fetch, Increment, Execute This refers to the process of running a program. First, the instruction pointed to the by the program counter is fetched into the CPU. Then, the program counter is incremented to point to the next instruction. Finally, the loaded instruction is executed by the CPU. Note that in a branch, the program counter is incremented by four in addition to whatever offset is specified by the branch.

Page 4: 08F CS 241FallOct30

3

Programming in MIPs Labels Labels make it easier to write the MIPs equivalent of if/else statements and loops, by naming a specific line of code in the program. We put them in beq and bne instructions to jump to the given line. Symbol Table These are used by the assembler to look up the value of a given label, its value being the memory address of the line it refers to. These are commonly implemented as a hash table, mapping strings (the label names) to integers (the memory address). Relocation We write MIPs programs as if they are loaded at memory address zero. When the program is actually being executed, the operating system will decide where to actually put it in memory. Any commands of the form .word labelname must be relocated by adding on the value of the address where the program is being loaded. Relocation Table These are used by a MERL assembler to mark the lines in the program that will need to be relocated. Linking We can also write a single program in separate files, similar to how we can put different classes in different files in Java and C++. This saves time when compiling large programs since only the file we modify needs to be reassembled. The only tricky part is that we want labels defined in one file to be usable by others. MERL Files MERL files allow us to take advantage of linking and relocation. They include a relocation table and an external symbol table. The external symbol table is a note to the assembler saying either that you have used a label defined in another file, or that a label defined is this file is going to be used by other files. A MERL specification is available on the course website at http://www.student.cs.uwaterloo.ca/~cs241/merl/merl.html.

Page 5: 08F CS 241FallOct30

4

Formal Languages Alphabet An alphabet is a finite set of characters. We often use the symbol ! to stand for an alphabet. For example:

• }1,0{=! , the binary alphabet • },...,,,{ zcba=! , the English alphabet in lowercase • The alphabet of ASCII characters

We could even have an empty alphabet, although that wouldn’t be very useful! Word A word is a finite sequence of symbols from the alphabet. We could write this as *!=w . Some examples:

• 01101, using the binary alphabet • 3399cc, using the hexadecimal alphabet • hello, using the English alphabet

An important word that can be made from any alphabet is å (epsilon). This is the empty word, containing zero characters. Language A language is any set of words. This one is allowed to be infinite. Regular Language A regular language can be represented by a regular expression, a DFA (deterministic finite automata), or an NFA (non-deterministic finite automata). It can be proven that these three things are all “the same” in that anything defined by one can be defined by the others. Regular Expressions Some languages like Java have built in regular expressions. Here we use very simple regular expressions:

• xy the pattern x followed by the pattern y • x* the pattern x repeated zero or more times (but must be finite!) • (x|y) either the pattern x or the pattern y

A pattern could be a regular expression itself, or simply a character from the alphabet.

Page 6: 08F CS 241FallOct30

5

State Machines DFAs Also called finite state machines, these are a set of states that we can walk through to determine if a word is in a language. You also work with state machines in CS 251. The formal definition is as follows: A DFA is a 5-tuple ( )ä,,,, 0 AqQ! , where

• ! is the alphabet • Q is a finite set of states • Qq !0 is the starting state • QA! is a set of accepting states • QQ !"#:ä is the transition function

To use a DFA, we go to the starting state, read in our input one character at a time (each character comes from the alphabet), and transition to the appropriate state. When we have no more input, we see if we have reached an accepting state. Given an input string

nccc ...21 , the pseudo code is as follows:

state 0qs! for 1=j to n ),( jwsTs! end for return As!

NFAs These are non-deterministic finite automata. The difference is that instead of knowing exactly which state to go to given a character of input, we can transition to a few different possible states. This is why they are not deterministic. NFAs are defined the same way as DFAs, except that the transition function returns a set of possible states instead of a single state. Our pseudo code becomes slightly more complicated – the idea is to call the transition function for each of our current possible sets and then union all the results together to create our new set of possible states. At the end, we need at least one of our possible states to be a successful one.

state set }{ 0q=S for 1=j to n U

S

S!

"s

jws ),(ä

end for return 0/!"AS

Page 7: 08F CS 241FallOct30

6

Exam Style Questions

Basic MIPs Programs 1. Given two numbers in registers one and two, put a one in register three if they are both

even and zero otherwise. Then jump to the address stored in register 31.

2. Given a number n in register one, write a MIPs program that calculates the sum n++++ ...321 using a loop. (i.e., do not use the formula for the sum of a series)

3. Translate the following program to hexadecimal machine code. add $3, $1, $0 lis $2 .word 2 mult $3, $2 mflo $3 jr $31

Assemblers 1. Write out the symbol table and relocation table for the following program.

lis $1 .word FOO ; comment FOO: add $1, $2, $0 BAR: ; another comment HELLO: lis $2 .word BAR WORLD: jr $31

2. Rewrite the program from the above question in MERL assembly format.

3. Relocate the following binary MERL program so it can be loaded at address 0x100. 10 00 00 02 00 00 00 20 00 00 00 18 00 00 08 14 00 00 00 08 03 e0 00 08 00 00 00 01 00 00 00 10

Page 8: 08F CS 241FallOct30

7

Recognizing Languages 1. Using the binary alphabet, draw DFAs that recognize the following languages:

• all words that contain at least three consecutive zeroes • all words that do not contain the substring “11” • all words in which the number of 1s is a multiple of four

2. Using the binary alphabet, write regular expressions that recognize the following languages: • all words that alternate ones and zeros (i.e. 1010 or 010) • anything but the empty string

3. Draw a DFA and an NFA (one that is not also a DFA) that each recognize the language }aababb,ab,a,{ .

Page 9: 08F CS 241FallOct30

8

ANSWERS Basic Mips Programs 1. Register four will hold the value two. Register three will hold a zero by default. We divide both numbers by two, and if either has a non-zero remainder we skip the part of the program that changes register three to a one. ; load 2 into $4 lis $4 .word 2 ; load 0 into $3 add $3, $0, $0 ; check the first number div $1, $4 mfhi $5 bne $0, $5, endProgram ; check the second number div $2, $4 mfhi $5 bne $0, $5, endProgram ; if we get here, both numbers are even lis $3 .word 1 endProgram: jr $31

Page 10: 08F CS 241FallOct30

9

2. Register 2 will hold the value negative one, used to decrement n. Register three will hold our sum. We continue adding until n = 0, at which point we are done. ; load 1 into $2 lis $2 .word -1 ; load 0 into $3 (our sum starts at zero) add $3, $0, $0 startLoop: beq $0, $1, endLoop ; if n is zero, we're done add $3, $3, $1 ; add the current value of n to our sum add $1, $1, $2 ; decrement n beq $0, $0, startLoop ; return to the start of the loop endLoop: jr $31 3. The translated program is: 0x00201820 0x00001014 0x00000002 0x00620018 0x00001812 0x03e00008

Page 11: 08F CS 241FallOct30

10

Assemblers 1. Symbol Table: Name Value FOO 0x08 BAR 0x0c HELLO 0x0c WORLD 0x14 Relocation Table: ; we have a label to relocate on line 4 .word 1 .word 0x04 ; and another one to relocate on line 16 .word 1 .word 0x10

Page 12: 08F CS 241FallOct30

11

2. The program is as follows: ; MERL Header beq $0, $0, 2 .word endFile .word endCode ; Our actual program lis $1 .word FOO ; comment FOO: add $1, $2, $0 BAR: ; another comment HELLO: lis $2 .word BAR WORLD: jr $31 ; Mark the end of the code endCode: ; Now put in our relocation table .word 1 .word 0x10 ; ".word FOO" is at address 16 .word 1 .word 0x1c ; ".word BAR" is at address 28 ; Mark the end of the file endFile: 3. Our program is actually only three lines long. The relocation table tells us that there's a label to relocate at address 16: "00 00 00 08", so we add 0x100 and then subtract 0x0c (12) because of the three line MERL header we took out. The output code is: 00 00 08 14 00 00 00 fc 03 e0 00 08

Page 13: 08F CS 241FallOct30

12

Page 14: 08F CS 241FallOct30

13