Hong Kong University of Science & Technology Computer Organization (COMP 180)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Student ID:

HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY Computer Organization (COMP 180) Spring Semester, 2010 Midterm Examination 1
Mar 6, 2010

Name: Solution Email:

Student ID: Lab Section Number:

Instructions: 1. This examination paper consists of 9 pages, with 7 questions and 2 appendix reference pages. 2. Please write your name, student ID, email and lab section number on this page. 3. Please answer all the questions in the spaces provided on the examination paper. 4. Please read each question very carefully, answer clearly and to the point. Make sure that your answers are neatly written. 5. Keep all pages stapled together. 6. Calculator and electronic devices are not allowed 7. The examination period will last for 1 hour and 45 mins. 8. Stop writing immediately when the time is up.

Question 1 2 3 4 5 6 7
TOTAL

Percentage % 10 12 22 20 16 10 10 100

Scores

Student ID:

___
(10 marks)

Question 1 Basics

For each of the following statements, write down T if it is true and F otherwise. (note: 1 mark will be deducted for each wrong answer) 1) The values stored in any MIPS general purpose register can be modified. Ans: F 2) The address of any byte unit in memory is 32 bit. Ans:T 3) Consider the instruction j L1. The location of L1 should be within 256 MB relative to the address of the current instruction. 4) All jump instructions are 32 bit long. Ans:T 5) Having fixed length, with different instruction formats leads to simpler hardware design. 6) Registers are some special locations in the main memory allowing faster access. Ans:F 7) The MIPS instruction sw $s0, 12($t0) is an R-type instruction. Ans:F 8) The MIPS instruction bne $s0, $s0, L1 always executes the statement labeled L1 Ans: F 9) In MIPS instruction set, 5 bits are always used to represent opcode Ans: F 10) slti $t0, $t1, 1 beq $t0, $zero, L1 If the value of register $t0 is 1, after the two instructions above are executed, the next instruction to be executed is labeled as L1

Ans:T

Ans:T

Ans: F

Page 2 of 11

Student ID:

___
(12 marks)

Question 2 System Call

Consider the following MIPS program. Fill in the blanks so that the console displays the following message ( being the white space character): HelloWorld! Note that only ONE instruction is allowed for each blank line. (2 marks per instruction) MIPS program: #----------Data Segment---------.data # declare the strings msg1 and msg2 msg1: .asciiz "World!\n" msg2: .byte 0x6c .byte 0x6c .byte 0 .byte 0x6f .byte 0 .byte 0x48 .byte 0x65 .byte 0 #----------Text Segment---------.text .globl __start __start: la $s0, msg2 # load the address of msg2 into register $s0

# character l # character l # character \0 # character o # character \0 # character H # character e # character \0

#---------- print the message----------

____________________li $v0, 4

# load the system call code for print

____________________la $a0, 5($s0) # print the string He syscall ____________________la $a0, 0($s0) # print the string ll syscall ____________________la $a0, 3($s0) syscall # print the string o

Page 3 of 11

Student ID:

___

____________________la $a0, -9($s0) syscall

# print the string World!\n

#----------execute the exit system call---------____________________li $v0, 10 syscall # load the system call code for exit

Question 3 Memory Access, Macros, and Loops

(22 marks)

a) Assume that n corresponds to register $t0 and that the base address of array x is pointed by label x. Show a sequence of instructions for this C++ statement: x[20] = x[ x[21] ] + n; You can assume that all the elements of x are positive integers. (6 marks)

la $s0,x lw $t1,84($s0) sll $t1,$t1,2 add $t1, $t1, $s0 lw $t1, 0($t1) add $t1, $t1, $t0 sw $t1, 80($s0)

(1 mark) ----| | ---- 2 ( marks) ----| (1 mark) (1 mark) (1 mark)

b) For the following Macros (pseudoinstructions), show the minimal sequence of actual MIPS instructions to accomplish the same task. You may use the register $at to store intermediate or temporary results. (5 marks) 1) bge $t5, $t3, L #if ($t5 >= $t3) goto L slt $at, $t5, $t3 beq $at, $zero, L

2) blt $t5,$t3, L slt $at,$r5,$r3 bne $at,$zero,L

#if ($t5 < $t3) goto L

Page 4 of 11

Student ID:

___

c) The following code fragment processes an array and produces two important values in registers $v0 and $v1. Assume that there is an array ARR consisting of 5 words indexed 0 through 4. The base address of ARR is stored in register $a0 and its size in $a1 (i.e. the value in $a1 is 5). add $v0, $zero, $a1 add $t0, $zero, $zero add $a1, $a1, $a1 add $a1, $a1, $a1 outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t5, $zero, $zero add $t1, $zero, $zero inner: add $t3, $a0, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $t5, $t5, 1 skip: addi $t1, $t1, 4 bne $t1, $a1, inner slt $t2, $v0, $t5 bne $t2, $zero, next add $v0, $t5, $zero add $v1, $t4, $zero next: addi $t0, $t0, 4 bne $t0, $a1, outer 1) What will be the final values in $v0 and $v1 if ARR is given as follows? (6 marks) ARR: .word 1 2 2 4 1 $v0 = 1 $v1 = 4 2) Explain in one sentence the functionality of this code fragment. (5 marks) To find the least frequently appeared integer in the array and its frequency

Question 4 1D array

(20 marks)

Given an array of ten integers, the following code fragment counts the number of negative integers in the array. Suppose that the array and its size are defined in the data segment as follows.

Page 5 of 11

Student ID:

___

#------- Data Segment ---------.data array: .word 10, -11, -2, 32, -24, 17, -6, -15, 21, -9 size: .word 10 #------End of Data Segment -----Fill in the blank spaces with the appropriate instructions. Each line holds one instruction. Not all the lines provided need to be filled, however no lines more than those provided can be used. Labels can be added to any line in the program if needed. Assembly Program Codes # Initialization lw $t0, size # $t2 is initialized as the starting address of the array la $t2, array addi $t1, $zero, 0 addi $t3, $zero, 0 for_i: slt $t4, $t1, $t0 # else go to for_i_done beq $t4, $zero, for_i_done count_neg: lw $t5, 0($t2) slt $t4, $t5, 0 beq $t4, $zero, next_round addi $t3, $t3, 1 # get an element from the array, put it in $t5 # if the element in $t5 negative, increment the # counter for negative integers # update the loop counter and the pointer to the array # $t1 is the loop counter, initialized as 0 # $t3 is the counter for negative integers # if $t1 is less than the size of array, continue Comments # $t0 is used to store the size of the array

next_round: addi $t1, $t1, 1 addi $t2, $t2, 4 j for_i for_i_done: # Print the result

# go back to for_i

Page 6 of 11

Student ID:

___
(16 marks)

Question 5 Memory addressing and nested procedures

Given the following MIPS program fragment in memory, answer the questions below (4 marks each): Memory Address 39988 39992 39996 40000 40004 40008 40012 40016 40020 40080 Instruction addi $t1, $zero, 0 addi $s0, $zero, 100 While: slt $t0, $s0, $t1 bne $t0, $zero, End_While addi $t1, $t1, 1 addi $s0, $s0, -1 j While End_While: jal Print_result Print_result: li v0, 4

a) The hardware instruction corresponding to the bne instruction at address 40004 is: 5 8 0 3

b) The hardware instruction corresponding to the j instruction at address 40016 is: 2 10000

c) When the PC content is 40080, the content of register $ra is: 40024 d) Assuming that procedure Print_result makes subsequently a procedure call using jal, write the two instructions that need be inserted, starting at address 40080 to avoid and endless loop? addi $sp, $sp, -4 sw $ra, 0($sp)

Page 7 of 11

Student ID:

___
(10 marks)

Question 6 Addressing Mode

Your answers should have a one-to-one correspondence with one of MIPS five addressing modes that best fits. (a) Which MIPS addressing mode is used for conditional branches? (2 marks) ______________________________ PC-relative addressing

(b) Which MIPS addressing mode allows me to choose among the 32 registers? (2 marks) ______________________________ Register addressing

(c) Which MIPS addressing mode is used for jumps? (2 marks) ______________________________ Pseudodirect addressing

(d) Which addressing mode is used to access constants in arithmetic operations directly without transferring them first from memory to a register? (2 marks) ______________________________ Immediate addressing

(e) Which MIPS addressing mode is used for load and store instructions to address memory locations? (2 marks) ______________________________ Base addressing

Question 7 Memory byte order

(10 marks)

Suppose you have two machines, one is a big-endian machine (i.e., the most significant bytes are stored at the lowest memory addresses), and the other is a little-endian machine (i.e., the most significant bytes are stored at the highest memory addresses). Assume that each memory address corresponds to a single byte on both machines. a) How should the 32-bit Hexadecimal number 0x12345678 be stored on the big-endian machine? Assume the starting address for the data is at 0x0000. ( 2 marks) Memory address Data stored 0x0000 0x12 0x0001 0x34 0x0002 0x56 0x0003 0x78

b) What if the same number is stored on the little-endian machine? Assume again that the data starts at address 0x0000. (2 marks) Memory address Data stored 0x0000 0x78 0x0001 0x56 0x0002 0x34 0x0003 0x12

Page 8 of 11

Student ID:

___

c) Suppose the number in part (a) is stored in a file on the big-endian machine. The file is then transferred from the big-endian machine to the little-endian machine. What number the little-endian machine would think it gets from the big-endian machine? State your number in hexadecimal format. Assume the network does not change the byte orders in the file. (1 mark) 0x78563412 d) One way to avoid potential problems in data communications between big-endian and little-endian machines is to implement a data conversion utility on the little-endian machine to perform the appropriate conversions whenever data is being transmitted to/received from the network. Assume the network transfer data in the big-endian fashion. Explain in one sentence (and fill the spaces below), what should the data conversion utility do to the 32-bit hexadecimal number 0x12345678 of part (a) before the number is sent from a little-endian machine to the network. (2 marks) Convert the number/data to be sent into the big-endian format. (i.e.)

Memory address Data stored

Base_addr 0x12

Base_addr+1 0x34

Base_addr+3 0x56

Base_addr+4 0x78

e) Similarly, what should the data conversion utility do before the same hexadecimal number 0x12345678 received from the network is stored in the memory of the littleendian machine? (2 marks) Convert the number/data received into the little-endian format. (i.e.)

Memory address Data stored

Base_addr 0x78

Base_addr+1 0x56

Base_addr+3 0x34

Base_addr+4 0x12

f) Why such a conversion utility is not necessary on big-endian machines? (1 mark) Because the utility on the little-endian machines has made all the data transmissions on the network in the big-endian format.

------ End of Paper ------

Page 9 of 11

Student ID:

___

Appendix Reference

Page 10 of 11

Student ID:

___

Page 11 of 11

You might also like