aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/ask
diff options
context:
space:
mode:
Diffstat (limited to 'Semestr 4/ask')
-rw-r--r--Semestr 4/ask/lista6/puzzle.c28
-rw-r--r--Semestr 4/ask/lista6/puzzle.s22
-rw-r--r--Semestr 4/ask/lista6/puzzle7.s29
-rw-r--r--Semestr 4/ask/lista6/puzzle8.c25
-rw-r--r--Semestr 4/ask/lista6/puzzle8.s30
-rw-r--r--Semestr 4/ask/lista6/test.c9
-rw-r--r--Semestr 4/ask/lista6/zad7.c18
-rw-r--r--Semestr 4/ask/materiały/x86-64-psABI-1.0.pdfbin0 -> 496280 bytes
8 files changed, 161 insertions, 0 deletions
diff --git a/Semestr 4/ask/lista6/puzzle.c b/Semestr 4/ask/lista6/puzzle.c
new file mode 100644
index 0000000..6a18d20
--- /dev/null
+++ b/Semestr 4/ask/lista6/puzzle.c
@@ -0,0 +1,28 @@
+```c=
+ 0: 55 pushq %rbp
+ 1: 48 89 e5 movq %rsp, %rbp
+ 4: 48 83 ec 10 subq $16, %rsp
+ 8: 48 89 e0 movq %rsp, %rax #wsk stosu to rax
+ b: 48 8d 0c fd 0f 00 00 00 leaq 15(,%rdi,8), %rcx #n * rozmiar longa + 15 do rcx (+15 to kwestia wyrównania/sufit)
+ 13: 48 83 e1 f0 andq $-16, %rcx #zerujemy ostatnie 4 bity
+ 17: 48 29 c8 subq %rcx, %rax #oblicz nowy wskaźnik stosu
+ 1a: 48 89 c4 movq %rax, %rsp #nowy wskaźnik wrzuć do rsp
+ 1d: 48 8d 4d f8 leaq -8(%rbp), %rcx
+ 21: 48 89 4c f8 f8 movq %rcx, -8(%rax,%rdi,8)
+ 26: 48 c7 45 f8 00 00 00 00 movq $0, -8(%rbp)
+ 2e: 48 85 ff testq %rdi, %rdi
+ 31: 7e 1d jle 29 <_aframe+0x50>
+ 33: 31 c9 xorl %ecx, %ecx
+ 35: 66 2e 0f 1f 84 00 00 00 00 00 nopw %cs:(%rax,%rax)
+ 3f: 90 nop
+ 40: 48 89 14 c8 movq %rdx, (%rax,%rcx,8)
+ 44: 48 ff c1 incq %rcx
+ 47: 48 39 cf cmpq %rcx, %rdi
+ 4a: 75 f4 jne -12 <_aframe+0x40>
+ 4c: 48 89 7d f8 movq %rdi, -8(%rbp)
+ 50: 48 8b 04 f0 movq (%rax,%rsi,8), %rax
+ 54: 48 8b 00 movq (%rax), %rax
+ 57: 48 89 ec movq %rbp, %rsp #powrót do poprzedniej ramki
+ 5a: 5d popq %rbp #wrzucenie do rbp poprzedniego adresu
+ 5b: c3 retq
+``` \ No newline at end of file
diff --git a/Semestr 4/ask/lista6/puzzle.s b/Semestr 4/ask/lista6/puzzle.s
new file mode 100644
index 0000000..3a39398
--- /dev/null
+++ b/Semestr 4/ask/lista6/puzzle.s
@@ -0,0 +1,22 @@
+ .globl puzzle
+
+ .text
+puzzle:
+ push %rbp # adres koncu ramki, callee-saved
+ xorl %eax, %eax # zeruje %rax
+ mov %rsi, %rbp # rbp := rsi, czyli rbp := p
+ push %rbx # rbx na stos, callee-saved
+ mov %rdi, %rbx # rbx := rdi, czyli rbx := n
+ sub $24, %rsp # rsp -= 24, czyli przesun rsp o 3 bajty w dół
+ test %rdi, %rdi # rdi == 0?, czyli n == 0?
+ jle .L1 # jesli tak, skocz do L1
+ lea 8(%rsp), %rsi # w p.w. rsi := rsp + 8
+ lea (%rdi,%rdi), %rdi # rdi := 2*rdi
+ call puzzle # wywolanie rekurencyjne puzzle
+ add 8(%rsp), %rax # rax := rax + *(rsp + 8)
+ add %rax, %rbx # rbx := rbx + rax
+.L1: mov %rbx, (%rbp) # *rbp := rbx, czyli *p := rbx
+ add $24, %rsp # rsp := rsp + 24 (przesun adres stacka o 3 bajty)
+ pop %rbx # przywracamy rbx
+ pop %rbp # przywracamy rbp
+ ret
diff --git a/Semestr 4/ask/lista6/puzzle7.s b/Semestr 4/ask/lista6/puzzle7.s
new file mode 100644
index 0000000..6d26961
--- /dev/null
+++ b/Semestr 4/ask/lista6/puzzle7.s
@@ -0,0 +1,29 @@
+ .global puzzle7
+
+ .text
+puzzle7:
+ movq %rsi, -40(%rsp)
+ movq %rdx, -32(%rsp)
+ movq %rcx, -24(%rsp)
+ movq %r8, -16(%rsp)
+ movq %r9, -8(%rsp)
+ movl $8, -72(%rsp)
+ leaq 8(%rsp), %rax
+ movq %rax, -64(%rsp)
+ leaq -48(%rsp), %rax
+ movq %rax, -56(%rsp)
+ movl $0, %eax
+ jmp .L2
+.L3: movq -64(%rsp), %rdx
+ leaq 8(%rdx), %rcx
+ movq %rcx, -64(%rsp)
+.L4: addq (%rdx), %rax
+.L2: subq $1, %rdi
+ js .L6
+ cmpl $47, -72(%rsp)
+ ja .L3
+ movl -72(%rsp), %edx
+ addq -56(%rsp), %rdx
+ addl $8, -72(%rsp)
+ jmp .L4
+.L6: ret
diff --git a/Semestr 4/ask/lista6/puzzle8.c b/Semestr 4/ask/lista6/puzzle8.c
new file mode 100644
index 0000000..4d65f9d
--- /dev/null
+++ b/Semestr 4/ask/lista6/puzzle8.c
@@ -0,0 +1,25 @@
+#include <limits.h>
+
+struct T {
+ long min;
+ long max;
+ long mean;
+};
+
+struct T puzzle8(long *a, long n);
+
+struct T decode(long *a, long n) {
+ long maks = LONG_MIN;
+ long mini = LONG_MAX;
+ long sum = 0;
+ for (int i = 0; i < n; i++) {
+ if (maks < a[i]) maks = a[i];
+ if (mini > a[i]) mini = a[i];
+ sum += a[i];
+ }
+ struct T ret;
+ ret.min = mini;
+ ret.max = maks;
+ ret.mean = sum / n;
+ return ret;
+} \ No newline at end of file
diff --git a/Semestr 4/ask/lista6/puzzle8.s b/Semestr 4/ask/lista6/puzzle8.s
new file mode 100644
index 0000000..cc3805d
--- /dev/null
+++ b/Semestr 4/ask/lista6/puzzle8.s
@@ -0,0 +1,30 @@
+# rdi - adres zwracanej struktury (niejawnie)
+# rsi - *a
+# rdx - n
+
+ .global puzzle8
+
+ .text
+puzzle8:
+ movq %rdx, %r11 # r11 := n
+ xorl %r10d, %r10d # r10 := 0
+ xorl %eax, %eax # rax := 0
+ movq $LONG_MIN, %r8 # r8 := 0x800...
+ movq $LONG_MAX, %r9 # r9 := 0x7ff...
+.L2: cmpq %r11, %r10 # if n <= r10
+ jge .L5 # then jump to L5, else
+ movq (%rsi,%r10,8), %rcx # rcx := a[r10]
+ cmpq %rcx, %r9 # if a[r10] < r9
+ cmovg %rcx, %r9 # then r9 = a[r10]
+ cmpq %rcx, %r8 # if a[r10] > r8
+ cmovl %rcx, %r8 # then r8 = a[r10]
+ addq %rcx, %rax # rax += a[r10]
+ incq %r10 # r10++
+ jmp .L2
+.L5: cqto # jeśli rax < 0 to niech rdx zaplonie
+ movq %r9, (%rdi) # rdi->0 := r9
+ idivq %r11 # podziel rax przez r11, efektywnie rax /= r11
+ movq %r8, 8(%rdi) # rdi->8 := r8
+ movq %rax, 16(%rdi) # rdi->16 := rax
+ movq %rdi, %rax # rax := rdi
+ ret \ No newline at end of file
diff --git a/Semestr 4/ask/lista6/test.c b/Semestr 4/ask/lista6/test.c
new file mode 100644
index 0000000..8a84ac2
--- /dev/null
+++ b/Semestr 4/ask/lista6/test.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int main() {
+ int32_t x = 0xc0000000;
+ int32_t y = x + x;
+
+ printf("%d, %d\n", x, y);
+} \ No newline at end of file
diff --git a/Semestr 4/ask/lista6/zad7.c b/Semestr 4/ask/lista6/zad7.c
new file mode 100644
index 0000000..9072504
--- /dev/null
+++ b/Semestr 4/ask/lista6/zad7.c
@@ -0,0 +1,18 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+int wypisuj(int cnt, ...) {
+ va_list v;
+ va_start(v, cnt);
+ int sum = 0;
+ for (int i = 0; i < cnt; i++) {
+ sum += va_arg(v, int);
+ }
+ return sum;
+}
+
+int puzzle7(int cnt, ...);
+
+int main() {
+ printf("%d\n", puzzle7(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
+} \ No newline at end of file
diff --git a/Semestr 4/ask/materiały/x86-64-psABI-1.0.pdf b/Semestr 4/ask/materiały/x86-64-psABI-1.0.pdf
new file mode 100644
index 0000000..2a5a923
--- /dev/null
+++ b/Semestr 4/ask/materiały/x86-64-psABI-1.0.pdf
Binary files differ