第三章 计算机组成原理.doc
(5页)
本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!
9.90 积分
Chapter:1,2,4,5,6,7,8,9,11,23,241. Add comments to the following MIPS code and describe in one sentence what it computes. Assume that $a0 is used for the input and initially contains n, a positive integer. Assume that $v0 is used for the output.begin: addi $t0, $zero, 0 # $t0 = 0 addi $t1, $zero, 1 # $t1 = 1 loop: slt $t2, $a0, $t1 # $t2 = 1 if $a0 < $t1 bne $t2, $zero, finish # if $a0 < $t1, jump to finish add $t0, $t0, $t1 # $t0 = $t0+ $t1 addi $t1, $t1, 2 # $t1 = $t1+2 j loop # jump to loopfinish: add $v0, $t0, $zero # return $t0A sample C program:int f(int n){ int i = 0; int j = 1; while( n >= i){ i = i + j; j += 2; } return i;}2. The following code fragment processes an array and produces two important values in registers $v0 and $v1. Assume that the array consists of 5000 words indexed 0 through 4999, and its base address is stored in $a0 and its size(5000) in $a1. Describe in one sentence what this code does. Specifically, what will be returned in $v0 and $v1?Find the element which appears most times, $v0 stores the the maxium number of the identical integer in the array, $v1 stores its value.4. Show the single MIPS instruction or minimal sequence of instructions for this C statement:a = b +100;; Assume that a corresponds to register $t0 and b corresponds to register $t1. addi $t0,$t1,1005. Show the single MIPS instruction or minimal sequence of instructions for this C statement:x[10] = x[11] + c; Assume that c corresponds to register $t0 and the array x has a base address of 4,000,000ten.4,000,000ten=11,1101,0000,1001,0000,0000twolui $t1,61addi $t1,$t1,2304lw $t2,44($t1)add $t2,$t2,$t0sw $t2,40($t1)6. The following program tries to cope words from the address in register $a0 to the address in register $a1, counting the number of words copied in register $v0. The program stops copying when it finds a word equal to 0. You do not have to preserve the contents of registers $v1, $a0, and $a1. This terminating word should be copied but not counted.loop: lw $v1, 0($a0) # Read next word from source addi $v0,$v0,1 # Increment count words copied sw $v1,0($a1) # Write to destination addi $a0,$a0,1 # Advance pointer to next source addi $a1,$a1,1 # Advance pointer to next dest bne $v1,$zero,loop # Loop if word copied ≠ zero Loop: lw $v1, 0($a0) # Read next word from source sw $v1, 0($a1) # Write to destination addi $a0, $a0, 4 # Advance pointer to next source addi $a1, $a1, 4 # Advance pointer to next dest beq $v1, $zero, End # Exit if word copied = 0 addi $v0, $v0, 1 # Increment count words copied j Loop # Do next loopEnd:7. Using the MIPS program in Exercise 3.6(with bugs intact), determine the instruction format for each instruction and the decimal values of each instruction field.MIPS ProgramFormatDecimal Valuelw $v1, 0($a0)I35430addi $v0, $v0, 1I8221sw $v1, 0($a1)I43530addi $a0, $a0, 1I8441addi $a1, $a1, 1I8551bne $v1, $zero, loopI53010000(address of Loop)8. Starting with the corrected program in the answer to Exercise 3.6, write the C code segment that might have produced this code. Assume that variable source corresponds to register $a0, variable destination corresponds to register $a1, and variable count corresponds to register $v0. Show variable declarations, but assume that source and destination have been initialized to the proper addrsses.1. function versionint copy(int *source, int *destination){ int count = 0; while(*destination++=*source++) ++count; return count;}2. plane versionint count;long *source, *destination;for (count = 0; (*destination++ = *source++) != 0; ) count++;9. The C segmentwhile (save[i] == k) i = i + j;on page 127 uses both a conditional branch and an unconditional jump each time through the loop. Only poor compiler would produce code with this loop overhead. Rewrite the assembly code so that it uses at most one branch or jump each time through the loop. How many instructions are executed before and after the optimization if the number of iterations of the loop is 10?My solution ( may be not the best?):add $t1,$s3,$s3 #Temp reg $t1 = 2*iadd $t1,$t1,$t1 #Temp reg $t1 = 4*iadd $t1,$t1,$s6 #Temp reg $t1 = Save+4*iadd $t2,$s4,$s4 #Temp reg $t2 = 2*jadd $t2,$t2,$t2 #Temp reg $t2 = 4*jsub $t1,$t1,$t2 # i = i - jLoop: add $t1,$t1,$t2 # i = i + jlw $t0,0($t1) # Temp reg $t0 = Save[i]beq $t0,$s5,Loop # if(Save[i] == k), LoopBefore Modify: 75After Modify: 3911. Consider the following fragment of C code:for (i=0; i<=100; i++) {a[i] = b[i] + c;}Assume that a and b are arrays of words and the base address of a is in $a0 and the base address of b is $a1. Register $t0 is associated with variable i and register $s0 with c. Write the code for MIPS. How many instructions are executed during the running of this code? How many memory data references will be made during execution?One of the solution: add $t0,$zero,$zero # i = 0Loop: add $t1,$t0,$a0 # $t1 = a+i add $t2,$t0,$a1 # $t2 = b+i lw $t2,0($t1) # $t2 = b[i] add $t2,$t1,$s0 # $t2 = b[i] + c sw $t2,0($t1) # a[i] = b[i] + c addi $t0,$t0,4 # i++ slti $t3,$t0,404 bne $t3,$zero,Loop #if(i<101), goto LoopThere are 809 instructions executed during the running of this code.Memory data references made during execution: 2*101*4 = 808 bytes.23. Write a procedure, bfind, in MIPS assuembly language. The procedure should take a single argument that is a pointer to a nullterminated string in register $a0. The bfind procedure should locate the first b character in the string and return its address in register $v0. If there are no b’s in the string,then bfind should return a pointer to the null character at the end of the string. For example, if the argument to bfind points to the string “imbibe,”then the return value will be a pointer to the third character of the string.A sample Program:bfind: addi $t0,$zero,98 # t0 = 'b' add $v0,$a0,$zero # $v0 = $a0Loop: lb $t1,0($v0) beq $t1,$t0,Exit beq $t1,$zero,Exit addi $v0,$v0,1 j Loop Exit: jr $ra24. Write a procedure, bcount, in MIPS assuembly language. The bcount procedure takes a single argument which is a pointer to a string in register $a0, and it returns a count of the total number of b characters in the string in register $v0. You must use your bfind procedure in Exercise 3.23 in your implementation of bcound.bcount: addi $sp,$sp,-12 sw $ra,8($sp) sw $a0,4($sp) sw $s0,0($sp) add $s0,$zero,$zeroNext: jal bfind add $a0,$v0,$zero lb $t0,0($a0) beq $t0,$zero,End #encounter null addi $s0,$s0,1 addi $v0,$v0,1 j NextEnd: add $v0,$s0,$zero lw $s0,0($sp) lw $a0,4($sp) lw $ra,8($sp) addi $sp,$sp,12 jr $ra 关 键 词: 第三章 计算机组成原理 第三 计算机 组成 原理
天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。