当前位置:   article > 正文

深入理解计算机系统 CSAPP 实验lab:Architecture Lab

深入理解计算机系统 CSAPP 实验lab:Architecture Lab

 前期准备参考: 深入理解计算机系统 CSAPP 第四章 Y86-64模拟器 安装与使用-CSDN博客

writeup上写了要求,这里就不赘述了.

Part A:

sum.ys:
  1. # Execution begins at address 0
  2. .pos 0
  3. irmovq stack, %rsp # Set up stack pointer
  4. call main # Execute main program
  5. halt # Terminate program
  6. # Sample linked list
  7. .align 8
  8. ele1:
  9. .quad 0x00a
  10. .quad ele2
  11. ele2:
  12. .quad 0x0b0
  13. .quad ele3
  14. ele3:
  15. .quad 0xc00
  16. .quad 0
  17. main: irmovq ele1,%rdi
  18. call sum # sum(ele1)
  19. ret
  20. # long sum(long *start)
  21. # start in %rdi
  22. sum: xorq %rax,%rax # sum = 0
  23. andq %rdi,%rdi # Set CC
  24. jmp test # Goto test
  25. loop: mrmovq (%rdi),%r10 # Get *start
  26. irmovq $8,%r8
  27. addq %r10,%rax # Add to sum
  28. addq %r8,%rdi # start++
  29. mrmovq (%rdi),%rdi # *start
  30. andq %rdi,%rdi # Set CC
  31. test: jne loop # Stop when 0
  32. ret # Return
  33. # Stack starts here and grows to lower addresses
  34. .pos 0x200
  35. stack:

addq %r8,%rdi 是下一个元素的地址,不是值.

rsum.ys: 
  1. # Execution begins at address 0
  2. .pos 0
  3. irmovq stack, %rsp # Set up stack pointer
  4. call main # Execute main program
  5. halt # Terminate program
  6. # Sample linked list
  7. .align 8
  8. ele1:
  9. .quad 0x00a
  10. .quad ele2
  11. ele2:
  12. .quad 0x0b0
  13. .quad ele3
  14. ele3:
  15. .quad 0xc00
  16. .quad 0
  17. main: irmovq ele1,%rdi
  18. xorq %rax,%rax # sum = 0
  19. call rsum # rsum(rsum)
  20. ret
  21. # long rsum(long *start)
  22. # start in %rdi
  23. rsum: andq %rdi,%rdi # Set CC
  24. je return # Stop when 0
  25. mrmovq (%rdi),%rbx # Get *start
  26. mrmovq 8(%rdi),%rdi # *start
  27. pushq %rbx
  28. call rsum
  29. popq %rbx
  30. addq %rbx,%rax # Add to sum
  31. return: ret # Return
  32. # Stack starts here and grows to lower addresses
  33. .pos 0x200
  34. stack:

copy.ys:
  1. # Execution begins at address 0
  2. .pos 0
  3. irmovq stack, %rsp # Set up stack pointer
  4. call main # Execute main program
  5. halt # Terminate program
  6. .align 8
  7. # Source block
  8. src:
  9. .quad 0x00a
  10. .quad 0x0b0
  11. .quad 0xc00
  12. # Destination block
  13. dest:
  14. .quad 0x111
  15. .quad 0x222
  16. .quad 0x333
  17. main: irmovq src,%rdi
  18. irmovq dest,%rsi
  19. irmovq $3,%rdx
  20. call copy # copy(src,dest,0)
  21. ret
  22. # long copy(long *src,long *dest,long len)
  23. # src in %rdi ,dest in %rsi,len in %rdx
  24. copy: xorq %rax,%rax # result = 0
  25. loop: andq %rdx,%rdx
  26. mrmovq (%rdi),%rcx
  27. rmmovq %rcx,(%rsi)
  28. irmovq $1,%r10
  29. irmovq $8,%r8
  30. xorq %rcx,%rax
  31. addq %r8,%rdi
  32. addq %r8,%rsi
  33. subq %r10,%rdx
  34. jg loop # >0
  35. return: ret # Return
  36. # Stack starts here and grows to lower addresses
  37. .pos 0x200
  38. stack:

Part B: 

因为前面的家庭作业和练习题已经做过这个,这里不在赘述了.

参考,图4-18,在不同的阶段需要用哪写寄存器,修改哪些逻辑,添加上 ,IIADDQ 即可.

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

sim文件夹中右键启动终端,重新生成版本为full的ssim版本:

make clean;make VERSION=full

 sim/seq文件夹中右键启动终端,运行单个程序测试:

./ssim -t ../y86-code/asumi.yo

单步调试命令: 

 ./ssim -g ../y86-code/asumi.yo

 sim/ptest文件夹中右键启动终端,运行所有测试:全部通过.

make SIM=../seq/ssim TFLAGS=-i

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/1006886
推荐阅读
相关标签
  

闽ICP备14008679号