CSc 256 Midterm 1 Spring 2011 NAME: Problem1a: GiventheC++functionprototypeandvariabledeclarations: intfunc(intarg0,int*arg1,int*arg2); int*ptr,n,arr[10]; whichofthefollowingstatementswillcausesyntaxerrorsorwarningsabouttype mismatch/missingcast?foreachcase,writeeitherbad(errororwarning),orok (noerrororwarning),andcirclealso*all*theargument/variablesthathavetype mismatches.nopointswillbeawardedforastatementifthereisasyntaxerrorand youcirclethewrongargument/variable.(6points) arr[*ptr]=func(*ptr,&arr[*ptr],&n); *ptr=func(arr[ptr],&arr[n],ptr); withpointer) OK BAD(1 st argumentarr[]indexed
Problem1b:SupposewearetracingaC/C++programusingadebuggersuchasgdb. Thecodeshowingallfunctioncallslookslikethis: main(){ F1(5); F1(2); } F1(intarg){ F2(arg*2); F2(arg/2); } F2(intarg){ F3(arg); intx=4; //somecodenotshown } F3(intarg){ //somecodenotshown } Supposewesetabreakpointatthelineintx=4(inF2()).Weruntheprogram,stop atthebreakpointforthefirsttime,andcheckthestackframesonthestackusinga commandsimilartogdb sbacktrace.whatarethestackframesthatyouseeatthis time?foreachstackframethatyousee,indicateclearwhatfunctionitbelongsto, theargumentsforthatfunctioncall,andtheorder(whichoneisatthebottom, whichisaboveit,etc).(4points) Bottomisstackframeformain AboveitisstackframeforF1(5) AboveitisstackframeforF2(10) Or: [stackframeforf2(10)] [stackframeforf1(5)] [stackframeformain]
Problem2: Translate the C++ function moro() into MIPS assembly language. (You don't have to write a main program, or the gnew() function.) Your solution must compile and run correctly in spim, and follow all MIPS register use conventions. You may use $s? or $t? as temporaries, but follow the specifications below. For moro:: void moro(int *arg0, int *arg1, int arg2) arg0 a0 arg1 a1 arg2 a2 For gnew: int gnew(int *arg0, int arg1) arg0 a0 arg1 a1 result returned in $v0 Make sure your loops are efficient (i.e., they should not have unnecessary branches). Points will be deducted for obvious inefficiencies. (45 points)
void moro(int *arg0, int *arg1, int arg2) { int *ptr, sum = 5; ptr = arg0; if (arg2 < sum *ptr > 0) { sum = sum + arg1[arg2];; } else { for (int i=arg2; i>0; i--) { ptr++; sum = sum + gnew(ptr, arg2); } } *ptr = sum; }
Solution: # $s0 sum # $s1 ptr # $s2 copy of arg2 # $s3 i moro: sw $31, -4($sp) sw $s0, -8($sp) sw $s1, -12($sp) sw $s2, -16($sp) addi $s0, $s0, -16 move $s2, $a2 # int *ptr, sum = 5; li $s0, 5 move $s1, $a0 # ptr = arg0; blt $a2, $s0, if# if (arg2 < sum *ptr > 0) { lw $t0, ($s1) ble $t0, $0, else if: sll $t0, $s2, 2 # sum = sum + arg1[arg2]; add $t0, $t0, $a1 lw $t1, ($t0) add $s0, $s0, $t1 j exit # } else: # else { move $s3, $s2 # for (int i=arg2; i>0; i--) { ble $s3, $0, exit for: addi $s1, $s1, 4 # ptr++; move $a0, $s1 # sum = sum + gnew(ptr, arg2); move $a1, $s2 jal gnew add $s0, $s0, $v0 addi $s3, $s3, -1 # } bgt $s3, $0, for # } exit: sw $s0, ($s1) # *ptr = sum; addi $s0, $s0, 16 lw $31, -4($sp) lw $s0, -8($sp) lw $s1, -12($sp) lw $s2, -16($sp) jr $31
Problem3: Show what is printed on the screen after this C++ program is executed. (20 points) ANSWER: obsvrvaqory #include <iostream> using std::cout; int main() { char str[] = "observatory"; char *ptr0, *ptr1; } ptr0 = &str[7]; ptr1 = ptr0-5; *ptr0 = (*ptr1) - 2; ptr1 = ptr1 + 1; ptr0 = ptr0-2; *ptr1 = *ptr0; cout << str; [Work:] ptr1 = ptr0 5 = &str[2] *ptr0 = (*ptr1) 2 // str[7 ] = str[2] 2 = q ptr1 = ptr1 + 1 = &str[3] ptr0 = ptr0 2 = &str[5] *ptr1 = *ptr0 // str[3] = str[5] = v
Problem 4: The following MIPS program is executed. Show the contents of registers $t0, $t1 and $t2, when the label "end" is reached. Assume the initial contents of registers as shown. All integers are in hex. Show each step clearly for partial credit. (25 points) Initially: $t0 0x10010008 $t1 0xffffff6a $t2 0x918120b0 label address contents here: 0x10010004 0x39383736 there: 0x10010008 0x2a2b2c2d.text main:sra $t1,$t1,3 sb $t1,-3($t0) or $t0, $t1, 0x00000099 lw $t2,here end: li $v0,10 syscall obsvrvaqory CONTENTS of $t0 (in hex): 0xfffffffd CONTENTS of $t1 (in hex): 0xffffffed CONTENTS of $t2 (in hex): 0x39ed3736 $t1 = 1111 1111 1111 1111 1111 1111 0110 1010 sra $t1, $t1, 3 $t1 = 1111 1111 1111 1111 1111 1111 1110 1101 = 0xffff ffed sb or $t1,-3($t0) ADDR = $t0 3 = 0x1001 0005 MEM[0x10010004] = 0x39ed3736 $t0, $t1, 0x00000099 $t1 = 1111 1111 1111 1111 1111 1111 1110 1101 0000 0000 0000 0000 0000 0000 1001 1001 $t0 = 1111 1111 1111 1111 1111 1111 1111 1101
= 0xffff fffd lw $t2,here $t2 = 0x39ed3736
MIPSinstructions op1,op2areregisters,op3isregisterorconstant cont[op1]meanscontentsofop1 moveop1,op2 cont[op1]=cont[op2] addop1,op2,op3 cont[op1]=cont[op2]+cont[op3] subop1,op2,op3 cont[op1]=cont[op2] cont[op3] mulop1,op2,op3 cont[op1]=cont[op2]*cont[op3] divop1,op2,op3 cont[op1]=cont[op2]/cont[op3] remop1,op2,op3 cont[op1]=cont[op2]%cont[op3] notop1,op2 cont[op1]=notcont[op2](bitwise) andop1,op2,op3 cont[op1]=cont[op2]andcont[op3](bitwise) orop1,op2,op3 cont[op1]=cont[op2]orcont[op3](bitwise) nandop1,op2,op3 cont[op1]=cont[op2]nandcont[op3](bitwise) norop1,op2,op3 cont[op1]=cont[op2]norcont[op3](bitwise) xorop1,op2,op3 cont[op1]=cont[op2]xorcont[op3](bitwise) sllop1,op2,amt cont[op1]=cont[op2]shiftleftlogical byamtbits srlop1,op2,amt cont[op1]=cont[op2]shiftrightlogical byamtbits sraop1,op2,amt cont[op1]=cont[op2]shiftrightarithmetic byamtbits rolop1,op2,amt cont[op1]=cont[op2]rotateleftbyamtbits rorop1,op2,amt cont[op1]=cont[op2]rotaterightbyamtbits blabel gotolabel jlabel gotolabel beqop1,op2,label if(cont[op1]==cont[op2])gotolabel bneop1,op2,label if(cont[op1]!=cont[op2])gotolabel bgtop1,op2,label if(cont[op1]>cont[op2])gotolabel bgeop1,op2,label if(cont[op1]>=cont[op2])gotolabel bltop1,op2,label if(cont[op1]<cont[op2])gotolabel bleop1,op2,label if(cont[op1]<=cont[op2])gotolabel beqzop1,label if(cont[op1]==0)gotolabel bnezop1,label if(cont[op1]!=0)gotolabel bgtzop1,label if(cont[op1]>0)gotolabel bgezop1,label if(cont[op1]>=0)gotolabel bltzop1,label if(cont[op1]<0)gotolabel blezop1,label if(cont[op1]<=0)gotolabel lar,label cont[r]=addressoflabel lir,constant cont[r]=constant lwr,?? cont[r]=m[addr]
lbr,?? cont[r]=m[addr],sign extended lbur,?? cont[r]=m[addr],zero extended swr,?? M[ADDR]=cont[R] sbr,?? m[addr]=low8 bitsofcont[r] if??isalabel,addr=addressoflabel if??is(r),addr=cont[r] if??isconstant(r),addr=cont[r]+constant if??islabel(r),addr=cont[r]+addressoflabel mtc0op1,op2 contentsofcoprocessor0registerop1= contentsofmipsregisterop2 mfc0op1,op2 contentsofmipsregisterop1= contentsofcoprocessor0registerop2 Syscallusage: printanint $v0=1,$a0=inttobeprinted printastring $v0=4,$a0=addressofstringtobeprinted readanint $v0=5,inputintappearsin$v0 exit $v0=10 MIPSregisternames: $0 $1 $2,$3 $v0,$v1 $4 $7 $a0 $a3 $8 $15 $t0 $t7 $16 $23 $s0 $s7 $24 $25 $t8 $t9 $26 $27 $k0 $k1 $28 $gp $29 $sp $30 $s8 $31 $ra