前期准备参考: 深入理解计算机系统 CSAPP 第四章 Y86-64模拟器 安装与使用-CSDN博客
writeup上写了要求,这里就不赘述了.
Part A:
sum.ys:
# Execution begins at address 0
.pos 0
irmovq stack, %rsp # Set up stack pointer
call main # Execute main program
halt # Terminate program
# Sample linked list
.align 8
ele1:
.quad 0x00a
.quad ele2
ele2:
.quad 0x0b0
.quad ele3
ele3:
.quad 0xc00
.quad 0
main: irmovq ele1,%rdi
call sum # sum(ele1)
ret
# long sum(long *start)
# start in %rdi
sum: xorq %rax,%rax # sum = 0
andq %rdi,%rdi # Set CC
jmp test # Goto test
loop: mrmovq (%rdi),%r10 # Get *start
irmovq $8,%r8
addq %r10,%rax # Add to sum
addq %r8,%rdi # start++
mrmovq (%rdi),%rdi # *start
andq %rdi,%rdi # Set CC
test: jne loop # Stop when 0
ret # Return
# Stack starts here and grows to lower addresses
.pos 0x200
stack:
addq %r8,%rdi 是下一个元素的地址,不是值.
rsum.ys:
# Execution begins at address 0
.pos 0
irmovq stack, %rsp # Set up stack pointer
call main # Execute main program
halt # Terminate program
# Sample linked list
.align 8
ele1:
.quad 0x00a
.quad ele2
ele2:
.quad 0x0b0
.quad ele3
ele3:
.quad 0xc00
.quad 0
main: irmovq ele1,%rdi
xorq %rax,%rax # sum = 0
call rsum # rsum(rsum)
ret
# long rsum(long *start)
# start in %rdi
rsum: andq %rdi,%rdi # Set CC
je return # Stop when 0
mrmovq (%rdi),%rbx # Get *start
mrmovq 8(%rdi),%rdi # *start
pushq %rbx
call rsum
popq %rbx
addq %rbx,%rax # Add to sum
return: ret # Return
# Stack starts here and grows to lower addresses
.pos 0x200
stack:
copy.ys:
# Execution begins at address 0
.pos 0
irmovq stack, %rsp # Set up stack pointer
call main # Execute main program
halt # Terminate program
.align 8
# Source block
src:
.quad 0x00a
.quad 0x0b0
.quad 0xc00
# Destination block
dest:
.quad 0x111
.quad 0x222
.quad 0x333
main: irmovq src,%rdi
irmovq dest,%rsi
irmovq $3,%rdx
call copy # copy(src,dest,0)
ret
# long copy(long *src,long *dest,long len)
# src in %rdi ,dest in %rsi,len in %rdx
copy: xorq %rax,%rax # result = 0
loop: andq %rdx,%rdx
mrmovq (%rdi),%rcx
rmmovq %rcx,(%rsi)
irmovq $1,%r10
irmovq $8,%r8
xorq %rcx,%rax
addq %r8,%rdi
addq %r8,%rsi
subq %r10,%rdx
jg loop # >0
return: ret # Return
# Stack starts here and grows to lower addresses
.pos 0x200
stack:
Part B:
因为前面的家庭作业和练习题已经做过这个,这里不在赘述了.
参考,图4-18,在不同的阶段需要用哪写寄存器,修改哪些逻辑,添加上 ,IIADDQ 即可.
#/* $begin seq-all-hcl */
####################################################################
# HCL Description of Control for Single Cycle Y86-64 Processor SEQ #
# Copyright (C) Randal E. Bryant, David R. O'Hallaron, 2010 #
####################################################################
## Your task is to implement the iaddq instruction
## The file contains a declaration of the icodes
## for iaddq (IIADDQ)
## Your job is to add the rest of the logic to make it work
####################################################################
# C Include's. Don't alter these #
####################################################################
quote '#include <stdio.h>'
quote '#include "isa.h"'
quote '#include "sim.h"'
quote 'int sim_main(int argc, char *argv[]);'
quote 'word_t gen_pc(){return 0;}'
quote 'int main(int argc, char *argv[])'
quote ' {plusmode=0;return sim_main(argc,argv);}'
####################################################################
# Declarations. Do not change/remove/delete any of these #
####################################################################
##### Symbolic representation of Y86-64 Instruction Codes #############
wordsig INOP 'I_NOP'
wordsig IHALT 'I_HALT'
wordsig IRRMOVQ 'I_RRMOVQ'
wordsig IIRMOVQ 'I_IRMOVQ'
wordsig IRMMOVQ 'I_RMMOVQ'
wordsig IMRMOVQ 'I_MRMOVQ'
wordsig IOPQ 'I_ALU'
wordsig IJXX 'I_JMP'
wordsig ICALL 'I_CALL'
wordsig IRET 'I_RET'
wordsig IPUSHQ 'I_PUSHQ'
wordsig IPOPQ 'I_POPQ'
# Instruction code for iaddq instruction
wordsig IIADDQ 'I_IADDQ'
##### Symbolic represenations of Y86-64 function codes #####
wordsig FNONE 'F_NONE' # Default function code
##### Symbolic representation of Y86-64 Registers referenced explicitly #####
wordsig RRSP 'REG_RSP' # Stack Pointer
wordsig RNONE 'REG_NONE' # Special value indicating "no register"
##### ALU Functions referenced explicitly #####
wordsig ALUADD 'A_ADD' # ALU should add its arguments
##### Possible instruction status values