CS 5330 hw2

pdf

School

University of Texas, Dallas *

*We aren’t endorsed by this school

Course

5330

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

11

Uploaded by PresidentFreedomWren42

Report
Name: FENGLONG YANG NetID: fxy230000 Date: 09/23/2023 CS5330 Assignment 2 QUESTION 1 (8%) Number conversion. a. Convert to binary: I. 2483 10 II. 3E8A 16 b. Convert the following 8-bit binary numbers to decimal: I. 11101011 2 II. 10000000 2 III. 01000101 2 Give 2 sets of answers: * Assuming that they are signed 2's complement numbers. * Assuming that the numbers are unsigned. (For example, as a signed number, 11110101 2 =-11; as unsigned number, 11110101 2 =245). a. 2483 10 (100110110011 2 ) 3E8A 16 (0011111010001010 2 ) b. Signed 2's complement numbers. -21 -128 69 The numbers are unsigned. 235 128 69
QUESTION 2 (6%) Do the following addition exercises by translating the numbers into 8-bit 2's complement binary numbers, performing the arithmetic, and translating the result back into a decimal number. Indicate where overflow occurs and why, based on the binary arithmetic: a. 47 + 38 b. 47 38 c. -47 38 d. 47 + 88 e. -47 + 88 f. 47 - 88 a. 47 (0010 1111) + 38(0010 0110) = 85 (0101 0101 two’s complement binary number) no overflow b. 47 - 38 = (0010 1111) - (0010 0110) one’s complement = 47(0010 1111) +(- 38) (1101 1010) two’s complement = 9 (1 0000 1001) no overflow c. -47 - 38 = 1101 0001 + 1101 1010 two’s complement = 1 1010 1011 =1010 1011 = 01010100( two’s complement) = 0101 0101= -85 no overflow d. 47 (0010 1111) + 88 (0101 1000) = 1000 0111 = 1000 0111( two’s complement) = 135, overflow e. -47 + 88 = 1101 001+0101 1000 = 1, 0010 1001 = 0010 1001=41, no overflow f. 47 - 88= 0010 1111 + 1010 1000( two’s complement ) = 1101 0111( two’s complement) = 1010 1001 = -41, no overflow
QUESTION 3 (9%) Assume that registers $s0 and $s1 hold the values 80000000 16 and D0000000 16 , respectively. a. What is the value of $t0 for the following assembly code? add $t0, $s0, $s1 b. Is the result in $t0 the desired result, or has there been overflow? c. For the contents of registers $s0 and $s1 as specified above, what is the values of $t0 for the following assembly code? sub $t0, $s0, $s1 d. Is the result in $t0 the desired result, or has there been overflow? e. For the contents of registers $s0 and $s1 as specified above, what is the values of $t0 for the following assembly code? add $t0, $s0, $s1 add $t0, $t0, $s0 f. Is the result in $t0 the desired result, or has there been overflow? a. $t0=13000 0000+80000000 =1000 0000 0000 0000 0000 0000 0000 0000 + 1101 0000 0000 0000 0000 0000 0000 0000 =1 0101 0000 0000 0000 0000 0000 0000 0000 =1 50000000 16 b. $t0=150000000 16 = 1 0101 0000 0000 0000 0000 0000 0000 0000 2 has been more than 32bits thus overflow c. $t0 = $s0 - $s1= 1000 0000 0000 0000 0000 0000 0000 0000 - 1101 0000 0000 0000 0000 0000 0000 0000 = - 0101 0000 0000 0000 0000 0000 0000 0000 = - 5000 0000 16 =1011 0000 0000 0000 0000 0000 0000 0000 d. The result in $t0 is the desired result. No overflow e. Because of the $t0 will not save data, in second step t0 is still equal to 0 . $t0=$t0+$s0=0+ D0000000 16 = D0000000 16 f. There has been overflow.
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
QUESTION 4 (10%) For the following C statement, write the corresponding MIPS assembly code. Assume that the variables i and j are assigned to registers $s3 and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.B[8] = A[ i j ]; (* use sll, shift left logical, for multiplication of 2, 4, …) .text sub $t0, $s3, $s4 sll $t0, $t0, 2 add $t0, $s6, $t0 lw $t1, 0($t0) sw $t1, 32($s7) #4*8=32bits
QUESTION 5 (12%) For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. sll $t0, $s0, 2 # $t0 = 4 * f add $t0, $s6, $t0 # $t0 = &A[ f ] sll $t1, $s1, 2 # $t1 = g * 4 add $t1, $s7, $t1 # $t1 = &B[ g ] lw $s0, 0($t0) # f = A[ f ] addi $t2, $t0, 4 lw $t0, 0($t2) add $t0, $t0, $s0 sw $t0, 0($t1) B [g]= A[f] + A[f+1] sll $t0, $s0, 2 # $t0 = f * 4 add $t0, $s6, $t0 # $t0 = &A[ f ] sll $t1, $s1, 2 # $t1 = 4 * g add $t1, $s7, $t1 # $t1 = &B[ g ] lw $s0, 0($t0) # f = A[ f ] addi $t2, $t0, 4 # $t2 = &A[f+1] lw $t0, 0($t2) # $t0 = A[f+1] add $t0, $t0, $s0 # $t0 = A[f+1] + A[f] sw $t0, 0($t1) # B[g] = A[f] + A[f+1]
QUESTION 6 (10%) Consider the following MIPS code assuming that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the array A is in register $s6. addi $t0, $s6, 4 add $t1, $s6, $0 sw $t1, 0($t0) lw $t0, 0($t0) add $s0, $t1, $t0 a. Translate the above MIPS code to C. b. For each MIPS instruction, show the values of the opcode (op), source register (rs), and funct field, and destination register (rd) fields. In addition, for the I-type instructions, show the value of the immediate field, and for the R-type instructions, show the value of the second source register (rt). a. f = 2*(&A) b. type opcode rs rt rd immediate addi $t0,$s6,4 I 8 22 8 4 add $t1,$s6,$0 R 0 22 0 9 sw $t1, 0($t0) I 43 8 9 0 lw $t0, 0($t0) I 35 8 8 0 add $s0, $t1,$t0 R 0 9 8 16
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
QUESTION 7 (6%) Assume the following register contents: $t0 = AAAAAAAA 16 , $t1 = 12345678 16 a. For the register values shown above, what is the value of $t2 for the following sequence of instructions? sll $t2, $t0, 4 or $t2, $t2, $t1 a. sll $t2, $t0, 4 $t0 = 0xAAAAAAAA = 1010 1010 1010 1010 1010 1010 1010 1010 $t1 = 0x12345678 = 0001 0010 0011 0100 0101 0110 0111 1000 $t2 = $t0 << 4; will shift the value of $t0 by 4 bits. $t2 = 1010 1010 1010 1010 1010 1010 1010 0000 = AAAAAAA0 BABEFEF8 16 b.For the register values shown above, what is the value of $t2 for the following sequence of instructions? sll $t2, $t0, 4 andi $t2, $t2, -1 b. AAAAAAA0 16 c. For the register values shown above, what is the value of $t2 for the following sequence of instructions? srl $t2, $t0, 3 andi $t2, $t2, 0xFFEF SRL $t2, $t0, 3; $t2 = $t0 >> 3; will shift the value of $t0 by 3 bits. Therefore, $t2 = 0001 0101 0101 0101 0101 0101 0101 0101 = 0x15555555 $t2 = $t1 & 0xFFEF; will perform an ANDI operation for $t2, and 0xFFEF, and stores the result in $t2. Therefore, $t2 = $t2 & 0xFFEF 0001 0101 0101 0101 0101 0101 0101 0101 0000 0000 0000 0000 1111 1111 1110 1111 0000 0000 0000 0000 0101 0101 0100 0101 = 00005545 16
QUESTION 8 (14%) Find the shortest sequence of MIPS instructions that extracts bits 16 down to 11 from register $t0 and uses the value of this field to replace bits 31 down to 26 in register $t1 without changing the other bits of register $t1. It can be done in seven MIPS instructions: srl $t0, $t0, 11 # $t0 bits 16 11 moved to bits 5 0 sll $t0, $t0, 26 # original $t0 bits No.16 11 moved to bits No.31 26 ori $t2, $0, 0x03ff # $t2 = 0x000003ff sll $t2, $t2, 16 # $t2 = 0x03ff0000 ori $t2, $t2, 0xffff # St2 = 0x03ffffff and $t1, $t1, $t2 or $t1, $t1, $t0 # done
QUESTION 9 (5%) Assume $t0 holds the value 00101000 16 . What is the value of $t2 after the following instructions? slt $t2, $0, $t0 bne $t2, $0, ELSE j DONE ELSE: addi $t2, $t2, 2 DONE: slt means if rs< rt, rd = 1 else rd = 0;(rs =$0, rt=$t0) bne means if (rs != rt) PC <- PC+4 + (sign-extend)immediate<<2 Thus the value of $t2 is 3
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
QUESTION 10 (15%) Consider the following C code. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D. for (i = 0; i < a; i ++) for (j = 0; j < b; j ++) D[4*j] = i + j; a. Translate the above C code to MIPS assembly code. Use a minimum number of instructions. b. How many MIPS instructions does it take to implement the C code? If the variables a and b are initialized to 10 and 1 and all elements of D are initially 0, what is the total number of MIPS instructions that is executed to complete the loop? a. addi $t0, $0, 0 beq $0, $0, attempt1 LOOP1: addi $t1, $0, 0 beq $0, $0, attempt2 LOOP2: add $t3, $t0, $t1 sll $t2, $t1, 2 add $t2, $t2, $s2 sw $t3, 0($t2) addi $t1, $t1, 1 attempt2: slt $t2, $t1, $s1 bne $t2, $0, LOOP2 addi $t0, $t0, 1 attempt1: slt $t2, $t0, $s0 bne $t2, $0, LOOP1 b. The C code requires 14 MIPS instructions. When a = 10 and b = 1, this results in 158 instructions being executed.
QUESTION 11 (5%) For the following code: lbu $t0, 0($t1) sw $t0, 0($t2) Assume that the register $t1 contains the address 0x1000 0000 and the data at the address is 0x11223344. Assume $t2 contains the address 0x1000 0004. a. What value is stored in 0x1000 0004 on a big-endian machine? b. What value is stored in 0x1000 0004 on a little-endian machine? a. Because of big-endian addressing, larger bits will stored in lower address, so the address of 0x1000 0000 stores 11. address 0x1000 0001 stores 22 address 0x1000 0002 stores 33 address 0x1000 0003 stores 44 so, 11 16 b. on opposite of big-endian machine, 44 16