CSAPP-Architecture lab

这个实验有三个任务,partA,partB,partC。

partA

这部分任务需要在archlab/sim/misc 目录下完成

重要代码:
./yas *.ys
./yis *.yo
gcc -c examples.c 
objdump -d examples.c > examples.d

需要查看examples.c反汇编的X86代码,然后写出相应的Y86版本代码:

  5 Disassembly of section .text:
  6 
  7 0000000000000000 :
  8    0:   55                      push   %rbp
  9    1:   48 89 e5                mov    %rsp,%rbp
 10    4:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
 11    8:   48 c7 45 f8 00 00 00    movq   $0x0,-0x8(%rbp)
 12    f:   00
 13   10:   eb 17                   jmp    29 
 14   12:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 15   16:   48 8b 00                mov    (%rax),%rax
 16   19:   48 01 45 f8             add    %rax,-0x8(%rbp)
 17   1d:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 18   21:   48 8b 40 08             mov    0x8(%rax),%rax
 19   25:   48 89 45 e8             mov    %rax,-0x18(%rbp)
 20   29:   48 83 7d e8 00          cmpq   $0x0,-0x18(%rbp)
 21   2e:   75 e2                   jne    12 
 22   30:   48 8b 45 f8             mov    -0x8(%rbp),%rax
 23   34:   5d                      pop    %rbp
 24   35:   c3                      retq

 26 0000000000000036 :
 27   36:   55                      push   %rbp
 28   37:   48 89 e5                mov    %rsp,%rbp
 29   3a:   48 83 ec 20             sub    $0x20,%rsp
 30   3e:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
 31   42:   48 83 7d e8 00          cmpq   $0x0,-0x18(%rbp)
 32   47:   75 07                   jne    50 
 33   49:   b8 00 00 00 00          mov    $0x0,%eax
 34   4e:   eb 2a                   jmp    7a 
 35   50:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 36   54:   48 8b 00                mov    (%rax),%rax
 37   57:   48 89 45 f0             mov    %rax,-0x10(%rbp)
 38   5b:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 39   5f:   48 8b 40 08             mov    0x8(%rax),%rax
 40   63:   48 89 c7                mov    %rax,%rdi
 41   66:   e8 00 00 00 00          callq  6b 
 42   6b:   48 89 45 f8             mov    %rax,-0x8(%rbp)
 43   6f:   48 8b 55 f0             mov    -0x10(%rbp),%rdx
 44   73:   48 8b 45 f8             mov    -0x8(%rbp),%rax
 45   77:   48 01 d0                add    %rdx,%rax
 46   7a:   c9                      leaveq
 47   7b:   c3                      retq

 49 000000000000007c :
 50   7c:   55                      push   %rbp
 51   7d:   48 89 e5                mov    %rsp,%rbp
 52   80:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
 53   84:   48 89 75 e0             mov    %rsi,-0x20(%rbp)
 54   88:   48 89 55 d8             mov    %rdx,-0x28(%rbp)
 55   8c:   48 c7 45 f0 00 00 00    movq   $0x0,-0x10(%rbp)
 56   93:   00
 57   94:   eb 33                   jmp    c9 
 58   96:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 59   9a:   48 8d 50 08             lea    0x8(%rax),%rdx
 60   9e:   48 89 55 e8             mov    %rdx,-0x18(%rbp)
 61   a2:   48 8b 00                mov    (%rax),%rax
 62   a5:   48 89 45 f8             mov    %rax,-0x8(%rbp)
 63   a9:   48 8b 45 e0             mov    -0x20(%rbp),%rax
 64   ad:   48 8d 50 08             lea    0x8(%rax),%rdx
 65   b1:   48 89 55 e0             mov    %rdx,-0x20(%rbp)
 66   b5:   48 8b 55 f8             mov    -0x8(%rbp),%rdx
 67   b9:   48 89 10                mov    %rdx,(%rax)
 68   bc:   48 8b 45 f8             mov    -0x8(%rbp),%rax
 69   c0:   48 31 45 f0             xor    %rax,-0x10(%rbp)
 70   c4:   48 83 6d d8 01          subq   $0x1,-0x28(%rbp)
 71   c9:   48 83 7d d8 00          cmpq   $0x0,-0x28(%rbp)
 72   ce:   7f c6                   jg     96 
 73   d0:   48 8b 45 f0             mov    -0x10(%rbp),%rax
 74   d4:   5d                      pop    %rbp
 75   d5:   c3                      retq

查看X86的汇编代码,然后照着写Y86的汇编代码。

sum.ys

  1 .pos 0
  2 irmovq stack,%rsp
  3 call main
  4 halt
  5 
  6 .align 8
  7 ele1:
  8     .quad 0x00a
  9     .quad ele2
 10 ele2:
 11     .quad 0x0b0
 12     .quad ele3
 13 ele3:
 14     .quad 0xc00
 15     .quad 0
 16 
 17 main:
 18     irmovq ele1,%rdi
 19     call sum_list
 20     ret
 21 sum_list:
 22     irmovq $8,%r8
 23     rrmovq %rdi,%rbx
 24     xorq %rax,%rax
 25     andq %rbx,%rbx
 26     je end
 27 loop:
 28     mrmovq (%rbx),%rcx
 29     addq %rcx,%rax
 30     addq %r8,%rbx
 31     mrmovq (%rbx),%rbx
 32     andq %rbx,%rbx
 33     jne loop
 34 end:
 35     ret
 36 
 37 .pos 0x200
 38 stack:
运行:
./yas sum.ys
./yis sum.yo
结果:
Stopped in 30 steps at PC = 0x13.  Status 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000000000000cba
%rcx:	0x0000000000000000	0x0000000000000c00
%rsp:	0x0000000000000000	0x0000000000000200
%rdi:	0x0000000000000000	0x0000000000000018
%r8:	0x0000000000000000	0x0000000000000008

Changes to memory:
0x01f0:	0x0000000000000000	0x000000000000005b
0x01f8:	0x0000000000000000	0x0000000000000013

rsum.ys

  1 .pos 0
  2 irmovq stack,%rsp
  3 call main
  4 halt
  5 
  6 .align 8
  7 ele1:
  8     .quad 0x00a
  9     .quad ele2
 10 ele2:
 11     .quad 0x0b0
 12     .quad ele3
 13 ele3:
 14     .quad 0xc00
 15     .quad 0
 16 
 17 main:
 18     irmovq ele1,%rdi
 19     xorq %rax,%rax
 20     xorq %rbx,%rbx
 21     call rsum_list
 22     ret
 23 rsum_list:
 24     andq %rdi,%rdi
 25     je end
 26     mrmovq (%rdi),%rcx
 27         pushq %rcx
 28     irmovq $8,%r8
 29     addq %r8,%rdi
 30     mrmovq (%rdi),%rdi
 31     call rsum_list
 32         popq %rcx
 33     addq %rcx,%rax
 34     ret
 35 end:
 36     ret
 37 
 38 .pos 1000
 39 stack:
运行结果:
Stopped in 44 steps at PC = 0x13.  Status 'HLT', CC Z=0 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000000000000cba
%rcx:	0x0000000000000000	0x000000000000000a
%rsp:	0x0000000000000000	0x00000000000003e8
%r8:	0x0000000000000000	0x0000000000000008

Changes to memory:
0x03a8:	0x0000000000000000	0x0000000000000096
0x03b0:	0x0000000000000000	0x0000000000000c00
0x03b8:	0x0000000000000000	0x0000000000000096
0x03c0:	0x0000000000000000	0x00000000000000b0
0x03c8:	0x0000000000000000	0x0000000000000096
0x03d0:	0x0000000000000000	0x000000000000000a
0x03d8:	0x0000000000000000	0x000000000000005f
0x03e0:	0x0000000000000000	0x0000000000000013

copy_block.ys

  1 .pos 0
  2 irmovq stack,%rsp
  3 call main
  4 halt
  5 
  6 .align 8
  7 src:
  8     .quad 0x00a
  9     .quad 0x0b0
 10     .quad 0xc00
 11 dist:
 12     .quad 0x111
 13     .quad 0x222
 14     .quad 0x333
 15 
 16 main:
 17     irmovq src,%rdi
 18     irmovq dist,%rsi
 19     irmovq $3,%rdx
 20     call copy_block
 21     ret
 22 copy_block:
 23     irmovq $8,%r8
 24     irmovq $1,%r9
 25     xorq %rax,%rax
 26     andq %rdx,%rdx
 27     je end
 28 loop:
 29     mrmovq (%rdi),%rbx
 30     addq %r8,%rdi
 31     rmmovq %rbx,(%rsi)
 32     addq %r8,%rsi
 33     xorq %rbx,%rax
 34     subq %r9,%rdx
 35     andq %rdx,%rdx
 36     jne loop
 37 end:
 38     ret
 39 
 40 .pos 1000
 41 stack:
运行结果:
Stopped in 38 steps at PC = 0x13.  Status 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000000000000cba
%rbx:	0x0000000000000000	0x0000000000000c00
%rsp:	0x0000000000000000	0x00000000000003e8
%rsi:	0x0000000000000000	0x0000000000000048
%rdi:	0x0000000000000000	0x0000000000000030
%r8:	0x0000000000000000	0x0000000000000008
%r9:	0x0000000000000000	0x0000000000000001

Changes to memory:
0x0030:	0x0000000000000111	0x000000000000000a
0x0038:	0x0000000000000222	0x00000000000000b0
0x0040:	0x0000000000000333	0x0000000000000c00
0x03d8:	0x0000000000000000	0x000000000000006f
0x03e0:	0x0000000000000000	0x0000000000000013

partB

需要实现一个iaddq指令,可参考下面这几个来实现
CSAPP-Architecture lab_第1张图片
CSAPP-Architecture lab_第2张图片

iaddq的流程:
210 icode:ifun <-- M1[PC] # 取指
211 rA:rB <-- M1[PC+1]
212 valC <-- M8[PC+2]
213 valP <-- PC+10
214 
215 valB <-- R[rB]        # 译码
216 
217 valE <-- valB + valC  # 执行
218 
219 R[rB] <-- valE        # 写回
220 PC <-- valP           # 更新 PC

注意出现IIADDQ的地方

223 然后根据实现修改 hcl 代码:
224 
225 ```
226 # 取指
227 # 指令是否有效?
228 bool instr_valid = icode in
230 
231 # Does fetched instruction require a regid byte?
232 bool need_regids =
233         icode in { IRRMOVQ, IOPQ, IPUSHQ, IPOPQ, IIRMOVQ, IRMMOVQ, IMRMOVQ, IIADDQ };
234 
235 # Does fetched instruction require a constant word?
236 bool need_valC =
237         icode in { IIRMOVQ, IRMMOVQ, IMRMOVQ, IJXX, ICALL, IIADDQ };
238 
239 # 译码和写回,指定读入和写入
240 ## What register should be used as the B source?
241 word srcB = [
242         icode in { IOPQ, IRMMOVQ, IMRMOVQ ,IIADDQ } : rB;
243         icode in { IPUSHQ, IPOPQ, ICALL, IRET } : RRSP;
244         1 : RNONE;  # Don't need register
245 ];
246 
247 ## What register should be used as the E destination?
252         1 : RNONE;  # Don't write any register
253 ];
254 
260         icode in { ICALL, IPUSHQ } : -8;
261         icode in { IRET, IPOPQ } : 8;
264 
265 ## Select input B to ALU
232 bool need_regids =
233         icode in { IRRMOVQ, IOPQ, IPUSHQ, IPOPQ, IIRMOVQ, IRMMOVQ, IMRMOVQ, IIADDQ };
234 
235 # Does fetched instruction require a constant word?
236 bool need_valC =
237         icode in { IIRMOVQ, IRMMOVQ, IMRMOVQ, IJXX, ICALL, IIADDQ };
238 
239 # 译码和写回,指定读入和写入
240 ## What register should be used as the B source?
241 word srcB = [
242         icode in { IOPQ, IRMMOVQ, IMRMOVQ ,IIADDQ } : rB;
243         icode in { IPUSHQ, IPOPQ, ICALL, IRET } : RRSP;
244         1 : RNONE;  # Don't need register
245 ];
246 
247 ## What register should be used as the E destination?
252         1 : RNONE;  # Don't write any register
253 ];
254 
260         icode in { ICALL, IPUSHQ } : -8;
261         icode in { IRET, IPOPQ } : 8;
264 
265 ## Select input B to ALU
266 word aluB = [
267         icode in { IRMMOVQ, IMRMOVQ, IOPQ, ICALL, IPUSHQ, IRET, IPOPQ, IIADDQ } : valB;
268         icode in { IRRMOVQ, IIRMOVQ } : 0;
269         # Other instructions don't need ALU
270 ];
271 
272 ## Should the condition codes be updated?
273 bool set_cc = icode in { IOPQ, IIADDQ };

接着测试:

完成后 `make` ,注意如果无法使用 gui 界面修改 `Makefile` ,注释掉相关内容,
使用官方文档所给的命令 `unix> ./ssim -t ../y86-code/asumi.yo` 
在命令行下测试得到 `ISA Check Succeeds` ,
再用 `unix> (cd ../y86-code; make testssim)` 和
 `unix> (cd ../ptest; make SIM=../seq/ssim)` 测试均成功。
接着我用的partA的sum.ys来测试
在程序结束前给%r8再加1,成功了
Stopped in 31 steps at PC = 0x13.  Status 'HLT', CC Z=0 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000000000000cba
%rcx:	0x0000000000000000	0x0000000000000c00
%rsp:	0x0000000000000000	0x0000000000000200
%rdi:	0x0000000000000000	0x0000000000000018
%r8:	0x0000000000000000	0x0000000000000009

Changes to memory:
0x01f0:	0x0000000000000000	0x000000000000005b
0x01f8:	0x0000000000000000	0x0000000000000013

partC

修改ncopy.ys,目的是熟悉流水线操作,加快程序的执行效率。

你可能感兴趣的:(OS,操作系统)