23F_test1_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

4

Uploaded by PresidentAardvarkPerson806

Report
COMP.2030 Test 1 10/22/23 CLOSED BOOK/NOTE/LAPTOP/Cell Phone/Calculator. In all relevant questions, a word has 4 bytes or 32 bits. NAME: _________________________________________ ID: ____________________ 1. (10 pt) A base-8 number XXY and a base-11 number YXX turn out to have identical values. List ALL pairs of (X,Y), where digits X and Y satisfy the relationship (Neither X nor Y is 0). [8*8 = 64 and 11*11 = 121] 72X + Y = 121Y + 12X 60X = 120 Y X=2Y ( X,Y): (2,1) (4,2) (6,3) ( X,Y): (2,1) (4,2) (6,3) 2. (9 pt) Suppose that the memory address of the label ‘array’ is 0x00008080. For each instruction below, write in the comment field the value of the given register after its execution is completed. .data array: .word 8, 7, -4, 8, -16, 10, -8,0,4,-7 .text la $t9, array lw $t0, ($t9) # _$t0 = __8__________ lw $t1, array($t0) # _$t1 = _ ___-4_________ lw $t2, array+4($t1) # _$t2 = ____8__________ 3. (10 pt) Variable X has a number with the decimal value of 2.7. Write the value of X in binary. For fractional part of the binary string, you retain only six bits.
4. (9 pt) When x and y are 8 bits long, and have 0x01 and 0xFF, respectively. Write the result of each of the following four operations in two hexadecimal digits under operations. ~x || ~y ~x & y !x & y 0x01 0xFE 0x00 5. (10 pt) In C, the following linked list node is declared: struct node { int val; struct node *next;} Three pointers are declared by “struct node *t1, *t2, *t3,” and t1 and t2 point to two successive nodes in the list. Namely, t1->next = t2. Now, *t3 is a pointer to an arbitrary link node. Assume that all links point to valid addresses with no null pointers, and that MIPS registers $t1, $t2, and $t3 have C pointers t1, t2, and t3, respectively. $a0 is used for a temporary storage. Write in two C statements the equivalent operation of the MIPS codes above. t3->next = t1->next; t1->next = t3; 6. (10 pt) An integer array ‘X’ has at least one element. The size of the array is passed as an argument in variable limit . Write the C function func() that performs the same operation as the MIPS code on the right. int func(int limit){ int i, temp; int i, temp; int val = X[0]; for (i=1; i<limit; i++){ if (val <= X[i]) val = X[i]; } lw $a0, 4($t1) sw $a0, 4($t3) sw $t3, 4($t1) # $t9 has the size of array ‘X’ # return value is via $a0 func: lw $a0, X li $t0, 1 rept: bge $t0, $t9, exit sll $t0, $t0, 2 lw $a1, X($t0) bgt $a0, $a1, next move $a0, $a1 next: sra $t0, $t0, 2 addi $t0, $t0, 1 b rept exit: jr $ra
return val;} 7. (16 pt) Convert the following C-code to its pseudo-C code with gots’s on the left, and MIPS codes on the right. Here, use $a0 for a, and $a1 for p. void cond(int a, int *p){ # assume $a0 for int a, and $a1 for p if (p==0) goto done cond: beq $a1, $zero, done if (a <= 0) goto done ble $a0, $zero, done *p += a; lw $t0, ($a1) done: return add $t0, $t0, $a0 } sw $t0, ($a1) done: jr $ra 8. (10 pt) Function ff() on the left is compiled into MIPS codes on the right. Fill in the underlined to make the C function equivalent to the MIPS codes (C operator for xor is “^” and srl (shift right logical) operator is ‘>>’). void cond(int a, int *p){ if (p && a >0) *p += a; } MIPS Pseudo-C int ff (unsigned x) { unsigned val = 0; while (____val != x ______) { val ^= x; x >> 1 ; } return val; } # $a0 -- x and return value ff: li $t0,0 xx: beq $t0, $a0, yy xor $t0, $t0, $a0 srl $a0, $a0, 1 b xx yy: jr $ra
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
9. (16 pt) An array of 100 words is allocated starting at the address ‘arr100’ (declared as ‘arr100: .word 0:100’ in the data segment). Fill in the comment area in the following MIPS codes using the pseudo-C syntax with ‘goto’. Use variables temp for $t0 , and x for $a1 in comment lines. For the codes shown above, write a C version without ‘goto.’ max = arr100[0] for (i=1; i<100; i++){ if (arr100[i] > max) max = arr100[i]; } li $s0,1 # i = 1, initialization lw $t0, arr100($zero) # temp = arr100[0] p0: bge $s0, 100, exit sll $s0, $s0, 2 # i*=4 lw $a1, arr100($s0) # x = arr100[i] ble $a1, $t0, skip # if (x<=temp) goto skip move $t0, $a1 # temp = x skip: sra $s0, $s0, 2 # i/=4 addi $s0, $s0, 1 # i++ b p0 exit: jr $ra