CS 241 Fll 2017 Midterm Review Solutions Octoer 24, 2017 Contents 1 Bits nd Bytes 1 2 MIPS Assemly Lnguge Progrmming 2 3 MIPS Assemler 6 4 Regulr Lnguges 7 5 Scnning 9 1 Bits nd Bytes 1. Give two s complement inry representtion (nd corresponding hexdeciml representtion) of the following deciml numers: () 127 0111 1111 (more leding zero its cceptle), ut not 111 1111 () 241 0 1111 0001 (more leding zero its cceptle), ut not 1111 0001 (c) 30 10 0010 (more leding one its cceptle) 2. Give the deciml nd inry representtions of the following two s complement numers: () 0x81 1000 0001, -127 () 0x7F 0111 1111, 127 3. Give the (unsigned) deciml nd inry representtions of 0xCC. 11001100, 204 4. Wht is the rnge (in deciml) of 32-it two s complement numer? 2 31... 2 31 1 1
2 MIPS Assemly Lnguge Progrmming 1. Write MIPS procedure clled StringMp tht interprets the vlue in $1 s the ddress of string (n rry of chrcters tht ends with -1), nd the vlue in $2 s the ddress of procedure which interprets the vlue in $1 s chrcter nd returns chrcter in $3 (we cll it the chrcter trnsformtion function). StringMp should pply the chrcter trnsformtion function to chrcter of the string nd plce the ddress of the trnsformed string in $3. You my ssume the chrcter trnsformtion function will restore ny registers it modifies. However, you my not ssume the cller of StringMp stores/restores ny registers. StringMp: ; Cllee sves registers sw $31, -4($30) sw $9, -8($30) sw $8, -12($30) sw $5, -16($30) sw $4, -20($30) lis $31.word 20 su $30, $30, $31 ; Constnts lis $5.word -1 ; $5 = EOF list $4.word 4 dd $9, $1, $0 ; $9 = current ddress dd $8, $1, $0 ; sve return vlue forech: lw $1, 0($9) eq $1, $5, end jlr $2 sw $3, 0($9) dd $9, $9, $4 eq $0, $0, forech end: dd $3, $8, $0 ; Restore registers lis $31.word 20 dd $30, $30, $31 lw $31, -4($30) lw $9, -8($30) lw $8, -12($30) lw $5, -16($30) lw $4, -20($30) jr $31 2. Write MIPS progrm tht interprets the vlue in $1 s the ddress of n rry of MIPS instructions 2
nd executes the progrm stored in the rry. You my ssume ech element contins exctly one instruction, nd the MIPS progrm stored in the rry is vlid (ending with jr $31 ) nd tkes no rguments. jr $1 3. Exercise. Write MIPS procedure clled gcd which interprets the vlues in $1 nd $2 s unsigned deciml integers nd computes their gretest common divisor using recursion, plcing the result in $3. Hint: https://en.wikipedi.org/wiki/gretest_common_divisor#using_euclid.27s_lgorithm ; Clculte gcd(,) ; $1 - ; $2 - ; $3 - gcd(,) ; $ ; Recll tht gcd(,) = gcd(, mod ) ; nd gcd(,0) = gcd: ; Sve ll registers used sw $1, -4 ($30) sw $2, -8 ($30) sw $4, -12 ($30) sw $31, -16($30) lis $31.word 16 su $30, $30, $31 ; Bse Cse ; if ==0 return ne $0, $2, recur dd $3, $0, $1 eq $0, $0, clen recur: ; Recurrence Cse ; $1 contins ; $2 contins mod ; $4 contins lel gcd divu $1, $2 dd $1, $2, $0 mfhi $2 lis $4.word gcd jlr $4 clen: ; Lod ll register used lis $31.word 16 dd $30, $30, $31 3
lw $31, -16($30) lw $4, -12($30) lw $2, -8 ($30) lw $1, -4 ($30) jr $31 4. Exercise. According to the MIPS ssemly lnguge reference sheet, if you lod word from ddress 0xffff0004, it will plce the next yte from stndrd input in the destintion register. Write progrm which tkes numer in $1 nd reds in tht mny elements from stndrd input into n rry, stores the ddress of the rry in $1 nd the numer of elements in $2, nd then jumps to symol clled strt. This is essentilly mips.rry except tht the rry elements re provided s chrcters rther thn s integers. ; Constnts lis $29.word 0xffff0004 lis $4.word 4 ; Move numer of elements to $2 dd $2, $1, $0 ; Allocte spce for rry on the stck mult $2, $4 mflo $3 su $30, $30, $3 dd $1, $30, $0 ; $5 points to current ddress dd $5, $30, $0 iloop: eq $3, $0, iendl ; Sve n item from stdin to the rry lw $6, 0($29) sw $6, 0($5) ; Increse rry* nd decrese counter dd $5, $5, $4 su $3, $3, $4 eq $0, $0, iloop ; We re done here: sve $31 nd cll strt iendl: lis $5.word strt sw $31, -4($30) su $30, $30, $4 jlr $5 ; Restore $31 nd exit upon returning dd $30, $30, $4 lw $31, -4($30) 4
jr $31 ; Test code follows ; Prints out the contents of the rry to stdout s chrs ; Does not preserve registers (irresponsile!) strt: lis $4.word 4 lis $11.word 1 lis $28.word 0xffff000c sloop: eq $2, $0, sendl ; Move from rry to stdout lw $5, 0($1) sw $5, 0($28) dd $1, $1, $4 su $2, $2, $11 eq $0, $0, sloop sendl: jr $31 5. Exercise. Given the following hexdump of n ssemled MIPS ssemly lnguge progrm, determine the originl progrm without using.word. 00000000: 0000 1814... 00000004: 0000 0001... 00000008: 0000 2014... 0000000c: 0000 0001... 00000010: 0062 0018... 00000014: 0000 1812... 00000018: 0024 0822.$." 0000001c: 1420 fffc... 00000020: 03e0 0008... Your progrm must use lels where possile. Wht does the progrm do? lis $3.word 1 lis $4.word 1 lel1: mult $3, $3 mflo $3 su $1, $1, $4 ne $1, $0, lel1 jr $31 Given in $2 nd in $1, this progrm clcultes nd stores the result in $3. 5
3 MIPS Assemler 1. Give the symol tle for following MIPS ssemly lnguge progrm: mult: mult $1, $2 eq $0, $0, rnchloction frog: gorf:.word gorf rnchloction: ; rnch here ; now return jr $31 end: ; end of the progrm Symol tle: mult 0 frog 8 gorf 8 rnchloction 12 end 16 2. Wht is the purpose of the first nd second pss in n ssemler? Why does ssemlers require two psses? First pss: crete symol tle (with some error-checking). Second pss: syntx & error checking + mchine code output Becuse lels cn e used efore they re defined 3. Ed tries to write his CS350 ssignment in MIPS tht requires to loop through 90,000 lines of code. Descrie potentil prolem tht Ed could encounter if he uses rnching instructions. How could Ed solve this prolem? Brnch instructions use 16-it two s complement integers, so they cn only encode rnches from 2 15 to 2 15 1 (-32768 to 32767). To encode jump of 90 000 instructions, we cn store the vlue of the trget ddress in register nd jump to tht dresss. lis $5.word jumptohere jr $5... jumptohere:... 4. Wht informtion cn we not get ck when we disssemle progrm? Any comments nd the nmes of lels will e lost when the progrm is ssemled. Although we cn retrieve the exct instructions used, it cn e difficult to determine the context of those instructions. 6
4 Regulr Lnguges Note tht for simplicity, you my chose to llow leding zeroes. 1. Prove tht the lnguge L = {A, B, C, D} is regulr. It suffices to provide the regulr expression (A B C D) to show tht L is regulr. 2. Give DFA for ech of the following lnguges: No solutions will e provided for this question () The lnguge L over Σ = Γ {+}, where Γ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, such tht: L = {u + v : u, v Γ, u 0 (mod2) nd v 1 (mod2)} () Exercise. The lnguge L over Σ = {0, 1} which contins strings with inry vlue tht is congruent to 3 modulo 8. (Tht is, the inry vlue of string x divided y 8 gives reminder of 3). Hint: Think out the 3 lest-significnt (rightmost) its. 3. Give regulr expression for ech of the following lnguges: () The lnguge L over Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} of positive deciml numers divisile y 4. (0 4 8) (Σ ( (0 2 4 6 8)(0 4 8) ) ) (1 3 5 7 9)(2 6) () Exercise. The lnguge L over Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,,, c, d, e, f,.word, x, } of vlid MIPS.word directives with positive deciml nd ll hexdeciml rguments. (Use to represent spce).word ((0 1 2 3 4 5 6 7 8 9) ( 0x(0 1 2 3 4 5 6 7 8 9 c d e f) )) 4. You re given regulr lnguge L nd DFA M = {Σ, Q, q 0, A, δ} for the lnguge. In pseudocode, write function tht tkes string s Σ s n rgument nd returns 1 if the string is in lnguge L, 0 otherwise. ssuming s hs length n stte <- q 0 for i from 1 to n stte <- δ( stte, s i ) return ( stte A) 5. Give n NFA for ech of the following lnguges: No solutions will e provided for this question () The lnguge A over Σ = {0, 1} of strings ends with either 100 or 1. () The lnguge B over Σ = {0, 1} tht is the complement of A. (For ny lnguge A Σ, the complement of A is Σ \ A) (c) The lnguge over Σ = {c,,r,t} where words in the lnguge must end with cr or hve ct s sustring. (d) The lnguge over Σ = {c,,r,t} where words must end with tr nd hve rt s sustring. (e) Exercise. The lnguge over Σ = {,,c,d} where words must either include t lest one nd no s, or must include t lest one followed y t lest one c. 6. For the 2 NFAs from the previous question, give n ɛ-nfa for the lnguge B A. Descrie the lnguge ccepted y this utomton. 7
7. Convert the following NFA into DFA., strt 0 1 2 3 DFA: strt 0 0,2 0,2,3 0,1 0,1,3 8
5 Scnning MINUS DECR 0-9 0-9 strt START 1-9 INT -f,0-9 0 e x -f,0-9 ZERO ZEROX HEXINT -d,f-z E -z,0-9 n -z,0-9, -m,o-z,0-9 ID d -z,0-9, EN END -c,e-z,0-9?? 1. Using the ove DFA, produce tokeniztion of the following strings using the Simplified Mximl Munch lgorithm. If the scnning produces n error, output ll the tokens recognized up until the error nd then the string ERROR. () 0xc0x1-23--70xff HEXINT 0xc0, ID x1, INT -23, DECR --, INT 70, ID xff () 0xeef--0xkcd HEXINT 0xeef, DECR --, ERROR 9