/usr/bin/ld: yas.o:/home/usr/CMU15-213 labs/archlab-handout/sim/misc/yas.h:13: multiple definition of `lineno'; yas-grammar.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status make: *** [Makefile:32: yas] Error1
I had the same problem, it is about gcc. gcc-10 changed default from “-fcommon” to “-fno-common”. You need to add “-fcommon” flag to Makefiles. For example,
# long absSum(long *start, long count) # start in %rdi, count in %rsi absSum: irmovq $8,%r8 # 表示每个数组元素占 8 字节 irmovq $1,%r9 # 用于递减计数器 count xorq %rax,%rax # sum = 0 andq %rsi,%rsi # 设置条件码,以便跳转指令可以检查 count 是否为零 jmp test /* $begin abs-sum-cmov-ys */ loop: mrmovq (%rdi),%r10 # x = *start xorq %r11,%r11 # Constant 0 subq %r10,%r11 # -x cmovg %r11,%r10 # If -x > 0 then x = -x addq %r10,%rax # Add to sum addq %r8,%rdi # start++ subq %r9,%rsi # count-- test: jne loop # Stop when 0 /* $end abs-sum-cmov-ys */ ret
# 栈定义,起始位置为0x200 .pos 0x200 stack:
sum.ys:求和链表元素
先给出examples.c中对应的C语言版本,包含了对链表的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
typedefstructELE { long val; structELE *next; } *list_ptr;
/* sum_list - Sum the elements of a linked list */ longsum_list(list_ptr ls) { long val = 0; while (ls) { val += ls->val; ls = ls->next; } return val; }
Stopped in 26 steps at PC = 0x13. Status 'HLT', CC Z=1 S=0 O=0 Changes to registers: %rax: 0x0000000000000000 0x0000000000000cba %rbx: 0x0000000000000000 0x0000000000000c00 %rsp: 0x0000000000000000 0x0000000000000200
Changes to memory: 0x01f0: 0x0000000000000000 0x000000000000005b 0x01f8: 0x0000000000000000 0x0000000000000013
可以看到%rax的值已经变成了0xcba,这是正确的结果。
rsum.ys:递归求和链表元素
与上一题一样,我们这次采取递归的方法。
1 2 3 4 5 6 7 8 9 10 11
/* rsum_list - Recursive version of sum_list */ longrsum_list(list_ptr ls) { if (!ls) return0; else { long val = ls->val; long rest = rsum_list(ls->next); return val + rest; } }
/* copy_block - Copy src to dest and return xor checksum of src */ longcopy_block(long *src, long *dest, long len) { long result = 0; while (len > 0) { long val = *src++; *dest++ = val; result ^= val; len--; } return result; }
word srcA = [ icode in { IRRMOVQ, IRMMOVQ, IOPQ, IPUSHQ } : rA; icode in { IIADDQ } : rB; icode in { IPOPQ, IRET } : RRSP; 1 : RNONE; # Don't need register ];
word dstE = [ icode in { IRRMOVQ } && Cnd : rB; icode in { IIRMOVQ, IOPQ, IIADDQ } : rB; icode in { IPUSHQ, IPOPQ, ICALL, IRET } : RRSP; 1 : RNONE; # Don't write any register ];
## Select input A to ALU word aluA = [ icode in { IRRMOVQ, IOPQ } : valA; icode in { IIRMOVQ, IRMMOVQ, IMRMOVQ, IIADDQ } : valC; icode in { ICALL, IPUSHQ } : -8; icode in { IRET, IPOPQ } : 8; # Other instructions don't need ALU ];
## Select input B to ALU word aluB = [ icode in { IRMMOVQ, IMRMOVQ, IOPQ, ICALL, IPUSHQ, IRET, IPOPQ } : valB; icode in { IRRMOVQ, IIRMOVQ } : 0; icode in { IIADDQ } : valA; # Other instructions don't need ALU ];
usr@Fabulous:~/CMU15-213 labs/archlab-handout/sim/seq$ cd ../ptest; make SIM=../seq/ssim TFLAGS=-i ./optest.pl -s ../seq/ssim -i Simulating with ../seq/ssim All 58 ISA Checks Succeed ./jtest.pl -s ../seq/ssim -i Simulating with ../seq/ssim All 96 ISA Checks Succeed ./ctest.pl -s ../seq/ssim -i Simulating with ../seq/ssim All 22 ISA Checks Succeed ./htest.pl -s ../seq/ssim -i Simulating with ../seq/ssim All 756 ISA Checks Succeed
usr@Fabulous:~/CMU15-213 labs/archlab-handout/sim/ptest$ cd ../ptest; make SIM=../seq/ssim ./optest.pl -s ../seq/ssim Simulating with ../seq/ssim All 49 ISA Checks Succeed ./jtest.pl -s ../seq/ssim Simulating with ../seq/ssim All 64 ISA Checks Succeed ./ctest.pl -s ../seq/ssim Simulating with ../seq/ssim All 22 ISA Checks Succeed ./htest.pl -s ../seq/ssim Simulating with ../seq/ssim All 600 ISA Checks Succeed
/usr/bin/ld: /tmp/cc9YqZcG.o:(.bss+0x0): multiple definition of `mem_wb_state'; /tmp/ccUxUYEs.o:(.bss+0x120): first defined here /usr/bin/ld: /tmp/cc9YqZcG.o:(.bss+0x8): multiple definition of `ex_mem_state'; /tmp/ccUxUYEs.o:(.bss+0x128): first defined here /usr/bin/ld: /tmp/cc9YqZcG.o:(.bss+0x10): multiple definition of `id_ex_state'; /tmp/ccUxUYEs.o:(.bss+0x130): first defined here /usr/bin/ld: /tmp/cc9YqZcG.o:(.bss+0x18): multiple definition of `if_id_state'; /tmp/ccUxUYEs.o:(.bss+0x138): first defined here /usr/bin/ld: /tmp/cc9YqZcG.o:(.bss+0x20): multiple definition of `pc_state'; /tmp/ccUxUYEs.o:(.bss+0x140): first defined here collect2: error: ld returned 1 exit status make: *** [Makefile:44: psim] Error 1
ncopy: xorq %rax,%rax # count = 0; andq %rdx,%rdx # len <= 0? jle Done # if so, goto Done:
Loop: mrmovq (%rdi), %r10 # read val from src... rmmovq %r10, (%rsi) # ...and store it to dst andq %r10, %r10 # val <= 0? jle Npos # if so, goto Npos: irmovq $1, %r10 addq %r10, %rax # count++
ncopy: xorq %rax,%rax # count = 0; andq %rdx,%rdx # len <= 0? jle Done # if so, goto Done:
Loop: mrmovq (%rdi), %r10 # read val from src... rmmovq %r10, (%rsi) # ...and store it to dst andq %r10, %r10 # val <= 0? jle Npos # if so, goto Npos: iaddq $1, %rax # count++
Npos: iaddq $-1, %rdx # len-- iaddq $8, %rdi # src++ iaddq $8, %rsi # dst++ andq %rdx,%rdx # len > 0? jg Loop # if so, goto Loop:
Done: ret End:
测试结果:
1 2 3 4
68/68 pass correctness test ncopy length = 96 bytes Average CPE 12.70 Score 0.0/60.0