CS 241
Fall 2012 Midterm Review
October 23rd, 2012
Any problem labelled ”exercise” may be briefly discussed in the review session, but it will not be solved.
You should look at these problems yourself and make sure you understand how to do them.
1
Bits, Bytes and Words
• What does the byte 10110110 represent?
• Convert the decimal number 241 to binary and hexadecimal.
• Exercise: Convert the hexadecimal number 0x8C to (unsigned) decimal and binary.
• What is the range (in decimal) of a 4-bit two’s complement integer? What about an n-bit two’s
complement integer?
• Convert the decimal number -43 to 8-bit two’s complement binary.
2
MIPS Assembly Language Programming
• Register 1 holds the address of the beginning of an array in memory. Register 2 holds the number of
elements in the array. Write an assembly language program thats prints out all the prime numbers
in the array. Assume your program will be combined with two procedures: the ”isPrime” procedure
described below and the ”print” procedure from Assignment 2 Problem 7.
• Exercise: Write an assembly language procedure called ”isPrime” that determines whether the number
in $1 is prime and returns either 0 or 1 (for ”false” or ”true”) in $3. Since this is a procedure, make
sure to save and restore all registers you modify (except for $3) using the stack.
• Convert the following MIPS machine code program to assembly without using the .word directive.
What will this program do when it is loaded at address 0 and executed? What values will $3 and $4
contain at the end, assuming they are initialized to zero when the program starts?
addr
0x00
0x04
0x08
0x0C
machine code
0x8c04000c
0x00001814
0x00831820
0x03e00008
1
• Exercise: Write an assembly language program that is equivalent to the following program, but does
not contain labels.
d: beq $1, $2, a
.word b
c: .word c
a: bne $3, $4, d
b: jr $31
3
Assembler Concepts
• Why does your assembler need to make two passes over the input program? What is done in each of
the two passes?
• Construct a symbol table for the following program.
mult: mult $1, $2
beq $0, $0, branchLocation
frog:
gorf: .word gorf
branchLocation: ; branch here
; now return
jr $31
end: ; end of the program
• Assemble the following MIPS program into machine code.
start: add $1, $2, $3
beq $14, $25, start
jr $31
4
Loading, Relocation and Linking
• Which lines of the following MIPS program need to be relocated?
beq $30, $29, endOfLabels
.word label
.word abcd
endOfLabels: lis $4
.word abcd
jr $4
label: .word 4
.word 0x10
abcd: jr $31
• Exercise: Suppose we want to relocate the above program to start from address 0x104. Make the
necessary modifications to the machine code version of the program so that it will run correctly from
this address. The MERL machine code version of this program is shown on the next page.
2
original addr.
0x0000
0x0004
0x0008
0x000c
0x0010
0x0014
0x0018
0x001c
0x0020
0x0024
0x0028
0x002c
0x0030
0x0034
0x0038
0x003c
0x0040
0x0044
relocated addr. MERL file
0x0104
0x10000002
0x0108
0x00000048
0x010c
0x00000030
0x0110
0x13dd0002
0x0114
0x00000024
0x0118
0x0000002c
0x011c
0x00002014
0x0120
0x0000002c
0x0124
0x00800008
0x0128
0x00000004
0x012c
0x00000010
0x0130
0x03e00008
0x0134
0x00000001
0x0138
0x00000010
0x013c
0x00000001
0x0140
0x00000014
0x0144
0x00000001
0x0148
0x0000001c
rel. machine code
0x________
0x________
0x________
0x________
0x________
0x________
0x________
0x________
0x________
• Consider the following MIPS program:
.import toop
.export goop
goop:
add $3, $0, $0
loop:
lis $17
.word toop
jalr $17
add $3, $3, $3
beq $3, $17, loop
jr $31
.word loop
The MERL version of this program is below, but only the MIPS code section of the file is shown. Fill
in the MERL header and relocation/external symbol information.
addr
0x0c
0x10
0x14
0x18
0x1c
0x20
0x24
0x28
machine code
0x00001820
0x00008814
0x00000000
0x02200009
0x00631820
0x1071fffb
0x03e00008
0x00000010
Some ASCII character codes:
g = 0x67 l = 0x6c o = 0x6f p = 0x70
Table entry codes:
REL = 0x01 ESD = 0x05 ESR = 0x11
3
t = 0x74
• Exercise: Below are two MIPS assembly language programs:
m1.asm
.import a
.word a
b: .word 0xbad
.word b
m2.asm
.export a
.word 0xabcd1234
a: .word a
Write the MERL versions of both programs (as sequences of hexadecimal words). Then link the two
MERL files together by hand.
To check your answer, you can use the tool cs241.linkasm to assemble the two programs into linkable
MERL files, then use cs241.linker to link them. For example:
java cs241.linkasm < m1.asm > m1.merl
java cs241.linkasm < m2.asm > m2.merl
cs241.linker m1.merl m2.merl > linked.merl
Note that cs241.linker is not a Java program.
5
Formal Languages, Finite Automata and Regular Expressions
• A DFA can be formally defined as a 5-tuple (Σ, Q, q0 , A, δ), where q0 ∈ Q, A ⊆ Q and δ : Q × Σ → Q.
What is the meaning of each of these symbols?
• The formal definition of an NFA is almost identical, but instead we have δ : Q × Σ → 2Q . What does
this mean?
• Draw a DFA that recognizes the language of all words over the alphabet Σ = {0, 1, 2, 3, 4} that are
even integers with no useless leading zeros.
Examples: 0, 120, 343212, 404, 31210
Not in the language: 13, 00024, 00, 204201, • Draw an NFA that recognizes the language of all words over the alphabet Σ = {a, b, c, d} that contain
”cab” as a substring and end in ”dad”.
Examples: cabdad, abadcabbadad, adacbabacabddcabdabccadad
• Draw a DFA that recognizes the language of all words over the alphabet Σ = {a, b, c, d} with an even
number of as and no cs.
Examples: aa, ababada, addaabbabaaaa, bdbd, Not in the language: c, aadba, acdc, aaaacaaaa
• Draw an NFA that recognizes the union of the languages recognized by the NFA and DFA from the
previous two problems. (Hint: use -transitions.)
• What are the “base cases” in the definition of regular expressions? What are the three operations we
use to build up regular expressions from these base cases?
• Write a regular expression for the language of words over the alphabet Σ = {a, c, t} that contain “cat”
as a substring, have an even number of as, and only have one c and one t.
Example words: acat, cataaa, aacata, aacataaa
Not in the language: caat, catcat, cat, attacat, ccatt
4
• Exercise: Write down the set of words recognized by this NFA.
a
c
b
rst_n
a
a
a
b
d
• Exercise: Draw a DFA that recognizes the language of all words over the alphabet Σ = {duck, goose}
that contain at least two occurences of ”duck” immediately before each occurence of ”goose”.
Examples: , duck duck goose, duck duck goose duck duck duck goose duck, duck
Not in the language: goose, duck goose, duck duck goose duck goose, goose duck duck goose
• Exercise: Draw an NFA that recognizes the language of all words over the alphabet Σ = {a, b, c, d}
that contain ”aaa” as a substring and end in ”aad”. Make sure you recognize words like ”aaad” and
”cabaaaad” but do not recognize words like ”aaabd”, ”aaacad” or ”aad”.
• Exercise: Write a regular expression for the language of words over the alphabet Σ = {c, d, k, u} that
fit in one or more of these three categories:
– Simply the string “duck” repeated one or more times
– Words that contain at least one u but do not contain k
– Words that contain two us immediately following each d
Example words: , duckduckduck, cuddud, duuuckduu, dududcc, kduu, uuuuuu
Not in the language: duckdu, kcud, duuduck, kuddud, duckduckduckuck
• Exercise: Let M = (Σ, Q, q0 , A, δ) be a DFA, where:
Σ = {a, b}
q0 = 0
δ(0, a) = 1
δ(1, a) = 1
δ(2, a) = 3
δ(3, a) = 1
Q = {0, 1, 2, 3}
A = {0, 2, 3}
δ(0, b) = 2
δ(1, b) = 1
δ(2, b) = 3
δ(3, b) = 1
Write down the set of words recognized by this DFA.
• Exercise: Write pseudocode for an program that, when given a DFA M = (Σ, Q, q0 , A, δ) and a word
w over Σ as input, returns true if and only if w ∈ L(M ) (that is, the input word is in the language
described by the DFA).
5
© Copyright 2026 Paperzz