├── C5 ├── homework │ ├── 5.16 │ │ ├── README.md │ │ └── innner4.c │ ├── 5.15 │ │ ├── README.md │ │ └── innner4.c │ ├── 5.17 │ │ ├── README.md │ │ └── memset.c │ ├── 5.18 │ │ └── README.md │ ├── 5.19 │ │ ├── README.md │ │ └── presum.c │ ├── README.md │ └── 5.14 │ │ ├── innner4.c │ │ └── README.md ├── practice │ ├── 5.11 │ │ └── README.md │ ├── 5.1 │ │ └── README.md │ ├── 5.8 │ │ └── README.md │ ├── 5.2 │ │ └── README.md │ ├── 5.5 │ │ └── README.md │ ├── 5.4 │ │ └── README.md │ ├── 5.10 │ │ └── README.md │ ├── 5.3 │ │ └── README.md │ ├── 5.6 │ │ └── README.md │ ├── 5.12 │ │ ├── psum1a.c │ │ └── README.md │ ├── README.md │ └── 5.9 │ │ ├── merge.c │ │ └── README.md ├── README.md └── note │ └── performance │ └── README.md ├── C4 ├── practice │ ├── 4.44 │ │ └── README.md │ ├── 4.9 │ │ └── README.md │ ├── 4.10 │ │ └── README.md │ ├── 4.43 │ │ └── README.md │ ├── 4.16 │ │ └── README.md │ ├── 4.26 │ │ └── README.md │ ├── 4.8 │ │ └── README.md │ ├── 4.19 │ │ └── README.md │ ├── 4.36 │ │ └── README.md │ ├── 4.21 │ │ └── README.md │ ├── 4.25 │ │ └── README.md │ ├── 4.29 │ │ └── README.md │ ├── 4.39 │ │ └── README.md │ ├── 4.4 │ │ ├── rsum.c │ │ ├── README.md │ │ └── rsum.asm │ ├── 4.22 │ │ └── README.md │ ├── 4.11 │ │ └── README.md │ ├── 4.41 │ │ └── README.md │ ├── 4.35 │ │ └── README.md │ ├── 4.30 │ │ └── README.md │ ├── 4.23 │ │ └── README.md │ ├── 4.15 │ │ └── README.md │ ├── 4.20 │ │ └── README.md │ ├── 4.27 │ │ └── README.md │ ├── 4.40 │ │ └── README.md │ ├── 4.42 │ │ └── README.md │ ├── 4.31 │ │ └── README.md │ ├── 4.12 │ │ └── README.md │ ├── 4.24 │ │ └── README.md │ ├── 4.7 │ │ └── README.md │ ├── 4.1 │ │ └── README.md │ ├── 4.38 │ │ └── README.md │ ├── 4.37 │ │ └── README.md │ ├── 4.3 │ │ └── README.md │ ├── 4.34 │ │ └── README.md │ ├── 4.28 │ │ └── README.md │ ├── 4.17 │ │ └── README.md │ ├── 4.18 │ │ └── README.md │ ├── 4.6 │ │ └── README.md │ ├── 4.5 │ │ └── README.md │ ├── 4.2 │ │ └── README.md │ ├── 4.13 │ │ └── README.md │ ├── 4.32 │ │ └── README.md │ ├── 4.14 │ │ └── README.md │ └── 4.33 │ │ └── README.md ├── homework │ └── README.md └── README.md ├── labs ├── attack │ ├── cookie.txt │ ├── inject2.s │ ├── inject3.s │ ├── ctarget │ ├── hex2raw │ ├── rtarget │ ├── inject2.o │ ├── inject3.o │ ├── result1.txt │ ├── result2.txt │ ├── result3.txt │ ├── result4.txt │ ├── inject2.d │ ├── inject3.d │ ├── result5.txt │ └── README.txt ├── data │ ├── auto_test.sh │ ├── dlc │ ├── Driverhdrs.pm │ ├── Makefile │ ├── bits.h │ └── btest.h ├── bomb │ ├── README │ ├── bomb │ └── reault.txt ├── archlab │ ├── archlab.pdf │ ├── sim │ │ ├── misc │ │ │ ├── hcl2c │ │ │ ├── isa.o │ │ │ ├── yas │ │ │ ├── yas.o │ │ │ ├── yis │ │ │ ├── yis.o │ │ │ ├── yas-grammar.o │ │ │ ├── yas.h │ │ │ ├── outgen.h │ │ │ ├── rsum.ys │ │ │ ├── sum.ys │ │ │ ├── copy.ys │ │ │ ├── examples.c │ │ │ └── node.h │ │ ├── pipe │ │ │ ├── psim │ │ │ ├── check-len.pl │ │ │ └── ncopy.c │ │ ├── seq │ │ │ └── ssim │ │ ├── y86-code │ │ │ ├── prog4.ys │ │ │ ├── prog3.ys │ │ │ ├── prog8.ys │ │ │ ├── prog2.ys │ │ │ ├── pushquestion.ys │ │ │ ├── prog1.ys │ │ │ ├── prog5.ys │ │ │ ├── poptest.ys │ │ │ ├── prog4.yo │ │ │ ├── prog8.yo │ │ │ ├── prog9.ys │ │ │ ├── pushquestion.yo │ │ │ ├── prog3.yo │ │ │ ├── pushtest.ys │ │ │ ├── prog7.ys │ │ │ ├── prog2.yo │ │ │ ├── prog10.ys │ │ │ ├── j-cc.ys │ │ │ ├── prog1.yo │ │ │ ├── poptest.yo │ │ │ ├── prog5.yo │ │ │ ├── prog6.ys │ │ │ ├── ret-hazard.ys │ │ │ ├── prog9.yo │ │ │ ├── pushtest.yo │ │ │ ├── prog10.yo │ │ │ ├── cjr.ys │ │ │ ├── prog7.yo │ │ │ ├── j-cc.yo │ │ │ ├── README │ │ │ ├── prog6.yo │ │ │ ├── ret-hazard.yo │ │ │ ├── asumi.ys │ │ │ ├── asum.ys │ │ │ ├── cjr.yo │ │ │ └── asumr.ys │ │ └── ptest │ │ │ └── Makefile │ ├── simguide.pdf │ └── README └── performance │ ├── clock.o │ ├── driver │ ├── fcyc.o │ ├── driver.o │ ├── kernels.o │ ├── Makefile │ ├── clock.h │ ├── defs.h │ └── config.h ├── C6 ├── homework │ ├── 6.42 │ │ └── README.md │ ├── 6.43 │ │ └── README.md │ ├── 6.41 │ │ └── README.md │ ├── 6.22 │ │ └── README.md │ ├── 6.23 │ │ └── README.md │ ├── 6.45 │ │ └── README.md │ ├── 6.46 │ │ └── README.md │ ├── 6.24 │ │ └── README.md │ ├── 6.39 │ │ └── README.md │ ├── 6.38 │ │ └── README.md │ ├── 6.40 │ │ └── README.md │ ├── 6.36 │ │ └── README.md │ ├── 6.33 │ │ └── README.md │ ├── 6.37 │ │ └── README.md │ ├── 6.27 │ │ └── README.md │ ├── 6.30 │ │ └── README.md │ ├── 6.28 │ │ └── README.md │ ├── 6.26 │ │ └── README.md │ ├── 6.31 │ │ └── README.md │ ├── 6.32 │ │ └── README.md │ ├── 6.29 │ │ └── README.md │ ├── 6.25 │ │ └── README.md │ ├── 6.35 │ │ └── README.md │ ├── 6.34 │ │ └── README.md │ └── README.md ├── practice │ ├── 6.6 │ │ └── README.md │ ├── 6.10 │ │ └── README.md │ ├── 6.8 │ │ └── README.md │ ├── 6.11 │ │ └── README.md │ ├── 6.2 │ │ └── README.md │ ├── 6.18 │ │ └── README.md │ ├── 6.20 │ │ └── README.md │ ├── 6.19 │ │ └── README.md │ ├── 6.3 │ │ └── README.md │ ├── 6.5 │ │ └── README.md │ ├── 6.4 │ │ └── README.md │ ├── 6.12 │ │ └── README.md │ ├── 6.16 │ │ └── README.md │ ├── 6.9 │ │ └── README.md │ ├── 6.14 │ │ └── README.md │ ├── 6.15 │ │ └── README.md │ ├── 6.13 │ │ └── README.md │ ├── 6.7 │ │ └── README.md │ ├── 6.1 │ │ └── README.md │ ├── README.md │ └── 6.17 │ │ └── README.md ├── README.md └── note │ ├── locality │ └── README.md │ └── README.md ├── C2 ├── practice │ ├── 2.15 │ │ └── README.md │ ├── 2.39 │ │ └── README.md │ ├── 2.43 │ │ └── README.md │ ├── 2.41 │ │ └── README.md │ ├── 2.49 │ │ └── README.md │ ├── 2.31 │ │ └── README.md │ ├── 2.50 │ │ └── README.md │ ├── 2.20 │ │ └── README.md │ ├── 2.22 │ │ └── README.md │ ├── 2.12 │ │ └── README.md │ ├── 2.48 │ │ └── README.md │ ├── 2.4 │ │ └── README.md │ ├── 2.53 │ │ └── README.md │ ├── 2.46 │ │ └── README.md │ ├── 2.51 │ │ └── README.md │ ├── 2.44 │ │ └── README.md │ ├── 2.54 │ │ └── README.md │ ├── 2.19 │ │ └── README.md │ ├── 2.27 │ │ └── README.md │ ├── 2.38 │ │ └── README.md │ ├── 2.42 │ │ └── README.md │ ├── 2.9 │ │ └── README.md │ ├── 2.8 │ │ └── README.md │ ├── 2.13 │ │ └── README.md │ ├── 2.18 │ │ └── README.md │ ├── 2.23 │ │ └── README.md │ ├── 2.14 │ │ └── README.md │ ├── 2.30 │ │ └── README.md │ ├── 2.10 │ │ ├── inplace_swap.c │ │ └── README.md │ ├── 2.17 │ │ └── README.md │ ├── 2.28 │ │ └── README.md │ ├── 2.33 │ │ └── README.md │ ├── 2.25 │ │ └── README.md │ ├── 2.2 │ │ └── README.md │ ├── 2.40 │ │ └── README.md │ ├── 2.32 │ │ └── README.md │ ├── 2.7 │ │ └── README.md │ ├── 2.3 │ │ └── README.md │ ├── 2.5 │ │ └── README.md │ ├── 2.16 │ │ └── README.md │ ├── 2.6 │ │ └── README.md │ ├── 2.1 │ │ └── README.md │ ├── 2.21 │ │ └── README.md │ ├── 2.36 │ │ └── README.md │ ├── 2.26 │ │ └── README.md │ ├── 2.35 │ │ └── README.md │ ├── 2.52 │ │ └── README.md │ ├── 2.11 │ │ └── README.md │ ├── 2.45 │ │ └── README.md │ ├── 2.34 │ │ └── README.md │ ├── 2.29 │ │ └── README.md │ └── 2.24 │ │ └── README.md ├── homework │ ├── 2.64 │ │ └── README.md │ ├── 2.63 │ │ ├── README.md │ │ └── srl-sra.c │ ├── 2.74 │ │ ├── README.md │ │ ├── tsub_ok │ │ └── tsub_ok.c │ ├── 2.76 │ │ ├── README.md │ │ └── calloc.c │ ├── 2.65 │ │ ├── README.md │ │ ├── odd_ones │ │ └── odd_ones.c │ ├── 2.70 │ │ ├── README.md │ │ ├── fits_bits │ │ └── fits_bits.c │ ├── 2.71 │ │ ├── xbyte │ │ ├── README.md │ │ └── xbyte.c │ ├── 2.79 │ │ ├── README.md │ │ ├── mul3div4 │ │ └── mul3div4.c │ ├── 2.95 │ │ ├── README.md │ │ └── float_half.c │ ├── 2.96 │ │ ├── README.md │ │ └── float_f2i.c │ ├── 2.97 │ │ ├── README.md │ │ └── float_i2f.c │ ├── 2.60 │ │ ├── README.md │ │ ├── replace_byte │ │ └── replace_byte.c │ ├── 2.66 │ │ ├── README.md │ │ └── leftmost_one.c │ ├── 2.69 │ │ ├── README.md │ │ ├── rotate_left │ │ └── rotate_left.c │ ├── 2.80 │ │ ├── README.md │ │ ├── threefourths │ │ └── threefourths.c │ ├── 2.92 │ │ ├── README.md │ │ └── float_negate.c │ ├── 2.93 │ │ ├── README.md │ │ └── float_absval.c │ ├── 2.94 │ │ ├── README.md │ │ └── float_twice.c │ ├── 2.68 │ │ ├── README.md │ │ ├── lower_one_mask │ │ └── lower_one_mask.c │ ├── 2.73 │ │ ├── README.md │ │ ├── saturating_add │ │ └── saturating_add.c │ ├── 2.78 │ │ ├── README.md │ │ ├── divide_power2 │ │ └── divide_power2.c │ ├── 2.81 │ │ ├── generate │ │ ├── README.md │ │ └── generate.c │ ├── 2.83 │ │ └── README.md │ ├── 2.57 │ │ ├── show_bytes_more │ │ ├── README.md │ │ └── show_bytes_more.c │ ├── 2.59 │ │ ├── generate_a_word │ │ ├── README.md │ │ └── generate_a_word.c │ ├── 2.75 │ │ ├── README.md │ │ ├── unsigned_high_prod │ │ └── unsigned_high_prod.c │ ├── 2.58 │ │ ├── is_little_endian │ │ ├── is_little_endian.c │ │ └── README.md │ ├── 2.62 │ │ ├── README.md │ │ ├── int_shifts_are_arithmetic │ │ └── int_shifts_are_arithmetic.c │ ├── 2.91 │ │ └── README.md │ ├── 2.82 │ │ └── README.md │ ├── 2.61 │ │ └── README.md │ ├── 2.77 │ │ └── README.md │ ├── 2.89 │ │ └── README.md │ ├── 2.72 │ │ └── README.md │ ├── 2.84 │ │ └── README.md │ ├── 2.86 │ │ └── README.md │ ├── 2.56 │ │ └── README.md │ ├── 2.67 │ │ └── README.md │ ├── 2.85 │ │ └── README.md │ ├── 2.55 │ │ └── README.md │ ├── 2.90 │ │ └── README.md │ ├── 2.87 │ │ └── README.md │ └── 2.88 │ │ └── README.md ├── note │ ├── storage │ │ ├── show_bytes │ │ └── code.md │ ├── operations │ │ └── logical.md │ └── README.md └── README.md ├── C3 ├── note │ ├── heterogeneous │ │ ├── tempCodeRunnerFile.c │ │ ├── test │ │ └── test.c │ ├── encodings │ │ ├── prog │ │ ├── mstore.o │ │ ├── mstore.c │ │ ├── main.c │ │ └── mstore.s │ ├── formats │ │ ├── exchange.o │ │ ├── exchange.c │ │ ├── exchange.asm │ │ └── exchange.s │ ├── README.md │ ├── floating-point │ │ └── README.md │ └── array │ │ └── README.md ├── practice │ ├── 3.22 │ │ ├── README.md │ │ ├── test │ │ └── test.c │ ├── 3.19 │ │ └── README.md │ ├── 3.47 │ │ └── README.md │ ├── 3.50 │ │ └── README.md │ ├── 3.26 │ │ ├── test │ │ ├── README.md │ │ └── test.c │ ├── 3.39 │ │ └── README.md │ ├── 3.2 │ │ └── README.md │ ├── 3.38 │ │ └── README.md │ ├── 3.56 │ │ └── README.md │ ├── 3.15 │ │ └── README.md │ ├── 3.54 │ │ └── README.md │ ├── 3.49 │ │ └── README.md │ ├── 3.7 │ │ └── README.md │ ├── 3.11 │ │ └── README.md │ ├── 3.34 │ │ └── README.md │ ├── 3.53 │ │ └── README.md │ ├── 3.9 │ │ └── README.md │ ├── 3.33 │ │ └── README.md │ ├── 3.10 │ │ └── README.md │ ├── 3.20 │ │ └── README.md │ ├── 3.52 │ │ └── README.md │ ├── 3.35 │ │ └── README.md │ ├── 3.25 │ │ └── README.md │ ├── 3.24 │ │ └── README.md │ ├── 3.48 │ │ └── README.md │ ├── 3.29 │ │ └── README.md │ ├── 3.44 │ │ └── README.md │ ├── 3.40 │ │ └── README.md │ ├── 3.57 │ │ └── README.md │ ├── 3.3 │ │ └── README.md │ ├── 3.41 │ │ └── README.md │ ├── 3.55 │ │ └── README.md │ ├── 3.5 │ │ └── README.md │ ├── 3.28 │ │ └── README.md │ ├── 3.27 │ │ └── README.md │ ├── 3.42 │ │ └── README.md │ ├── 3.16 │ │ └── README.md │ ├── 3.21 │ │ └── README.md │ ├── 3.13 │ │ └── README.md │ ├── 3.18 │ │ └── README.md │ ├── 3.1 │ │ └── README.md │ ├── 3.17 │ │ └── README.md │ ├── 3.45 │ │ └── README.md │ ├── 3.51 │ │ └── README.md │ ├── 3.6 │ │ └── README.md │ ├── 3.12 │ │ └── README.md │ ├── 3.14 │ │ └── README.md │ ├── 3.36 │ │ └── README.md │ ├── 3.31 │ │ └── README.md │ ├── 3.8 │ │ └── README.md │ ├── 3.30 │ │ └── README.md │ ├── 3.23 │ │ └── README.md │ ├── 3.37 │ │ └── README.md │ ├── 3.43 │ │ └── README.md │ ├── 3.46 │ │ └── README.md │ └── 3.4 │ │ └── README.md ├── homework │ ├── 3.73 │ │ ├── README.md │ │ ├── find_range │ │ └── find_range.c │ ├── 3.74 │ │ ├── README.md │ │ ├── find_range │ │ └── find_range.c │ ├── 3.61 │ │ ├── cread │ │ ├── cread_alt │ │ ├── README.md │ │ ├── cread.c │ │ └── cread_alt.c │ ├── 3.64 │ │ └── README.md │ ├── 3.65 │ │ └── README.md │ ├── 3.68 │ │ └── README.md │ ├── 3.58 │ │ └── README.md │ ├── 3.72 │ │ └── README.md │ ├── 3.69 │ │ └── README.md │ ├── 3.75 │ │ └── README.md │ ├── 3.70 │ │ └── README.md │ ├── 3.66 │ │ └── README.md │ ├── 3.71 │ │ └── README.md │ ├── 3.60 │ │ └── README.md │ ├── 3.63 │ │ └── README.md │ ├── README.md │ ├── 3.67 │ │ └── README.md │ ├── 3.62 │ │ └── README.md │ └── 3.59 │ │ └── README.md └── README.md ├── .DS_Store ├── C1 ├── note │ ├── .DS_Store │ ├── hello │ │ ├── a.out │ │ ├── hello │ │ ├── hello2 │ │ ├── hello3 │ │ ├── .DS_Store │ │ ├── hello.o │ │ └── hello.s │ ├── README.md │ ├── GCC │ │ └── README.md │ ├── os │ │ └── abstractions.md │ └── network │ │ └── README.md ├── practice │ ├── README.md │ ├── 1.2 │ │ └── README.md │ └── 1.1 │ │ └── README.md └── README.md └── .vscode ├── settings.json └── launch.json /C5/homework/5.16/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /C4/practice/4.44/README.md: -------------------------------------------------------------------------------- 1 | # 4.44 2 | -------------------------------------------------------------------------------- /C5/homework/5.15/README.md: -------------------------------------------------------------------------------- 1 | # 5.15 2 | -------------------------------------------------------------------------------- /C5/homework/5.17/README.md: -------------------------------------------------------------------------------- 1 | # 5.17 2 | -------------------------------------------------------------------------------- /C5/homework/5.18/README.md: -------------------------------------------------------------------------------- 1 | # 5.18 2 | -------------------------------------------------------------------------------- /C5/homework/5.19/README.md: -------------------------------------------------------------------------------- 1 | # 5.19 2 | -------------------------------------------------------------------------------- /labs/attack/cookie.txt: -------------------------------------------------------------------------------- 1 | 0x59b997fa 2 | -------------------------------------------------------------------------------- /C6/homework/6.42/README.md: -------------------------------------------------------------------------------- 1 | # 6.42 2 | 3 | 1/4. -------------------------------------------------------------------------------- /C6/homework/6.43/README.md: -------------------------------------------------------------------------------- 1 | # 6.43 2 | 3 | 1 4 | -------------------------------------------------------------------------------- /C2/practice/2.15/README.md: -------------------------------------------------------------------------------- 1 | # 2.5 2 | 3 | `!(x^y)` -------------------------------------------------------------------------------- /C6/homework/6.41/README.md: -------------------------------------------------------------------------------- 1 | # 6.41 2 | 3 | 1/4 4 | -------------------------------------------------------------------------------- /C6/practice/6.6/README.md: -------------------------------------------------------------------------------- 1 | # 6.6 2 | 3 | 2025. 4 | -------------------------------------------------------------------------------- /C6/homework/6.22/README.md: -------------------------------------------------------------------------------- 1 | # 6.22 2 | 3 | x = 1/2. 4 | -------------------------------------------------------------------------------- /C6/homework/6.23/README.md: -------------------------------------------------------------------------------- 1 | # 6.23 2 | 3 | 6.005ms 4 | -------------------------------------------------------------------------------- /C6/homework/6.45/README.md: -------------------------------------------------------------------------------- 1 | # 6.45 2 | 3 | -------------------------------------------------------------------------------- /C6/homework/6.46/README.md: -------------------------------------------------------------------------------- 1 | # 6.46 2 | 3 | -------------------------------------------------------------------------------- /C3/note/heterogeneous/tempCodeRunnerFile.c: -------------------------------------------------------------------------------- 1 | 2 | temp.t -------------------------------------------------------------------------------- /C5/practice/5.11/README.md: -------------------------------------------------------------------------------- 1 | # 5.11 2 | 3 | 4 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/.DS_Store -------------------------------------------------------------------------------- /C2/homework/2.64/README.md: -------------------------------------------------------------------------------- 1 | # 2.64 2 | 3 | `!!(x & 0xAAAAAAAA)` 4 | -------------------------------------------------------------------------------- /C2/practice/2.39/README.md: -------------------------------------------------------------------------------- 1 | # 2.39 2 | 3 | $(x << w) - (x << m)$ 4 | -------------------------------------------------------------------------------- /C2/practice/2.43/README.md: -------------------------------------------------------------------------------- 1 | # 2.43 2 | 3 | M = 31 4 | 5 | N = 8 6 | -------------------------------------------------------------------------------- /C4/practice/4.9/README.md: -------------------------------------------------------------------------------- 1 | # 4.9 2 | 3 | (a && !b) || (!a && b) 4 | -------------------------------------------------------------------------------- /C6/practice/6.10/README.md: -------------------------------------------------------------------------------- 1 | # 6.10 2 | 3 | if padded, it is 3/4. 4 | -------------------------------------------------------------------------------- /labs/data/auto_test.sh: -------------------------------------------------------------------------------- 1 | ./dlc bits.c 2 | make 3 | ./btest -T 20 4 | -------------------------------------------------------------------------------- /C3/practice/3.22/README.md: -------------------------------------------------------------------------------- 1 | # 3.22 2 | 3 | A. 13. 4 | 5 | B. 20. 6 | -------------------------------------------------------------------------------- /C4/practice/4.10/README.md: -------------------------------------------------------------------------------- 1 | # 4.10 2 | 3 | `&&` equals `!(a ^ b)`. 4 | -------------------------------------------------------------------------------- /C6/homework/6.24/README.md: -------------------------------------------------------------------------------- 1 | # 6.24 2 | 3 | A. 16ms 4 | 5 | B. 24s 6 | -------------------------------------------------------------------------------- /C6/practice/6.8/README.md: -------------------------------------------------------------------------------- 1 | # 6.8 2 | 3 | spatial locality: b > c > d. 4 | -------------------------------------------------------------------------------- /labs/attack/inject2.s: -------------------------------------------------------------------------------- 1 | pushq $0x4017ec 2 | mov $0x59b997fa, %rdi 3 | retq -------------------------------------------------------------------------------- /labs/attack/inject3.s: -------------------------------------------------------------------------------- 1 | pushq $0x4018fa 2 | mov $0x5561dca8, %rdi 3 | retq -------------------------------------------------------------------------------- /labs/bomb/README: -------------------------------------------------------------------------------- 1 | This is an x86-64 bomb for self-study students. 2 | -------------------------------------------------------------------------------- /C2/practice/2.41/README.md: -------------------------------------------------------------------------------- 1 | # 2.41 2 | 3 | Find Less clock cycle cost. 4 | -------------------------------------------------------------------------------- /C3/homework/3.73/README.md: -------------------------------------------------------------------------------- 1 | # 3.73 2 | 3 | [find_range.c](./find_range.c) 4 | -------------------------------------------------------------------------------- /C3/homework/3.74/README.md: -------------------------------------------------------------------------------- 1 | # 3.74 2 | 3 | [find_range.c](./find_range.c) 4 | -------------------------------------------------------------------------------- /C3/practice/3.19/README.md: -------------------------------------------------------------------------------- 1 | # 3.19 2 | 3 | A. 30 4 | 5 | B. 30 + 16 = 46 6 | -------------------------------------------------------------------------------- /C3/practice/3.47/README.md: -------------------------------------------------------------------------------- 1 | # 3.47 2 | 3 | A. $2^13$ 4 | 5 | B. $2^6$ 6 | -------------------------------------------------------------------------------- /C4/homework/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 HomeWork Problems 2 | 3 | 4 | -------------------------------------------------------------------------------- /C6/practice/6.11/README.md: -------------------------------------------------------------------------------- 1 | # 6.11 2 | 3 | A. $2^t$. 4 | 5 | B. 1 array block. -------------------------------------------------------------------------------- /labs/bomb/bomb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/bomb/bomb -------------------------------------------------------------------------------- /labs/data/dlc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/data/dlc -------------------------------------------------------------------------------- /C1/note/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/.DS_Store -------------------------------------------------------------------------------- /C2/practice/2.49/README.md: -------------------------------------------------------------------------------- 1 | # 2.49 2 | 3 | A. $1 + 2^{n+1}$ 4 | 5 | B. 16777217. -------------------------------------------------------------------------------- /C1/note/hello/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/hello/a.out -------------------------------------------------------------------------------- /C1/note/hello/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/hello/hello -------------------------------------------------------------------------------- /C2/homework/2.63/README.md: -------------------------------------------------------------------------------- 1 | # 2.63 2 | 3 | You can look at [srl-sra.c](./srl-sra.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.74/README.md: -------------------------------------------------------------------------------- 1 | # 2.74 2 | 3 | You can look at [tsub_ok.c](./tsub_ok.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.76/README.md: -------------------------------------------------------------------------------- 1 | # 2.76 2 | 3 | You can look at [calloc.c](./calloc.c) 4 | -------------------------------------------------------------------------------- /C3/practice/3.50/README.md: -------------------------------------------------------------------------------- 1 | # 3.50 2 | 3 | 1. `d` 4 | 2. `i` 5 | 3. `l` 6 | 4. `f` 7 | -------------------------------------------------------------------------------- /C6/homework/6.39/README.md: -------------------------------------------------------------------------------- 1 | # 6.39 2 | 3 | A. 1024 4 | 5 | B. 256 6 | 7 | C. 1/4 8 | -------------------------------------------------------------------------------- /C6/practice/6.2/README.md: -------------------------------------------------------------------------------- 1 | # 6.2 2 | 3 | $capacity = 512b *400* 10000 *2* 2 = 8GB$ 4 | -------------------------------------------------------------------------------- /labs/attack/ctarget: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/attack/ctarget -------------------------------------------------------------------------------- /labs/attack/hex2raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/attack/hex2raw -------------------------------------------------------------------------------- /labs/attack/rtarget: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/attack/rtarget -------------------------------------------------------------------------------- /C1/note/hello/hello2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/hello/hello2 -------------------------------------------------------------------------------- /C1/note/hello/hello3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/hello/hello3 -------------------------------------------------------------------------------- /C2/homework/2.65/README.md: -------------------------------------------------------------------------------- 1 | # 2.65 2 | 3 | You can look at [odd_ones.c](./odd_ones.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.70/README.md: -------------------------------------------------------------------------------- 1 | # 2.70 2 | 3 | You can look at [fits_bits.c](./fits_bits.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.71/xbyte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.71/xbyte -------------------------------------------------------------------------------- /C2/homework/2.79/README.md: -------------------------------------------------------------------------------- 1 | # 2.79 2 | 3 | You can look at [mul3div4.c](./mul3div4.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.95/README.md: -------------------------------------------------------------------------------- 1 | # 2.95 2 | 3 | You can look at [float_half.c](./float_half.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.96/README.md: -------------------------------------------------------------------------------- 1 | # 2.96 2 | 3 | You can look at [float_f2i.c](./float_f2i.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.97/README.md: -------------------------------------------------------------------------------- 1 | # 2.97 2 | 3 | You can look at [float_i2f.c](./float_i2f.c) 4 | -------------------------------------------------------------------------------- /C3/homework/3.61/cread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/homework/3.61/cread -------------------------------------------------------------------------------- /C3/note/encodings/prog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/note/encodings/prog -------------------------------------------------------------------------------- /C3/practice/3.22/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/practice/3.22/test -------------------------------------------------------------------------------- /C3/practice/3.26/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/practice/3.26/test -------------------------------------------------------------------------------- /C4/practice/4.43/README.md: -------------------------------------------------------------------------------- 1 | # 4.43 2 | 3 | mispredict penalty 0.2 \* 0.35 \* 2 = 0.14. 4 | -------------------------------------------------------------------------------- /C5/practice/5.1/README.md: -------------------------------------------------------------------------------- 1 | # 5.1 2 | 3 | if `xp` equal `yp`, then `xp` will be set 0. 4 | -------------------------------------------------------------------------------- /labs/attack/inject2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/attack/inject2.o -------------------------------------------------------------------------------- /labs/attack/inject3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/attack/inject3.o -------------------------------------------------------------------------------- /C1/note/hello/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C1/note/hello/.DS_Store -------------------------------------------------------------------------------- /C2/homework/2.60/README.md: -------------------------------------------------------------------------------- 1 | # 2.60 2 | 3 | You can look at [replace_byte](./replace_byte.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.66/README.md: -------------------------------------------------------------------------------- 1 | # 2.66 2 | 3 | You can look at [leftmost_one.c](./leftmost_one.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.69/README.md: -------------------------------------------------------------------------------- 1 | # 2.69 2 | 3 | You can look at [rotate_left.c](./rotate_left.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.74/tsub_ok: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.74/tsub_ok -------------------------------------------------------------------------------- /C2/homework/2.80/README.md: -------------------------------------------------------------------------------- 1 | # 2.80 2 | 3 | You can look at [threefourths.c](./threefourths.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.92/README.md: -------------------------------------------------------------------------------- 1 | # 2.92 2 | 3 | You can look at [float_negate.c](./float_negate.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.93/README.md: -------------------------------------------------------------------------------- 1 | # 2.93 2 | 3 | You can look at [float_absval.c](./float_absval.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.94/README.md: -------------------------------------------------------------------------------- 1 | # 2.94 2 | 3 | You can look at [float_twice.c](./float_twice.c) 4 | -------------------------------------------------------------------------------- /C3/practice/3.39/README.md: -------------------------------------------------------------------------------- 1 | # 3.39 2 | 3 | ```cpp 4 | typedef int[256][16] fix_matrix; 5 | ``` 6 | -------------------------------------------------------------------------------- /labs/archlab/archlab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/archlab.pdf -------------------------------------------------------------------------------- /labs/performance/clock.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/performance/clock.o -------------------------------------------------------------------------------- /labs/performance/driver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/performance/driver -------------------------------------------------------------------------------- /labs/performance/fcyc.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/performance/fcyc.o -------------------------------------------------------------------------------- /C2/homework/2.65/odd_ones: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.65/odd_ones -------------------------------------------------------------------------------- /C2/homework/2.68/README.md: -------------------------------------------------------------------------------- 1 | # 2.68 2 | 3 | You can look at [lower_one_mask.c](./lower_one_mask.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.70/fits_bits: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.70/fits_bits -------------------------------------------------------------------------------- /C2/homework/2.73/README.md: -------------------------------------------------------------------------------- 1 | # 2.73 2 | 3 | You can look at [saturating_add.c](./saturating_add.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.78/README.md: -------------------------------------------------------------------------------- 1 | # 2.78 2 | 3 | You can look at [divide_power2.c](./divide_power2.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.79/mul3div4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.79/mul3div4 -------------------------------------------------------------------------------- /C2/homework/2.81/generate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.81/generate -------------------------------------------------------------------------------- /C2/homework/2.83/README.md: -------------------------------------------------------------------------------- 1 | # 2.83 2 | 3 | A. $n = Y/(2^k-1)$ 4 | 5 | B. a. 5/7; b. 0.4; c. 19/31 6 | -------------------------------------------------------------------------------- /C2/note/storage/show_bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/note/storage/show_bytes -------------------------------------------------------------------------------- /C2/practice/2.31/README.md: -------------------------------------------------------------------------------- 1 | # 2.31 2 | 3 | This equal must be right, because it's a abelian group. 4 | -------------------------------------------------------------------------------- /C2/practice/2.50/README.md: -------------------------------------------------------------------------------- 1 | # 2.50 2 | 3 | A. 2 4 | 5 | B. 2 6 | 7 | C. 3 8 | 9 | D. 3 10 | -------------------------------------------------------------------------------- /C3/homework/3.61/cread_alt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/homework/3.61/cread_alt -------------------------------------------------------------------------------- /C3/homework/3.73/find_range: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/homework/3.73/find_range -------------------------------------------------------------------------------- /C3/homework/3.74/find_range: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/homework/3.74/find_range -------------------------------------------------------------------------------- /C3/note/encodings/mstore.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/note/encodings/mstore.o -------------------------------------------------------------------------------- /C3/note/formats/exchange.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/note/formats/exchange.o -------------------------------------------------------------------------------- /C3/note/heterogeneous/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C3/note/heterogeneous/test -------------------------------------------------------------------------------- /labs/archlab/sim/misc/hcl2c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/hcl2c -------------------------------------------------------------------------------- /labs/archlab/sim/misc/isa.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/isa.o -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/yas -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yas.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/yas.o -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/yis -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yis.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/yis.o -------------------------------------------------------------------------------- /labs/archlab/sim/pipe/psim: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/pipe/psim -------------------------------------------------------------------------------- /labs/archlab/sim/seq/ssim: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/seq/ssim -------------------------------------------------------------------------------- /labs/archlab/simguide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/simguide.pdf -------------------------------------------------------------------------------- /labs/performance/driver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/performance/driver.o -------------------------------------------------------------------------------- /labs/performance/kernels.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/performance/kernels.o -------------------------------------------------------------------------------- /C2/homework/2.60/replace_byte: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.60/replace_byte -------------------------------------------------------------------------------- /C2/homework/2.69/rotate_left: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.69/rotate_left -------------------------------------------------------------------------------- /C2/homework/2.80/threefourths: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.80/threefourths -------------------------------------------------------------------------------- /C6/homework/6.38/README.md: -------------------------------------------------------------------------------- 1 | # 6.38 2 | 3 | A. 16 \* 16 \* 4 = 1024. 4 | 5 | B. 128. 6 | 7 | C. 1/8. 8 | -------------------------------------------------------------------------------- /C6/practice/6.18/README.md: -------------------------------------------------------------------------------- 1 | # 6.18 2 | 3 | A. 16 \* 16 \* 2 = 512. 4 | 5 | B. 256. 6 | 7 | C. 1/2. 8 | -------------------------------------------------------------------------------- /C1/practice/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 Practice 2 | 3 | - [1.1](./1.1/README.md) 4 | - [1.2](./1.2/README.md) 5 | -------------------------------------------------------------------------------- /C2/homework/2.57/show_bytes_more: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.57/show_bytes_more -------------------------------------------------------------------------------- /C2/homework/2.59/generate_a_word: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.59/generate_a_word -------------------------------------------------------------------------------- /C2/homework/2.68/lower_one_mask: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.68/lower_one_mask -------------------------------------------------------------------------------- /C2/homework/2.73/saturating_add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.73/saturating_add -------------------------------------------------------------------------------- /C2/homework/2.75/README.md: -------------------------------------------------------------------------------- 1 | # 2.75 2 | 3 | You can look at [unsigned_high_prod.c](./unsigned_high_prod.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.78/divide_power2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.78/divide_power2 -------------------------------------------------------------------------------- /C2/practice/2.20/README.md: -------------------------------------------------------------------------------- 1 | # 2.20 2 | 3 | T2U(x) = $x + 2^4$ if $x < 0$ 4 | 5 | T2U(x) = $x$ if $x \ge 0$ 6 | -------------------------------------------------------------------------------- /C2/practice/2.22/README.md: -------------------------------------------------------------------------------- 1 | # 2.22 2 | 3 | A. [1011] = -5 4 | 5 | B. [11011] = -5 6 | 7 | C. [111011] = -5 8 | -------------------------------------------------------------------------------- /C3/practice/3.2/README.md: -------------------------------------------------------------------------------- 1 | # 3.2 2 | 3 | 1. movl 4 | 2. movw 5 | 3. movb 6 | 4. movb 7 | 5. movq 8 | 6. movw 9 | -------------------------------------------------------------------------------- /C5/practice/5.8/README.md: -------------------------------------------------------------------------------- 1 | # 5.8 2 | 3 | - A1: 5 4 | - A2: 3.33 5 | - A3: 1.67 6 | - A4: 1.67 7 | - A5: 3.33 8 | -------------------------------------------------------------------------------- /C6/homework/6.40/README.md: -------------------------------------------------------------------------------- 1 | # 6.40 2 | 3 | A. 16 \* 16 \* 4 = 1024. 4 | 5 | B. 16*16 = 256. 6 | 7 | C. 1/4. 8 | -------------------------------------------------------------------------------- /C2/homework/2.58/is_little_endian: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.58/is_little_endian -------------------------------------------------------------------------------- /C3/homework/3.64/README.md: -------------------------------------------------------------------------------- 1 | # 3.64 2 | 3 | A.`( i * T * S + j * T + k) + &A;` 4 | 5 | B. `R = 7, S = 5, T = 13`. 6 | -------------------------------------------------------------------------------- /C4/practice/4.16/README.md: -------------------------------------------------------------------------------- 1 | # 4.16 2 | 3 | Yes. 4 | 5 | Just set the `rA` to `%rsp`, we can see the same result. 6 | -------------------------------------------------------------------------------- /C4/practice/4.26/README.md: -------------------------------------------------------------------------------- 1 | # 4.26 2 | 3 | ```cpp 4 | bool mem_write = icode in { IRMMOVQ, IPUSHQ, ICALL } 5 | ``` 6 | -------------------------------------------------------------------------------- /C6/practice/6.20/README.md: -------------------------------------------------------------------------------- 1 | # 6.20 2 | 3 | A. 16 \* 16 \* 2 = 512. 4 | 5 | B. 128. 6 | 7 | C. 1/4. 8 | 9 | D. 1/4. -------------------------------------------------------------------------------- /C2/homework/2.62/README.md: -------------------------------------------------------------------------------- 1 | # 2.62 2 | 3 | You can look at [int_shifts_are_arithmetic](./int_shifts_are_arithmetic.c) 4 | -------------------------------------------------------------------------------- /C2/homework/2.75/unsigned_high_prod: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.75/unsigned_high_prod -------------------------------------------------------------------------------- /C3/note/formats/exchange.c: -------------------------------------------------------------------------------- 1 | long exchange(long* xp, long y) { 2 | long x = *xp; 3 | *xp = y; 4 | return x; 5 | } -------------------------------------------------------------------------------- /C3/practice/3.38/README.md: -------------------------------------------------------------------------------- 1 | # 3.38 2 | 3 | - `M[P + 8 * (7i + j)]` 4 | - `M[Q + 8 * (i + 5j)]` 5 | 6 | N = 7, M = 5. 7 | -------------------------------------------------------------------------------- /C3/practice/3.56/README.md: -------------------------------------------------------------------------------- 1 | # 3.56 2 | 3 | A. `return x > 0 ? x : -x;` 4 | 5 | B. `return 0;` 6 | 7 | C. `return -x;` 8 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yas-grammar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/labs/archlab/sim/misc/yas-grammar.o -------------------------------------------------------------------------------- /C2/homework/2.91/README.md: -------------------------------------------------------------------------------- 1 | # 2.91 2 | 3 | A. `0b11.00 1001 0000 1111 1101 1011` 4 | 5 | B. `0b11.001(001)` 6 | 7 | C. 9 8 | -------------------------------------------------------------------------------- /C1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter1: A Tour of Computer System 2 | 3 | - [note](./note/README.md) 4 | - [practice](./practice/README.md) 5 | -------------------------------------------------------------------------------- /C2/homework/2.62/int_shifts_are_arithmetic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bvanjoi/CSAPP/HEAD/C2/homework/2.62/int_shifts_are_arithmetic -------------------------------------------------------------------------------- /C2/homework/2.71/README.md: -------------------------------------------------------------------------------- 1 | # 2.71 2 | 3 | A. The code return positive number. 4 | 5 | B. You can look at [xbyte.c](./xbyte.c) 6 | -------------------------------------------------------------------------------- /C4/practice/4.8/README.md: -------------------------------------------------------------------------------- 1 | # 4.8 2 | 3 | It means under x86-64, `popq %rsp` equivalent to the instruction `mrmovq (%rsp), %rsp`; 4 | -------------------------------------------------------------------------------- /C6/homework/6.36/README.md: -------------------------------------------------------------------------------- 1 | # 6.36 2 | 3 | A. 1. 4 | 5 | B. 1/4. 6 | 7 | C. 1/4. 8 | 9 | D. No. 10 | 11 | E. Yes. 12 | -------------------------------------------------------------------------------- /C6/practice/6.19/README.md: -------------------------------------------------------------------------------- 1 | # 6.19 2 | 3 | A. 16 \* 16 \* 2 = 512. 4 | 5 | B. 256. 6 | 7 | C. 1/2. 8 | 9 | D. 1/4. 10 | -------------------------------------------------------------------------------- /C4/practice/4.19/README.md: -------------------------------------------------------------------------------- 1 | # 4.19 2 | 3 | ```cpp 4 | bool need_valC = icode in { IIRMOVQ, IRMMOVQ, IMRMOVQ, IJXX, ICALL}; 5 | ``` 6 | -------------------------------------------------------------------------------- /C4/practice/4.36/README.md: -------------------------------------------------------------------------------- 1 | # 4.36 2 | 3 | ```python 4 | word m_stat = [ 5 | dmem_error: SADR; 6 | 1 : M_stat; 7 | ] 8 | ``` 9 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog4.ys: -------------------------------------------------------------------------------- 1 | # prog4: No padding 2 | irmovq $10,%rdx 3 | irmovq $3,%rax 4 | addq %rdx,%rax 5 | halt 6 | -------------------------------------------------------------------------------- /C2/homework/2.59/README.md: -------------------------------------------------------------------------------- 1 | # 2.59 2 | 3 | You can look at [generate_a_word](./generate_a_word.c) 4 | 5 | or: `(x & 0xFF) | (y & (~0xFF))` 6 | -------------------------------------------------------------------------------- /C2/homework/2.82/README.md: -------------------------------------------------------------------------------- 1 | # 2.82 2 | 3 | A. 0. `x = INT_MIN`. 4 | 5 | B. 1. 6 | 7 | C. 1. 8 | 9 | D. 1. 10 | 11 | E. 1. 12 | -------------------------------------------------------------------------------- /C3/practice/3.15/README.md: -------------------------------------------------------------------------------- 1 | # 3.15 2 | 3 | A. 0x4003fe 4 | 5 | B. 0x400425 6 | 7 | C. 0x500543, 0x400545 8 | 9 | D. 0x400560 10 | -------------------------------------------------------------------------------- /C2/practice/2.12/README.md: -------------------------------------------------------------------------------- 1 | # 2.12 2 | 3 | A. `x & 0xFF` 4 | 5 | B. `(x&0xFF) | ~(x&0xFFFFFF00)`, and equals `x ^ ~0xFF` 6 | 7 | C. `x | 0xFF` 8 | -------------------------------------------------------------------------------- /C2/practice/2.48/README.md: -------------------------------------------------------------------------------- 1 | # 2.48 2 | 3 | ``` 4 | 0000 0000 0011 0101 1001 0001 0100 0001 5 | 0100 1010 0101 0110 0100 0101 0000 0100 6 | ``` 7 | -------------------------------------------------------------------------------- /C4/practice/4.21/README.md: -------------------------------------------------------------------------------- 1 | # 4.21 2 | 3 | ```cpp 4 | word dstM = [ 5 | icode in { IMRMOVQ, IPOPQ } : rA; 6 | 1 : RNONE; 7 | ]; 8 | ``` 9 | -------------------------------------------------------------------------------- /C6/practice/6.3/README.md: -------------------------------------------------------------------------------- 1 | # 6.3 2 | 3 | $T_{access} = 8ms + \frac{1}{2} * \frac{60 * 1000}{15000}ms + \frac{60*1000}{500 * 15000}ms = 10.008ms$ 4 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog3.ys: -------------------------------------------------------------------------------- 1 | # prog3: Pad with 1 nop 2 | irmovq $10,%rdx 3 | irmovq $3,%rax 4 | nop 5 | addq %rdx,%rax 6 | halt 7 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog8.ys: -------------------------------------------------------------------------------- 1 | # prog8: Forwarding Priority 2 | irmovq $10,%rdx 3 | irmovq $3,%rdx 4 | rrmovq %rdx,%rax 5 | halt 6 | -------------------------------------------------------------------------------- /C2/homework/2.61/README.md: -------------------------------------------------------------------------------- 1 | # 2.61 2 | 3 | A. `!~x` 4 | 5 | B. `!x` 6 | 7 | C. `!((x & 0xFF) ^ 0xFF)` 8 | 9 | D. `!((x >> 24) & 0xFF)` 10 | -------------------------------------------------------------------------------- /C3/practice/3.54/README.md: -------------------------------------------------------------------------------- 1 | # 3.54 2 | 3 | ```cpp 4 | double funct2(double w, int x, float y, long z) { 5 | return x * y - w / z; 6 | } 7 | ``` 8 | -------------------------------------------------------------------------------- /labs/bomb/reault.txt: -------------------------------------------------------------------------------- 1 | Border relations with Canada have never been better. 2 | 1 2 4 8 16 32 3 | 0 207 4 | 7 0 DrEvil 5 | 9?>567 6 | 4 3 2 1 6 5 7 | 20 -------------------------------------------------------------------------------- /C3/homework/3.65/README.md: -------------------------------------------------------------------------------- 1 | # 3.65 2 | 3 | A. address of `A[i][j]` in `%rdx`. 4 | 5 | B. address of `A[j][i]` in `%rax`. 6 | 7 | C. M is 120 / 8 = 15. 8 | -------------------------------------------------------------------------------- /C3/homework/3.68/README.md: -------------------------------------------------------------------------------- 1 | # 3.68 2 | 3 | ```txt 4 | 5 <= B <= 8 5 | 9<= A <= 10 6 | 44 < A * B <= 46 7 | ``` 8 | 9 | `A` is `9`, `B` is `5`. 10 | -------------------------------------------------------------------------------- /C3/practice/3.49/README.md: -------------------------------------------------------------------------------- 1 | # 3.49 2 | 3 | A. $s2 = s1 - ((8 * n + 22) & (-16))$ 4 | 5 | B. $p = ((s2 + 7) >> 3) * 8$ 6 | 7 | C. 8 | 9 | D. 10 | -------------------------------------------------------------------------------- /C4/practice/4.25/README.md: -------------------------------------------------------------------------------- 1 | # 4.25 2 | 3 | ```cpp 4 | word mem_data = [ 5 | icode in { IRMMOVQ, IPUSHQ } : valA; 6 | icode == iCALL : valP; 7 | ] 8 | -------------------------------------------------------------------------------- /C4/practice/4.29/README.md: -------------------------------------------------------------------------------- 1 | # 4.29 2 | 3 | A. latency: (300/k + 20) * k, throughput: 1/((300/k + 20)e-12). 4 | 5 | B. if k -> ∞, the throughput is 50GIPS. -------------------------------------------------------------------------------- /C4/practice/4.39/README.md: -------------------------------------------------------------------------------- 1 | # 4.39 2 | 3 | ```python 4 | bool D_stall = 5 | E_icode in { IMRMOVQ, IPOPQ } && 6 | E_dstM in { d_srcA, d_srcB }; 7 | ``` 8 | -------------------------------------------------------------------------------- /C1/practice/1.2/README.md: -------------------------------------------------------------------------------- 1 | # 1.2 2 | 3 | From Amdahl's Law, if we want S = 2, and the $\alpha = 0.8$, we need $k = \frac{\alpha}{1/S + \alpha - 1} = 2.667$ 4 | -------------------------------------------------------------------------------- /C2/homework/2.77/README.md: -------------------------------------------------------------------------------- 1 | # 2.77 2 | 3 | A. `(x<<4) + x`. 4 | 5 | B. `x - (x<<3>>)`. 6 | 7 | C. `(x<<6) - (x<<2)`. 8 | 9 | D. `(x<<4) - (x<<7)`. 10 | -------------------------------------------------------------------------------- /C6/README.md: -------------------------------------------------------------------------------- 1 | # Chapter6: The Memory Hierarchy 2 | 3 | - [note](./note/README.md) 4 | - [practice](./practice/README.md) 5 | - [homework](./homework/README.md) 6 | -------------------------------------------------------------------------------- /C6/homework/6.33/README.md: -------------------------------------------------------------------------------- 1 | # 6.33 2 | 3 | - 0x1788 4 | - 0x1789 5 | - 0x178A 6 | - 0x178B 7 | 8 | - 0x16C8 9 | - 0x16C9 10 | - 0x16CA 11 | - 0x16CB 12 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog2.ys: -------------------------------------------------------------------------------- 1 | # prog2: Pad with 2 nop's 2 | irmovq $10,%rdx 3 | irmovq $3,%rax 4 | nop 5 | nop 6 | addq %rdx,%rax 7 | halt 8 | -------------------------------------------------------------------------------- /labs/attack/result1.txt: -------------------------------------------------------------------------------- 1 | 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2 | 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 3 | 30 30 30 30 30 30 30 30 c0 17 40 -------------------------------------------------------------------------------- /C2/homework/2.81/README.md: -------------------------------------------------------------------------------- 1 | # 2.81 2 | 3 | A. `~((1< 2 | 3 | int main(){ 4 | int num = 0x01; 5 | printf("%x", ((char*)&num)[0]); 6 | return ((char*)&num)[0]; 7 | } -------------------------------------------------------------------------------- /C3/practice/3.11/README.md: -------------------------------------------------------------------------------- 1 | # 3.11 2 | 3 | A. set `%rdx` to zero. 4 | 5 | B. `movq $0, %rdx` 6 | 7 | C.`xorq` require only 3 bytes, while the version `movq` need 7 bytes. 8 | -------------------------------------------------------------------------------- /C3/practice/3.34/README.md: -------------------------------------------------------------------------------- 1 | # 3.34 2 | 3 | A. a0 - a5 stored in `%rbx`, `%r15`, `%r14`, `%r13`, `%r12`, `%rbp`. 4 | 5 | B. a6 - a7 stored in stack. 6 | 7 | C. Not enough. 8 | -------------------------------------------------------------------------------- /C4/practice/4.22/README.md: -------------------------------------------------------------------------------- 1 | # 4.22 2 | 3 | Write via the M port to take priority over the write via the E port in order to store the value read from the memory into `%rsp`. 4 | -------------------------------------------------------------------------------- /labs/attack/result2.txt: -------------------------------------------------------------------------------- 1 | 68 ec 17 40 00 48 c7 c7 2 | fa 97 b9 59 c3 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 00 00 00 00 6 | 78 dc 61 55 -------------------------------------------------------------------------------- /C2/practice/2.46/README.md: -------------------------------------------------------------------------------- 1 | # 2.46 2 | 3 | A. 0.00011001100110011001101 4 | 5 | B. 0.00000000000000000000000[1100], $2.38 * 10^-8$ 6 | 7 | C. 0.08s 8 | 9 | D. 0.08 * 2000 = 160m. -------------------------------------------------------------------------------- /C3/README.md: -------------------------------------------------------------------------------- 1 | # Chapter3: Machine-Level Representation of Programs 2 | 3 | - [note](./note/README.md) 4 | - [practice](./practice/README.md) 5 | - [homework](./homework/README.md) 6 | -------------------------------------------------------------------------------- /C6/homework/6.37/README.md: -------------------------------------------------------------------------------- 1 | # 6.37 2 | 3 | | Function | N = 64 | N = 60 | 4 | | sumA | 1/4 | 1/4 | 5 | | sumB | 1/4 | 1/4 | 6 | | sumC | 1/4 | 1/4 | 7 | -------------------------------------------------------------------------------- /C2/README.md: -------------------------------------------------------------------------------- 1 | # Chapter2: Representing and Manipulating Information 2 | 3 | - [note](./note/README.md) 4 | - [practice](./practice/README.md) 5 | - [homework](./homework/README.md) 6 | -------------------------------------------------------------------------------- /C3/note/encodings/mstore.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long mult2(long, long) ; 4 | 5 | void multstore(long x, long y, long* dest) { 6 | long t = mult2(x, y); 7 | *dest = t; 8 | } -------------------------------------------------------------------------------- /C4/practice/4.11/README.md: -------------------------------------------------------------------------------- 1 | # 4.11 2 | 3 | ```HCL 4 | word Min = [ 5 | A <= B && A <= C : A; 6 | B <= C : B; 7 | 1 : C; 8 | ] 9 | ``` 10 | -------------------------------------------------------------------------------- /C4/practice/4.41/README.md: -------------------------------------------------------------------------------- 1 | # 4.41 2 | 3 | ```python 4 | bool set_cc = 5 | E_icode == IOPQ && 6 | !m_stat in { SADR, SINS, SHLT } && 7 | !W_stat in { SADR, SINS, SHLT }; 8 | ``` 9 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "cstdlib": "c", 4 | "__locale": "c", 5 | "__string": "c", 6 | "string": "c", 7 | "string_view": "c" 8 | } 9 | } -------------------------------------------------------------------------------- /C3/practice/3.53/README.md: -------------------------------------------------------------------------------- 1 | # 3.53 2 | 3 | - p: int 4 | - q: long 5 | - r: float 6 | - s: double 7 | 8 | or 9 | 10 | - p: int 11 | - q: float 12 | - r: long 13 | - s: double 14 | -------------------------------------------------------------------------------- /C6/practice/6.4/README.md: -------------------------------------------------------------------------------- 1 | # 6.4 2 | 3 | - $T_{max_rotation} =\frac{60 * 1000}{10000} = 6ms$ 4 | - $T_{avg_rotation} = 3ms$ 5 | 6 | A. $5ms + 6ms * 2 + 3ms = 20ms$ 7 | 8 | B. $16000ms$ 9 | -------------------------------------------------------------------------------- /C2/homework/2.72/README.md: -------------------------------------------------------------------------------- 1 | # 2.72 2 | 3 | A. Because `maxbytes-sizeof(val)` always return `unsigned` type, so the `memcpy` will exe anytime. 4 | 5 | B. `if(maxbytes - int(sizeof(val)) >= 0)` 6 | -------------------------------------------------------------------------------- /C2/homework/2.84/README.md: -------------------------------------------------------------------------------- 1 | # 2.84 2 | 3 | ```cpp 4 | return (ux << 1 == 0 && uy << 1 == 0) 5 | || (sx && !sy) 6 | || (sx && sy && uxx >= uy) 7 | || (!sx && !sy && ux <= uy) 8 | ``` 9 | -------------------------------------------------------------------------------- /C2/practice/2.51/README.md: -------------------------------------------------------------------------------- 1 | # 2.51 2 | 3 | A. 0.0001 1001 1001 1001 1001 101 4 | 5 | B. 0.0000 0000 0000 0000 0000 001100. $\frac{10}{2^{22}}$ 6 | 7 | C. 0.086s 8 | 9 | D. 171m. 10 | -------------------------------------------------------------------------------- /C3/practice/3.9/README.md: -------------------------------------------------------------------------------- 1 | # 3.9 2 | 3 | ```x86asm 4 | shift_left4_rightn: 5 | movq %rdi, %rax 6 | salq $4, %rax 7 | movl %esi, %ecx 8 | sarq %cl, %rax 9 | ``` 10 | -------------------------------------------------------------------------------- /C4/practice/4.35/README.md: -------------------------------------------------------------------------------- 1 | # 4.35 2 | 3 | ```x86asm 4 | irmovq $0x123, %rax 5 | irmovq $0x321, %rdx 6 | xorq %rcx, %rcx 7 | cmovne %rax, %rdx 8 | add $rdx, %rdx 9 | halt 10 | ``` 11 | -------------------------------------------------------------------------------- /C5/practice/5.5/README.md: -------------------------------------------------------------------------------- 1 | # 5.5 2 | 3 | A. 4 | 5 | - additions count: $n$. 6 | - multiplications count: $degree * 2$. 7 | 8 | B. `xpwr = x * xpwr` require a floating-point multiplication. 9 | -------------------------------------------------------------------------------- /C2/practice/2.44/README.md: -------------------------------------------------------------------------------- 1 | # 2.44 2 | 3 | A. `false`, x = TMIN. 4 | 5 | B. `true` 6 | 7 | C. `false` 8 | 9 | D. `true` 10 | 11 | E. `false`, 12 | 13 | F. `true` 14 | 15 | G. `true`. 16 | -------------------------------------------------------------------------------- /C2/practice/2.54/README.md: -------------------------------------------------------------------------------- 1 | # 2.54 2 | 3 | A. true 4 | 5 | B. false. 6 | 7 | C. false. 8 | 9 | D. true. 10 | 11 | E. true. 12 | 13 | F. true. 14 | 15 | G. true. 16 | 17 | H. false. 18 | -------------------------------------------------------------------------------- /C3/homework/3.61/README.md: -------------------------------------------------------------------------------- 1 | # 3.61 2 | 3 | ```cpp 4 | long cread_alt(long *xp) { 5 | int result = 0; 6 | if (xp == null) { 7 | return 0; 8 | } 9 | return *xp; 10 | } 11 | ``` 12 | -------------------------------------------------------------------------------- /C4/practice/4.30/README.md: -------------------------------------------------------------------------------- 1 | # 4.30 2 | 3 | ```python 4 | word f_stat = [ 5 | imem_error: SADR; 6 | !instr_valid: SINS; 7 | f_icode == IHALT : SHLT; 8 | 1 : SAOK; 9 | ] 10 | ``` -------------------------------------------------------------------------------- /C3/homework/3.61/cread.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long cread_alt(long *xp) { 4 | return xp ? *xp : 0; 5 | } 6 | 7 | int main() { 8 | long* xp = 1; 9 | cread_alt(xp); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /C4/practice/4.23/README.md: -------------------------------------------------------------------------------- 1 | # 4.23 2 | 3 | ```cpp 4 | word aluB = [ 5 | icode in { IRMMOVQ, IMRMOVQ, IOPQ, ICALL, IPUSHQ, IRET, IPOQ } : valB; 6 | icode in { IRRMOVQ, IIRMOVQ } : 0; 7 | ] 8 | ``` 9 | -------------------------------------------------------------------------------- /C5/practice/5.4/README.md: -------------------------------------------------------------------------------- 1 | # 5.4 2 | 3 | A. in first, `%xmm0` is result; in second, `%xmm0` is `acc`. 4 | 5 | B. Yes. 6 | 7 | C. This transformation can be made without changing the program behavior. 8 | -------------------------------------------------------------------------------- /C2/practice/2.19/README.md: -------------------------------------------------------------------------------- 1 | # 2.19 2 | 3 | | x | T2U(x) | 4 | | - | - | 5 | | -8 | 8 | 6 | | -3 | 13 | 7 | | -2 | 14 | 8 | | -1 | 15 | 9 | | 0 | 0 | 10 | | 5 | 5 | 11 | -------------------------------------------------------------------------------- /C3/practice/3.33/README.md: -------------------------------------------------------------------------------- 1 | # 3.33 2 | 3 | ```cpp 4 | int procprob(int a, short b, long* u, char* v); 5 | ``` 6 | 7 | or 8 | 9 | ```cpp 10 | int procprob(int b, short a, long* v, char* u); 11 | ``` 12 | -------------------------------------------------------------------------------- /C4/practice/4.15/README.md: -------------------------------------------------------------------------------- 1 | # 4.15 2 | 3 | Yes. 4 | 5 | if `rA` equals to `%rsp` in figure 4.20, we can see that in the memory stage the instruction will store `valA`, the original value of the stack pointer. 6 | -------------------------------------------------------------------------------- /C2/homework/2.57/README.md: -------------------------------------------------------------------------------- 1 | # 2.57 2 | 3 | You can look at [show_bytes_more](./show_bytes_more.c). 4 | 5 | And the output is: 6 | 7 | ``` 8 | 2301 9 | efcdab8967452301 10 | e7c6f484454a9340 11 | ``` 12 | -------------------------------------------------------------------------------- /C2/practice/2.27/README.md: -------------------------------------------------------------------------------- 1 | # 2.27 2 | 3 | ```cpp 4 | /* return 1 if argument s and y can be added without causing overflow. */ 5 | int uadd_ok(unsigned x, unsigned y) { 6 | return x + y >= x; 7 | } 8 | ``` 9 | -------------------------------------------------------------------------------- /C4/practice/4.20/README.md: -------------------------------------------------------------------------------- 1 | # 4.20 2 | 3 | ```cpp 4 | word srcB = [ 5 | icode in { IMRMOVQ, IRMMOVQ, IOPQ } : rB; 6 | icode in { IPUSH, IPOP, ICALL, IRET } : RRSP; 7 | 1: RNONE; 8 | ] 9 | ``` 10 | -------------------------------------------------------------------------------- /C4/practice/4.27/README.md: -------------------------------------------------------------------------------- 1 | # 4.27 2 | 3 | ```cpp 4 | word Stat = [ 5 | imem_error || deme_error : SADR; 6 | !instr_valid: SINS; 7 | icode == IHALT : SHLT; 8 | 1 : SAOK; 9 | ] 10 | ``` 11 | -------------------------------------------------------------------------------- /C4/practice/4.40/README.md: -------------------------------------------------------------------------------- 1 | # 4.40 2 | 3 | ```python 4 | bool E_bubble = 5 | (E_icode == IJXX && !e_Cnd) || 6 | !(E_icode in { IMRMOVQ, IPOPQ } && 7 | E_dstM in { d_srcA, d_srcB } 8 | ); 9 | ``` 10 | -------------------------------------------------------------------------------- /C5/practice/5.10/README.md: -------------------------------------------------------------------------------- 1 | # 5.10 2 | 3 | A. array become `a[1], .., a[1000], a[1000]`. 4 | 5 | B. all array is `a[0]` 6 | 7 | C. because in the second, it need the pre result. 8 | 9 | D. CPE is 1.2. 10 | -------------------------------------------------------------------------------- /labs/attack/result3.txt: -------------------------------------------------------------------------------- 1 | 68 fa 18 40 00 48 c7 c7 2 | a8 dc 61 55 c3 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 00 00 00 00 6 | 78 dc 61 55 00 00 00 00 7 | 35 39 62 39 39 37 66 61 -------------------------------------------------------------------------------- /C3/homework/3.58/README.md: -------------------------------------------------------------------------------- 1 | # 3.58 2 | 3 | ```cpp 4 | long decode2(long x, long y, long z) { 5 | // x in %rdi, y in %rsi, z in %rdx 6 | y -= z; 7 | x *= y; 8 | return (y<<63>>63) ^ x; 9 | } 10 | ``` 11 | -------------------------------------------------------------------------------- /C2/homework/2.59/generate_a_word.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x = 0x89ABCDEF; 5 | int y = 0x76543210; 6 | int res = (x & 0xFF) | (y & (~0xFF)); 7 | printf("%x",res); 8 | return res; 9 | } -------------------------------------------------------------------------------- /C4/practice/4.42/README.md: -------------------------------------------------------------------------------- 1 | # 4.42 2 | 3 | ```python 4 | bool M_bubble = 5 | m_stat in { SADR, SINS, SHLT } || 6 | W_stat in { SADR, SINS, SHLT }; 7 | 8 | bool W_stall = W_stat in { SADR, SINS, SHLT }; 9 | ``` 10 | -------------------------------------------------------------------------------- /C2/homework/2.68/lower_one_mask.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int lower_one_mask(int n) { 4 | return (unsigned) -1 >> ((sizeof(int) << 3) - n); 5 | } 6 | int main(){ 7 | printf("%x",lower_one_mask(32)); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /C2/practice/2.38/README.md: -------------------------------------------------------------------------------- 1 | # 2.38 2 | 3 | if b = 0, k = 0, 1, 2, 3 4 | 5 | a *1, a* 2, a *4, a* 8 6 | 7 | else if b = a, k = 0, 1, 2, 3 8 | 9 | a *2, a* 3, a *5, a* 9 10 | 11 | so the result is 1, 2, 3, 4, 5, 8, 9 12 | -------------------------------------------------------------------------------- /C2/practice/2.42/README.md: -------------------------------------------------------------------------------- 1 | # 2.42 2 | 3 | ```cpp 4 | int div16(int x){ 5 | int sign = (x >> 31) ; // if x < 0 then sign is 0xFFFFFFFF; 6 | int bias = sign & 0xF; 7 | return (x + bias) >> 4; 8 | } 9 | ``` 10 | -------------------------------------------------------------------------------- /C3/homework/3.72/README.md: -------------------------------------------------------------------------------- 1 | # 3.72 2 | 3 | A. $s_2 = s_1 - ((30 + 8n) & (-16))$ equals $s_2 = s_1 - (30 + 8n)>>4<<4$ 4 | 5 | b. $p = s_1 - (s_2 + 15)>>4<<4$ 6 | 7 | C. $e_1$, max is $24$. min is $1$. 8 | 9 | D. 16 10 | -------------------------------------------------------------------------------- /C4/practice/4.31/README.md: -------------------------------------------------------------------------------- 1 | # 4.31 2 | 3 | ```python 4 | word d_dstE = [ 5 | D_icode in { IRRMOVQ, IIRMOVQ, IOPQ } : D_rB; 6 | D_icode in { IPUSHQ, IPOPQ, ICALL, IRET } : RRSP; 7 | 1 : RNONE; 8 | ] 9 | ``` 10 | -------------------------------------------------------------------------------- /C6/practice/6.12/README.md: -------------------------------------------------------------------------------- 1 | # 6.12 2 | 3 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 4 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 5 | | CT | CT | CT | CT | CT | CT | CT | CT | CI | CI | CI | CO | CO | 6 | -------------------------------------------------------------------------------- /C3/practice/3.10/README.md: -------------------------------------------------------------------------------- 1 | # 3.10 2 | 3 | ```cpp 4 | long arith2(long x, long y, long z) { 5 | long t1 = x | y; 6 | long t2 = t1 >> 3; 7 | long t3 = ~t2; 8 | long t4 = z - t3; 9 | return t4; 10 | } 11 | ``` 12 | -------------------------------------------------------------------------------- /C3/practice/3.20/README.md: -------------------------------------------------------------------------------- 1 | # 3.20 2 | 3 | A. `/` 4 | 5 | B. 6 | 7 | ```cpp 8 | long arith(long x) { 9 | long temp = x + 7; 10 | if( x >= 0) { 11 | temp = x; 12 | } 13 | return temp >> 3; 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /C3/homework/3.69/README.md: -------------------------------------------------------------------------------- 1 | # 3.69 2 | 3 | Because algin is 8, so `sizeof(a_struct) * CNT = 0x118`. 4 | 5 | A. `CNT` is 7. 6 | 7 | B. 8 | 9 | ```cpp 10 | typedef struct { 11 | long idx; 12 | long x[4]; 13 | }a_struct; 14 | ``` 15 | -------------------------------------------------------------------------------- /C3/practice/3.52/README.md: -------------------------------------------------------------------------------- 1 | # 3.52 2 | 3 | A. a: %xmm0, b: %rdi, c: %xmm1, d: %rsi. 4 | 5 | B. a: %rdi, b: %rsi, c: %rdx, d: %rcx. 6 | 7 | C. a: %rdi, b: %xmm0, c: %rsi, d: %xmm1. 8 | 9 | D. a: %xmm0, b: %rdi, c: %xmm1, d: %xmm2. 10 | -------------------------------------------------------------------------------- /C5/practice/5.3/README.md: -------------------------------------------------------------------------------- 1 | # 5.3 2 | 3 | | Code | min | max | incr | square | 4 | | - | - | - | - | - | 5 | | A | 1 | 91 | 90 | 90 | 6 | | B | 91 | 1 | 90 | 90 | 7 | | B | 1 | 1 | 90 | 90 | 8 | -------------------------------------------------------------------------------- /C6/homework/6.27/README.md: -------------------------------------------------------------------------------- 1 | # 6.27 2 | 3 | A. 4 | 5 | - 0x08A4 6 | - 0x08A5 7 | - 0x08A6 8 | - 0x08A7 9 | - 0x0704 10 | - 0x0705 11 | - 0x0706 12 | - 0x0707 13 | 14 | B. 15 | 16 | - 0x1238 17 | - 0x1239 18 | - 0x123A 19 | - 0x123B 20 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog5.ys: -------------------------------------------------------------------------------- 1 | # prog5: Load/use hazard 2 | irmovq $128,%rdx 3 | irmovq $3,%rcx 4 | rmmovq %rcx, 0(%rdx) 5 | irmovq $10,%rbx 6 | mrmovq 0(%rdx), %rax # Load %rax 7 | addq %rbx,%rax # Use %rax 8 | halt 9 | -------------------------------------------------------------------------------- /C2/homework/2.86/README.md: -------------------------------------------------------------------------------- 1 | # 2.86 2 | 3 | $bias = 2^14-1$ 4 | 5 | | description| decimal| 6 | | smallest positive denormalized| 2^{1-bias-63}| 7 | | smallest positive normalized| 2^{1-bias}| 8 | | largest normalized| 2^{bias} * (2 - 2^{-63})| 9 | -------------------------------------------------------------------------------- /C3/practice/3.35/README.md: -------------------------------------------------------------------------------- 1 | # 3.35 2 | 3 | ```cpp 4 | long rfun(unsigned long x) { 5 | if (x == 0) { 6 | return x; 7 | } 8 | unsigned long nx = x>>2; 9 | long rv = rfun(nx); 10 | return rv + x; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /C4/practice/4.12/README.md: -------------------------------------------------------------------------------- 1 | # 4.12 2 | 3 | ```HCL 4 | word Median = [ 5 | A <= B && B <= C : B; 6 | C <= B && B <= A : B; 7 | A <= C && C <= B : C; 8 | B <= C && C <= A : C; 9 | 1 : A; 10 | ] 11 | ``` 12 | -------------------------------------------------------------------------------- /C3/practice/3.25/README.md: -------------------------------------------------------------------------------- 1 | # 3.25 2 | 3 | ```cpp 4 | long loop_while2(long a, long b) { 5 | long result = b; 6 | while (b > 0) { 7 | result = result * a; 8 | b = b - a; 9 | } 10 | return result; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /C4/practice/4.24/README.md: -------------------------------------------------------------------------------- 1 | # 4.24 2 | 3 | ```cpp 4 | word dstE = [ 5 | icode in { IRRMOVQ } && Cnd : rB; 6 | icode in { IIRMOVQ, IOPQ } : rB; 7 | icode in { IPUSHQ, IPOPQ, ICALL, IRET } : RRSP; 8 | 1 : RNONE; 9 | ] 10 | ``` 11 | -------------------------------------------------------------------------------- /C3/practice/3.24/README.md: -------------------------------------------------------------------------------- 1 | # 3.24 2 | 3 | ```cpp 4 | long loop_while(long a, long b) { 5 | long result = 1; 6 | while (a < b) { 7 | result = result * (a + b); 8 | a = a + 1; 9 | } 10 | return result; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /C3/practice/3.48/README.md: -------------------------------------------------------------------------------- 1 | # 3.48 2 | 3 | A. 4 | 5 | - no canary value: `buf` in `(%rsp)`, `v` in `(%rsp) + 0x24`. 6 | - had canary value: `buf` in `(%rsp) + 0x16`, `v` in `(%rsp) + 0x8`. 7 | 8 | B. `buf` can't corrupt the value of `v`. 9 | 10 | -------------------------------------------------------------------------------- /C4/practice/4.7/README.md: -------------------------------------------------------------------------------- 1 | # 4.7 2 | 3 | It's means under x86-64, push the original value of `%rsp`. 4 | 5 | (In fact, depending on what mode an x86 processor operates under, it will do different things when instructed to push the stack pointer register.) 6 | -------------------------------------------------------------------------------- /C5/note/performance/README.md: -------------------------------------------------------------------------------- 1 | # Expressing Program Performance 2 | 3 | - CPI: cycles per instruction. 4 | - CPE: cycles per element. 5 | - GHz: gigahertz, billions of cycles per second. 6 | 7 | practice: 8 | 9 | - [5.2](../../practice/5.2/README.md) 10 | -------------------------------------------------------------------------------- /C3/homework/3.61/cread_alt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long cread_alt(long *xp) { 4 | int result = 0; 5 | if (!xp) { 6 | return 0; 7 | } 8 | return *xp; 9 | } 10 | 11 | int main() { 12 | long* xp = 1; 13 | cread_alt(xp); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /C4/practice/4.1/README.md: -------------------------------------------------------------------------------- 1 | # 4.1 2 | 3 | ```x86asm 4 | .pos 5 | 0x100: 30 F3 0F 00 00 00 00 00 00 00 6 | 0x10A: 20 31 7 | loop: 8 | 0x10C: 40 13 FD FF FF FF FF FF FF FF 9 | 0x116: 60 31 10 | 0x118: 70 0C 01 00 00 00 00 00 00 00 11 | ``` 12 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/poptest.ys: -------------------------------------------------------------------------------- 1 | # Test of Pop semantics for Y86-64 2 | irmovq $0x100,%rsp # Initialize stack pointer 3 | irmovq $0xABCD,%rax 4 | pushq %rax # Put known value on stack 5 | popq %rsp # Either get 0xABCD, or 0xfc 6 | halt 7 | -------------------------------------------------------------------------------- /C2/homework/2.62/int_shifts_are_arithmetic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int int_shifts_are_arithmetic() { 4 | int x = (int)0x80000000 >> 31; 5 | return !(x ^ ~0); 6 | } 7 | int main(){ 8 | printf("%d", int_shifts_are_arithmetic()); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /C2/practice/2.9/README.md: -------------------------------------------------------------------------------- 1 | # 2.9 2 | 3 | A. 4 | 5 | | | | 6 | |-|-| 7 | | white| black| 8 | | blue | yellow| 9 | | green| magenta| 10 | | cyan | red | 11 | 12 | B. 13 | 14 | - Blue | Green = `Cyan` 15 | - Yellow & Cyan = `Green` 16 | - Red ^ Magenta = `Blue` 17 | -------------------------------------------------------------------------------- /C3/homework/3.75/README.md: -------------------------------------------------------------------------------- 1 | # 3.75 2 | 3 | A. 4 | 5 | 1. the first argument imag number: %xmm1, real number: %xmm0. 6 | 2. ... 7 | 3. the n argument imag number: %xmm(2n+1), and real number: %xmm(2n). 8 | 9 | B. return %xmm0 as real part, %xmm1 as img part. 10 | -------------------------------------------------------------------------------- /C3/practice/3.29/README.md: -------------------------------------------------------------------------------- 1 | # 3.29 2 | 3 | A. Will never stop. 4 | 5 | B. 6 | 7 | ```cpp 8 | long sum = 0; 9 | long i; 10 | while(i < 10) { 11 | if( i & 1) { 12 | goto update; 13 | } 14 | sum += i; 15 | update: 16 | i++; 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /C6/homework/6.30/README.md: -------------------------------------------------------------------------------- 1 | # 6.30 2 | 3 | A. 128B 4 | 5 | B. 6 | 7 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 8 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 9 | | CT | CT | CT | CT | CT | CT | CT | CT | CI | CI | CI | CO | CO | 10 | -------------------------------------------------------------------------------- /labs/attack/result4.txt: -------------------------------------------------------------------------------- 1 | 00 00 00 00 00 00 00 00 2 | 00 00 00 00 00 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 00 00 00 00 6 | cc 19 40 00 00 00 00 00 7 | fa 97 b9 59 00 00 00 00 8 | c5 19 40 00 00 00 00 00 9 | ec 17 40 00 00 00 00 00 -------------------------------------------------------------------------------- /C2/practice/2.8/README.md: -------------------------------------------------------------------------------- 1 | # 2.8 2 | 3 | - a = [01101001] 4 | - b = [01010101] 5 | 6 | |Operation|Result| 7 | | - | - | 8 | | ~a | [10010110] | 9 | | ~b | [10101010] | 10 | | a & b | [01000001] | 11 | | a \| b | [01111101] | 12 | | a ^ b | [00111100] | 13 | -------------------------------------------------------------------------------- /C3/practice/3.44/README.md: -------------------------------------------------------------------------------- 1 | # 3.44 2 | 3 | | order | size | alignment | 4 | | - | - | - | 5 | | A | 16 | 4 | 6 | | B | 16 | 8 | 7 | | C | 10 | 2 | 8 | | D | 40 | 8 | 9 | | E | 40 | 8 | 10 | -------------------------------------------------------------------------------- /C3/practice/3.40/README.md: -------------------------------------------------------------------------------- 1 | # 3.40 2 | 3 | ```cpp 4 | void fix_set_diag_opt(fix_martix A, int val) { 5 | int* Aptr = &A[0][0]; 6 | int* Aend = &A[N][N]; 7 | do { 8 | *Aptr = val; 9 | Aptr += N + 1; 10 | } while (Aptr != Aend); 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /C4/practice/4.38/README.md: -------------------------------------------------------------------------------- 1 | # 4.38 2 | 3 | ```x86asm 4 | irmovq mem, %rbx 5 | mrmovq 0(%rbx), %rsp 6 | ret 7 | halt 8 | rtnpt: 9 | irmovq $5, %rsi 10 | halt 11 | 12 | .pos 0x40 13 | mem: .quad stack 14 | .pos 0x50 15 | stack: .quad rtnpt 16 | ``` 17 | -------------------------------------------------------------------------------- /C5/homework/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 HomeWork Problems 2 | 3 | - [5.13](./5.13/README.md) 4 | - [5.14](./5.14/README.md) 5 | - [5.15](./5.15/README.md) 6 | - [5.16](./5.16/README.md) 7 | - [5.17](./5.17/README.md) 8 | - [5.18](./5.18/README.md) 9 | - [5.19](./5.19/README.md) 10 | -------------------------------------------------------------------------------- /C5/practice/5.6/README.md: -------------------------------------------------------------------------------- 1 | # 5.6 2 | 3 | A. 4 | 5 | - additions count: $n$. 6 | - multiplications count: $n$. 7 | 8 | B. Because `result` need await the result of `x * result`. 9 | 10 | C. Because the critical path of 5.5 only had a single multiplication per iteration. 11 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog4.yo: -------------------------------------------------------------------------------- 1 | | # prog4: No padding 2 | 0x000: 30f20a00000000000000 | irmovq $10,%rdx 3 | 0x00a: 30f00300000000000000 | irmovq $3,%rax 4 | 0x014: 6020 | addq %rdx,%rax 5 | 0x016: 00 | halt 6 | -------------------------------------------------------------------------------- /C3/practice/3.57/README.md: -------------------------------------------------------------------------------- 1 | # 3.57 2 | 3 | ```cpp 4 | double funct3(int* ap, double b, long c, float *dp) { 5 | // ap in %rdi, b in %xmm0, c in %rsi, dp in %rdx 6 | if (b >= *ap) { 7 | return *dp * 2 + c; 8 | } else { 9 | return *dp * c; 10 | } 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /C6/practice/6.16/README.md: -------------------------------------------------------------------------------- 1 | # 6.16 2 | 3 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 4 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 5 | | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 6 | 7 | - 0x064C 8 | - 0x064D 9 | - 0x064E 10 | - 0x064F 11 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog8.yo: -------------------------------------------------------------------------------- 1 | | # prog8: Forwarding Priority 2 | 0x000: 30f20a00000000000000 | irmovq $10,%rdx 3 | 0x00a: 30f20300000000000000 | irmovq $3,%rdx 4 | 0x014: 2020 | rrmovq %rdx,%rax 5 | 0x016: 00 | halt 6 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog9.ys: -------------------------------------------------------------------------------- 1 | # Exception handling 2 | # /* $begin prog9-yo */ 3 | xorq %rax,%rax 4 | jne target # Not taken 5 | irmovq $1, %rax # Fall through 6 | halt 7 | target: 8 | .byte 0xFF # Invalid instruction code 9 | # /* $end prog9-yo */ 10 | -------------------------------------------------------------------------------- /C2/homework/2.70/fits_bits.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fits_bits(int x, int n) { 4 | int w = sizeof(int) << 3; 5 | int offset = w - n; 6 | return !(( x << offset >> offset) ^ x); 7 | } 8 | 9 | int main(){ 10 | printf("%d",fits_bits(0b0110,3)); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /C3/practice/3.3/README.md: -------------------------------------------------------------------------------- 1 | # 3.3 2 | 3 | 1. can't use %ebx as address register. 4 | 2. %rax is 64-bit, so we need `movq` 5 | 3. Can't memory to memory. 6 | 4. No register named %sl. 7 | 5. The destination can't be a immediate. 8 | 6. Destination operand incorrect size. 9 | 7. movb -> movw 10 | -------------------------------------------------------------------------------- /C3/practice/3.41/README.md: -------------------------------------------------------------------------------- 1 | # 3.41 2 | 3 | A. 4 | 5 | - p: 0 6 | - s.x: 8 7 | - s.y: 12 8 | - next: 16 9 | 10 | B. 24. 11 | 12 | C. 13 | 14 | ```cpp 15 | void sp_init(struct prob *sp) { 16 | sp->s.x = sp->s.y; 17 | sp->p = &(sp->s.x); 18 | sp->next = sp; 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /C2/homework/2.65/odd_ones.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int odd_ones(int x) { 4 | x ^= x>>16; 5 | x ^= x>>8; 6 | x ^= x>>4; 7 | x ^= x>>2; 8 | x ^= x>>1; 9 | return x & 0x1; 10 | } 11 | 12 | int main(){ 13 | printf("%d",odd_ones(0x01010101)); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /C3/practice/3.55/README.md: -------------------------------------------------------------------------------- 1 | # 3.35 2 | 3 | - low-order 4 bytes: 0x00000000 4 | - high-order 4 bytes: 0x40400000 5 | 6 | all: 0x40400000 00000000 7 | 8 | bin: 0100 0000 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 9 | 10 | result = 1 * 2^(4 + 1024 - 1023) = 32. 11 | -------------------------------------------------------------------------------- /C5/practice/5.12/psum1a.c: -------------------------------------------------------------------------------- 1 | void psum1a(float a[], float b[], long n) { 2 | long i; 3 | float last_value = 0; 4 | last_value = p[0] = a[0]; 5 | for (i = 1; i < n; i++) { 6 | float val = last_value + a[i]; 7 | p[i] = val; 8 | last_value = val; 9 | } 10 | } -------------------------------------------------------------------------------- /labs/attack/inject2.d: -------------------------------------------------------------------------------- 1 | 2 | inject2.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 <.text>: 8 | 0: 68 ec 17 40 00 pushq $0x4017ec 9 | 5: 48 c7 c7 fa 97 b9 59 mov $0x59b997fa,%rdi 10 | c: c3 retq 11 | -------------------------------------------------------------------------------- /labs/attack/inject3.d: -------------------------------------------------------------------------------- 1 | 2 | inject3.o: file format elf64-x86-64 3 | 4 | 5 | Disassembly of section .text: 6 | 7 | 0000000000000000 <.text>: 8 | 0: 68 fa 18 40 00 pushq $0x4018fa 9 | 5: 48 c7 c7 a8 dc 61 55 mov $0x5561dca8,%rdi 10 | c: c3 retq 11 | -------------------------------------------------------------------------------- /C2/homework/2.56/README.md: -------------------------------------------------------------------------------- 1 | # 2.56 2 | 3 | First, you should look at [show_bytes](../../note/storage/show_bytes.c). 4 | 5 | then, `test_show_bytes(0x87654321)`. 6 | 7 | output: 8 | 9 | ``` 10 | show_int: 11 | 21436587 12 | show_float: 13 | 7a35f1ce 14 | show_void*: 15 | 38f5c0e9fe7f0000 16 | ``` 17 | -------------------------------------------------------------------------------- /C3/note/encodings/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void multstore(long, long, long*); 4 | 5 | int main() { 6 | long d; 7 | multstore(2,3,&d); 8 | printf("2 * 3 --> %ld\n", d); 9 | return 0; 10 | } 11 | 12 | long mult2(long a, long b) { 13 | long s = a * b; 14 | return s; 15 | }; -------------------------------------------------------------------------------- /C3/practice/3.26/README.md: -------------------------------------------------------------------------------- 1 | # 3.26 2 | 3 | A. `jmp` 4 | 5 | B. 6 | 7 | ```cpp 8 | long fun_a(unsigned long x) { 9 | long val = 0; 10 | while(x) { 11 | val = x ^ val; 12 | x = x >> 1; 13 | } 14 | return val & 1; 15 | } 16 | ``` 17 | 18 | C. Even or odd number fo one. 19 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/pushquestion.yo: -------------------------------------------------------------------------------- 1 | | # Assembly Code to test semantics of pushq 2 | 0x000: 30f40001000000000000 | irmovq 0x100, %rsp 3 | 0x00a: a04f | pushq %rsp # Ambiguous 4 | 0x00c: b00f | popq %rax 5 | 0x00e: 00 | halt 6 | -------------------------------------------------------------------------------- /C2/practice/2.13/README.md: -------------------------------------------------------------------------------- 1 | # 2.13 2 | 3 | From above discuss, we know: 4 | 5 | |x|m|bis|bic| 6 | |-|-| -| -| 7 | |0|0| 0| 0| 8 | |0|1| 1| 0| 9 | |1|0| 1| 1| 10 | |1|1| 1| 0| 11 | 12 | So: 13 | 14 | A. bool_or: `result = bis(x,y)`. 15 | 16 | B. bool_xor: `result = bis( bic(x,y), bic(y,x))`. 17 | -------------------------------------------------------------------------------- /C2/practice/2.18/README.md: -------------------------------------------------------------------------------- 1 | # 2.18 2 | 3 | |order| hex| dec| 4 | | -| -| -| 5 | |A | 0x2e0| 736| 6 | |B | -0x58| -88| 7 | |C | 0x28 | 40| 8 | |D | -0x30| -48| 9 | |E | 0x78 | 120| 10 | |F | 0x88 | 136| 11 | |G | 0x1f8| 504| 12 | |H | 0xc0 | 192| 13 | |I | -0x48| -72| 14 | -------------------------------------------------------------------------------- /C2/practice/2.23/README.md: -------------------------------------------------------------------------------- 1 | # 2.23 2 | 3 | A. 4 | 5 | | w | fun1(w) | fun2(w) | 6 | | -| -| -| 7 | |0x0000 0076|0x0000 0076|0x0000 0076| 8 | |0x8765 4321|0x0000 0021|0x0000 0021| 9 | |0x0000 00C9|0x0000 00C9|0xFFFF FFC9| 10 | |0xEDCB A987|0x0000 0087|0xFFFF FF87| 11 | 12 | B. 13 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog3.yo: -------------------------------------------------------------------------------- 1 | | # prog3: Pad with 1 nop 2 | 0x000: 30f20a00000000000000 | irmovq $10,%rdx 3 | 0x00a: 30f00300000000000000 | irmovq $3,%rax 4 | 0x014: 10 | nop 5 | 0x015: 6020 | addq %rdx,%rax 6 | 0x017: 00 | halt 7 | -------------------------------------------------------------------------------- /C2/homework/2.69/rotate_left.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int rotate_left(unsigned x, int n) { 4 | int w = sizeof(int) << 3; 5 | int mask = (~((unsigned) -1 >> n) & x) >> (w-n); 6 | return (x << n) | mask; 7 | } 8 | 9 | int main(){ 10 | printf("%x", rotate_left(0x12345678, 0)); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /C3/practice/3.26/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long fun_a(unsigned long x) { 4 | long val = 0; 5 | while(x) { 6 | val = x ^ val; 7 | x = x >> 1; 8 | } 9 | return val & 1; 10 | } 11 | 12 | int main() { 13 | long n = 5; 14 | printf("%ld\n", fun_a(n)); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C6/practice/6.9/README.md: -------------------------------------------------------------------------------- 1 | # 6.9 2 | 3 | | Cache | m | C | B | E | S | t | s | b | 4 | | - | - | - | - | - | - | - | - | - | 5 | | 1 | 32 | 1024 | 4 | 1 | 256 | 22 | 8 | 2 | 6 | | 2 | 32 | 1024 | 8 | 4 | 32 | 24 | 5 | 3 | 7 | | 3 | 32 | 1024 | 32 | 32 | 1 | 27 | 0 | 5 | 8 | -------------------------------------------------------------------------------- /C2/practice/2.14/README.md: -------------------------------------------------------------------------------- 1 | # 2.14 2 | 3 | x = 0x66, y = 0x39. 4 | 5 | |Expression| value| 6 | |-|-| 7 | |x & y | 0x20| 8 | |x \| y | 0x7F| 9 | |~x \| ~y | 0xDF| 10 | |x & !y | 0x00| 11 | 12 | |Expression| value| 13 | |-|-| 14 | |x && y | 1| 15 | |x \|\| y | 1| 16 | |!x \|\|!y | 0| 17 | |x && ~y | 1| 18 | -------------------------------------------------------------------------------- /C2/homework/2.67/README.md: -------------------------------------------------------------------------------- 1 | # 2.67 2 | 3 | A. because `1<<32` may equal `1` or even not defined. 4 | 5 | B. `int beyond_msb = set_msb << 1`; 6 | 7 | C. 8 | 9 | ```cpp 10 | int int_size_is_16() { 11 | int set_msb = 1 << 15; 12 | int beyond_msb = set_msb << 1; 13 | return set_msb && !beyond_msb; 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /C2/homework/2.85/README.md: -------------------------------------------------------------------------------- 1 | # 2.85 2 | 3 | $bias = 2^(k-1)-14$ 4 | 5 | $V = 2^E * M$ 6 | 7 | 1. 8 | 9 | | num | E | M | f | V | 10 | | - | - | - | - | - | 11 | | 7.0 | 2 | 1.11 | 0.11 | 7 | 12 | 13 | 2. Largest odd integer that can be represented exactly: $2^{n+1} - 1$ 14 | 15 | 3. $V = 2^(bias-1)$. 16 | -------------------------------------------------------------------------------- /C2/practice/2.30/README.md: -------------------------------------------------------------------------------- 1 | # 2.30 2 | 3 | ```cpp 4 | /* Determine whether arguments can be added without overflow */ 5 | int tadd_ok(int x, int y) { 6 | if (x > 0 && y > 0 && x + y < x ) { 7 | return 0; 8 | } else if( x < 0 && y < 0 && x + y > x) { 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /C3/practice/3.5/README.md: -------------------------------------------------------------------------------- 1 | # 3.5 2 | 3 | `xp` in `%rdi`, `yp` in `%rsi`, `zp` in `%rdx`. 4 | 5 | The c code: 6 | 7 | ```cpp 8 | void decode1(long* xp, long* yp, long* zp) { 9 | long x = *xp; 10 | long y = *yp; 11 | long z = zp; 12 | *yp = x; 13 | *zp = y; 14 | *xp = z; 15 | return ; 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /C6/homework/6.28/README.md: -------------------------------------------------------------------------------- 1 | # 6.28 2 | 3 | A. none 4 | 5 | B. 6 | 7 | - 0x18F0 8 | - 0x18F1 9 | - 0x18F2 10 | - 0x18F3 11 | - 0x00B0 12 | - 0x00B1 13 | - 0x00B2 14 | - 0x00B3 15 | 16 | C. 17 | 18 | - 0x0E34 19 | - 0x0E35 20 | - 0x0E36 21 | - 0x0E37 22 | 23 | D. 24 | 25 | - 0x1BDC 26 | - 0x1BDD 27 | - 0x1BDE 28 | - 0x1BDF 29 | -------------------------------------------------------------------------------- /C3/note/heterogeneous/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double d = 2.2; 5 | unsigned long l = (unsigned long)d; 6 | printf("l = %ld\n", l); 7 | 8 | union 9 | { 10 | double t; 11 | unsigned long u; 12 | } temp; 13 | temp.t = 2.2; 14 | printf("temp.u = %ld\n", temp.u); 15 | } -------------------------------------------------------------------------------- /C5/practice/5.12/README.md: -------------------------------------------------------------------------------- 1 | # 5.12 2 | 3 | ```cpp 4 | void psum1a(float a[], float b[], long n) { 5 | long i; 6 | float last_value = 0; 7 | last_value = p[0] = a[0]; 8 | for (i = 1; i < n; i++) { 9 | float val = last_value + a[i]; 10 | p[i] = val; 11 | last_value = val; 12 | } 13 | } 14 | ``` -------------------------------------------------------------------------------- /C2/practice/2.10/inplace_swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void inplace_swap(int* x, int* y) { 4 | *y = *x ^ *y; // Step 1 5 | *x = *x ^ *y; // Step 2 6 | *y = *x ^ *y; // Step 3 7 | } 8 | 9 | int main() { 10 | int a = 1, b = 1; 11 | inplace_swap(&a,&b); 12 | printf("%d, %d", a,b); 13 | // 1, 1 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /C3/practice/3.28/README.md: -------------------------------------------------------------------------------- 1 | # 3.28 2 | 3 | A. 4 | 5 | ```cpp 6 | long fun_b(unsigned long x) { 7 | long val = 0; 8 | long i; 9 | for (i = 64; i != 0; i--) { 10 | val = (val << 1) | (x & 0x1); 11 | x = x >> 1; 12 | } 13 | return val; 14 | } 15 | ``` 16 | 17 | B. 18 | 19 | C. reverse the bits in x. 20 | -------------------------------------------------------------------------------- /C6/practice/6.14/README.md: -------------------------------------------------------------------------------- 1 | # 6.14 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 8 | 9 | B. 10 | 11 | - CO: 0x1 12 | - CI: 0x5 13 | - CT: 0x6E 14 | - is hit?: N 15 | - return byte: - 16 | -------------------------------------------------------------------------------- /C6/practice/6.15/README.md: -------------------------------------------------------------------------------- 1 | # 6.15 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 8 | 9 | B. 10 | 11 | - CO: 0x0 12 | - CI: 0x1 13 | - CT: 0xFF 14 | - is hit?: N 15 | - return byte: - 16 | -------------------------------------------------------------------------------- /labs/data/Driverhdrs.pm: -------------------------------------------------------------------------------- 1 | # 2 | # This file contains configuration variables for drivers. 3 | # It was generated by genhdrs.pl. Do not modify it. 4 | # 5 | package Driverhdrs; 6 | 7 | $LAB = "datalab"; 8 | $SERVER_NAME = "changeme.ics.cs.cmu.edu"; 9 | $SERVER_PORT = 8081; 10 | $COURSE_NAME = "csapp"; 11 | $AUTOGRADE_TIMEOUT = 0; 12 | 1; 13 | -------------------------------------------------------------------------------- /C2/practice/2.17/README.md: -------------------------------------------------------------------------------- 1 | # 2.17 2 | 3 | Assume w = 4, then: 4 | 5 | |Hex| Bin| B2U(x) | B2T(x)| 6 | |- | -| -| -| 7 | |0xE|[1110]| 14 | -2 | 8 | |0x0|[0000]| 0 | 0 | 9 | |0x5|[0101]| 5 | 5 | 10 | |0x8|[1000]| 8 | -8 | 11 | |0xD|[1101]| 13 | -3 | 12 | |0xF|[1111]| 15 | -1 | 13 | -------------------------------------------------------------------------------- /C6/practice/6.13/README.md: -------------------------------------------------------------------------------- 1 | # 6.13 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 8 | 9 | B. 10 | 11 | - CO: 0x0 12 | - CI: 0x5 13 | - CT: 0x71 14 | - is hit?: Y 15 | - return byte: 0x0B 16 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/yas.h: -------------------------------------------------------------------------------- 1 | void save_line(char *); 2 | void finish_line(); 3 | void add_reg(char *); 4 | void add_ident(char *); 5 | void add_instr(char *); 6 | void add_punct(char); 7 | void add_num(long long); 8 | void fail(char *msg); 9 | unsigned long long atollh(const char *); 10 | 11 | 12 | /* Current line number */ 13 | int lineno; 14 | -------------------------------------------------------------------------------- /C3/practice/3.27/README.md: -------------------------------------------------------------------------------- 1 | # 3.27 2 | 3 | ```cpp 4 | long fact_for_goto(long n) { 5 | long i = 2; 6 | long result = 1; 7 | if (n <= 1) { 8 | goto done; 9 | } 10 | loop: 11 | result = result * i; 12 | i++; 13 | if (i <= n) { 14 | goto loop; 15 | } 16 | done: 17 | return result; 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /C3/practice/3.42/README.md: -------------------------------------------------------------------------------- 1 | # 3.42 2 | 3 | A. 4 | 5 | ```cpp 6 | struct ELE { 7 | long v; 8 | struct ELE *p; 9 | } 10 | 11 | long fun(struct ELE *ptr) { 12 | long res = 0; 13 | while (ptr) { 14 | res += ptr->v; 15 | ptr = ptr->p; 16 | } 17 | return res; 18 | } 19 | ``` 20 | 21 | B. the sum of single link. 22 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/pushtest.ys: -------------------------------------------------------------------------------- 1 | # Test of Push semantics for Y86-64 2 | irmovq $0x100,%rsp # Initialize stack pointer 3 | rrmovq %rsp,%rax # Save stack pointer 4 | pushq %rsp # Push the stack pointer (old or new?) 5 | popq %rdx # Get it back 6 | subq %rdx,%rax # Compute difference. Either 0 (old) or 4 (new). 7 | halt 8 | -------------------------------------------------------------------------------- /C6/practice/6.7/README.md: -------------------------------------------------------------------------------- 1 | # 6.7 2 | 3 | ```cpp 4 | int sumarray3d(int a[N][N][N]) { 5 | int i, j, k, sum = 0; 6 | for (k = 0; k < N; k++) { 7 | for (i = 0; i < N; i++) { 8 | for (j = 0; j < N; j++) { 9 | sum += a[k][i][j]; 10 | } 11 | } 12 | } 13 | return sum; 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog7.ys: -------------------------------------------------------------------------------- 1 | # Demonstrate branch cancellation 2 | # /* $begin prog7-ys */ 3 | # prog7 4 | xorq %rax,%rax 5 | jne target # Not taken 6 | irmovq $1, %rax # Fall through 7 | halt 8 | target: 9 | irmovq $2, %rdx # Target 10 | irmovq $3, %rbx # Target+1 11 | # /* $end prog7-ys */ 12 | halt 13 | 14 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog2.yo: -------------------------------------------------------------------------------- 1 | | # prog2: Pad with 2 nop's 2 | 0x000: 30f20a00000000000000 | irmovq $10,%rdx 3 | 0x00a: 30f00300000000000000 | irmovq $3,%rax 4 | 0x014: 10 | nop 5 | 0x015: 10 | nop 6 | 0x016: 6020 | addq %rdx,%rax 7 | 0x018: 00 | halt 8 | -------------------------------------------------------------------------------- /C2/practice/2.28/README.md: -------------------------------------------------------------------------------- 1 | # 2.28 2 | 3 | | x Hex | x Decimal | -x Decimal| -x Hex| 4 | | - | - | - | - | 5 | | 0 | 0 | 0 | 0 | 6 | | 5 | 5 | 11 | B | 7 | | 8 | 8 | 8 | 8 | 8 | | D | 13 | 3 | 3 | 9 | | F | 15 | 1 | 1 | 10 | -------------------------------------------------------------------------------- /C3/practice/3.16/README.md: -------------------------------------------------------------------------------- 1 | # 3.16 2 | 3 | A. 4 | 5 | ```cpp 6 | void cond(long a, long *p) { 7 | if (p) { 8 | goto calc; 9 | } 10 | else if ( a > *p) { 11 | goto calc; 12 | } 13 | return ; 14 | calc: 15 | *p = a; 16 | return ; 17 | } 18 | ``` 19 | 20 | B. Because we used `&&` to combine two condition statements. 21 | -------------------------------------------------------------------------------- /C4/practice/4.37/README.md: -------------------------------------------------------------------------------- 1 | # 4.37 2 | 3 | ```x86asm 4 | irmovq stack, %rsp 5 | irmovq rtnp, %rax 6 | pushq %rax 7 | xorq %rax, %rax 8 | jne target 9 | irmovq $1, %rax 10 | halt 11 | target: 12 | ret 13 | irmovq $2, %rbx 14 | halt 15 | rtnp: 16 | irmovq $3, %rdx 17 | halt 18 | 19 | .pos 0x40 20 | stack: 21 | ``` 22 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog10.ys: -------------------------------------------------------------------------------- 1 | # prog10 2 | irmovq $1,%rax 3 | xorq %rsp,%rsp # Set stack pointer to 0 and CC to 100 4 | pushq %rax # Attempt to write to 0xfffffffffffffff8 5 | addq %rax,%rax # (Should not be executed) Would set CC to 000 6 | irmovq $2, %rax # Not executed 7 | irmovq $3, %rax # Not executed 8 | -------------------------------------------------------------------------------- /C4/practice/4.3/README.md: -------------------------------------------------------------------------------- 1 | # 4.3 2 | 3 | ```x86asm 4 | ;long sum(long *start, long count) 5 | ; start in %rdi, count in %rsi 6 | 7 | sum: 8 | xorq %rax, %rax 9 | andq %rsi, %rsi 10 | jmp test 11 | loop: 12 | mrmovq (%rdi), %r10 13 | addq %r10, %rax 14 | iaddq $8, %rdi 15 | iaddq $-1, %rsi 16 | test: 17 | jne loop 18 | ret 19 | ``` 20 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/j-cc.ys: -------------------------------------------------------------------------------- 1 | irmovq $1, %rsi 2 | irmovq $2, %rdi 3 | irmovq $4, %rbp 4 | irmovq $-32, %rax 5 | irmovq $64, %rdx 6 | subq %rdx,%rax 7 | je target 8 | nop 9 | halt 10 | target: 11 | addq %rsi,%rdx 12 | nop 13 | nop 14 | nop 15 | halt 16 | -------------------------------------------------------------------------------- /C3/practice/3.21/README.md: -------------------------------------------------------------------------------- 1 | # 3.21 2 | 3 | ```cpp 4 | long test(long x, long y) { 5 | long val = 8 * x; 6 | if (y > 0) { 7 | if (x >= y) { 8 | val = x & y; 9 | } 10 | else { 11 | val = y - x; 12 | } 13 | } 14 | else if (y <= -2) { 15 | val = x + y; 16 | } 17 | return val; 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /C6/homework/6.26/README.md: -------------------------------------------------------------------------------- 1 | # 6.26 2 | 3 | | Cache | m | C | B | E | S | t | s | b | 4 | | - | - | - | - | - | - | - | - | - | 5 | | 1 | 32 | 2048 | 8 | 1 | 256 | 21 | 8 | 3 | 6 | | 2 | 32 | 2048 | 4 | 4 | 128 | 23 | 7 | 2 | 7 | | 3 | 32 | 1024 | 2 | 8 | 64 | 25 | 6 | 1 | 8 | | 4 | 32 | 1024 | 32 | 2 | 16 | 23 | 4 | 5 | 9 | -------------------------------------------------------------------------------- /C2/homework/2.81/generate.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int generate_A(int k) { 4 | return ~((1<>2<<2); 13 | printf("%x\n", generate_A(8)); 14 | printf("%x\n", generate_B(16,8)); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C2/practice/2.33/README.md: -------------------------------------------------------------------------------- 1 | # 2.33 2 | 3 | w = 4, two's-complement. 4 | 5 | |Hex x| Decimal x | -x Decimal | -x Hex| 6 | | - | - | - | - | 7 | | 0 | 0 | 0 | 0 | 8 | | 5 | 5 | -5 | B | 9 | | 8 | -8 | -8 | 8 | 10 | | D | -3 | 3 | 3 | 11 | | F | -1 | 1 | 1 | 12 | -------------------------------------------------------------------------------- /C3/homework/3.70/README.md: -------------------------------------------------------------------------------- 1 | # 3.70 2 | 3 | A. 4 | 5 | | filed | offsets | 6 | | - | - | 7 | | `e1.p` | 0 | 8 | | `e1.y` | 8 | 9 | | `e2.x` | 0 | 10 | | `e2.next` | 8 | 11 | 12 | B. 16. 13 | 14 | C. 15 | 16 | ```cpp 17 | void proc (union ele *up) { 18 | up->e2.x = *(*(up->e2.next).e1.p) - (*(up->e2.next).e1.y); 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /C3/practice/3.13/README.md: -------------------------------------------------------------------------------- 1 | # 3.13 2 | 3 | ```cpp 4 | int comp(data_t a, data_t b) { 5 | return a COMP b; 6 | } 7 | ``` 8 | 9 | | order | `data_t` | `COMP` | 10 | | - | - | - | 11 | | A | `int` | `<` | 12 | | B | `short` | `>=` | 13 | | C | `unsigned char` | `<=` | 14 | | D | `long` | `!=` | 15 | -------------------------------------------------------------------------------- /C3/practice/3.18/README.md: -------------------------------------------------------------------------------- 1 | # 3.18 2 | 3 | ```cpp 4 | long test(long x, long y, long z) { 5 | long val = x + y + z; 6 | if (x < -3) { 7 | if ( y < z) { 8 | val = x * y; 9 | } 10 | else { 11 | val = y * z; 12 | } 13 | } 14 | else if ( x > 2) { 15 | val = x * z; 16 | } 17 | return val; 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /C2/practice/2.25/README.md: -------------------------------------------------------------------------------- 1 | # 2.25 2 | 3 | ```cpp 4 | float sum_elements( float a[], unsigned length) { 5 | int i; 6 | float result = 0; 7 | for ( i = 0; i <= length - 1; i++) { 8 | result += a[i]; 9 | } 10 | return result; 11 | } 12 | ``` 13 | 14 | The error: if we input the unsigned type `length` as 0, then `length - 1` is the UMax, so the loop will never stop. 15 | -------------------------------------------------------------------------------- /C3/practice/3.1/README.md: -------------------------------------------------------------------------------- 1 | # 3.1 2 | 3 | | Operand | Value | 4 | | - | - | 5 | | %rax | 0x100 | 6 | | 0x104 | 0xAB | 7 | | $0x108 | 0x108 | 8 | | (%rax) | 0xFF | 9 | | 4(%rax) | 0xAB | 10 | | 9(%rax, %rdx) | 0x11 | 11 | | 260(%rax, %rdx) | 0x13 | 12 | | 0xFC(, %rcx, 4) | 0xFF | 13 | | (%rax, %rdx, 4) | 0x11 | 14 | -------------------------------------------------------------------------------- /labs/archlab/sim/ptest/Makefile: -------------------------------------------------------------------------------- 1 | SIM=../pipe/psim 2 | TFLAGS= 3 | 4 | ISADIR = ../misc 5 | YAS=$(ISADIR)/yas 6 | 7 | .SUFFIXES: .ys .yo 8 | 9 | .ys.yo: 10 | $(YAS) $*.ys 11 | 12 | test: 13 | ./optest.pl -s $(SIM) $(TFLAGS) 14 | ./jtest.pl -s $(SIM) $(TFLAGS) 15 | ./ctest.pl -s $(SIM) $(TFLAGS) 16 | ./htest.pl -s $(SIM) $(TFLAGS) 17 | 18 | clean: 19 | rm -f *.o *~ *.yo *.ys 20 | 21 | -------------------------------------------------------------------------------- /C2/homework/2.71/xbyte.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned packed_t; 4 | 5 | int xbyte(packed_t word, int bytenum) { 6 | int w = sizeof(packed_t)<<3; 7 | int left_shift = w - 8 - (bytenum<<3); 8 | int right_shift = w - 8; 9 | return (int) word << left_shift >> right_shift; 10 | } 11 | 12 | int main(){ 13 | printf("%x", xbyte(0x00812233, 2)); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /C2/practice/2.2/README.md: -------------------------------------------------------------------------------- 1 | # 2.1 2 | 3 | | n | decimal: $2^n$ | hex: $2^n$ | 4 | | - | - | - | 5 | | 9 | 512 | 0x200 | 6 | | 19| 524,288 | 0x80000 | 7 | | 14| 16,384 | 0x4000 | 8 | | 16| 65,536 | 0x10000 | 9 | | 17| 131,072 | 0x20000 | 10 | | 5 | 32 | 0x20 | 11 | | 7 | 128 | 0x80 | 12 | -------------------------------------------------------------------------------- /C3/homework/3.66/README.md: -------------------------------------------------------------------------------- 1 | # 3.66 2 | 3 | ``` 4 | %r8 <- 4n + 1 5 | %rax <- 3n 6 | n <- %rax; 7 | if (n <= 0) { 8 | return 0; 9 | } else { 10 | %r8 <- %r8<<3 11 | %rcx <- 8j + A 12 | res <- 0 13 | i <- 0 14 | while ( n != i) { 15 | res <- res + *%rcx 16 | i <- i + 1 17 | %rcx <- %rcx + %r8 18 | } 19 | return res; 20 | } 21 | ``` 22 | 23 | - NR: `3n` 24 | - NC: `4n+1` 25 | -------------------------------------------------------------------------------- /C3/practice/3.17/README.md: -------------------------------------------------------------------------------- 1 | # 3.17 2 | 3 | A. 4 | 5 | ```cpp 6 | void goto_absdiff_se(long x, long y) { 7 | long t = x - y; 8 | if( t >= 0) { 9 | goto x_ge_y; 10 | } 11 | lt_cnt++; 12 | result = y - x; 13 | goto done; 14 | x_ge_y: 15 | ge_cnt++; 16 | result = x - y; 17 | done: 18 | return result; 19 | } 20 | ``` 21 | 22 | B. The choice is arbitrary. 23 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog1.yo: -------------------------------------------------------------------------------- 1 | | # prog1: Pad with 3 nop's 2 | 0x000: 30f20a00000000000000 | irmovq $10,%rdx 3 | 0x00a: 30f00300000000000000 | irmovq $3,%rax 4 | 0x014: 10 | nop 5 | 0x015: 10 | nop 6 | 0x016: 10 | nop 7 | 0x017: 6020 | addq %rdx,%rax 8 | 0x019: 00 | halt 9 | -------------------------------------------------------------------------------- /C3/practice/3.45/README.md: -------------------------------------------------------------------------------- 1 | # 3.45 2 | 3 | A. 4 | 5 | | a | b | c | d | e | f | g | h | 6 | | - | - | - | - | - | - | - | - | 7 | | 0 | 8 | 16 | 24 | 28 | 32 | 40 | 48 | 8 | 9 | B. 56 10 | 11 | C. 40 12 | 13 | ```cpp 14 | struct { 15 | char* a; 16 | short b; 17 | char d; 18 | char f; 19 | float e; 20 | double c; 21 | long g; 22 | int h; 23 | } rec; 24 | ``` 25 | -------------------------------------------------------------------------------- /C5/practice/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 Practice 2 | 3 | - [5.1](./5.1/README.md) 4 | - [5.2](./5.2/README.md) 5 | - [5.3](./5.3/README.md) 6 | - [5.4](./5.4/README.md) 7 | - [5.5](./5.5/README.md) 8 | - [5.6](./5.6/README.md) 9 | - [5.7](./5.7/README.md) 10 | - [5.8](./5.8/README.md) 11 | - [5.9](./5.9/README.md) 12 | - [5.10](./5.10/README.md) 13 | - [5.11](./5.11/README.md) 14 | - [5.12](./5.12/README.md) 15 | -------------------------------------------------------------------------------- /C1/practice/1.1/README.md: -------------------------------------------------------------------------------- 1 | # 1.1 2 | 3 | 1. $T_{old}$ = 25 hours, and $T_{new}$ = 1500 / 150 + (2500 - 1500)/100 = 20 hours. And the speedup for the trip is 25/20 = 1.25. 4 | 2. if we want to get an overall speedup of 1.67*, we need spend 15 hours in this trip. Because of speed limits, we had spent 10 hours in 1000km, thus we use spend 5 hours in unlimited road, which constitutes 1,500km, and it's 1500km/5h = 300km/h. 5 | -------------------------------------------------------------------------------- /C2/homework/2.78/divide_power2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int divide_power2(int x, int k) { 4 | int sign = (x>>31) & 0x1; 5 | int bias = (1 << k) -1; 6 | bias = (~sign + 1) & bias; 7 | return (x + bias) >> k; 8 | } 9 | 10 | int main(){ 11 | int x = 0x80000007; 12 | printf("%d\n",x); 13 | printf("%x\n", divide_power2(x,2)); 14 | printf("%x\n", x / 4); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C2/practice/2.40/README.md: -------------------------------------------------------------------------------- 1 | # 2.40 2 | 3 | x * K, and limited by shifts and add/subs number. 4 | 5 | | K | shifts | add/subs | expression | 6 | | - | - | - | - | 7 | | 6 | 2 | 1 | (x<<2) + (x<<1) | 8 | | 31 | 1 | 1 | (x<<5) - x | 9 | | -6 | 2 | 1 | (x<<1) - (x<<3) | 10 | | 55 | 2 | 2 | (x<<6) - (x<<3) - x| 11 | -------------------------------------------------------------------------------- /C2/homework/2.93/float_absval.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | /* compute |f|. if f is NaN, then return f*/ 6 | float_bits float_absval(float_bits f) { 7 | unsigned exp = f>>23 & 0xFF; 8 | unsigned frac = f & 0x7fffff; 9 | if( exp == 0xff && frac) { 10 | return f; 11 | } 12 | return (f & 0x7fffffff); 13 | } 14 | 15 | int main(){ 16 | return 0; 17 | } -------------------------------------------------------------------------------- /C2/practice/2.32/README.md: -------------------------------------------------------------------------------- 1 | # 2.32 2 | 3 | First, please read this function: [tadd_ok](../2.30/README.md). 4 | 5 | ```cpp 6 | /* Determine whether arguments can be subtracted without overflow */ 7 | int tsub_ok(int x, int y) { 8 | return tadd_ok(x, -y); 9 | } 10 | ``` 11 | 12 | This code has bug, if `x = 0x8000 0000`, `y = 0x8000 0000`, then `-y = 0x8000 0000` also, and the result is `0`, and its overflow. 13 | -------------------------------------------------------------------------------- /C2/practice/2.7/README.md: -------------------------------------------------------------------------------- 1 | # 2.7 2 | 3 | First, please read the c file: [show_bytes](../../note/storage/show_bytes.c) 4 | 5 | ```cpp 6 | const char* s = "abcdef"; 7 | show_bytes( (byte_pointer) s, strlen(s)); 8 | ``` 9 | 10 | output: 0x61 62 63 64 65 66. 11 | 12 | Recall that the library routine `strlen` dose not count the terminating null character, and so `show_bytes` printed only through the character 'f'. 13 | -------------------------------------------------------------------------------- /C2/practice/2.3/README.md: -------------------------------------------------------------------------------- 1 | # 2.3 2 | 3 | | Decimal | Binary | Hexadecimal | 4 | | - | - | - | 5 | | 0 | 0000 0000 | 0x00 | 6 | | 167 | 1010 0111 | 0xA7 | 7 | | 62 | 0011 1110 | 0x3E | 8 | | 188 | 1011 1100 | 0xBC | 9 | | 55 | 0011 0111 | 0x37 | 10 | | 136 | 1000 1000 | 0x88 | 11 | | 243 | 1111 0011 | 0xF3 | 12 | | 82 | 0101 0010 | 0x52 | 13 | | 172 | 1010 1100 | 0xAC | 14 | | 231 | 1110 0111 | 0xE7 | 15 | -------------------------------------------------------------------------------- /C3/practice/3.51/README.md: -------------------------------------------------------------------------------- 1 | # 3.51 2 | 3 | | $T_x$ | $T_y$ | Instructions | 4 | | - | - | - | 5 | | `long` | `double`| `vcvtsi2sdq %rdi, %xmm0`| 6 | | `double`| `int` | `vcvtsd2si %xmm0, %eax` | 7 | | `double`| `float` | `vunpcklpd %xmm0, %xmm0, %xmm0`, `vcvtpd2ps %xmm0, %xmm0`| 8 | | `long` | `float` | `vcvtsi2ssq %rdi, %xmm0, %xmm0` | 9 | | `float` | `long` | `vcvtss2siq %xmm0, %rax`| 10 | -------------------------------------------------------------------------------- /C4/practice/4.34/README.md: -------------------------------------------------------------------------------- 1 | # 4.34 2 | 3 | ```python 4 | word d_valB = [ 5 | d_srcB == e_dstE : e_valE; # Forward valE from execute 6 | d_srcB == M_dstM : m_valM; # Forward valM from memory 7 | d_srcB == M_dstE : M_valE; # Forward valE from memory 8 | d_srcB == W_dstM : W_valM; # Forward valM from write back 9 | d_srcB == W_dstE : W_valE; # Forward valE from write back 10 | 1 : d_rvalB; 11 | ] 12 | ``` 13 | -------------------------------------------------------------------------------- /C6/practice/6.1/README.md: -------------------------------------------------------------------------------- 1 | # 6.1 2 | 3 | | Organization | r | c | $b_r$ | $b_c$ | max($b_r$, $b_c$) | 4 | | - | - | - | - | - | - | 5 | | 16 * 1 | 4 | 4 | 2 | 2 | 2 | 6 | | 16 * 4 | 4 | 4 | 2 | 2 | 2 | 7 | | 128 * 8 | 16 | 8 | 4 | 3 | 4 | 8 | | 512 * 4 | 32 | 16| 5 | 4 | 5 | 9 | | 1024 * 4 | 32 | 32| 5 | 5 | 5 | 10 | -------------------------------------------------------------------------------- /C2/homework/2.79/mul3div4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int mul3div4(int x) { 4 | int mul_res = (x << 1) + x; 5 | int sign_x = (x>>31) & 0x1; 6 | int bias = (~sign_x+1) & ((1<<2)-1); 7 | int res = (mul_res + bias)>>2; 8 | return res; 9 | } 10 | 11 | int main(){ 12 | int TMin = 1<<31; 13 | int TMax = TMin + ~0; 14 | printf("%x\n", mul3div4(-4)); 15 | printf("%x\n", 3 * (-4) / 4); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/poptest.yo: -------------------------------------------------------------------------------- 1 | | # Test of Pop semantics for Y86-64 2 | 0x000: 30f40001000000000000 | irmovq $0x100,%rsp # Initialize stack pointer 3 | 0x00a: 30f0cdab000000000000 | irmovq $0xABCD,%rax 4 | 0x014: a00f | pushq %rax # Put known value on stack 5 | 0x016: b04f | popq %rsp # Either get 0xABCD, or 0xfc 6 | 0x018: 00 | halt 7 | -------------------------------------------------------------------------------- /C2/homework/2.55/README.md: -------------------------------------------------------------------------------- 1 | # 2.55 2 | 3 | First, you should look at [show_bytes](../../note/storage/show_bytes.c). 4 | 5 | And then: 6 | 7 | `uname -mr` is `19.6.0 x86_64` 8 | 9 | compile `gcc show_bytes.c -o show_bytes`. 10 | 11 | run `./show_bytes`. 12 | 13 | output: 14 | 15 | ``` 16 | show_int: 17 | 78563412 18 | show_float: 19 | b4a2914d 20 | show_void*: 21 | 38050deafe7f0000 22 | ``` 23 | 24 | My computer is *Little endian*. 25 | -------------------------------------------------------------------------------- /C2/homework/2.60/replace_byte.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned replace_byte( unsigned x, int i, unsigned char b) { 4 | if( i < 0) { 5 | printf("error: i is negative\n"); 6 | return x; 7 | } 8 | unsigned mask1 = b<<(i<<3); 9 | unsigned mask2 = 0xFF << (i<<3); 10 | return (x & (~mask2)) | mask1; 11 | } 12 | 13 | int main() { 14 | printf("%x", replace_byte(0x12345678, 3, 0xAB)); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C6/homework/6.31/README.md: -------------------------------------------------------------------------------- 1 | # 6.31 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 8 | 9 | B. 10 | 11 | | Parameter | Value | 12 | | - | - | 13 | | CO | 0x2 | 14 | | CI | 0x6 | 15 | | CT | 0x38 | 16 | | hit? | yes | 17 | | return byte | EB | 18 | -------------------------------------------------------------------------------- /C6/homework/6.32/README.md: -------------------------------------------------------------------------------- 1 | # 6.32 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 8 | 9 | B. 10 | 11 | | Parameter | Value | 12 | | - | - | 13 | | CO | 0x0 | 14 | | CI | 0x2 | 15 | | CT | 0xB7 | 16 | | hit? | No | 17 | | return byte | unknown | 18 | -------------------------------------------------------------------------------- /C3/practice/3.6/README.md: -------------------------------------------------------------------------------- 1 | # 3.6 2 | 3 | | Instruction | Result | 4 | | - | - | 5 | | `leaq 6(%rax), %rdx` | x + 6 | 6 | | `leaq (%rax, %rcx), %rdx` | x + y | 7 | | `leaq (%rax, %rcx, 4), %rdx` | x + 4y | 8 | | `leaq 7(%rax, %rax, 8), %rdx` | 9x + 7 | 9 | | `leaq 0xA(, %rcx, 4), %rdx` | 4y + 10 | 10 | | `leaq 9(%rax, %rcx, 2), %rdx` | x + 2y + 9 | 11 | -------------------------------------------------------------------------------- /C2/homework/2.76/calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void* calloc(size_t nmemb, size_t size) { 8 | if (!nmemb || !size) { 9 | return NULL; 10 | } 11 | if( nmemb == nmemb * size / size) { 12 | return memset(malloc(nmemb * size), 0, nmemb * size); 13 | } 14 | return NULL; 15 | } 16 | 17 | int main(){ 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C3/practice/3.12/README.md: -------------------------------------------------------------------------------- 1 | # 3.12 2 | 3 | ```cpp 4 | void uremdiv(unsigned long x, unsigned long y, unsigned long *qp, unsigned long * rp) 5 | // x in %rdi 6 | // y in %rsi 7 | // qp in %rdx 8 | // rp in %rcx 9 | ``` 10 | 11 | result: 12 | 13 | ```x86asm 14 | uremdiv: 15 | movq %rdx, %r8 16 | movq %rdi, %rax 17 | movq $0, %edx 18 | cqto 19 | divq %rsi 20 | movq %rax, (%r8) 21 | movq %rdx, (%rcx) 22 | ret 23 | ``` 24 | -------------------------------------------------------------------------------- /C3/practice/3.14/README.md: -------------------------------------------------------------------------------- 1 | # 3.14 2 | 3 | ```cpp 4 | int test(data_t a) { 5 | return a TEST 0; 6 | } 7 | ``` 8 | 9 | | order | `data_t` | `COMP` | 10 | | - | - | - | 11 | | A | `long` | `>=` | 12 | | B | `short` or `unsigned short` | `==` | 13 | | C | `unsigned char` | `>` | 14 | | D | `int` | `<=` | 15 | -------------------------------------------------------------------------------- /C2/homework/2.66/leftmost_one.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int leftmost_one(unsigned x) { 4 | /* 5 | * first, generate a mask that all bits after leftmost one are one 6 | * e.g. 0xFF00 -> 0xFFFF, and 0x6000 -> 0x7FFF 7 | * If x = 0, get 0 8 | */ 9 | x |= x >> 1; 10 | x |= x >> 2; 11 | x |= x >> 4; 12 | x |= x >> 8; 13 | x |= x >> 16; 14 | return (x >> 1) + (x && 1); 15 | } 16 | int main(){ 17 | return 0; 18 | } -------------------------------------------------------------------------------- /C3/practice/3.22/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fact_int(long n) { 4 | if (n == 1) { 5 | return 1; 6 | } 7 | return n * fact_int(n-1); 8 | } 9 | 10 | long fact_long(long n) { 11 | if (n == 1) { 12 | return 1; 13 | } 14 | return n * fact_long(n-1); 15 | } 16 | 17 | int main() { 18 | long n = 21; 19 | printf("int: %d\n", fact_int(n)); 20 | printf("long: %ld\n", fact_long(n)); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /C2/homework/2.80/threefourths.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int threefourths(int x) { 4 | int64_t mul_res = ((int64_t)x << 1) + x; 5 | int sign_x = (x>>31) & 0x1; 6 | int bias = (~sign_x+1) & ((1<<2)-1); 7 | int res = (mul_res + bias)>>2; 8 | return res; 9 | } 10 | 11 | int main(){ 12 | int TMin = 1<<31; 13 | int TMax = TMin + ~0; 14 | printf("%d\n", threefourths(TMax)); 15 | printf("%x\n", 3 * (TMax) / 4); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /C2/practice/2.5/README.md: -------------------------------------------------------------------------------- 1 | # 2.5 2 | 3 | First, please read the c file: [show_bytes](../../note/storage/show_bytes.c) 4 | 5 | ```cpp 6 | int val = 0x87654321; 7 | byte_pointer valp = (byte_pointer) &val; 8 | show_bytes(valp,1); // A 9 | show_bytes(valp,2); // B 10 | show_bytes(valp,3); // C 11 | ``` 12 | 13 | A. Little endian: `21`, Big endian: `87`. 14 | B. Little endian: `21 43`, Big endian: `87 65`. 15 | C. Little endian: `21 43 65`, Big endian: `87 65 43`. 16 | -------------------------------------------------------------------------------- /C2/homework/2.58/README.md: -------------------------------------------------------------------------------- 1 | # 2.58 2 | 3 | You can run [is_little_endian](./is_little_endian.c) 4 | 5 | ```cpp 6 | #include 7 | 8 | int main(){ 9 | int num = 0x01; 10 | printf("%x", ((char*)&num)[0]); 11 | return ((char*)&num)[0]; 12 | } 13 | ``` 14 | 15 | If your computer is little-endian, then `num` insides of computer is `0x01 00 00 00`. else if is big-endian, it's `0x00 00 00 01`. 16 | 17 | So we just need judge the first number in memory. 18 | -------------------------------------------------------------------------------- /C2/practice/2.16/README.md: -------------------------------------------------------------------------------- 1 | # 2.16 2 | 3 | |x | x<<3| x>>2(logical)|x>>2(arithmetic)| 4 | | -| -| -| -| 5 | |0xC3 0b1100 0011|0x18 0b0001 1000|0x30 0b0011 0000|0xF0 0b1111 0000| 6 | |0x75 0b0111 0101|0xA8 0b1010 1000|0x1D 0b0001 1101|0x1D 0b0001 1101| 7 | |0x87 0b1000 0111|0x38 0b0011 1000|0x21 0b0010 0001|0xE1 0b1110 0001| 8 | |0x66 0b0110 0110|0x30 0b0011 0000|0x19 0b0001 1001|0x19 0b0001 1001| 9 | -------------------------------------------------------------------------------- /C2/practice/2.6/README.md: -------------------------------------------------------------------------------- 1 | # 2.6 2 | 3 | A. 0x00359141: `0000 0000 0011 0101 1001 0001 0100 0001`. 4 | 5 | 0x4A564504:`0100 1010 0101 0110 0100 0101 0000 0100`. 6 | 7 | B. 21 bits match. 8 | 9 | C. 10 | 11 | `0000 000000 1101 0110 0100 0101 0000 01` 12 | 13 |      `0100 1010 0101 0110 0100 0101 0000 0100` 14 | 15 | We find all bits of the integer embedded in the floating-point number, except for the most significant bit having value 1. 16 | -------------------------------------------------------------------------------- /C4/practice/4.4/README.md: -------------------------------------------------------------------------------- 1 | # 4.4 2 | 3 | ```x86asm 4 | ;long rsum(long *start, long count) 5 | ; start in %rdi, count in %rsi 6 | 7 | rsum: 8 | xorq %rax, %rax 9 | andq %rsi, %rsi 10 | jle .L2 11 | pushq %rbx 12 | mrmovq (%rdi), %rbx 13 | irmovq $1, %r8 14 | irmovq $8, %r9 15 | subq %r8, %rsi ; count-- 16 | addq %r9, %rdi ; start++ 17 | call rsum 18 | addq %rbx, %rax 19 | popq %rbx 20 | .L2: 21 | ret 22 | 23 | ``` 24 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog5.yo: -------------------------------------------------------------------------------- 1 | | # prog5: Load/use hazard 2 | 0x000: 30f28000000000000000 | irmovq $128,%rdx 3 | 0x00a: 30f10300000000000000 | irmovq $3,%rcx 4 | 0x014: 40120000000000000000 | rmmovq %rcx, 0(%rdx) 5 | 0x01e: 30f30a00000000000000 | irmovq $10,%rbx 6 | 0x028: 50020000000000000000 | mrmovq 0(%rdx), %rax # Load %rax 7 | 0x032: 6030 | addq %rbx,%rax # Use %rax 8 | 0x034: 00 | halt 9 | -------------------------------------------------------------------------------- /C6/homework/6.29/README.md: -------------------------------------------------------------------------------- 1 | # 6.29 2 | 3 | A. 4 | 5 | | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 6 | | - | - | - | - | - | - | - | - | - | - | - | - | - | 7 | | CT | CT | CT | CT | CT | CT | CT | CT | CT | CI | CI | CO | CO | 8 | 9 | B. 10 | 11 | |operation | addr | hit? | value(or unknown)| 12 | | - | - | - | - | 13 | | read | 0x834 | No | - | 14 | | write | 0x836 | Yes | unknown| 15 | | read | 0xFFD | Yes | 0xC0 | 16 | 17 | -------------------------------------------------------------------------------- /C2/homework/2.92/float_negate.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | /* compute -f. if f is NaN, then return f*/ 6 | float_bits float_negate(float_bits f) { 7 | unsigned sign = f>>31; 8 | unsigned exp = f>>23 & 0xFF; 9 | unsigned frac = f & 0x7fffff; 10 | if( exp == 0xff && frac) { 11 | return f; 12 | } 13 | sign = !sign; 14 | return (sign<<31) | (f & 0x7fffffff); 15 | } 16 | 17 | int main(){ 18 | return 0; 19 | } -------------------------------------------------------------------------------- /labs/attack/result5.txt: -------------------------------------------------------------------------------- 1 | 00 00 00 00 00 00 00 00 2 | 00 00 00 00 00 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 00 00 00 00 6 | cc 19 40 00 00 00 00 00 7 | 20 00 00 00 00 00 00 00 8 | 42 1a 40 00 00 00 00 00 9 | 69 1a 40 00 00 00 00 00 10 | 27 1a 40 00 00 00 00 00 11 | 06 1a 40 00 00 00 00 00 12 | c5 19 40 00 00 00 00 00 13 | d6 19 40 00 00 00 00 00 14 | c5 19 40 00 00 00 00 00 15 | fa 18 40 00 00 00 00 00 16 | 35 39 62 39 39 37 66 61 -------------------------------------------------------------------------------- /C2/homework/2.63/srl-sra.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned srl(unsigned x, int k) { 4 | unsigned xsra = (int)x >> k; 5 | int w = sizeof(int)<<3; 6 | int mask = ((int)-1 << (w-k)); 7 | return xsra & ~mask; 8 | } 9 | int sra(int x, int k) { 10 | int xsrl = (unsigned)x >> k; 11 | int w = sizeof(int) << 3; 12 | int mask = ((int)-1 << (w-k)); 13 | mask = mask & (!(x&(1<<(w-1)))-1); 14 | return mask | xsrl; 15 | } 16 | int main(){ 17 | return 0; 18 | } -------------------------------------------------------------------------------- /C4/practice/4.28/README.md: -------------------------------------------------------------------------------- 1 | # 4.28 2 | 3 | | order | position | throughput | latency | 4 | | - | - | - | - | 5 | | A | after C | 5.26 GIPS | 380ps | 6 | | B | after B and after D | 7.69 GIPS | 390ps | 7 | | C | after A and after C and after D | 9.09 GIPS | 440ps | 8 | 9 | D. The optimal design is five-stage pipeline(insert after A and after B and after C and after D), the latency is 500ps, but the throughput is 1/(100e-12) 10GIPS. 10 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog6.ys: -------------------------------------------------------------------------------- 1 | # Demonstration of return 2 | # /* $begin prog6-ys */ 3 | # prog6 4 | irmovq stack,%rsp # Initialize stack pointer 5 | call proc # Procedure call 6 | irmovq $10,%rdx # Return point 7 | halt 8 | .pos 0x20 9 | proc: # proc: 10 | ret # Return immediately 11 | rrmovq %rdx,%rbx # Not executed 12 | .pos 0x30 13 | stack: # stack: Stack pointer 14 | # /* $end prog6-ys */ 15 | -------------------------------------------------------------------------------- /C2/practice/2.1/README.md: -------------------------------------------------------------------------------- 1 | # 2.1 2 | 3 | Perform the following number conversions: 4 | 5 | A. 0x39A7F8 to binary: `0b0011 1001 1010 0111 1111 1000`. 6 | 7 | B. binary 1100 1001 0111 1011 to hex: `0xC97B`. 8 | 9 | C. 0xD5E4C to bin: `0b1101 0101 1110 0100 1100`. 10 | 11 | D. binary 10 0110 1110 0111 1011 0101 to hex: `0x26E7B5`. 12 | 13 | Pay attention! In Hex, if the total number of bits is not a multiple of 4, you should make the *leftmost* group be the one width fewer than 4 bits. 14 | -------------------------------------------------------------------------------- /C2/practice/2.21/README.md: -------------------------------------------------------------------------------- 1 | # 2.21 2 | 3 | |Expression | Type | evaluation| 4 | | -| -| -| 5 | | -2147483647 - 1 == 2147483648U | unsigned | 1 | 6 | | -2147483647 - 1 < 2147483647 | signed | 1 | 7 | | -2147483647 - 1u < 2147483647 | unsigned | 0 | 8 | | -2147483647 - 1 < -2147483647 | signed | 1 | 9 | | -2147483647 - 1u < -2147483647 | unsigned | 1 | 10 | -------------------------------------------------------------------------------- /C2/practice/2.36/README.md: -------------------------------------------------------------------------------- 1 | # 2.36 2 | 3 | ```cpp 4 | /* Determine whether arguments can be multiplied without overflow */ 5 | int tmult_ok(int32_t x, int32_t y) { 6 | int64_t p = x * y; 7 | return !((p >= ((1<<31) - 1)) || (p < 1<<31)) ; 8 | } 9 | ``` 10 | 11 | or other solution: 12 | 13 | ```cpp 14 | /* Determine whether arguments can be multiplied without overflow */ 15 | int tmult_ok(int32_t x, int32_t y) { 16 | int64_t p = x * y; 17 | return p == (int32_t)p; 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /C3/homework/3.71/README.md: -------------------------------------------------------------------------------- 1 | # 3.71 2 | 3 | ```cpp 4 | #include 5 | #include 6 | #define BUF_SIZE 12 7 | 8 | void good_echo(void) { 9 | char buf[BUF_SIZE]; 10 | while(1) { 11 | /* function fgets is interesting */ 12 | char* p = fgets(buf, BUF_SIZE, stdin); 13 | if (p == NULL) { 14 | break; 15 | } 16 | printf("%s", p); 17 | } 18 | return; 19 | } 20 | 21 | int main(int argc, char* argv[]) { 22 | good_echo(); 23 | return 0; 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /C4/practice/4.17/README.md: -------------------------------------------------------------------------------- 1 | # 4.17 2 | 3 | | Stage | `cmovq rA, rB` | 4 | | - | - | 5 | | Fetch | `icode:ifun <- M1[PC]` | 6 | | | `rA:rB <- M1[PC+1]` | 7 | | | `valP <- PC + 2` | 8 | | Decode | `valA <- R[rA]` | 9 | | Execute| `valE <- 0 + valA` | 10 | | | `Cnd <- Cond(CC, ifun)` | 11 | | Memory | | 12 | | Write back | `if(Cnd) R[rB] <- valE`| 13 | | PC update | `PC <- valP` | 14 | -------------------------------------------------------------------------------- /C4/practice/4.18/README.md: -------------------------------------------------------------------------------- 1 | # 4.18 2 | 3 | | Stage | `call 0x041` | 4 | | - | - | 5 | | Fetch | `icode:ifun <- M1[0x37] = 8:0` | 6 | | | `valC <- M8[0x38] = 0x41` | 7 | | | `valP <- PC + 9 = 0x40` | 8 | | Decode | `valB <- R[%rsp] = 128` | 9 | | Execute| `valE <- valB - 8 = 120` | 10 | | Memory | `M8[valE] = valP = 0x40` | 11 | | Write back | `R[%rsp] <- valE = 120` | 12 | | PC update | `PC <- 0x41` | 13 | -------------------------------------------------------------------------------- /C4/practice/4.6/README.md: -------------------------------------------------------------------------------- 1 | # 4.6 2 | 3 | ```x86asm 4 | ;long abssum(long *start, long count) 5 | ; start in %rdi, count in %rsi 6 | 7 | abssum: 8 | irmovq: $8, %r8 9 | irmovq: $1, %r9 10 | xorq %rax, %rax 11 | andq %rsi, %rsi 12 | jmp test 13 | loop: 14 | mrmovq (%rdi), %r10 15 | xorq %r11, %r11 16 | subq %r10, %r11 17 | cmovg %r11, %r10 18 | addq %r10, %rax 19 | addq %r8, %rdi 20 | subq %r9, %rsi 21 | test: 22 | jne loop 23 | ret 24 | ``` 25 | -------------------------------------------------------------------------------- /C6/homework/6.25/README.md: -------------------------------------------------------------------------------- 1 | # 6.25 2 | 3 | | Cache | m | C | B | E | S | t | s | b | 4 | | - | - | - | - | - | - | - | - | - | 5 | | 1 | 32 | 1024 | 4 | 4 | 64 | 24 | 6 | 2 | 6 | | 2 | 32 | 1024 | 4 | 256| 1 | 30 | 0 | 2 | 7 | | 3 | 32 | 1024 | 8 | 1 | 128 | 22 | 7 | 3 | 8 | | 4 | 32 | 1024 | 8 | 128| 1 | 29 | 0 | 3 | 9 | | 5 | 32 | 1024 | 32 | 1 | 32 | 22 | 5 | 5 | 10 | | 6 | 32 | 1024 | 32 | 4 | 8 | 24 | 3 | 5 | 11 | -------------------------------------------------------------------------------- /C3/practice/3.36/README.md: -------------------------------------------------------------------------------- 1 | # 3.36 2 | 3 | |Array | Element size| Total size| Start address| Element i | 4 | | - | - | - | - | - | 5 | | S | 2 | 14 | x | x + 2 * i | 6 | | T | 8 | 24 | x | x + 8 * i | 7 | | U | 8 | 48 | x | x + 8 * i | 8 | | V | 4 | 32 | x | x + 4 * i | 9 | | W | 8 | 32 | x | x + 8 * i | 10 | -------------------------------------------------------------------------------- /C2/practice/2.26/README.md: -------------------------------------------------------------------------------- 1 | # 2.26 2 | 3 | ```cpp 4 | size_t strlen(const char *s); 5 | 6 | int strlonger(char *s, char *t) { 7 | return strlen(s) - strlen(t) > 0; 8 | } 9 | ``` 10 | 11 | `size_t` is defined via `typeof` to be `unsigned`. Find bug of the function `strlonger`. 12 | 13 | A. when `strlen(s) < strlen(t)`. 14 | B. the sub of type `unsgned` is always greater or equally 0. 15 | C. 16 | 17 | ```cpp 18 | int strlonger(char *s, char *t) { 19 | return strlen(s) > strlen(t); 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /C3/note/formats/exchange.asm: -------------------------------------------------------------------------------- 1 | 2 | exchange.o: file format Mach-O 64-bit x86-64 3 | 4 | 5 | Disassembly of section __TEXT,__text: 6 | 7 | 0000000000000000 _exchange: 8 | 0: 55 pushq %rbp 9 | 1: 48 89 e5 movq %rsp, %rbp 10 | 4: 48 8b 07 movq (%rdi), %rax 11 | 7: 48 89 37 movq %rsi, (%rdi) 12 | a: 5d popq %rbp 13 | b: c3 retq 14 | -------------------------------------------------------------------------------- /C3/practice/3.31/README.md: -------------------------------------------------------------------------------- 1 | # 3.31 2 | 3 | ```cpp 4 | void switcher(long a, long b, long c, long* dest) { 5 | long val = 0; 6 | switch (a) { 7 | case 5: 8 | c = b ^ 15; 9 | case 0: 10 | val = 112 + c; 11 | break; 12 | case 2: 13 | case 7: 14 | val = (b + c) << 2; 15 | break; 16 | case 4: 17 | val = a; 18 | break; 19 | default: // a > 7, l2 20 | val = b; 21 | } 22 | *dest = val; 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /C3/practice/3.8/README.md: -------------------------------------------------------------------------------- 1 | # 3.8 2 | 3 | | Instruction | Destination | Value | 4 | | - | - | - | 5 | | `addq %rcx, (%rax)` | 0x100 | 0x100 | 6 | | `subq %rdx, 8(%rax)` | 0x108 | 0xA8 | 7 | | `imulq $16, (%rax, %rdx, 8)` | 0x118 | 272 | 8 | | `incq 16(%rax)` | 0x110 | 0x14 | 9 | | `decq %rcx` | %rcx | 0x0 | 10 | | `subq %rdx, %rax` | %rax | 0xFD | 11 | -------------------------------------------------------------------------------- /labs/performance/Makefile: -------------------------------------------------------------------------------- 1 | # Student's Makefile for the CS:APP Performance Lab 2 | TEAM = bovik 3 | VERSION = 1 4 | HANDINDIR = 5 | 6 | CC = gcc 7 | CFLAGS = -Wall -O2 -m32 8 | LIBS = -lm 9 | 10 | OBJS = driver.o kernels.o fcyc.o clock.o 11 | 12 | all: driver 13 | 14 | driver: $(OBJS) fcyc.h clock.h defs.h config.h 15 | $(CC) $(CFLAGS) $(OBJS) $(LIBS) -o driver 16 | 17 | handin: 18 | cp kernels.c $(HANDINDIR)/$(TEAM)-$(VERSION)-kernels.c 19 | 20 | clean: 21 | -rm -f $(OBJS) driver core *~ *.o 22 | 23 | 24 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/ret-hazard.ys: -------------------------------------------------------------------------------- 1 | /* $begin ret-hazard-ys */ 2 | # Test instruction that modifies %esp followed by ret 3 | irmovq mem,%rbx 4 | mrmovq 0(%rbx),%rsp # Sets %rsp to point to return point 5 | ret # Returns to return point 6 | halt # 7 | rtnpt: irmovq $5,%rsi # Return point 8 | halt 9 | .pos 0x40 10 | mem: .quad stack # Holds desired stack pointer 11 | .pos 0x50 12 | stack: .quad rtnpt # Top of stack: Holds return point 13 | /* $end ret-hazard-ys */ 14 | -------------------------------------------------------------------------------- /C3/practice/3.30/README.md: -------------------------------------------------------------------------------- 1 | # 3.30 2 | 3 | ```cpp 4 | void switch2(long x, long* dest) { 5 | long val = 0; 6 | switch (x) { 7 | case -1: 8 | L9 9 | break; 10 | case 1: 11 | L6 12 | break; 13 | case 2: 14 | case 4: 15 | L7 16 | break; 17 | case 5: 18 | L8 19 | break; 20 | case 0: 21 | case 7: 22 | L5 23 | break; 24 | default: 25 | L2 26 | } 27 | *dest = val; 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /C4/practice/4.5/README.md: -------------------------------------------------------------------------------- 1 | # 4.5 2 | 3 | ```x86asm 4 | ;long abssum(long *start, long count) 5 | ; start in %rdi, count in %rsi 6 | 7 | abssum: 8 | irmovq: $8, %r8 9 | irmovq: $1, %r9 10 | xorq %rax, %rax 11 | andq %rsi, %rsi 12 | jmp test 13 | loop: 14 | mrmovq (%rdi), %r10 15 | xorq %r11, %r11 16 | subq %r10, %r11 17 | jle pos 18 | rrmovq %r11, %r10 19 | pos: 20 | addq %r10, %rax 21 | addq %r8, %rdi 22 | subq %r9, %rsi 23 | test: 24 | jne loop 25 | ret 26 | ``` 27 | -------------------------------------------------------------------------------- /C2/note/operations/logical.md: -------------------------------------------------------------------------------- 1 | # Logical Operations in C 2 | 3 | C also provides a set of **logical operations**: ||, &&, ~, which correspond to the OR, AND, NOT. 4 | 5 | These can easily be confused with the *bit-level operations*, but their behavior is quite different. 6 | 7 | The logical operations return either 1 or 0 in C language, indicating a result of either *true* or *false* respectively. 8 | 9 | Here are some practice: 10 | 11 | - [2.14](../../practice/2.14/README.md) 12 | - [2.15](../../practice/2.15/README.md) 13 | -------------------------------------------------------------------------------- /C3/homework/3.60/README.md: -------------------------------------------------------------------------------- 1 | # 3.60 2 | 3 | A. `x`: `%rdi`, `n`: `%ecx` and `esi`, `result`:`%eax`, `mask`:`%rdx`. 4 | 5 | B. `result` is 0, `mask` is 1. 6 | 7 | C. `mask != 0`; 8 | 9 | D. `mask <<= n;` 10 | 11 | E. `result |= (x & mask);` 12 | 13 | F. 14 | 15 | ```cpp 16 | long loop (long x, int n) { 17 | // x in %rdi 18 | // n in %esi 19 | long result = 0; 20 | long mask; 21 | for (mask = 1; mask != 0; mask <<= n) { 22 | result |= (x & mask); 23 | } 24 | return result; 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /C5/practice/5.9/merge.c: -------------------------------------------------------------------------------- 1 | void merge(long src1[], long src2[], long dest[], long n) { 2 | long i1 = 0; 3 | long i2 = 0; 4 | long id = 0; 5 | while (i1 < n && i2 < n) { 6 | long v1 = src1[i1]; 7 | long v2 = src2[i2]; 8 | long tack1 = v1 < v2; 9 | dest[id++] = tack1 ? v1 : v2; 10 | i1 += tack1; 11 | i2 += 1 - tack1; 12 | } 13 | 14 | while (i1 < n) { 15 | dest[id++] = src1[i1++]; 16 | } 17 | while (i2 < n) { 18 | dest[id++] = src2[i2++]; 19 | } 20 | } -------------------------------------------------------------------------------- /C6/note/locality/README.md: -------------------------------------------------------------------------------- 1 | # Locality 2 | 3 | Locality means the computer tend to reference data items that are near other recently referenced data items or that were referenced themselves. 4 | 5 | - temporal locality: programs that repeatedly reference the same variables enjoy good temporal locality. 6 | - spatial locality: for programs with stride-k reference patterns, the smaller the stride, the better the spatial locality. 7 | 8 | Practices: 9 | 10 | - [6.7](../../practice/6.7/README.md) 11 | - [6.8](../../practice/6.8/README.md) 12 | -------------------------------------------------------------------------------- /C2/practice/2.35/README.md: -------------------------------------------------------------------------------- 1 | # 2.35 2 | 3 | ```cpp 4 | /* Determine whether arguments can be multiplied without overflow */ 5 | int tmult_ok(int x, int y) { 6 | int p = x * y; 7 | return !x || p/x == y; 8 | } 9 | ``` 10 | 11 | 1. For x and y such that $TMin \le x,y \le TMax$: $x *y = U2T(( x* y ) mod 2^w) = p + t * 2^w$, if positive overflow, $ t = -1$, else if negative overflow, $t = 1$. 12 | 2. $r$ is remainder. 13 | 3. if $q$ equal $y$, then $x*y - t*2^w = x *y + r$, then $r + t*w^ = 0$, And $|r|< |x| < 2^w$, so $t=0$, then $r=0$. 14 | -------------------------------------------------------------------------------- /C3/homework/3.63/README.md: -------------------------------------------------------------------------------- 1 | # 3.63 2 | 3 | ```cpp 4 | long switch_prob(long x, long n) { 5 | long result = x; 6 | switch (n) { 7 | case 60: 8 | case 62: 9 | result = x * 8; 10 | break; 11 | case 63: 12 | result = x >> 3; 13 | break; 14 | case 64: 15 | x = x<<4 - x 16 | case 65: 17 | x = x *x; 18 | default: 19 | result = x + 0x4b; 20 | break; 21 | } 22 | return result; 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog9.yo: -------------------------------------------------------------------------------- 1 | | # Exception handling 2 | | # /* $begin prog9-yo */ 3 | 0x000: 6300 | xorq %rax,%rax 4 | 0x002: 741600000000000000 | jne target # Not taken 5 | 0x00b: 30f00100000000000000 | irmovq $1, %rax # Fall through 6 | 0x015: 00 | halt 7 | 0x016: | target: 8 | 0x016: ff | .byte 0xFF # Invalid instruction code 9 | | # /* $end prog9-yo */ 10 | -------------------------------------------------------------------------------- /C4/practice/4.2/README.md: -------------------------------------------------------------------------------- 1 | # 4.2 2 | 3 | A. 4 | 5 | ```x86asm 6 | irmovq $-4, %rbx 7 | rmmovq %rsi, 0x800(%rbx) 8 | halt 9 | ``` 10 | 11 | B. 12 | 13 | ```x86asm 14 | pushq %rsi 15 | call 0x20c 16 | halt 17 | irmovq $10, %rbx 18 | ret 19 | ``` 20 | 21 | C. 22 | 23 | ```x86asm 24 | mrmovq 0x7(%rbp), %rbp 25 | nop 26 | .byte f0 ; invalid byte 27 | pop %rcx 28 | ``` 29 | 30 | D. 31 | 32 | ```x86asm 33 | subq %rcx, %rbx 34 | je 0x400 35 | halt 36 | ``` 37 | 38 | E. 39 | 40 | ```x86asm 41 | xorq %rsi, %rdx 42 | .byte a0f0 ; invalid byte 43 | ``` 44 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/pushtest.yo: -------------------------------------------------------------------------------- 1 | | # Test of Push semantics for Y86-64 2 | 0x000: 30f40001000000000000 | irmovq $0x100,%rsp # Initialize stack pointer 3 | 0x00a: 2040 | rrmovq %rsp,%rax # Save stack pointer 4 | 0x00c: a04f | pushq %rsp # Push the stack pointer (old or new?) 5 | 0x00e: b02f | popq %rdx # Get it back 6 | 0x010: 6120 | subq %rdx,%rax # Compute difference. Either 0 (old) or 4 (new). 7 | 0x012: 00 | halt 8 | -------------------------------------------------------------------------------- /C4/practice/4.13/README.md: -------------------------------------------------------------------------------- 1 | # 4.13 2 | 3 | | Stage | `irmovq $128, %rsp` | 4 | | - | - | 5 | | Fetch | `icode:ifun <- M1[0x16] = 3:0` | 6 | | | `rA:rB <- M1[0x17] = f:4` | 7 | | | `valC <- M8[0x18] = 0x80` | 8 | | | `valP <- PC + 10 = 0x20` | 9 | | Decode | | 10 | | Execute| `valE <- 0 + valC = 0x80` | 11 | | Memory | | 12 | | Write back | `R[%rsp] <- valE = 0x80` | 13 | | PC update | `PC <- valP = 0x20` | 14 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/outgen.h: -------------------------------------------------------------------------------- 1 | /* Output generator that ensures no line exceeds specified number of columns */ 2 | 3 | /* Controlling parameters */ 4 | void outgen_init(FILE *outfile, int max_column, int first_indent, int other_indents); 5 | 6 | /* Terminate statement and reset indentations */ 7 | void outgen_terminate(); 8 | 9 | /* Output generator printing */ 10 | void outgen_print(char *fmt, ...); 11 | 12 | /* Increase indentation level */ 13 | void outgen_upindent(); 14 | /* Decrease indentation level */ 15 | void outgen_downindent(); 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /C2/homework/2.90/README.md: -------------------------------------------------------------------------------- 1 | # 2.90 2 | 3 | ```cpp 4 | // compute 2^x. 5 | float fpwr2(int x) { 6 | unsigned exp, frac; 7 | unsigned u; 8 | 9 | if( x < -149) { 10 | // to small 11 | exp = 0; 12 | frac = 0; 13 | } else if( x < -126) { 14 | exp = 0; 15 | frac = 1 << (23 + x + 126); 16 | } else if ( x < 128) { 17 | exp = x + 127; 18 | frac = 0; 19 | } else { 20 | exp = 255; 21 | frac = 0; 22 | } 23 | 24 | u = exp << 23 | frac; 25 | return u2f(u); 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog10.yo: -------------------------------------------------------------------------------- 1 | | # prog10 2 | 0x000: 30f00100000000000000 | irmovq $1,%rax 3 | 0x00a: 6344 | xorq %rsp,%rsp # Set stack pointer to 0 and CC to 100 4 | 0x00c: a00f | pushq %rax # Attempt to write to 0xfffffffffffffff8 5 | 0x00e: 6000 | addq %rax,%rax # (Should not be executed) Would set CC to 000 6 | 0x010: 30f00200000000000000 | irmovq $2, %rax # Not executed 7 | 0x01a: 30f00300000000000000 | irmovq $3, %rax # Not executed 8 | -------------------------------------------------------------------------------- /C5/practice/5.9/README.md: -------------------------------------------------------------------------------- 1 | # 5.9 2 | 3 | ```cpp 4 | void merge(long src1[], long src2[], long dest[], long n) { 5 | long i1 = 0; 6 | long i2 = 0; 7 | long id = 0; 8 | while (i1 < n && i2 < n) { 9 | long v1 = src1[i1]; 10 | long v2 = src2[i2]; 11 | long tack1 = v1 < v2; 12 | dest[id++] = tack1 ? v1 : v2; 13 | i1 += tack1; 14 | i2 += 1 - tack1; 15 | } 16 | 17 | while (i1 < n) { 18 | dest[id++] = src1[i1++]; 19 | } 20 | while (i2 < n) { 21 | dest[id++] = src2[i2++]; 22 | } 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /C2/practice/2.52/README.md: -------------------------------------------------------------------------------- 1 | # 2.52 2 | 3 | 7-bit floating-point representations based on the IEEE floating-point format. 4 | 5 | Format A: k = 3 exp bits, bias is 3, n = 4 fraction bits. 6 | 7 | | Bits | Value | 8 | | 011 0000 | 1 | 9 | | 101 1110 | 15/2 | 10 | | 010 1001 | 25/32 | 11 | | 110 1111 | 31/2 | 12 | | 000 0001 | 1/64 | 13 | 14 | Format B: k = 4 exp bits, bias is 7, n = 3 fraction bits. 15 | 16 | | Bits | Value | 17 | | 0111 000 | 1 | 18 | | 1001 111 | 15/2 | 19 | | 0110 100 | 3/4 | 20 | | 1011 000 | 16 | 21 | | 0001 000 | 1/64 | 22 | -------------------------------------------------------------------------------- /C4/practice/4.32/README.md: -------------------------------------------------------------------------------- 1 | # 4.32 2 | 3 | ```x86asm 4 | ; cycle 0 1 2 3 4 5 6 7 8 5 | irmovq $5, %rdx ; F E D M W 6 | irmovq $0x100, %rsp ; F E D M W 7 | rmmovq %rdx,0(%rsp) ; F E D M W 8 | popq %rsp ; F E D M W 9 | rrmovq %rsp, %rax ; F E D M W 10 | ``` 11 | 12 | If the two cases were reversed, then the write back from `M_valE` would take priority, causing the incremented stack pointer to be passed as the argument to the `rrmovq` instruction. 13 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/cjr.ys: -------------------------------------------------------------------------------- 1 | # /* $begin cjr-ys */ 2 | # Code to generate a combination of not-taken branch and ret 3 | irmovq Stack, %rsp 4 | irmovq rtnp,%rax 5 | pushq %rax # Set up return pointer 6 | xorq %rax,%rax # Set Z condition code 7 | jne target # Not taken (First part of combination) 8 | irmovq $1,%rax # Should execute this 9 | halt 10 | target: ret # Second part of combination 11 | irmovq $2,%rbx # Should not execute this 12 | halt 13 | rtnp: irmovq $3,%rdx # Should not execute this 14 | halt 15 | .pos 0x40 16 | Stack: 17 | # /* $end cjr-ys */ 18 | -------------------------------------------------------------------------------- /C4/practice/4.14/README.md: -------------------------------------------------------------------------------- 1 | # 4.14 2 | 3 | | Stage | `popq $128, %rsp` | 4 | | - | - | 5 | | Fetch | `icode:ifun <- M1[0x2c] = b:0` | 6 | | | `rA:rB <- M1[0x2d] = 0:f` | 7 | | | `valP <- PC + 2 = 0x2e` | 8 | | Decode | `valA <- R[%rsp] = 120` | 9 | | | `valB <- R[%rsp] = 120` | 10 | | Execute| `valE <- valB + 8 = 128` | 11 | | Memory | `valM <- M8[valA] = 9` | 12 | | Write back | `R[%rsp] <- valE = 128` | 13 | | | `R[%rax] <- valM = 9` | 14 | | PC update | `PC <- valP = 0x2e` | 15 | -------------------------------------------------------------------------------- /C2/homework/2.74/tsub_ok.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int tsub_ok(int x, int y) { 4 | int sub = x - y; 5 | int sign_x = (x>>31) & 0x1; 6 | int sign_y = (y>>31) & 0x1; 7 | int sign_sub = (sub>>31) & 0x1; 8 | int positive_overflow = !sign_x && sign_y && sign_sub; 9 | int negative_overflow = sign_x && !sign_y && !sign_sub; 10 | int y_is_TMin = !(y ^ (1<<31)); 11 | return !y_is_TMin && !(positive_overflow || negative_overflow); 12 | } 13 | 14 | int main(){ 15 | int TMin = 1 << 31; 16 | int TMax = TMin + ~0; 17 | printf("%x", tsub_ok(TMax, -0x01)); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C3/homework/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 HomeWork Problems 2 | 3 | - [3.58](./3.58/README.md) 4 | - [3.59](./3.59/README.md) 5 | - [3.60](./3.60/README.md) 6 | - [3.61](./3.61/README.md) 7 | - [3.62](./3.62/README.md) 8 | - [3.63](./3.63/README.md) 9 | - [3.64](./3.64/README.md) 10 | - [3.65](./3.65/README.md) 11 | - [3.66](./3.66/README.md) 12 | - [3.67](./3.67/README.md) 13 | - [3.68](./3.68/README.md) 14 | - [3.69](./3.69/README.md) 15 | - [3.70](./3.70/README.md) 16 | - [3.71](./3.71/README.md) 17 | - [3.72](./3.72/README.md) 18 | - [3.73](./3.73/README.md) 19 | - [3.74](./3.74/README.md) 20 | - [3.75](./3.75/README.md) 21 | -------------------------------------------------------------------------------- /C3/note/formats/exchange.s: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .build_version macos, 10, 15 sdk_version 10, 15, 6 3 | .globl _exchange ## -- Begin function exchange 4 | .p2align 4, 0x90 5 | _exchange: ## @exchange 6 | .cfi_startproc 7 | ## %bb.0: 8 | pushq %rbp 9 | .cfi_def_cfa_offset 16 10 | .cfi_offset %rbp, -16 11 | movq %rsp, %rbp 12 | .cfi_def_cfa_register %rbp 13 | movq (%rdi), %rax 14 | movq %rsi, (%rdi) 15 | popq %rbp 16 | retq 17 | .cfi_endproc 18 | ## -- End function 19 | .subsections_via_symbols 20 | -------------------------------------------------------------------------------- /C3/homework/3.67/README.md: -------------------------------------------------------------------------------- 1 | # 3.67 2 | 3 | A. before calling `process`. 4 | 5 | ``` 6 | |stack| 7 | | ... | 8 | | z | 9 | | &z | 10 | | y | 11 | | x | <- %rsp 12 | ``` 13 | 14 | B. The first address of `s`, `%rsp + 64`. 15 | 16 | C. `%rsp + offset` 17 | 18 | D. `u[0]` in `%rdi`, `u[1]` in `%rdi + 8`, `y` in `%rdi + 16`, and return `%rdi`. 19 | 20 | E. return from `process`. 21 | 22 | ``` 23 | |stack| 24 | | ... | 25 | | z | 26 | | x | 27 | | y | <- %rdi 28 | | ... | 29 | | z | 30 | | &z | 31 | | y | 32 | | x | `eval`'s %rsp 33 | | | `process`'s %rsp 34 | ``` 35 | 36 | F. The stack address. 37 | -------------------------------------------------------------------------------- /C6/homework/6.35/README.md: -------------------------------------------------------------------------------- 1 | # 6.35 2 | 3 | `dst` array: 4 | 5 | | | Col.0 | Col.1 | Col.2 | Col.3 | 6 | | - | - | - | - | - | 7 | | Row 0 | m | h | h | h | 8 | | Row 1 | m | h | h | h | 9 | | Row 2 | m | h | h | h | 10 | | Row 3 | m | h | h | h | 11 | 12 | `src` array: 13 | 14 | | | Col.0 | Col.1 | Col.2 | Col.3 | 15 | | - | - | - | - | - | 16 | | Row 0 | m | h | h | h | 17 | | Row 1 | m | h | h | h | 18 | | Row 2 | m | h | h | h | 19 | | Row 3 | m | h | h | h | 20 | -------------------------------------------------------------------------------- /C2/homework/2.73/saturating_add.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int saturating_add(int x, int y) { 4 | int TMin = 1<<31; 5 | int TMax = TMin + ~0; 6 | int res = x + y; 7 | int sign_x = (x>>31) & 0x1; 8 | int sign_y = (y>>31) & 0x1; 9 | int sign_res = (res>>31) & 0x1; 10 | int positive_overflow = !sign_x && !sign_y && sign_res; 11 | int negative_overflow = sign_x && sign_y && !sign_res; 12 | (positive_overflow && (res = TMax)); 13 | (negative_overflow && (res = TMin)); 14 | return res; 15 | } 16 | 17 | 18 | int main(){ 19 | printf("%x", saturating_add(((1<<31) + (~0)), 0x1234)); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /labs/archlab/sim/misc/rsum.ys: -------------------------------------------------------------------------------- 1 | # Execution begins at address 0 2 | .pos 0 3 | irmovq 0x100, %rsp 4 | call main 5 | halt 6 | 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 | 18 | main: 19 | irmovq ele1, %rdi 20 | call rsum_list 21 | ret 22 | 23 | rsum_list: 24 | pushq %rbx 25 | xorq %rax, %rax 26 | andq %rdi, %rdi 27 | je finish 28 | mrmovq (%rdi), %rbx 29 | mrmovq 8(%rdi), %rdi 30 | call rsum_list 31 | addq %rbx, %rax 32 | finish: 33 | popq %rbx 34 | ret 35 | -------------------------------------------------------------------------------- /C2/homework/2.94/float_twice.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | /* compute 2*f. if f is NaN, then return f*/ 6 | float_bits float_twice(float_bits f) { 7 | unsigned sign = f >> 31; 8 | unsigned exp = f>>23 & 0xFF; 9 | unsigned frac = f & 0x7fffff; 10 | if( exp == 0xff) { 11 | return f; 12 | } else if( exp == 0) { //denormalized 13 | frac <<= 1; 14 | } else if( exp == 0xfe) { // 2f is inf 15 | exp = 0xff; 16 | frac = 0; 17 | } else { 18 | exp += 1; 19 | } 20 | return (sign << 31)| (exp << 23) | frac; 21 | } 22 | 23 | int main(){ 24 | return 0; 25 | } -------------------------------------------------------------------------------- /C3/practice/3.23/README.md: -------------------------------------------------------------------------------- 1 | # 3.23 2 | 3 | A. 4 | 5 | | register | values | 6 | | - | - | 7 | | `%rax` | `x` | 8 | | `%rcx` | `y` | 9 | | `%rdx` | `n` | 10 | 11 | B. `leap 1(%rcx, %rax), %rax`. 12 | 13 | C. 14 | 15 | ```x86asm 16 | dw_loop: 17 | movq %rdi, %rax // Copy x to %rax 18 | movq %rdi, %rcx 19 | imulq %rdi, %rcx // y = x * x; 20 | leap (%rdi,%rdi), %rdx 21 | .L2: 22 | leaq 1(%rcx, %rax), %rax // x = x + y + 1; 23 | subq $1, %rdx // n = n - 1; 24 | testq %rdx, %rdx // Test n 25 | jg .L2 // if n > 0, go .L2 26 | rep; ret 27 | ``` 28 | -------------------------------------------------------------------------------- /C6/practice/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 Practice 2 | 3 | - [6.1](./6.1/README.md) 4 | - [6.2](./6.2/README.md) 5 | - [6.3](./6.3/README.md) 6 | - [6.4](./6.4/README.md) 7 | - [6.5](./6.5/README.md) 8 | - [6.6](./6.6/README.md) 9 | - [6.7](./6.7/README.md) 10 | - [6.8](./6.8/README.md) 11 | - [6.9](./6.9/README.md) 12 | - [6.10](./6.10/README.md) 13 | - [6.11](./6.11/README.md) 14 | - [6.12](./6.12/README.md) 15 | - [6.13](./6.13/README.md) 16 | - [6.14](./6.14/README.md) 17 | - [6.15](./6.15/README.md) 18 | - [6.16](./6.16/README.md) 19 | - [6.17](./6.17/README.md) 20 | - [6.18](./6.18/README.md) 21 | - [6.19](./6.19/README.md) 22 | - [6.20](./6.20/README.md) 23 | -------------------------------------------------------------------------------- /labs/performance/clock.h: -------------------------------------------------------------------------------- 1 | /* Routines for using cycle counter */ 2 | 3 | /* Start the counter */ 4 | void start_counter(); 5 | 6 | /* Get # cycles since counter started */ 7 | double get_counter(); 8 | 9 | /* Measure overhead for counter */ 10 | double ovhd(); 11 | 12 | /* Determine clock rate of processor (using a default sleeptime) */ 13 | double mhz(int verbose); 14 | 15 | /* Determine clock rate of processor, having more control over accuracy */ 16 | double mhz_full(int verbose, int sleeptime); 17 | 18 | /** Special counters that compensate for timer interrupt overhead */ 19 | 20 | void start_comp_counter(); 21 | 22 | double get_comp_counter(); 23 | -------------------------------------------------------------------------------- /C5/homework/5.19/presum.c: -------------------------------------------------------------------------------- 1 | /* version 4*1a */ 2 | void psum_4_1a(float a[], float p[], long n) { 3 | long i; 4 | float val, last_val; 5 | float tmp, tmp1, tmp2, tmp3; 6 | last_val = p[0] = a[0]; 7 | 8 | for (i = 1; i < n - 4; i++) { 9 | tmp = last_val + a[i]; 10 | tmp1 = tmp + a[i+1]; 11 | tmp2 = tmp1 + a[i+2]; 12 | tmp3 = tmp2 + a[i+3]; 13 | 14 | p[i] = tmp; 15 | p[i+1] = tmp1; 16 | p[i+2] = tmp2; 17 | p[i+3] = tmp3; 18 | 19 | /* key point */ 20 | last_val = last_val + (a[i] + a[i+1] + a[i+2] + a[i+3]); 21 | } 22 | 23 | for (; i < n; i++) { 24 | last_val += a[i]; 25 | p[i] = last_val; 26 | } 27 | } -------------------------------------------------------------------------------- /C6/practice/6.17/README.md: -------------------------------------------------------------------------------- 1 | # 6.17 2 | 3 | A. 4 | 5 | `dst` array: 6 | 7 | | | Col.0 | Col.1 | 8 | | - | - | - | 9 | | Row 0 | m | m | 10 | | Row 1 | m | m | 11 | 12 | `src` array: 13 | 14 | | | Col.0 | Col.1 | 15 | | - | - | - | 16 | | Row 0 | m | m | 17 | | Row 1 | m | h | 18 | 19 | B. 20 | 21 | `dst` array: 22 | 23 | | | Col.0 | Col.1 | 24 | | - | - | - | 25 | | Row 0 | m | h | 26 | | Row 1 | m | h | 27 | 28 | `src` array: 29 | 30 | | | Col.0 | Col.1 | 31 | | - | - | - | 32 | | Row 0 | m | h | 33 | | Row 1 | m | h | 34 | -------------------------------------------------------------------------------- /labs/archlab/sim/pipe/check-len.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | # Check length of ncopy function in .yo file 4 | # Assumes that function starts with label "ncopy:" 5 | # and finishes with label "End:" 6 | 7 | $startpos = -1; 8 | $endpos = -1; 9 | 10 | while (<>) { 11 | $line = $_; 12 | if ($line =~ /(0x[0-9a-fA-F]+):.* ncopy:/) { 13 | $startpos = hex($1); 14 | } 15 | if ($line =~ /(0x[0-9a-fA-F]+):.* End:/) { 16 | $endpos = hex($1); 17 | } 18 | } 19 | 20 | if ($startpos >= 0 && $endpos > $startpos) { 21 | $len = $endpos - $startpos; 22 | print "ncopy length = $len bytes\n"; 23 | } else { 24 | print "Couldn't determine ncopy length\n"; 25 | } 26 | -------------------------------------------------------------------------------- /C3/practice/3.37/README.md: -------------------------------------------------------------------------------- 1 | # 3.37 2 | 3 | ```cpp 4 | short S[]; // %rdx 5 | long i; // %rcx 6 | ``` 7 | 8 | | Expression | Type | Value | Assembly code | 9 | | - | - | - | - | 10 | | S + 1 | `short*` | x + 2 | `leaq 2(%rdx), %rax` | 11 | | S[3] | `short` | M[x + 6] | `movw 6(%rdx), %ax` | 12 | | &S[i] | `short*` | x + 2 * i | `leaq (%rdx,%rcx,2), %rax` | 13 | | S[4*i+1] | `short` | M[x+8*i+2] | `movw 2(%%rdx,%rcx,8),%ax` | 14 | | S + i - 5 | `short*` | x + 2 * i - 10 | `leaq -10(%%rdx,%rcx,2),%rax`| 15 | -------------------------------------------------------------------------------- /labs/data/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile that builds btest and other helper programs for the CS:APP data lab 3 | # 4 | CC = gcc 5 | CFLAGS = -O -Wall -m32 6 | LIBS = -lm 7 | 8 | all: btest fshow ishow 9 | 10 | btest: btest.c bits.c decl.c tests.c btest.h bits.h 11 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 12 | 13 | fshow: fshow.c 14 | $(CC) $(CFLAGS) -o fshow fshow.c 15 | 16 | ishow: ishow.c 17 | $(CC) $(CFLAGS) -o ishow ishow.c 18 | 19 | # Forces a recompile. Used by the driver program. 20 | btestexplicit: 21 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 22 | 23 | clean: 24 | rm -f *.o btest fshow ishow *~ 25 | 26 | 27 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/sum.ys: -------------------------------------------------------------------------------- 1 | .pos 0 2 | irmovq 0x100, %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 | 22 | sum_list: 23 | pushq %r8 24 | irmovq $8, %r8 25 | xorq %rax, %rax 26 | andq %rdi, %rdi 27 | jmp test 28 | loop: 29 | mrmovq (%rdi), %rsi 30 | addq %rsi, %rax 31 | addq %r8, %rdi 32 | mrmovq (%rdi), %rdi 33 | andq %rdi, %rdi 34 | test: 35 | jne loop 36 | popq %r8 37 | ret 38 | -------------------------------------------------------------------------------- /C2/practice/2.11/README.md: -------------------------------------------------------------------------------- 1 | # 2.11 2 | 3 | First, please read the c file: [inplace_swap](../2.10/inplace_swap.c) 4 | 5 | consider following program: 6 | 7 | ```cpp 8 | void reverse_array(int a[], int cnt) { 9 | int first, last; 10 | for ( first = 0, last = cnt - 1; first <= last; first++, last--) { 11 | inplace_swap(&a[first], &a[last]); 12 | } 13 | } 14 | ``` 15 | 16 | A. The value of `first` and `last` both is `k`. 17 | 18 | B. When cnt = 2k+1, in the finally, `a[first]` and `a[last]` have same address, and due to a ^ a = 0, after run step1 in `inplace_swap`, the value of `a[first]` and `a[last]` become 0. 19 | 20 | C. modify `first <= last` to `first < last`. 21 | -------------------------------------------------------------------------------- /C3/practice/3.43/README.md: -------------------------------------------------------------------------------- 1 | # 3.43 2 | 3 | | expr | type | code | 4 | | - | - | - | 5 | |`up->t1.u` | long | `movq (%rdi), %rax` `movq %rax, (%rsi)` | 6 | |`up->t1.v` | short | `movw 8(%rdi), %ax` `movw %ax, (%rsi)` | 7 | |`&up->t1.w` | char* | `leaq 10(%rdi), %rax` `movq %rax, (%rsi)`| 8 | |`up->t2.a` | int* | `movq %rdi, (%rsi)` | 9 | |`up->t2.a[up->t1.u]` | int | `movq (%rdi), %rax` `movl (%rdi, %rsi, 4),%rsi` `movl %eax, (%rsi)`| 10 | |`*up->t2.p` | char | `movq 8(%rdi), %rax` `movq (%rax), (%rsi)` | 11 | -------------------------------------------------------------------------------- /C4/practice/4.33/README.md: -------------------------------------------------------------------------------- 1 | # 4.33 2 | 3 | ```x86asm 4 | ; cycle 0 1 2 3 4 5 6 7 8 9 10 5 | irmovq $5, %rdx ; F E D M W 6 | irmovq $0x100, %rsp ; F E D M W 7 | rmmovq %rdx,0(%rsp) ; F E D M W 8 | popq %rsp ; F E D M W 9 | nop ; F E D M W 10 | nop ; F E D M W 11 | rrmovq %rsp, %rax ; F E D M W 12 | ``` 13 | 14 | If the two cases were reversed, then the register `%rax` will be set to the incremented program counter rather than the value read from memory. 15 | -------------------------------------------------------------------------------- /C2/practice/2.45/README.md: -------------------------------------------------------------------------------- 1 | # 2.45 2 | 3 | | Fractional value | Binary representation | Decimal representation | 4 | | - | - | - | 5 | | 1/8 | 0.001 | 0.125 | 6 | | 3/4 | 0.11 | 0.75 | 7 | | 25/16 | 1.1001 | 1.5625 | 8 | | 43/16 | 10.1011 | 2.6875 | 9 | | 9/8 | 1.001 | 1.125 | 10 | | 47/8 | 101.111 | 5.875 | 11 | | 51/16 | 11.0011 | 3.1875 | 12 | -------------------------------------------------------------------------------- /C2/practice/2.34/README.md: -------------------------------------------------------------------------------- 1 | # 2.34 2 | 3 | w = 3. 4 | 5 | | Mode | x | y | x *y | Truncated x* y | 6 | | - | - | - | - | - | 7 | | Unsigned | 4 [100] | 5 [101] | 20 [010100] | 4 [100] | 8 | | Two's complement | -4 [100] | -3 [101] | 12 [001100] | -4 [100] | 9 | | Unsigned | 2 [010] | 7 [111] | 14 [001110] | 6 [110] | 10 | | Two's complement | 2 [010] | -1 [111] | -2 [111110] | -2 [110] | 11 | | Unsigned | 6 [110] | 6 [110] | 36 [100100] | 4 [100] | 12 | | Two's complement | -2 [110] | -2 [110] | 4 [000100] | -4 [100] | 13 | -------------------------------------------------------------------------------- /C5/homework/5.14/innner4.c: -------------------------------------------------------------------------------- 1 | void inner4_loop_unrolling(vec_ptr u, vec_ptr v, data_t *dest) { 2 | long i; 3 | long length = vec_length(u); 4 | data_t *udata = get_vec_start(u); 5 | data_t *vdata = get_vec_start(v); 6 | 7 | data_t sum = (data_t) 0; 8 | 9 | for (i = 0; i < length; i+=6) { 10 | sum = sum + udata[i] * vdata[i] 11 | + udata[i + 1] * vdata[i + 1] 12 | + udata[i + 2] * vdata[i + 2] 13 | + udata[i + 3] * vdata[i + 3] 14 | + udata[i + 4] * vdata[i + 4] 15 | + udata[i + 5] * vdata[i + 5]; 16 | } 17 | 18 | for (; i < length; i++) { 19 | sum = sum + udata[i] * vdata[i]; 20 | } 21 | 22 | *dest = sum; 23 | 24 | } -------------------------------------------------------------------------------- /C5/homework/5.16/innner4.c: -------------------------------------------------------------------------------- 1 | void inner4_loop_unrolling(vec_ptr u, vec_ptr v, data_t *dest) { 2 | long i; 3 | long length = vec_length(u); 4 | data_t *udata = get_vec_start(u); 5 | data_t *vdata = get_vec_start(v); 6 | 7 | data_t sum = (data_t) 0; 8 | 9 | for (i = 0; i < length; i+=6) { 10 | sum = sum + (udata[i] * vdata[i] 11 | + udata[i + 1] * vdata[i + 1] 12 | + udata[i + 2] * vdata[i + 2] 13 | + udata[i + 3] * vdata[i + 3] 14 | + udata[i + 4] * vdata[i + 4] 15 | + udata[i + 5] * vdata[i + 5]); 16 | } 17 | 18 | for (; i < length; i++) { 19 | sum = sum + udata[i] * vdata[i]; 20 | } 21 | 22 | *dest = sum; 23 | 24 | } -------------------------------------------------------------------------------- /C3/practice/3.46/README.md: -------------------------------------------------------------------------------- 1 | # 3.46 2 | 3 | Type in string: `0123456789012345678901234`. 4 | 5 | A. 6 | 7 | | diagram | description| 8 | | - | - | 9 | |00 00 00 00 00 40 00 76| return address| 10 | |01 23 45 67 89 AB CD EF| | 11 | |00 00 00 00 00 00 00 00| | 12 | |00 00 00 00 00 00 00 00| %rsp | 13 | 14 | B. 15 | 16 | | diagram | description| 17 | | - | - | 18 | |00 00 00 00 00 40 00 34| return address| 19 | |33 32 31 30 39 38 37 36| | 20 | |35 34 33 32 31 30 39 38| | 21 | |37 36 35 34 33 32 31 30| %rsp | 22 | 23 | C. `0x400034`. 24 | 25 | D. `%rbx`. 26 | 27 | E. `result = malloc(strlen(buf));` ->`result = malloc(strlen(buf) + 1);` 28 | -------------------------------------------------------------------------------- /C2/note/storage/code.md: -------------------------------------------------------------------------------- 1 | # Representing Code 2 | 3 | Considering the following C function: 4 | 5 | ```cpp 6 | int sum(int x, int y) { 7 | return x + y; 8 | } 9 | ``` 10 | 11 | When compiled on our sample machines. we generate machine code having the following byte representations: 12 | 13 | - Linux 32: `55 89 e5 8b 45 0c 03 45 08 c9 c3` 14 | - Windows: `55 89 e5 8b 45 0c 03 45 08 5d c3` 15 | - Sun: `81 c3 e0 08 90 02 00 09` 16 | - Linux 64: `55 48 89 e5 89 7d fc 89 75 f8 03 45 fc c9 c3` 17 | 18 | Hence we find that the instruction codings are different. Different machine types use different instructions and encodings, so `binary code is seldom portable across different combinations of machine and operating system`. 19 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "gcc - Build and debug active file", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "${fileDirname}/${fileBasenameNoExtension}", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${workspaceFolder}", 15 | "environment": [], 16 | "externalConsole": false, 17 | "MIMode": "lldb", 18 | "preLaunchTask": "C/C++: gcc build active file" 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /C3/note/encodings/mstore.s: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .build_version macos, 10, 15 sdk_version 10, 15, 6 3 | .globl _multstore ## -- Begin function multstore 4 | .p2align 4, 0x90 5 | _multstore: ## @multstore 6 | .cfi_startproc 7 | ## %bb.0: 8 | pushq %rbp 9 | .cfi_def_cfa_offset 16 10 | .cfi_offset %rbp, -16 11 | movq %rsp, %rbp 12 | .cfi_def_cfa_register %rbp 13 | pushq %rbx 14 | pushq %rax 15 | .cfi_offset %rbx, -24 16 | movq %rdx, %rbx 17 | callq _mult2 18 | movq %rax, (%rbx) 19 | addq $8, %rsp 20 | popq %rbx 21 | popq %rbp 22 | retq 23 | .cfi_endproc 24 | ## -- End function 25 | .subsections_via_symbols 26 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog7.yo: -------------------------------------------------------------------------------- 1 | | # Demonstrate branch cancellation 2 | | # /* $begin prog7-ys */ 3 | | # prog7 4 | 0x000: 6300 | xorq %rax,%rax 5 | 0x002: 741600000000000000 | jne target # Not taken 6 | 0x00b: 30f00100000000000000 | irmovq $1, %rax # Fall through 7 | 0x015: 00 | halt 8 | 0x016: | target: 9 | 0x016: 30f20200000000000000 | irmovq $2, %rdx # Target 10 | 0x020: 30f30300000000000000 | irmovq $3, %rbx # Target+1 11 | | # /* $end prog7-ys */ 12 | 0x02a: 00 | halt 13 | | 14 | -------------------------------------------------------------------------------- /labs/archlab/README: -------------------------------------------------------------------------------- 1 | ##################################################################### 2 | # CS:APP Architecture Lab 3 | # Handout files for students 4 | # 5 | # Copyright (c) 2002, 2010, 2015, R. Bryant and D. O'Hallaron, 6 | # All rights reserved. May not be used, modified, or copied 7 | # without permission. 8 | # 9 | ###################################################################### 10 | 11 | This directory contains the files that you need for the CS:APP 12 | architecture lab. 13 | 14 | ****** 15 | Files: 16 | ****** 17 | 18 | Makefile Use this to handin your solutions 19 | README This file 20 | archlab.{ps,pdf} Lab writeup 21 | sim.tar Archive of the Y86-64 tools in tar format 22 | simguide.{ps,pdf} CS:APP Guide to Simulators document 23 | -------------------------------------------------------------------------------- /labs/archlab/sim/pipe/ncopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef word_t word_t; 4 | 5 | word_t src[8], dst[8]; 6 | 7 | /* $begin ncopy */ 8 | /* 9 | * ncopy - copy src to dst, returning number of positive ints 10 | * contained in src array. 11 | */ 12 | word_t ncopy(word_t *src, word_t *dst, word_t len) 13 | { 14 | word_t count = 0; 15 | word_t val; 16 | 17 | while (len > 0) { 18 | val = *src++; 19 | *dst++ = val; 20 | if (val > 0) 21 | count++; 22 | len--; 23 | } 24 | return count; 25 | } 26 | /* $end ncopy */ 27 | 28 | int main() 29 | { 30 | word_t i, count; 31 | 32 | for (i=0; i<8; i++) 33 | src[i]= i+1; 34 | count = ncopy(src, dst, 8); 35 | printf ("count=%d\n", count); 36 | exit(0); 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /C2/homework/2.97/float_i2f.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | int bits_length(int rest_i) { 6 | int length = 0; 7 | while( rest_i) { 8 | rest_i >>= 1; 9 | length += 1; 10 | } 11 | return length; 12 | } 13 | /* compute (float)i. */ 14 | float_bits float_i2f(int i) { 15 | if( i == 0) { 16 | return i; 17 | } 18 | int sign = (i >> 31) & 0x1; 19 | if( sign) { 20 | i = ~i + 1; 21 | } 22 | int rest_i = i & (1<<31 + ~0); 23 | int rest_i_length = bits_length(rest_i); 24 | unsigned exp = 127 + rest_i_length - 1; 25 | unsigned frac = (1< hits: 4 | > 5 | > b1 : src[0][] src[2][] dst[0][] dst[2][] 6 | > 7 | > b2 : src[1][] src[3][] dst[1][] dst[3][] 8 | 9 | `dst` array: 10 | 11 | | | Col.0 | Col.1 | Col.2 | Col.3 | 12 | | - | - | - | - | - | 13 | | Row 0 | m | m | m | m | 14 | | Row 1 | m | m | m | m | 15 | | Row 2 | m | m | m | m | 16 | | Row 3 | m | m | m | m | 17 | 18 | `src` array: 19 | 20 | | | Col.0 | Col.1 | Col.2 | Col.3 | 21 | | - | - | - | - | - | 22 | | Row 0 | m | m | h | m | 23 | | Row 1 | m | h | m | h | 24 | | Row 2 | m | m | h | m | 25 | | Row 3 | m | h | m | h | 26 | -------------------------------------------------------------------------------- /labs/attack/README.txt: -------------------------------------------------------------------------------- 1 | This file contains materials for one instance of the attacklab. 2 | 3 | Files: 4 | 5 | ctarget 6 | 7 | Linux binary with code-injection vulnerability. To be used for phases 8 | 1-3 of the assignment. 9 | 10 | rtarget 11 | 12 | Linux binary with return-oriented programming vulnerability. To be 13 | used for phases 4-5 of the assignment. 14 | 15 | cookie.txt 16 | 17 | Text file containing 4-byte signature required for this lab instance. 18 | 19 | farm.c 20 | 21 | Source code for gadget farm present in this instance of rtarget. You 22 | can compile (use flag -Og) and disassemble it to look for gadgets. 23 | 24 | hex2raw 25 | 26 | Utility program to generate byte sequences. See documentation in lab 27 | handout. 28 | 29 | -------------------------------------------------------------------------------- /C3/homework/3.74/find_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef enum {NEG, ZERO, POS, OTHER} range_t; 5 | 6 | range_t find_range(float x) { 7 | __asm__( 8 | "vxorps %xmm1, %xmm1, %xmm1\n\t" 9 | "movq $1, %rax\n\t" 10 | "movq $2, %r8\n\t" 11 | "movq $0, %r9\n\t" 12 | "movq $3, %r10\n\t" 13 | "vucomiss %xmm1, %xmm0\n\t" 14 | "cmovaq %r8, %rax\n\t" 15 | "cmovbq %r9, %rax\n\t" 16 | "cmovpq %r10, %rax\n\t" 17 | ); 18 | } 19 | 20 | int main(int argc, char* argv[]) { 21 | range_t n = NEG, z = ZERO, p = POS, o = OTHER; 22 | assert(o == find_range(0.0/0.0)); 23 | assert(n == find_range(-2.3)); 24 | assert(z == find_range(0.0)); 25 | assert(p == find_range(3.33)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C1/note/hello/hello.o: -------------------------------------------------------------------------------- 1 | ???? ??(?__text__TEXT*(??__cstring__TEXT*R__compact_unwind__LD8 `?__eh_frame__TEXTX@? h2 2 |  3 | ?? PUH??H???E?H?=??1?E???H??]?hello, world 4 | *zRx ?$????????*A?C 5 | -_main_printf -------------------------------------------------------------------------------- /C2/homework/2.87/README.md: -------------------------------------------------------------------------------- 1 | # 2.87 2 | 3 | 1 sign bit, 5 exp bits, 10 fractions bits, bias is 2^4 - 1 = 15. 4 | 5 | | Description | Hex | M | E | V | D | 6 | | - | - | - | - | - | - | 7 | | -0 | 0x8000 | 0 | -14 | -0 | -0.0 | 8 | | Smallest value > 2 | 0x0401 | 1025/1024 | 1 | 1025/512 | 2.00195312 | 9 | | 512 | 0x6000 | 1 | 9 | 512 | 512.0 | 10 | | Largest denormalized| 0x03ff | 1023/1024 | -14 | 1025/2^{24}| 6.09755516e-5| 11 | | -inf | 0xfc00 | - | - | -inf | -inf | 12 | | Number with Hex | 0x3bb0 | 123/64 | -1 | 123/128 | 0.9609375 | 13 | -------------------------------------------------------------------------------- /C1/note/README.md: -------------------------------------------------------------------------------- 1 | # Chapter1: A Tour of Computer System 2 | 3 | A computer system consists of hardware and systems software that cooperate to run application programs. 4 | 5 | sections: 6 | 7 | 1. [Begin with hello.c](./hello/hello.c) 8 | 2. [A brief introduce for the usage of GCC](./GCC/README.md) 9 | 3. [The hardware organization of a typical system](./Hardware/README.md) 10 | 4. [How running the *hello* program and introduce cache memories](./hello/README.md) 11 | 5. [The Operating System Manages the Hardware](./os/README.md) 12 | 6. [Systems Communicate with Other Systems Using Networks](./network/README.md) 13 | 7. [Amdahl's Law](./Amdahls/README.md) 14 | 8. [Concurrency and Parallelism](./os/Concurrency&Parallelism.md) 15 | 9. [The Importance of Abstractions in Computer System](./os/abstractions.md)9 16 | -------------------------------------------------------------------------------- /C2/homework/2.96/float_f2i.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | /* compute (int)f. if f is NaN or cause overflow, then return 0x80000000*/ 6 | int float_f2i(float_bits f) { 7 | unsigned sign = f >> 31; 8 | unsigned exp = f>>23 & 0xFF; 9 | unsigned frac = f & 0x7fffff; 10 | int res; 11 | if( exp == 0xff) { 12 | return 0x80000000; 13 | } else if( exp < 127) { //denormalized 14 | // less 0 15 | res = 0; 16 | } else if( exp >= 158) { 17 | return 0x80000000; 18 | } else if( exp - 127 > 23) { 19 | res = (frac | 0x8000000) >> (exp - 127 - 23); 20 | } else { 21 | res = (frac | 0x8000000) >> (23 - (exp - 127)); 22 | } 23 | return sign ? -res : res; 24 | } 25 | 26 | int main(){ 27 | return 0; 28 | } -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/j-cc.yo: -------------------------------------------------------------------------------- 1 | 0x000: 30f60100000000000000 | irmovq $1, %rsi 2 | 0x00a: 30f70200000000000000 | irmovq $2, %rdi 3 | 0x014: 30f50400000000000000 | irmovq $4, %rbp 4 | 0x01e: 30f0e0ffffffffffffff | irmovq $-32, %rax 5 | 0x028: 30f24000000000000000 | irmovq $64, %rdx 6 | 0x032: 6120 | subq %rdx,%rax 7 | 0x034: 733f00000000000000 | je target 8 | 0x03d: 10 | nop 9 | 0x03e: 00 | halt 10 | 0x03f: | target: 11 | 0x03f: 6062 | addq %rsi,%rdx 12 | 0x041: 10 | nop 13 | 0x042: 10 | nop 14 | 0x043: 10 | nop 15 | 0x044: 00 | halt 16 | -------------------------------------------------------------------------------- /C6/note/README.md: -------------------------------------------------------------------------------- 1 | # Chapter6: The Memory Hierarchy 2 | 3 | In practice, a **memory system** is a hierarchy of storage devices with different capacities, costs, and access times. 4 | 5 | - CPU registers hold the most frequently used data. 6 | - Small, fast cache memories nearby the CPU act as staging areas for a subset of the data and instructions stored in the relatively slow main memory. 7 | - The main memory stages data stored on large, slow disks, which in turn often serve as staging areas for data stored on the disks or tapes of other machines connected by networks. 8 | 9 | This idea center around a fundamental property of computer programs known as **locality**. 10 | 11 | - [Storage Technologies](./storage/README.md) 12 | - [Locality](./locality/README.md) 13 | - [The Memory Hierarchy](./hierarchy/README.md) 14 | -------------------------------------------------------------------------------- /C2/homework/2.88/README.md: -------------------------------------------------------------------------------- 1 | # 2.88 2 | 3 | based on IEEE floating-pointing format. 4 | 5 | Format A: 6 | 7 | - 1 sign bit. 8 | - 5 epx bits. bias = 15. 9 | - 3 fraction bits. 10 | 11 | | Bits | Value | 12 | | - | - | 13 | | 1 01111 001 | -9/8 | 14 | | 0 10110 011 | 176 | 15 | | 1 00111 010 | -5/1024 | 16 | | 0 00000 111 | 7/2^{14} | 17 | | 1 11100 000 | -2^{13} | 18 | | 0 10111 100 | 384 | 19 | 20 | Format B: 21 | 22 | - 1 sign bit. 23 | - 4 epx bits. bias = 7. 24 | - 4 fraction bits. 25 | 26 | | Bits | Value | 27 | | - | - | 28 | | 1 0111 0010 | -9/8 | 29 | | 0 1110 0110 | 176 | 30 | | 1 0000 0101 | -5/1024 | 31 | | 0 0000 0001 | 1/2^{10} | 32 | | 1 1111 0000 | -inf | 33 | | 0 1111 0000 | inf | 34 | -------------------------------------------------------------------------------- /C2/homework/2.75/unsigned_high_prod.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned another_unsigned_high_prod(unsigned x, unsigned y) { 4 | u_int64_t res = (u_int64_t)x * y; 5 | return res >> 32; 6 | } 7 | 8 | int signed_high_prod(int x, int y) { 9 | int64_t res = (int64_t)x * y; 10 | return res >> 32; 11 | } 12 | 13 | unsigned unsigned_high_prod(unsigned x, unsigned y) { 14 | int sign_x = x >> 31; 15 | int sign_y = y >> 31; 16 | return signed_high_prod(x, y) + (-sign_y & x) + (-sign_x & y); 17 | } 18 | 19 | int main(){ 20 | unsigned x = 0x12345678; 21 | unsigned y = 0xFFFFFFFF; 22 | printf("%x\n", unsigned_high_prod(x,y)); 23 | printf("%x\n", another_unsigned_high_prod(x,y)); 24 | 25 | printf("%d", unsigned_high_prod(x, y) == another_unsigned_high_prod(x, y)); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /C3/homework/3.62/README.md: -------------------------------------------------------------------------------- 1 | # 3.62 2 | 3 | ```cpp 4 | typedef enum { 5 | MODE_A, 6 | MODE_B, 7 | MODE_C, 8 | MODE_D, 9 | MODE_E 10 | } mode_t; 11 | 12 | long switch3(long* p1, long* p2, mode_t action) { 13 | long result = 0; 14 | switch(action) { 15 | case MODE_A: 16 | result = *p2; 17 | *p2 = *p1; 18 | break; 19 | case MODE_B: 20 | result = *p1 + *P2; 21 | *p1 = result; 22 | break; 23 | case MODE_C: 24 | *p1 = 59; 25 | result = *p2; 26 | break; 27 | case MODE_D: 28 | *p1 = *p2; 29 | case MODE_E: 30 | result = 27; 31 | break; 32 | default: 33 | result = 12; 34 | } 35 | return result; 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /C2/practice/2.10/README.md: -------------------------------------------------------------------------------- 1 | # 2.10 2 | 3 | consider following program: 4 | 5 | ```cpp 6 | void inplace_swap(int* x, int* y) { 7 | *y = *x ^ *y; // Step 1 8 | *x = *x ^ *y; // Step 2 9 | *y = *x ^ *y; // Step 3 10 | } 11 | ``` 12 | 13 | We claim that the effect of this procedure is to swap the values stored at the locations denoted by pointer variables x and y. Note that unlike the usual technique for wapping two value while we are moving the other. 14 | 15 | There is no performance advantage to this way of swapping, it's merely an intellectual amusement. 16 | 17 | Fill the chart, giving the values stored at the two locations **after** each step of the procedure. 18 | 19 | | step | *x |*y | 20 | | ---- | -- | -- | 21 | | initially | a | b | 22 | | step1 | a | a^b| 23 | | step2 | b | a^b| 24 | | step3 | b | a | 25 | -------------------------------------------------------------------------------- /C3/homework/3.59/README.md: -------------------------------------------------------------------------------- 1 | # 3.59 2 | 3 | ```cpp 4 | typedef __int128 int128_t; 5 | void store_prod(int128_t *dest, int64_t x, int64_t y) { 6 | *dest = x * (int128_t)y; 7 | } 8 | ``` 9 | 10 | Gcc generates the following assembly code implementing the computation: 11 | 12 | ```x86asm 13 | store_prod: 14 | ; dest in %rdi 15 | ; x in %rsi 16 | ; y in %rdx 17 | movq %rdx, %rax ; %rax = y 18 | cqto ; (int128_t)y 19 | movq %rsi, %rcx ; %rcx = x; 20 | sarq $63, %rcx; ; %rcx = x >> 63 21 | imulq %rax, %rcx ; %rcx = %rcx * y 22 | imulq %rsi, %rdx ; y = y * x 23 | addq %rdx, %rcx ; %rcx = %rcx + y; 24 | mulq %rsi ; %rdx:%rax <= x * y 25 | addq %rcx, %rdx; 26 | movq %rax, (%rdi) ; low 8 bytes 27 | movq %rdx, 8(%rdi) ; high 8 bytes 28 | ret 29 | ``` 30 | -------------------------------------------------------------------------------- /C6/homework/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 HomeWork Problems 2 | 3 | - [6.22](./6.22/README.md) 4 | - [6.23](./6.23/README.md) 5 | - [6.24](./6.24/README.md) 6 | - [6.25](./6.25/README.md) 7 | - [6.26](./6.26/README.md) 8 | - [6.27](./6.27/README.md) 9 | - [6.28](./6.28/README.md) 10 | - [6.29](./6.29/README.md) 11 | - [6.30](./6.30/README.md) 12 | - [6.31](./6.31/README.md) 13 | - [6.32](./6.32/README.md) 14 | - [6.33](./6.33/README.md) 15 | - [6.34](./6.34/README.md) 16 | - [6.35](./6.35/README.md) 17 | - [6.36](./6.36/README.md) 18 | - [6.37](./6.37/README.md) 19 | - [6.38](./6.38/README.md) 20 | - [6.39](./6.39/README.md) 21 | - [6.40](./6.40/README.md) 22 | - [6.41](./6.41/README.md) 23 | - [6.42](./6.42/README.md) 24 | - [6.43](./6.43/README.md) 25 | - [6.44](./6.44/README.md) 26 | - [6.45](./6.45/README.md) 27 | - [6.46](./6.46/README.md) 28 | -------------------------------------------------------------------------------- /labs/data/bits.h: -------------------------------------------------------------------------------- 1 | //1 2 | int bitXor(int, int); 3 | int test_bitXor(int, int); 4 | int tmin(); 5 | int test_tmin(); 6 | //2 7 | int isTmax(int); 8 | int test_isTmax(int); 9 | int allOddBits(); 10 | int test_allOddBits(); 11 | int negate(int); 12 | int test_negate(int); 13 | //3 14 | int isAsciiDigit(int); 15 | int test_isAsciiDigit(int); 16 | int conditional(int, int, int); 17 | int test_conditional(int, int, int); 18 | int isLessOrEqual(int, int); 19 | int test_isLessOrEqual(int, int); 20 | //4 21 | int logicalNeg(int); 22 | int test_logicalNeg(int); 23 | int howManyBits(int); 24 | int test_howManyBits(int); 25 | //float 26 | unsigned floatScale2(unsigned); 27 | unsigned test_floatScale2(unsigned); 28 | int floatFloat2Int(unsigned); 29 | int test_floatFloat2Int(unsigned); 30 | unsigned floatPower2(int); 31 | unsigned test_floatPower2(int); 32 | -------------------------------------------------------------------------------- /C2/homework/2.95/float_half.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned float_bits; 4 | 5 | /* compute 0.5*f. if f is NaN, then return f*/ 6 | float_bits float_half(float_bits f) { 7 | unsigned sign = f >> 31; 8 | unsigned exp = f>>23 & 0xFF; 9 | unsigned frac = f & 0x7fffff; 10 | unsigned rest = f & 0x7fffffff; 11 | int addition = (frac & 0x3) == 0x3; 12 | 13 | if( exp == 0xff) { 14 | return f; 15 | } else if( exp == 0) { //denormalized 16 | frac >>= 1; 17 | frac += addition; 18 | } else if( exp == 1) { 19 | rest >>= 1; 20 | rest += addition; 21 | exp = rest >> 23 & 0xFF; 22 | frac = rest & 0x7fffff; 23 | } 24 | else { 25 | exp -= 1; 26 | } 27 | return (sign << 31)| (exp << 23) | frac; 28 | } 29 | 30 | int main(){ 31 | return 0; 32 | } -------------------------------------------------------------------------------- /C2/homework/2.57/show_bytes_more.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned char *byte_pointer; // len: 8 4 | 5 | void show_bytes( byte_pointer start, size_t len) { 6 | size_t i; 7 | for( i = 0; i < len; i++) { 8 | printf("%.2x", start[i]); // $.2x indicates that an interger should be printed in hex. 9 | } 10 | printf("\n"); 11 | } 12 | 13 | void show_short(short x) { 14 | show_bytes((byte_pointer) &x, sizeof(short)); 15 | } 16 | 17 | void show_long(long x) { 18 | show_bytes((byte_pointer) &x, sizeof(long)); 19 | } 20 | 21 | void show_double(double x) { 22 | show_bytes((byte_pointer) &x, sizeof(double)); 23 | } 24 | 25 | int main() { 26 | short x = 0x0123; 27 | long y = 0x0123456789ABCDEF; 28 | double z = 1234.56789; 29 | 30 | show_short(x); 31 | show_long(y); 32 | show_double(z); 33 | return 0;} -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/README: -------------------------------------------------------------------------------- 1 | This directory contains examples of Y86-64 assembly code programs 2 | (extension `.ys') used in Chapter 4 of CS:APP3e. 3 | 4 | Given an assembly code file "file.ys", you can assemble it with the 5 | command "make file.yo". The resulting file is in the "object code" 6 | format described in the book. 7 | 8 | You can assemble and simulate all the test programs in this directory. 9 | First, you need to make the different simulators in the pipe (psim) 10 | and seq (ssim and ssim+) directories. Then use the following 11 | commands: 12 | 13 | PIPE: make testpsim 14 | SEQ: make testssim 15 | SEQ+: make testssim+ 16 | 17 | Each of these commands will cause a number of programs to be assembled 18 | and simulated. Lots of things will scroll by, but you should see the message 19 | "ISA Check Succeeds" for each of the programs tested. 20 | 21 | -------------------------------------------------------------------------------- /C5/homework/5.17/memset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * K = sizeof(unsigned long) 3 | * cs store K chars for memset 4 | */ 5 | void* effective_memset(void *s, unsigned long cs, size_t n) { 6 | /* align to K */ 7 | size_t K = sizeof(unsigned long); 8 | size_t cnt = 0; 9 | unsigned char *schar = s; 10 | while (cnt < n) { 11 | if ((size_t)schar % K == 0) { 12 | break; 13 | } 14 | *schar++ = (unsigned char)cs; 15 | cnt++; 16 | } 17 | 18 | /* set K chars one time */ 19 | unsigned long *slong = (unsigned long *)schar; 20 | size_t rest = n - cnt; 21 | size_t loop = rest / K; 22 | size_t tail = rest % K; 23 | 24 | for (size_t i = 0; i < loop; i++) { 25 | *slong++ = cs; 26 | } 27 | 28 | /* pad the tail part */ 29 | schar = (unsigned char *)slong; 30 | for (size_t i = 0; i < tail; i++) { 31 | *schar++ = (unsigned char)cs; 32 | } 33 | return s; 34 | } -------------------------------------------------------------------------------- /C2/practice/2.29/README.md: -------------------------------------------------------------------------------- 1 | # 2.29 2 | 3 | w = 5, two's-complement. 4 | 5 | | x | y | x + y | x + y after deal overflow | 6 | | - | - | - | - | 7 | | -12 | -15 | -27 | 5 | 8 | | [10100] | [10001] | [100101] | [00101] | 9 | | -8 | -8 | -16 | -16 | 10 | | [11000] | [11000] | [110000] | [10000] | 11 | | -9 | 8 | -1 | -1 | 12 | | [10111] | [01000] | [11111] | [11111] | 13 | | 2 | 5 | 7 | 7 | 14 | | [00010] | [00101] | [00111] | [00111] | 15 | | 12 | 4 | -16 | -16 | 16 | | [01100] | [00100] | [10000] | [10000] | 17 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/prog6.yo: -------------------------------------------------------------------------------- 1 | | # Demonstration of return 2 | | # /* $begin prog6-ys */ 3 | | # prog6 4 | 0x000: 30f43000000000000000 | irmovq stack,%rsp # Initialize stack pointer 5 | 0x00a: 802000000000000000 | call proc # Procedure call 6 | 0x013: 30f20a00000000000000 | irmovq $10,%rdx # Return point 7 | 0x01d: 00 | halt 8 | 0x020: | .pos 0x20 9 | 0x020: | proc: # proc: 10 | 0x020: 90 | ret # Return immediately 11 | 0x021: 2023 | rrmovq %rdx,%rbx # Not executed 12 | 0x030: | .pos 0x30 13 | 0x030: | stack: # stack: Stack pointer 14 | | # /* $end prog6-ys */ 15 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/copy.ys: -------------------------------------------------------------------------------- 1 | 2 | .pos 0 3 | irmovq 0x100, %rsp 4 | call main 5 | halt 6 | 7 | .align 8 8 | # Source block 9 | src: 10 | .quad 0x00a 11 | .quad 0x0b0 12 | .quad 0xc00 13 | # Destination block 14 | dest: 15 | .quad 0x111 16 | .quad 0x222 17 | .quad 0x333 18 | 19 | main: 20 | irmovq src, %rdi 21 | irmovq dest, %rsi 22 | irmovq $3, %rdx 23 | call copy_block 24 | ret 25 | 26 | copy_block: 27 | pushq %r8 28 | pushq %r9 29 | pushq %r10 30 | irmovq $8, %r8 31 | irmovq $1, %r9 32 | xorq %rax, %rax 33 | andq %rdx, %rdx 34 | jmp test 35 | loop: 36 | mrmovq (%rdi), %r10 37 | addq %r8, %rdi 38 | rmmovq %r10, (%rsi) 39 | addq %r8, %rsi 40 | xorq %r10, %rax 41 | subq %r9, %rdx 42 | test: 43 | jne loop 44 | popq %r10 45 | popq %r9 46 | popq %r8 47 | ret 48 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/ret-hazard.yo: -------------------------------------------------------------------------------- 1 | | /* $begin ret-hazard-ys */ 2 | | # Test instruction that modifies %esp followed by ret 3 | 0x000: 30f34000000000000000 | irmovq mem,%rbx 4 | 0x00a: 50430000000000000000 | mrmovq 0(%rbx),%rsp # Sets %rsp to point to return point 5 | 0x014: 90 | ret # Returns to return point 6 | 0x015: 00 | halt # 7 | 0x016: 30f60500000000000000 | rtnpt: irmovq $5,%rsi # Return point 8 | 0x020: 00 | halt 9 | 0x040: | .pos 0x40 10 | 0x040: 5000000000000000 | mem: .quad stack # Holds desired stack pointer 11 | 0x050: | .pos 0x50 12 | 0x050: 1600000000000000 | stack: .quad rtnpt # Top of stack: Holds return point 13 | | /* $end ret-hazard-ys */ 14 | -------------------------------------------------------------------------------- /labs/performance/defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * driver.h - Various definitions for the Performance Lab. 3 | * 4 | * DO NOT MODIFY ANYTHING IN THIS FILE 5 | */ 6 | #ifndef _DEFS_H_ 7 | #define _DEFS_H_ 8 | 9 | #include 10 | 11 | #define RIDX(i,j,n) ((i)*(n)+(j)) 12 | 13 | typedef struct { 14 | char *team; 15 | char *name1, *email1; 16 | char *name2, *email2; 17 | } team_t; 18 | 19 | extern team_t team; 20 | 21 | typedef struct { 22 | unsigned short red; 23 | unsigned short green; 24 | unsigned short blue; 25 | } pixel; 26 | 27 | typedef void (*lab_test_func) (int, pixel*, pixel*); 28 | 29 | void smooth(int, pixel *, pixel *); 30 | void rotate(int, pixel *, pixel *); 31 | 32 | void register_rotate_functions(void); 33 | void register_smooth_functions(void); 34 | void add_smooth_function(lab_test_func, char*); 35 | void add_rotate_function(lab_test_func, char*); 36 | 37 | #endif /* _DEFS_H_ */ 38 | 39 | -------------------------------------------------------------------------------- /C1/note/GCC/README.md: -------------------------------------------------------------------------------- 1 | # GUN and GCC 2 | 3 | GNU is short for GNU's Not Unix. 4 | 5 | The GUN environment includes the EMACS editor, GCC compiler, GDB debugger, assembler, linker, utilities for manipulating binaries, and other components. 6 | 7 | The GCC compiler has grown to support many different languages, with the ability to generate code for many different machines. Supported languages include C, C++, Fortran, Java, Pascal, Objective-C and Ada. 8 | 9 | ## usage 10 | 11 | Firstly, you need enter `gcc --version` in Terminal to check is weather installed. 12 | 13 | And the general usage is: `gcc(option) (parameters)` 14 | 15 | option list; 16 | 17 | ``` 18 | -o: to specify the output filename 19 | -E: only preprocessing 20 | -S: translate .c to .s 21 | -wall: show warnings 22 | -c: do not linking, and only compiler 23 | -Og: level of optimization 24 | ``` 25 | 26 | ## instance 27 | 28 | [GCC instance](../hello/hello.c) 29 | -------------------------------------------------------------------------------- /C1/note/hello/hello.s: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .build_version macos, 10, 15 sdk_version 10, 15, 4 3 | .globl _main ## -- Begin function main 4 | .p2align 4, 0x90 5 | _main: ## @main 6 | .cfi_startproc 7 | ## %bb.0: 8 | pushq %rbp 9 | .cfi_def_cfa_offset 16 10 | .cfi_offset %rbp, -16 11 | movq %rsp, %rbp 12 | .cfi_def_cfa_register %rbp 13 | subq $16, %rsp 14 | movl $0, -4(%rbp) 15 | leaq L_.str(%rip), %rdi 16 | movb $0, %al 17 | callq _printf 18 | xorl %ecx, %ecx 19 | movl %eax, -8(%rbp) ## 4-byte Spill 20 | movl %ecx, %eax 21 | addq $16, %rsp 22 | popq %rbp 23 | retq 24 | .cfi_endproc 25 | ## -- End function 26 | .section __TEXT,__cstring,cstring_literals 27 | L_.str: ## @.str 28 | .asciz "hello, world\n" 29 | 30 | 31 | .subsections_via_symbols 32 | -------------------------------------------------------------------------------- /C3/homework/3.73/find_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef enum {NEG, ZERO, POS, OTHER} range_t; 5 | 6 | range_t find_range(float x) { 7 | __asm__( 8 | "vxorps %xmm1, %xmm1, %xmm1\n\t" 9 | "vucomiss %xmm1, %xmm0\n\t" 10 | "jp .P\n\t" 11 | "ja .A\n\t" 12 | "jb .B\n\t" 13 | "je .E\n\t" 14 | ".A:\n\t" 15 | "movl $2, %eax\n\t" 16 | "jmp .Done\n\t" 17 | ".B:\n\t" 18 | "movl $0, %eax\n\t" 19 | "jmp .Done\n\t" 20 | ".E:\n\t" 21 | "movl $1, %eax\n\t" 22 | "jmp .Done\n\t" 23 | ".P:\n\t" 24 | "movl $3, %eax\n\t" 25 | ".Done:\n\t" 26 | ); 27 | } 28 | 29 | int main(int argc, char* argv[]) { 30 | range_t n = NEG, z = ZERO, p = POS, o = OTHER; 31 | assert(o == find_range(0.0/0.0)); 32 | assert(n == find_range(-2.3)); 33 | assert(z == find_range(0.0)); 34 | assert(p == find_range(3.33)); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /C3/practice/3.4/README.md: -------------------------------------------------------------------------------- 1 | # 3.4 2 | 3 | we had code: 4 | 5 | ```c 6 | src_t *sp; 7 | dest_t *dp; 8 | ``` 9 | 10 | and we want implement the operation `*dp = (dest_t) *sp;`. 11 | 12 | Assume that the value of `sp` and `dp` are stored in register `%rdi` and `%rsi`, respectively. 13 | 14 | | src_t | dest_t | instruction | 15 | | - | - | - | 16 | | long | long | `movq (%rdi), %rax`, `movq %rax, (%rsi)` | 17 | | char | int | `movsbl (%rdi), %eax`, `movl %eax, (%rsi)` | 18 | | char | unsigned | `movsbl (%rdi), %eax`, `movl %eax, (%rsi)` | 19 | | unsigned char | long | `movzbl (%di), %eax`, `movq %rax, (%rsi)` | 20 | | int | char | `movl (%rdi), %eax`, `movb %al, (%rsi)` | 21 | | unsigned | unsigned char | `movl (%rdi), %eax`, `movb %al, (%rsi)` | 22 | | char | short | `mosbw (%rdi), %ax`, `movw %ax, (%rsi)` | 23 | -------------------------------------------------------------------------------- /C1/note/os/abstractions.md: -------------------------------------------------------------------------------- 1 | # The Importance of Abstractions in Computer System 2 | 3 | The use of abstractions is one of the most important concepts in computer science. 4 | 5 | Foe example: 6 | 7 | - API(application program interface): a set of functions that allow programmers to use the code without having to delve into its inner workings. Such as Java class and C function prototypes. 8 | - abstractions in computer systems: 1. **instruction set architecture** provides an abstraction of the actual processor hardware, with this abstraction, a machine-code program behaves as if it were executed on a processor that performs just one instruction at a time. 2. **files** as an abstraction of I/O devices; 3. **virtual memory** as an abstraction of program memory; 4. **virtual machine** providing an abstraction of the entire computer, including the operating system, the processor, and the programs. 9 | ![SOme abstractions provided by a computer system](https://img-blog.csdnimg.cn/20200912110526891.png) 10 | -------------------------------------------------------------------------------- /C5/homework/5.15/innner4.c: -------------------------------------------------------------------------------- 1 | // 6 * 6 2 | void inner4_loop_unrolling(vec_ptr u, vec_ptr v, data_t *dest) { 3 | long i; 4 | long length = vec_length(u); 5 | data_t *udata = get_vec_start(u); 6 | data_t *vdata = get_vec_start(v); 7 | 8 | data_t sum1 = (data_t) 0; 9 | data_t sum2 = (data_t) 0; 10 | data_t sum3 = (data_t) 0; 11 | data_t sum4 = (data_t) 0; 12 | data_t sum5 = (data_t) 0; 13 | data_t sum6 = (data_t) 0; 14 | 15 | for (i = 0; i < length; i+=6) { 16 | sum1 = sum1 + udata[i] * vdata[i]; 17 | sum2 = sum2 + udata[i + 1] * vdata[i + 1]; 18 | sum3 = sum3 + udata[i + 2] * vdata[i + 2]; 19 | sum4 = sum4 + udata[i + 3] * vdata[i + 3]; 20 | sum5 = sum5 + udata[i + 4] * vdata[i + 4]; 21 | sum6 = sum6 + udata[i + 5] * vdata[i + 5]; 22 | } 23 | 24 | for (; i < length; i++) { 25 | sum1 = sum1 + udata[i] * vdata[i]; 26 | } 27 | 28 | *dest = sum1 + sum2 + sum3 + sum4 + sum5 + sum6; 29 | 30 | } -------------------------------------------------------------------------------- /C2/practice/2.24/README.md: -------------------------------------------------------------------------------- 1 | # 2.24 2 | 3 | Suppose we truncate 4-bit value to 3-bit value. 4 | 5 | Hex: 6 | 7 | | Hex Original| Hex Truncated| Unsigned Original | Unsigned Truncated| Two's complement Original| Two's complement Truncated| 8 | | - | - | - | - | - | - | 9 | |0 | 0 | 0 | 0 | 0 | 0 | 10 | |2 | 2 | 2 | 2 | 2 | 2 | 11 | |9 | 1 | 9 | 1 | -7 | 1 | 12 | |B | 3 | 11 | 3 | -5 | 3 | 13 | |F | 7 | 15 | 7 | -1 | -1 | 14 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/asumi.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 | 7 | # Array of 4 elements 8 | .align 8 9 | array: .quad 0x000d000d000d 10 | .quad 0x00c000c000c0 11 | .quad 0x0b000b000b00 12 | .quad 0xa000a000a000 13 | 14 | main: irmovq array,%rdi 15 | irmovq $4,%rsi 16 | call sum # sum(array, 4) 17 | ret 18 | 19 | /* $begin sumi-ys */ 20 | # long sum(long *start, long count) 21 | # start in %rdi, count in %rsi 22 | sum: 23 | xorq %rax,%rax # sum = 0 24 | andq %rsi,%rsi # Set condition codes 25 | jmp test 26 | loop: 27 | mrmovq (%rdi),%r10 # Get *start 28 | addq %r10,%rax # Add to sum 29 | iaddq $8,%rdi # start++ 30 | iaddq $-1,%rsi # count-- 31 | test: 32 | jne loop # Stop when 0 33 | ret 34 | /* $end sumi-ys */ 35 | 36 | # The stack starts here and grows to lower addresses 37 | .pos 0x100 38 | stack: 39 | -------------------------------------------------------------------------------- /C3/note/README.md: -------------------------------------------------------------------------------- 1 | # Chapter3: Machine-Level Representation of Programs 2 | 3 | Even though compilers do most of the work in generating assembly code, being able to read and understand it is an important skill for serious programmers. 4 | 5 | Some reasons: 6 | 7 | - get a sense of how efficiently the program will run. 8 | - when writing concurrent programs using a thread package, it's important to understand how program data are shared or kept private by the different threads and precisely how and where shared data are accessed. 9 | - how to guard against malware. 10 | 11 | sections: 12 | 13 | - [Program Encodings](./encodings/README.md) 14 | - [Data Formats](./formats/README.md) 15 | - [Operations](./operations/README.md) 16 | - [Procedures](./procedures/README.md) 17 | - [Array Allocation and access](./array/README.md) 18 | - [Heterogeneous Data Structure](./heterogeneous/README.md) 19 | - [Combining Control and Data in Machine-Level Programs](./control/README.md) 20 | - [Floating-Point Code](./floating-point/README.md) 21 | -------------------------------------------------------------------------------- /labs/performance/config.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | * config.h - Configuration data for the driver.c program. 3 | *********************************************************/ 4 | #ifndef _CONFIG_H_ 5 | #define _CONFIG_H_ 6 | 7 | /* 8 | * CPEs for the baseline (naive) version of the rotate function that 9 | * was handed out to the students. Rd is the measured CPE for a dxd 10 | * image. Run the driver.c program on your system to get these 11 | * numbers. 12 | */ 13 | #define R64 14.7 14 | #define R128 40.1 15 | #define R256 46.4 16 | #define R512 65.9 17 | #define R1024 94.5 18 | 19 | /* 20 | * CPEs for the baseline (naive) version of the smooth function that 21 | * was handed out to the students. Sd is the measure CPE for a dxd 22 | * image. Run the driver.c program on your system to get these 23 | * numbers. 24 | */ 25 | #define S32 695 26 | #define S64 698 27 | #define S128 702 28 | #define S256 717 29 | #define S512 722 30 | 31 | 32 | #endif /* _CONFIG_H_ */ 33 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/asum.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 | 7 | # Array of 4 elements 8 | .align 8 9 | array: .quad 0x000d000d000d 10 | .quad 0x00c000c000c0 11 | .quad 0x0b000b000b00 12 | .quad 0xa000a000a000 13 | 14 | main: irmovq array,%rdi 15 | irmovq $4,%rsi 16 | call sum # sum(array, 4) 17 | ret 18 | 19 | # long sum(long *start, long count) 20 | # start in %rdi, count in %rsi 21 | sum: irmovq $8,%r8 # Constant 8 22 | irmovq $1,%r9 # Constant 1 23 | xorq %rax,%rax # sum = 0 24 | andq %rsi,%rsi # Set CC 25 | jmp test # Goto test 26 | loop: mrmovq (%rdi),%r10 # Get *start 27 | addq %r10,%rax # Add to sum 28 | addq %r8,%rdi # start++ 29 | subq %r9,%rsi # count--. Set CC 30 | test: jne loop # Stop when 0 31 | ret # Return 32 | 33 | # Stack starts here and grows to lower addresses 34 | .pos 0x200 35 | stack: 36 | -------------------------------------------------------------------------------- /C3/note/floating-point/README.md: -------------------------------------------------------------------------------- 1 | # Floating-Point Code 2 | 3 | The floating-point architecture for a processor consists of the different aspects that affect how programs operating on floating--point data are mapped onto the machine, including: 4 | 5 | - How floating-point values are stored and accessed. This is typically via some form of registers. 6 | - The instructions that operate on floating-point data. 7 | - The conventions used for passing floating-point values as arguments to functions and for returning them as results. 8 | - The conventions for how registers are preserved during function calls -- for example, with some registers designated as caller saved, and others as callee saved. 9 | 10 | Practices: 11 | 12 | - [3.50](../../practice/3.50/README.md) 13 | - [3.51](../../practice/3.51/README.md) 14 | - [3.52](../../practice/3.52/README.md) 15 | - [3.53](../../practice/3.53/README.md) 16 | - [3.54](../../practice/3.54/README.md) 17 | - [3.55](../../practice/3.55/README.md) 18 | - [3.56](../../practice/3.56/README.md) 19 | - [3.57](../../practice/3.57/README.md) 20 | -------------------------------------------------------------------------------- /C5/homework/5.14/README.md: -------------------------------------------------------------------------------- 1 | # 5.14 2 | 3 | ```cpp 4 | void inner4_loop_unrolling(vec_ptr u, vec_ptr v, data_t *dest) { 5 | long i; 6 | long length = vec_length(u); 7 | data_t *udata = get_vec_start(u); 8 | data_t *vdata = get_vec_start(v); 9 | 10 | data_t sum1 = (data_t) 0; 11 | data_t sum2 = (data_t) 0; 12 | data_t sum3 = (data_t) 0; 13 | data_t sum4 = (data_t) 0; 14 | data_t sum5 = (data_t) 0; 15 | data_t sum6 = (data_t) 0; 16 | 17 | for (i = 0; i < length; i+=6) { 18 | sum1 = sum1 + udata[i] * vdata[i]; 19 | sum2 = sum2 + udata[i + 1] * vdata[i + 1]; 20 | sum3 = sum3 + udata[i + 2] * vdata[i + 2]; 21 | sum4 = sum4 + udata[i + 3] * vdata[i + 3]; 22 | sum5 = sum5 + udata[i + 4] * vdata[i + 4]; 23 | sum6 = sum6 + udata[i + 5] * vdata[i + 5]; 24 | } 25 | 26 | for (; i < length; i++) { 27 | sum1 = sum1 + udata[i] * vdata[i]; 28 | } 29 | 30 | *dest = sum1 + sum2 + sum3 + sum4 + sum5 + sum6; 31 | 32 | } 33 | ``` 34 | 35 | A. Because the bound of CPE is 1.0 36 | 37 | B. 38 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/cjr.yo: -------------------------------------------------------------------------------- 1 | | # /* $begin cjr-ys */ 2 | | # Code to generate a combination of not-taken branch and ret 3 | 0x000: 30f44000000000000000 | irmovq Stack, %rsp 4 | 0x00a: 30f03800000000000000 | irmovq rtnp,%rax 5 | 0x014: a00f | pushq %rax # Set up return pointer 6 | 0x016: 6300 | xorq %rax,%rax # Set Z condition code 7 | 0x018: 742c00000000000000 | jne target # Not taken (First part of combination) 8 | 0x021: 30f00100000000000000 | irmovq $1,%rax # Should execute this 9 | 0x02b: 00 | halt 10 | 0x02c: 90 | target: ret # Second part of combination 11 | 0x02d: 30f30200000000000000 | irmovq $2,%rbx # Should not execute this 12 | 0x037: 00 | halt 13 | 0x038: 30f20300000000000000 | rtnp: irmovq $3,%rdx # Should not execute this 14 | 0x042: 00 | halt 15 | 0x040: | .pos 0x40 16 | 0x040: | Stack: 17 | | # /* $end cjr-ys */ 18 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/examples.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Architecture Lab: Part A 3 | * 4 | * High level specs for the functions that the students will rewrite 5 | * in Y86-64 assembly language 6 | */ 7 | 8 | /* $begin examples */ 9 | /* linked list element */ 10 | typedef struct ELE { 11 | long val; 12 | struct ELE *next; 13 | } *list_ptr; 14 | 15 | /* sum_list - Sum the elements of a linked list */ 16 | long sum_list(list_ptr ls) 17 | { 18 | long val = 0; 19 | while (ls) { 20 | val += ls->val; 21 | ls = ls->next; 22 | } 23 | return val; 24 | } 25 | 26 | /* rsum_list - Recursive version of sum_list */ 27 | long rsum_list(list_ptr ls) 28 | { 29 | if (!ls) 30 | return 0; 31 | else { 32 | long val = ls->val; 33 | long rest = rsum_list(ls->next); 34 | return val + rest; 35 | } 36 | } 37 | 38 | /* copy_block - Copy src to dest and return xor checksum of src */ 39 | long copy_block(long *src, long *dest, long len) 40 | { 41 | long result = 0; 42 | while (len > 0) { 43 | long val = *src++; 44 | *dest++ = val; 45 | result ^= val; 46 | len--; 47 | } 48 | return result; 49 | } 50 | /* $end examples */ 51 | -------------------------------------------------------------------------------- /C4/practice/4.4/rsum.asm: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .build_version macos, 10, 15 sdk_version 10, 15, 6 3 | .globl _rsum ## -- Begin function rsum 4 | .p2align 4, 0x90 5 | _rsum: ## @rsum 6 | .cfi_startproc 7 | ## %bb.0: 8 | pushq %rbp 9 | .cfi_def_cfa_offset 16 10 | .cfi_offset %rbp, -16 11 | movq %rsp, %rbp 12 | .cfi_def_cfa_register %rbp 13 | subq $32, %rsp 14 | movq %rdi, -16(%rbp) 15 | movq %rsi, -24(%rbp) 16 | cmpq $0, -24(%rbp) 17 | jg LBB0_2 18 | ## %bb.1: 19 | movq $0, -8(%rbp) 20 | jmp LBB0_3 21 | LBB0_2: 22 | movq -16(%rbp), %rax 23 | movq (%rax), %rax 24 | movq -16(%rbp), %rcx 25 | addq $8, %rcx 26 | movq -24(%rbp), %rdx 27 | subq $1, %rdx 28 | movq %rcx, %rdi 29 | movq %rdx, %rsi 30 | movq %rax, -32(%rbp) ## 8-byte Spill 31 | callq _rsum 32 | movq -32(%rbp), %rcx ## 8-byte Reload 33 | addq %rax, %rcx 34 | movq %rcx, -8(%rbp) 35 | LBB0_3: 36 | movq -8(%rbp), %rax 37 | addq $32, %rsp 38 | popq %rbp 39 | retq 40 | .cfi_endproc 41 | ## -- End function 42 | .subsections_via_symbols 43 | -------------------------------------------------------------------------------- /labs/data/btest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CS:APP Data Lab 3 | */ 4 | 5 | /* Declare different function types */ 6 | typedef int (*funct_t) (void); 7 | typedef int (*funct1_t)(int); 8 | typedef int (*funct2_t)(int, int); 9 | typedef int (*funct3_t)(int, int, int); 10 | 11 | /* Combine all the information about a function and its tests as structure */ 12 | typedef struct { 13 | char *name; /* String name */ 14 | funct_t solution_funct; /* Function */ 15 | funct_t test_funct; /* Test function */ 16 | int args; /* Number of function arguments */ 17 | char *ops; /* List of legal operators. Special case: "$" for floating point */ 18 | int op_limit; /* Max number of ops allowed in solution */ 19 | int rating; /* Problem rating (1 -- 4) */ 20 | int arg_ranges[3][2]; /* Argument ranges. Always defined for 3 args, even if */ 21 | /* the function takes fewer. Special case: First arg */ 22 | /* must be set to {1,1} for f.p. puzzles */ 23 | } test_rec, *test_ptr; 24 | 25 | extern test_rec test_set[]; 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /C2/note/README.md: -------------------------------------------------------------------------------- 1 | # Chapter2: Representing and Manipulating Information 2 | 3 | Modern computer store and process information represented as **two-valued** signals. 4 | 5 | Three most important representations of numbers: 6 | 7 | - Unsigned: encodings are base on traditional binary notation, representing number greater than or equal to 0. 8 | - Two's-complement: encodings are the most common way to represent signed integers, that is, numbers that may be either positive or negative. 9 | - Floating-point: encodings are base-two version of scientific notation for representing real numbers. 10 | 11 | sections: 12 | 13 | 1. [Storage Notation](./storage/notation.md) 14 | 2. [Data Sizes](./storage/data_size.md) 15 | 3. [Addressing and ByteOrdering](./storage/ordering.md) 16 | 4. [Representing Code](./storage/code.md) 17 | 5. [Introduction to Boolean Algebra And apply in C](./operations/boolean_algebra.md) 18 | 6. [Logical Operations in C](./operations/logical.md) 19 | 7. [Shift Operations in C](./operations/shift.md) 20 | 8. [Integer Representations](./integer/representations.md) 21 | 9. [Integer arithmetic](./integer/arithmetic.md) 22 | 10. [Floating Point](./floating/README.md) 23 | -------------------------------------------------------------------------------- /labs/archlab/sim/y86-code/asumr.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 | 7 | # Array of 4 elements 8 | .align 8 9 | array: .quad 0x000d000d000d 10 | .quad 0x00c000c000c0 11 | .quad 0x0b000b000b00 12 | .quad 0xa000a000a000 13 | 14 | main: irmovq array,%rdi 15 | irmovq $4,%rsi 16 | call rsum # rsum(array, 4) 17 | ret 18 | 19 | /* $begin rsum-ys */ 20 | # long rsum(long *start, long count) 21 | # start in %rdi, count in %rsi 22 | rsum: 23 | xorq %rax,%rax # Set return value to 0 24 | andq %rsi,%rsi # Set condition codes 25 | jle return # If count <= 0, return 0 26 | pushq %rbx # Save callee-saved register 27 | mrmovq (%rdi),%rbx # Get *start 28 | irmovq $-1,%r10 29 | addq %r10,%rsi # count-- 30 | irmovq $8,%r10 31 | addq %r10,%rdi # start++ 32 | call rsum 33 | addq %rbx,%rax # Add *start to sum 34 | popq %rbx # Restore callee-saved register 35 | return: 36 | ret 37 | /* $end rsum-ys */ 38 | 39 | # The stack starts here and grows to lower addresses 40 | .pos 0x200 41 | stack: 42 | 43 | -------------------------------------------------------------------------------- /labs/archlab/sim/misc/node.h: -------------------------------------------------------------------------------- 1 | #ifndef NODE_H 2 | typedef enum { N_QUOTE, N_VAR, N_NUM, N_AND, N_OR, N_NOT, N_COMP, N_ELE, N_CASE } node_type_t; 3 | 4 | typedef struct NODE { 5 | node_type_t type; 6 | int isbool; /* Is this node a Boolean expression? */ 7 | char *sval; 8 | struct NODE *arg1; 9 | struct NODE *arg2; 10 | int ref; /* For var, how many times has it been referenced? */ 11 | struct NODE *next; 12 | } node_rec, *node_ptr; 13 | 14 | void init_node(int argc, char **argv); 15 | void finish_node(int check_ref); 16 | 17 | node_ptr make_quote(char *qstring); 18 | node_ptr make_var(char *name); 19 | node_ptr make_num(char *name); 20 | void set_bool(node_ptr varnode); 21 | node_ptr make_not(node_ptr arg); 22 | node_ptr make_and(node_ptr arg1, node_ptr arg2); 23 | node_ptr make_or(node_ptr arg1, node_ptr arg2); 24 | node_ptr make_comp(node_ptr op, node_ptr arg1, node_ptr arg2); 25 | node_ptr make_ele(node_ptr arg1, node_ptr arg2); 26 | node_ptr make_case(node_ptr arg1, node_ptr arg2); 27 | 28 | node_ptr concat(node_ptr n1, node_ptr n2); 29 | 30 | void insert_code(node_ptr qstring); 31 | void add_arg(node_ptr var, node_ptr qstring, int isbool); 32 | void gen_funct(node_ptr var, node_ptr expr, int isbool); 33 | #define NODE_H 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /C3/note/array/README.md: -------------------------------------------------------------------------------- 1 | # Array Allocation and Access 2 | 3 | Array in C are one means of aggregating scalar data into larger data types. 4 | 5 | For example: 6 | 7 | ```cpp 8 | char A[12]; 9 | char *B[8]; 10 | int C[6]; 11 | double *D[5]; 12 | ``` 13 | 14 | |Array | Element size| Total size| Start address| Element i | 15 | | - | - | - | - | - | 16 | | A | 1 | 12 | x | x + i | 17 | | B | 8 | 64 | x | x + 8 * i| 18 | | C | 4 | 24 | x | x + 4 * i| 19 | | D | 8 | 40 | x | x + 8 * i| 20 | 21 | Practice: 22 | 23 | - [3.36](../../practice/3.36/README.md) 24 | 25 | ## Pointer Arithmetic 26 | 27 | Pay attention: the array reference `A[i]` is identical to the expression `*(A+i)`. 28 | 29 | Practice: 30 | 31 | - [3.37](../../practice/3.37/README.md) 32 | 33 | ## Nested Arrays 34 | 35 | For an array declared as: 36 | 37 | ```cpp 38 | T D[R][C]; 39 | ``` 40 | 41 | Any element of `D[i][j]` is at memory address: 42 | 43 | $\&D[i][j] = x_d + L(C * i + j)$ 44 | 45 | Practices: 46 | 47 | - [3.38](../../practice/3.38/README.md) 48 | - [3.39](../../practice/3.39/README.md) 49 | - [3.40](../../practice/3.40/README.md) 50 | -------------------------------------------------------------------------------- /C1/note/network/README.md: -------------------------------------------------------------------------------- 1 | # Systems Communicate with Other Systems Using Networks 2 | 3 | Up to this point in our tour of system, we have treated a system as an isolated collection of hardware and software. In practice, modern systems are often linked to other systems by networks. 4 | 5 | From the point of view of an individual system, the network can be viewed as just another I/O device. 6 | ![A network is another I/O device](https://img-blog.csdnimg.cn/20200911082952919.png) 7 | 8 | For example, we could use the familiar telnet application to run hello on a remote machine: 9 | 10 | 1. Suppose we use a telnet client running on our local machine to connect to a telnet server on a remote machine. After we log in to the remote machine and run a shell, the remote shell is waiting to receive an input command. 11 | 2. After we type in the hello string to the telnet client and hit the enter key, the client sends the string to the telnet server. 12 | 3. After the telnet server receives the string from the network, it passes it along to the remote shell program. 13 | 4. Next, the remote shell runs the hello program and passes the output line back to the telnet server. 14 | 5. Finally, the telnet server forward the output string across the network to the telnet client, which prints the output string on our local terminal. 15 | --------------------------------------------------------------------------------