22F_test2_keys

docx

School

University of Massachusetts, Lowell *

*We aren’t endorsed by this school

Course

2030

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

5

Uploaded by PresidentAardvarkPerson806

Report
COMP.2030 Test 2 11/4/22 Not allowed to use electronic devices including a calculator, a cell phone, etc. ID:________________________ NAME:____________________________________________ 1. (10 pt) Variables x and y are of type integer and have arbitrary values. Variables ux and uy are unsigned values of x and y, respectively (ux = (unsigned) x; uy = (unsigned) y;). For each of the C expressions below, indicate whether the expression always yields True. Otherwise, give a reason(s) that it results in False. 1. (ux-uy) == -(unsigned)(y-x) True: Both sides are unsigned, and identical 2. ((x>>2)<<2) <=x True: The last two bits of x are zeroed out 2. (15 pt) Consider an IEEE floating point format with 4-bit exponent and 8-bit fraction (no sign bit). Show the values of the following IEEE floating point representations in binary scientific notations (1.1x2 -3 , for example) (the first four bits are exponent bits). 1000 01010000 – 1.0101x2 0000 01010000 – 0.0101x2 -6 or 1.01x2 -8 0001 01010000 – 1.0101x2 -6 3. (10 pt) Function ff() is compiled into MIPS codes on the right. Complete the C function below to make it equivalent to the MIPS code (C operator for xor is “^”). int ff (unsigned x) { int val = 0; while ( val != x ) { val ^ =x; x = x>>1 (or x /= 2); } return val; } # argument x in $a0 ff: li $t0,0 xx: beq $t0, $a0, yy xor $t0, $t0, $a0 sr a $a0, $a0, 1 b xx yy: jr $ra
4. (10 pt) Around 250 B.C., the Greek mathematician Archimedes proved that 223/71 < <22/7. A single- precision floating-point approximation of used in <math.h> of C language is 0x40490FDB. Write 22/7 in four hexadecimal digits according to a 16-bit floating point format with a sign bit , 6-bit exp , and 9-bit frac . 22/7 = 3 1 7 1/7*8 = 1 1/7 repeats Fraction: 001001001… 3 1 7 = 11.001001001.. in binary 11.001001001 = 1.1001001x2 1 E = 1; 6-bit exp = 100000 9-bit frac = 100100100 Sign bit = 0 Putting all together: 0 100 000 1 0010 0100 0x4124 5. (15 pt) The symbol table and the memory map of a segment of MIPS program are shown below right. Also shown are MIPS codes of three functions F0, F1, and F2. Consider the MIPS program below. As instructions are executed in the given sequence successively , write the value stored in $a0 when the program returns from each jalr instruction below . la $s1, Q2 lw $s2, ($s1) jalr $v1, $s2 $a0 = 2 lw $s1, 8($s1) lw $s2, ($s1) jalr $v1, $s2 $a0 = 1 lw $s1, 8($s1) lw $s2, ($s1) jalr $v1, $s2 $a0 = 2 Symbol Value Q0 0x10010000 Q1 0x1001000c Q2 0x10010018 F0 0x00400000 F1 0x00400008 F2 0x00400010 Addr Value Q0: F1 Q1 Q2 Q1: F0 Q0 Q2 Q2: F2 Q2 Q0 Address Value 0x10010000 0x00400008 0x10010004 0x1001000c 0x10010008 0x10010018 0x1001000c 0x00400000 0x10010010 0x10010000 0x10010014 0x10010018 0x10010018 0x00400010 0x1001001c 0x10010018 0x10010020 0x10010000 F0: li $a0, 0 jr $v1 F1: li $a0, 1 jr $v1 F2: li $a0, 2 jr $v1
6. (10 pt) A C function prob(), declared as void prob(int *p1, int *p2){ …..}, is compiled to the MIPS code on the right. Write the C function prob() in ON E statement. void prob(int *p1, int *p2){ *p2 += *p1; } 7. A linked list node is declared at the right. The function func0() whose declaration is shown below has two arguments ptr and head which are pointers to arbitrary nodes and are not NULLs. The function is compiled into the MIPS code as shown on the right. Translate the MIPS code to a pseudo-code in C-like syntax with ‘goto’ statements, and write a C function corresponding to the pseudo-C code. In pseudo-C code, use variables t0 and t1 corresponding to MIPS registers $t0 and $t1, respectively. No such temporary variables should be used in the C code. # argument p1 and p2 are passed by # $a0 and $a1, respectively prob: lw $t0, ($a0) lw $t1, ($a1) add $t0, $t0, $t1 sw $t0, ($a1) jr $ra struct node { int c; struct node *next;} #$a0 has ptr, $a1 has head func0: lw $t0, ($a0) lw $t1, ($a1) bgt $t0,$t1, L1 sw $a1, 4($a0) or $a1, $a0, $0 L1: jr $ra Pseudo-C (10 pt) C code (10 pt) void func0(struct node *ptr, struct node *head){ if (ptr->c <= head->c) { ptr->next = head; head = ptr; } (In C, you should not have temporary variables t0 or t1.) void func0(struct node *ptr, struct node *head){ int t0 = ptr->c; int t1 = head->c; if (t0 > t1) goto L1 ptr->next = head; head = ptr; return
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
8. (10 pt) Portions of a text and a data segment of a MIPS program are shown below. Functions FUN0 and FUN1 update $s0 and return the value, where the return value in $s0 is an integer between 1 and 6 (inclusive). (All three registers have to have correct answers to get credits. If any one of them is incorrect, you can give 0 grade.) a. When ‘b rept’ instruction is executed for the first time, what values will be in $s0, $s1 and $s2 ? $s0: ___2_______ $s1: ______Q0__ $s2: ______FUN1_ b. When ‘b rept’ instruction is executed for the first time, what values will be in $s1 and $s2 ? $s0: ____1______ $s1: ______Q1__ $s2: ______FUN0_ la $s1, Q1 li $s0, 1 rept: lw $s2, ($s1) jalr $v1, $s2 sll $s0, $s0, 2 add $s1, $s1, $s0 lw $s1, ($s1) b rept FUN0: li $s0, 1 jr $v1 FUN1: li $s0, 2 jr $v1 .data Q0: .word FUN0 .word Q1 .word Q1 .word Q1 .word Q1 .word Q1 .word Q1 Q1: .word FUN1 .word Q0 .word Q0 .word Q0 .word Q0 .word Q0 .word Q0