diff options
| -rw-r--r-- | 4/practice_problem_4_10_xor_equal.c | 1 | ||||
| -rw-r--r-- | 4/practice_problem_4_11_rewrite.c | 12 | ||||
| -rw-r--r-- | 4/practice_problem_4_12_median.c | 6 | ||||
| -rw-r--r-- | 4/practice_problem_4_13_irmovq_trace.c | 23 | ||||
| -rw-r--r-- | 4/practice_problem_4_3_iaddq.c | 79 | ||||
| -rw-r--r-- | 4/practice_problem_4_4_rsum.c | 50 | ||||
| -rw-r--r-- | 4/practice_problem_4_5.c | 25 | ||||
| -rw-r--r-- | 4/practice_problem_4_6.c | 23 | ||||
| -rw-r--r-- | 4/practice_problem_4_7_and_8_ambiguities.c | 26 | ||||
| -rw-r--r-- | 4/practice_problem_4_9_hcl_xor.hcl | 3 |
10 files changed, 248 insertions, 0 deletions
diff --git a/4/practice_problem_4_10_xor_equal.c b/4/practice_problem_4_10_xor_equal.c new file mode 100644 index 0000000..96c5054 --- /dev/null +++ b/4/practice_problem_4_10_xor_equal.c @@ -0,0 +1 @@ +bool eq = !any(bit_xor(A)) diff --git a/4/practice_problem_4_11_rewrite.c b/4/practice_problem_4_11_rewrite.c new file mode 100644 index 0000000..a1e9eac --- /dev/null +++ b/4/practice_problem_4_11_rewrite.c @@ -0,0 +1,12 @@ +word Min3 = [ + A <= B && A <= C : A; + B <= C : B; + 1 : C; +]; + +word Min3 = [ + C <= B && C <= A : C; + B <= A : B; + 1 : A; +]; + diff --git a/4/practice_problem_4_12_median.c b/4/practice_problem_4_12_median.c new file mode 100644 index 0000000..aabd56e --- /dev/null +++ b/4/practice_problem_4_12_median.c @@ -0,0 +1,6 @@ +word Median3 = [ + (B <= A && A <= C) || (C <= A && A <= B) : A; + (A <= B && B <= C) || (C <= B && B <= A) : B; + 1 : C; +]; + diff --git a/4/practice_problem_4_13_irmovq_trace.c b/4/practice_problem_4_13_irmovq_trace.c new file mode 100644 index 0000000..47e13b5 --- /dev/null +++ b/4/practice_problem_4_13_irmovq_trace.c @@ -0,0 +1,23 @@ +/* +stage Generic Specific + irmovq kv, rB irmovq $128, %rsp + +Fetch icode:ifun <- M_1[PC] 0x3:0x0 <- M_1[PC] + rA:rB <- M_1[PC+1] F:0x4 <- M_1[PC+1] + valC <- M_8[PC+2] $0x80 <- M_8[PC+2] + valP <- PC + 10 valP <- PC + 10 + +Execute valE <- 0 + valC valE <- $0x80 + +Memory + +Write Back R[rB] <- valE %rsp <- $0x80 + +PC Update PC <- valP PC <- PC + 10 + + +PC <- PC + 10 +rsp <- $0x80 + +forgot to substitute the addresses from diagram 4.17. +*/ diff --git a/4/practice_problem_4_3_iaddq.c b/4/practice_problem_4_3_iaddq.c new file mode 100644 index 0000000..4e8add2 --- /dev/null +++ b/4/practice_problem_4_3_iaddq.c @@ -0,0 +1,79 @@ +#include <stdio.h> + +long sum(long *start, long count) +{ + long sum = 0; + while (count) { + sum += *start; + start++; + count--; + } + return sum; +} + +int main() { + long start[] = { 42, 11, 12 }; + return printf("%d\n", sum(start, (long)1)); +} + +/* +long sum(long *start, long count) +start in %rdi, count in %rsi +sum: + movl $0, %eax sum = 0 + jmp .12 Goto test +.L3: loop: + addq (%rdi), %rax Add *start + addq $8, %rdi start++ + subq $1, %rsi count-- +.L2: test: + testq %rsi, %rsi Test sum + jne .L3 If !=O, goto loop + rep; ret Return +*/ + +/* +long sum(long *start, long count) +start in %rdi, count in %rsi +sum: + irmovq $8,%r8 Constant 8 + irmovq $1,%r9 Constant 1 + xorq %rax,%rax sum = 0 + andq %rsi,%rsi Set cc + jmp test Goto test +loop: + mrmovq (%rdi), %r10 Get *start + addq %r10,%rax Add to sum + addq %r8,%rdi start++ + subq %r9,%rsi count--. Set CC +test: + jne loop Stop when 0 + ret +*/ + +/* Using iaddq +First byte +- C, 12 +- 0, first variant of iadd + +Second byte +- F, 15, absence of first argument +- rB, register number 0-15 to add V to + +Other 0-8 bytes V + +long sum(long *start, long count) +start in %rdi, count in %rsi +sum: + xorq %rax,%rax sum = 0 + andq %rsi,%rsi Set cc (nop), ZF=0 if count != 0 else 1, SF=0 if count > 0, OF=0 + jmp test Goto test +loop: + mrmovq (%rdi), %r10 Get *start + addq %r10,%rax Add to sum + iaddq $8,%rdi start++ + iaddq $-1,%rsi count--. Set CC +test: + jne loop Stop when 0, (~ZF) + ret +*/ diff --git a/4/practice_problem_4_4_rsum.c b/4/practice_problem_4_4_rsum.c new file mode 100644 index 0000000..44960e6 --- /dev/null +++ b/4/practice_problem_4_4_rsum.c @@ -0,0 +1,50 @@ +#include <stdio.h> + +long rsum(long *start, long count) { + if (count <= 0) + return 0; + return *start + rsum(start + 1, count - 1); +} + +int main() { + long nums[] = {1, 2, 3}; + printf("%d\n", rsum(nums, 3)); +} + +/* +long rsum(long *start, long count) +start in %rdi, count in %rsi +test: + xorq %rax, %rax return = 0 + andq %rsi, %rsi, Set CC again? + jle return Count <= 0, or jump + push %rbx keep original rbx around + mrmovq (%rdi), %rbx Get *start + irmovq $8,%r8 Constant 8 + irmovq $1,%r9 Constant 1 + addq %r8,%rdi start++ + subq %r9,%rsi count--. Set CC + call rsum Recurse + addq %rbx, %rax Accumulate + popq %rbx Restore original rbx +return: + ret +*/ + +/* +0000000000001149 <rsum>: + 1149: 48 85 f6 test %rsi,%rsi + 114c: 7e 16 jle 1164 <rsum+0x1b> + + 114e: 53 push %rbx + 114f: 48 8b 1f mov (%rdi),%rbx + 1152: 48 83 ee 01 sub $0x1,%rsi + 1156: 48 83 c7 08 add $0x8,%rdi + 115a: e8 ea ff ff ff call 1149 <rsum> + 115f: 48 01 d8 add %rbx,%rax + 1162: 5b pop %rbx Return accumulated %rbx + 1163: c3 ret + + 1164: b8 00 00 00 00 mov $0x0,%eax Return 0 after first test + 1169: c3 ret +*/ diff --git a/4/practice_problem_4_5.c b/4/practice_problem_4_5.c new file mode 100644 index 0000000..604de44 --- /dev/null +++ b/4/practice_problem_4_5.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +/* +long sum(long *start, long count) +start in %rdi, count in %rsi +sum: + irmovq $8,%r8 Constant 8 + irmovq $1,%r9 Constant 1 + xorq %rax,%rax sum = 0 + andq %rsi,%rsi Set cc + jmp test Goto test +loop: + mrmovq (%rdi), %r10 Get *start + xorq %r11, %r11 Constant 0 + subq %r10, %r11 -x + jle add if x was positive, skip + rrmovq %r11, %r10 x = -x +add: + addq %r10,%rax Add to sum + addq %r8,%rdi start++ + subq %r9,%rsi count--. Set CC +test: + jne loop Stop when 0 + ret +*/ diff --git a/4/practice_problem_4_6.c b/4/practice_problem_4_6.c new file mode 100644 index 0000000..c6808b7 --- /dev/null +++ b/4/practice_problem_4_6.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +/* +long sum(long *start, long count) +start in %rdi, count in %rsi +sum: + irmovq $8,%r8 Constant 8 + irmovq $1,%r9 Constant 1 + xorq %rax,%rax sum = 0 + andq %rsi,%rsi Set cc + jmp test Goto test +loop: + mrmovq (%rdi), %r10 Get *start + xorq %r11, %r11 Constant 0 + subq %r10, %r11 -x + cmovg %r11, %r10 x = -x, if -x was positive + addq %r10,%rax Add to sum + addq %r8,%rdi start++ + subq %r9,%rsi count--. Set CC +test: + jne loop Stop when 0 + ret +*/ diff --git a/4/practice_problem_4_7_and_8_ambiguities.c b/4/practice_problem_4_7_and_8_ambiguities.c new file mode 100644 index 0000000..043a692 --- /dev/null +++ b/4/practice_problem_4_7_and_8_ambiguities.c @@ -0,0 +1,26 @@ +/* +pushtest: + movq %rsp, %rax Copy stack pointer + pushq %rsp Push stack pointer, original or after incrementing + popq %rdx Pop the value back + subq %rdx, %rax Always 0 on this machine + +It must first push the original rsp decremented address and then decrement rsp itself, otherwise a difference is expected after pushing and popping. +*/ + +/* +poptest: + movq %rsp, %rdi Save the original sp + pushq $0xabcd Push test value + popq %rsp Pop test value to rsp + movq %rsp, %rax Set popped value as return value + movq %rdi, %rsp + ret + +Rsp gets set the value in memory at rsp. + +From solutions: +mrmovq (%rsp), %rsp + +I guess it just reads the stack value and overwrites it into rsp +*/ diff --git a/4/practice_problem_4_9_hcl_xor.hcl b/4/practice_problem_4_9_hcl_xor.hcl new file mode 100644 index 0000000..bb474e7 --- /dev/null +++ b/4/practice_problem_4_9_hcl_xor.hcl @@ -0,0 +1,3 @@ +bool eq = (a && b) || (!a && !b) + +bool xor = (!a && b) || (a && !b) |
