[关闭]
@Radon 2014-11-12T14:21:21.000000Z 字数 8796 阅读 1837

Homework

1100012749


Homework 10

6.23

磁盘容量 ~ xr(1 - x)
--> x = 0.5

6.25

Taccess= Tavgseek + Tavgrotation + Tavgtransfer
Tavgrotation = 1/2 x(60 secs/15000 RPM) x1000 ms/sec = 2 ms.
定位时间 = 3 + 2 = 5 ms
A. 5 + 4000 / 1000 * 4 = 21ms
B. 5 * 4000 = 20000ms

6.27

Cache C B E S t s b
1. 2048 256
2. 4 4
3. 25 6
4. 32 5

6.29

A. 无
B. 0x18F0, 0x18F1, 0x18fF2, 0x18F3, 0xB0, 0xB1, 0xB2, 0xB3
C. 0xE34, 0xE35, 0xE36, 0xE37
D. 0x1BDC, 0x1BDD, 0x1BDE, 0x1BDF

6.46

  1. void transpose(int *dst,int *src,int dim)
  2. {
  3. int i, j, ii, jj, i_;
  4. int Dim = dim*4;
  5. for(i = 0, ii = 0; i < dim-3; i += 4, ii += Dim)
  6. {
  7. for(j = 0, jj = 0; j < dim-3; j += 4, jj += Dim)
  8. {
  9. dst[jj + i] = src[ii + j];
  10. dst[jj + i+1] = src[ii+dim + j];
  11. dst[jj + i+2] = src[ii+dim*2 + j];
  12. dst[jj + i+3] = src[ii+dim*3 + j];
  13. dst[jj+dim + i] = src[ii + j+1];
  14. dst[jj+dim + i+1] = src[ii+dim + j+1];
  15. dst[jj+dim + i+2] = src[ii+dim*2 + j+1];
  16. dst[jj+dim + i+3] = src[ii+dim*3 + j+1];
  17. dst[jj+dim*2 + i] = src[ii + j+2];
  18. dst[jj+dim*2 + i+1] = src[ii+dim + j+2];
  19. dst[jj+dim*2 + i+2] = src[ii+dim*2 + j+2];
  20. dst[jj+dim*2 + i+3] = src[ii+dim*3 + j+2];
  21. dst[jj+dim*3 + i] = src[ii + j+3];
  22. dst[jj+dim*3 + i+1] = src[ii+dim + j+3];
  23. dst[jj+dim*3 + i+2] = src[ii+dim*2 + j+3];
  24. dst[jj+dim*3 + i+3] = src[ii+dim*3 + j+3];
  25. }
  26. }
  27. i_ = i;//j
  28. for(i = 0; i < dim; i++)
  29. for(j = i_; j < dim; j++)
  30. dst[j*dim + i] = src[i*dim + j];
  31. for(i = i_;i < dim;i++)
  32. for(j = 0;j < i_;j++)
  33. dst[j*dim + i] = src[i*dim + j];
  34. }

Homework 7

4.43

A. 当REG是%esp时,这段代码将会把REG-4压入栈中。
B.

  1. movl REG,-4(%esp)
  2. subl $4,%esp

4.45

A.

  1. void bubble_a(int *data,int count) {
  2. int i, last;
  3. for (last = count-1; last > 0; last--) {
  4. for (i = 0; i < last; i++)
  5. if (*(data+i+1) < *(data+i)) {
  6. int t = *(data+i+1);
  7. *(data+i+1) = *(data+i);
  8. *(data+i) = t;
  9. }
  10. }
  11. }
  12. int main(){
  13. int data[3] = {3,2,1};
  14. bubble_a(data,3);
  15. return 0;
  16. }

B.

  1. .pos 0
  2. init:
  3. irmovl Stack, %esp # Set up Stack pointer
  4. irmovl Stack, %ebp # Set up base pointer
  5. call Main # Execute main program
  6. halt
  7. # Array of 4 elements
  8. .align 4
  9. array:
  10. .long 5
  11. .long 2
  12. .long 0
  13. .long 1
  14. Main:
  15. pushl %ebp
  16. rrmovl %esp,%ebp
  17. irmovl $4,%eax
  18. pushl %eax # Push count=4
  19. irmovl array,%edx
  20. pushl %edx # Push array
  21. call bubble_a # bubble_a(array, 4)
  22. rrmovl %ebp,%esp
  23. popl %ebp
  24. ret
  25. bubble_a:
  26. pushl %ebp
  27. rrmovl %esp, %ebp
  28. mrmovl 12(%ebp), %esi
  29. mrmovl 8(%ebp), %edx # edx = data
  30. irmovl $-1,%eax
  31. addl %eax, %esi # esi = last == count-1
  32. jle L1
  33. #innerLoop
  34. L10:
  35. xorl %eax, %eax # i = 0
  36. L7:
  37. pushl %esi
  38. rrmovl %eax, %esi
  39. addl %esi, %esi
  40. addl %esi, %esi
  41. addl %edx, %esi
  42. mrmovl 4(%esi), %ecx #movl 4(%edx,%eax,4), %ecx
  43. mrmovl (%esi), %ebx #movl (%edx,%eax,4), %ebx
  44. subl %ebx, %ecx
  45. jge L3
  46. addl %ebx, %ecx
  47. rmmovl %ebx, 4(%esi)
  48. rmmovl %ecx, (%esi)
  49. L3:
  50. popl %esi
  51. irmovl $1,%ecx
  52. addl %ecx, %eax
  53. rrmovl %eax, %ecx
  54. subl %esi, %ecx
  55. jl L7
  56. #innerLoop
  57. irmovl $-1, %ecx
  58. addl %ecx, %esi
  59. jne L10
  60. L1:
  61. rrmovl %ebp,%esp
  62. popl %ebp
  63. ret
  64. .pos 0x200
  65. Stack:

4.46

  1. #innerLoop
  2. L10:
  3. xorl %eax, %eax # i = 0
  4. L7:
  5. pushl %esi
  6. rrmovl %eax, %esi
  7. addl %esi, %esi
  8. addl %esi, %esi
  9. addl %edx, %esi
  10. mrmovl 4(%esi), %ecx #movl 4(%edx,%eax,4), %ecx
  11. mrmovl (%esi), %ebx #movl (%edx,%eax,4), %ebx
  12. pushl %esi
  13. rrmovl %ecx, %esi
  14. subl %ebx, %esi
  15. rrmovl %ecx, %esi
  16. cmovl %ebx, %ecx
  17. cmovl %esi, %ebx
  18. popl %esi
  19. rmmovl %ecx, 4(%esi)
  20. rmmovl %ebx, (%esi)
  21. L3:
  22. popl %esi
  23. irmovl $1,%ecx
  24. addl %ecx, %eax
  25. rrmovl %eax, %ecx
  26. subl %esi, %ecx
  27. jl L7
  28. #innerLoop

4.49

  1. # Stage iaddl V, rB
  2. #
  3. # Fetch:
  4. # icode:ifun <- M1[PC]
  5. # rA:rB <- M1[PC+1]
  6. # valC <- M4[PC+2]
  7. # valP <- PC+6
  8. #
  9. # Decode:
  10. # valB <- R[rB]
  11. #
  12. # Execute:
  13. # valE <- valB+valC
  14. # Set CC
  15. #
  16. # Memory
  17. #
  18. # Write back:
  19. # R[rB] <- valE
  20. #
  21. # PC update:
  22. # PC <- valP
  23. #
  24. #/* $begin seq-all-hcl */
  25. ####################################################################
  26. # HCL Description of Control for Single Cycle Y86 Processor SEQ #
  27. # Copyright (C) Randal E. Bryant, David R. O'Hallaron, 2010 #
  28. ####################################################################
  29. ## Your task is to implement the iaddl and leave instructions
  30. ## The file contains a declaration of the icodes
  31. ## for iaddl (IIADDL) and leave (ILEAVE).
  32. ## Your job is to add the rest of the logic to make it work
  33. ####################################################################
  34. # C Include's. Don't alter these #
  35. ####################################################################
  36. quote '#include <stdio.h>'
  37. quote '#include "isa.h"'
  38. quote '#include "sim.h"'
  39. quote 'int sim_main(int argc, char *argv[]);'
  40. quote 'int gen_pc(){return 0;}'
  41. quote 'int main(int argc, char *argv[])'
  42. quote ' {plusmode=0;return sim_main(argc,argv);}'
  43. ####################################################################
  44. # Declarations. Do not change/remove/delete any of these #
  45. ####################################################################
  46. ##### Symbolic representation of Y86 Instruction Codes #############
  47. intsig INOP 'I_NOP'
  48. intsig IHALT 'I_HALT'
  49. intsig IRRMOVL 'I_RRMOVL'
  50. intsig IIRMOVL 'I_IRMOVL'
  51. intsig IRMMOVL 'I_RMMOVL'
  52. intsig IMRMOVL 'I_MRMOVL'
  53. intsig IOPL 'I_ALU'
  54. intsig IJXX 'I_JMP'
  55. intsig ICALL 'I_CALL'
  56. intsig IRET 'I_RET'
  57. intsig IPUSHL 'I_PUSHL'
  58. intsig IPOPL 'I_POPL'
  59. # Instruction code for iaddl instruction
  60. intsig IIADDL 'I_IADDL'
  61. # Instruction code for leave instruction
  62. intsig ILEAVE 'I_LEAVE'
  63. ##### Symbolic represenations of Y86 function codes #####
  64. intsig FNONE 'F_NONE' # Default function code
  65. ##### Symbolic representation of Y86 Registers referenced explicitly #####
  66. intsig RESP 'REG_ESP' # Stack Pointer
  67. intsig REBP 'REG_EBP' # Frame Pointer
  68. intsig RNONE 'REG_NONE' # Special value indicating "no register"
  69. ##### ALU Functions referenced explicitly #####
  70. intsig ALUADD 'A_ADD' # ALU should add its arguments
  71. ##### Possible instruction status values #####
  72. intsig SAOK 'STAT_AOK' # Normal execution
  73. intsig SADR 'STAT_ADR' # Invalid memory address
  74. intsig SINS 'STAT_INS' # Invalid instruction
  75. intsig SHLT 'STAT_HLT' # Halt instruction encountered
  76. ##### Signals that can be referenced by control logic ####################
  77. ##### Fetch stage inputs #####
  78. intsig pc 'pc' # Program counter
  79. ##### Fetch stage computations #####
  80. intsig imem_icode 'imem_icode' # icode field from instruction memory
  81. intsig imem_ifun 'imem_ifun' # ifun field from instruction memory
  82. intsig icode 'icode' # Instruction control code
  83. intsig ifun 'ifun' # Instruction function
  84. intsig rA 'ra' # rA field from instruction
  85. intsig rB 'rb' # rB field from instruction
  86. intsig valC 'valc' # Constant from instruction
  87. intsig valP 'valp' # Address of following instruction
  88. boolsig imem_error 'imem_error' # Error signal from instruction memory
  89. boolsig instr_valid 'instr_valid' # Is fetched instruction valid?
  90. ##### Decode stage computations #####
  91. intsig valA 'vala' # Value from register A port
  92. intsig valB 'valb' # Value from register B port
  93. ##### Execute stage computations #####
  94. intsig valE 'vale' # Value computed by ALU
  95. boolsig Cnd 'cond' # Branch test
  96. ##### Memory stage computations #####
  97. intsig valM 'valm' # Value read from memory
  98. boolsig dmem_error 'dmem_error' # Error signal from data memory
  99. ####################################################################
  100. # Control Signal Definitions. #
  101. ####################################################################
  102. ################ Fetch Stage ###################################
  103. # Determine instruction code
  104. int icode = [
  105. imem_error: INOP;
  106. 1: imem_icode; # Default: get from instruction memory
  107. ];
  108. # Determine instruction function
  109. int ifun = [
  110. imem_error: FNONE;
  111. 1: imem_ifun; # Default: get from instruction memory
  112. ];
  113. bool instr_valid = icode in
  114. { INOP, IHALT, IRRMOVL, IIRMOVL, IRMMOVL, IMRMOVL,
  115. IOPL, IJXX, ICALL, IRET, IPUSHL, IPOPL, IIADDL, ILEAVE };
  116. # Does fetched instruction require a regid byte?
  117. bool need_regids =
  118. icode in { IRRMOVL, IOPL, IPUSHL, IPOPL,
  119. IIRMOVL, IRMMOVL, IMRMOVL, IIADDL };
  120. # Does fetched instruction require a constant word?
  121. bool need_valC =
  122. icode in { IIRMOVL, IRMMOVL, IMRMOVL, IJXX, ICALL, IIADDL };
  123. ################ Decode Stage ###################################
  124. ## What register should be used as the A source?
  125. int srcA = [
  126. icode in { IRRMOVL, IRMMOVL, IOPL, IPUSHL } : rA;
  127. icode in { IPOPL, IRET } : RESP;
  128. icode in { ILEAVE } : REBP;
  129. 1 : RNONE; # Don't need register
  130. ];
  131. ## What register should be used as the B source?
  132. int srcB = [
  133. icode in { IOPL, IRMMOVL, IMRMOVL, IIADDL } : rB;
  134. icode in { IPUSHL, IPOPL, ICALL, IRET } : RESP;
  135. icode in { ILEAVE } : REBP;
  136. 1 : RNONE; # Don't need register
  137. ];
  138. ## What register should be used as the E destination?
  139. int dstE = [
  140. icode in { IRRMOVL } && Cnd : rB;
  141. icode in { IIRMOVL, IOPL, IIADDL} : rB;
  142. icode in { IPUSHL, IPOPL, ICALL, IRET, ILEAVE } : RESP;
  143. 1 : RNONE; # Don't write any register
  144. ];
  145. ## What register should be used as the M destination?
  146. int dstM = [
  147. icode in { IMRMOVL, IPOPL } : rA;
  148. icode in { ILEAVE } : REBP;
  149. 1 : RNONE; # Don't write any register
  150. ];
  151. ################ Execute Stage ###################################
  152. ## Select input A to ALU
  153. int aluA = [
  154. icode in { IRRMOVL, IOPL } : valA;
  155. icode in { IIRMOVL, IRMMOVL, IMRMOVL, IIADDL } : valC;
  156. icode in { ICALL, IPUSHL } : -4;
  157. icode in { IRET, IPOPL, ILEAVE } : 4;
  158. # Other instructions don't need ALU
  159. ];
  160. ## Select input B to ALU
  161. int aluB = [
  162. icode in { IRMMOVL, IMRMOVL, IOPL, ICALL,
  163. IPUSHL, IRET, IPOPL, IIADDL, ILEAVE } : valB;
  164. icode in { IRRMOVL, IIRMOVL } : 0;
  165. # Other instructions don't need ALU
  166. ];
  167. ## Set the ALU function
  168. int alufun = [
  169. icode == IOPL : ifun;
  170. 1 : ALUADD;
  171. ];
  172. ## Should the condition codes be updated?
  173. bool set_cc = icode in { IOPL, IIADDL };
  174. ################ Memory Stage ###################################
  175. ## Set read control signal
  176. bool mem_read = icode in { IMRMOVL, IPOPL, IRET, ILEAVE };
  177. ## Set write control signal
  178. bool mem_write = icode in { IRMMOVL, IPUSHL, ICALL };
  179. ## Select memory address
  180. int mem_addr = [
  181. icode in { IRMMOVL, IPUSHL, ICALL, IMRMOVL } : valE;
  182. icode in { IPOPL, IRET, ILEAVE } : valA;
  183. # Other instructions don't need address
  184. ];
  185. ## Select memory input data
  186. int mem_data = [
  187. # Value from register
  188. icode in { IRMMOVL, IPUSHL } : valA;
  189. # Return PC
  190. icode == ICALL : valP;
  191. # Default: Don't write anything
  192. ];
  193. ## Determine instruction status
  194. int Stat = [
  195. imem_error || dmem_error : SADR;
  196. !instr_valid: SINS;
  197. icode == IHALT : SHLT;
  198. 1 : SAOK;
  199. ];
  200. ################ Program Counter Update ############################
  201. ## What address should instruction be fetched at
  202. int new_pc = [
  203. # Call. Use instruction constant
  204. icode == ICALL : valC;
  205. # Taken branch. Use instruction constant
  206. icode == IJXX && Cnd : valC;
  207. # Completion of RET instruction. Use value from stack
  208. icode == IRET : valM;
  209. # Default: Use incremented PC
  210. 1 : valP;
  211. ];
  212. #/* $end seq-all-hcl */
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注