aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/ask/lista5
diff options
context:
space:
mode:
authorFranciszek Malinka <franciszek.malinka@gmail.com>2021-03-29 01:45:30 +0200
committerFranciszek Malinka <franciszek.malinka@gmail.com>2021-03-29 01:45:30 +0200
commit0843094b9d862f015077867263c775f4f0b3af5e (patch)
tree27cccc81cac833bbd7c74beb0e9256ad8a4d0153 /Semestr 4/ask/lista5
parentefca1869bbb5d3a7764d6e8be406246a09cd0634 (diff)
update
Diffstat (limited to 'Semestr 4/ask/lista5')
-rw-r--r--Semestr 4/ask/lista5/zad1.asm15
-rw-r--r--Semestr 4/ask/lista5/zad1.c25
-rw-r--r--Semestr 4/ask/lista5/zad1.s98
-rw-r--r--Semestr 4/ask/lista5/zad2.asm15
-rw-r--r--Semestr 4/ask/lista5/zad2.c44
-rw-r--r--Semestr 4/ask/lista5/zad3.c60
-rw-r--r--Semestr 4/ask/lista5/zad4.c66
7 files changed, 323 insertions, 0 deletions
diff --git a/Semestr 4/ask/lista5/zad1.asm b/Semestr 4/ask/lista5/zad1.asm
new file mode 100644
index 0000000..22aef29
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad1.asm
@@ -0,0 +1,15 @@
+puzzle: testl %esi, %esi
+ je .L4 ; jesli esi == 0, skocz do L4
+ xorl %edx, %edx ; edx := 0
+ xorl %eax, %eax ; eax := 0
+.L3: movl %edi, %ecx ; ecx := edi
+ andl $1, %ecx ; ecx &= 1
+ addl %ecx, %eax ; eax += eax
+ sarq %rdi ; rdi >>= 1
+ incl %edx, ; edx++
+ cmpl %edx, %esi ; edx - esi != 0? <=> edx != esi
+ jne .L3 ; jesli tak, skocz do L3
+ ret
+.L4 movl %esi, %eax ; return esi
+ ret
+
diff --git a/Semestr 4/ask/lista5/zad1.c b/Semestr 4/ask/lista5/zad1.c
new file mode 100644
index 0000000..9379bb2
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad1.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int puzzle(long x /* rdi */ , unsigned n /* rsi */) {
+ if (n == 0) {
+ return n;
+ }
+ int t = 0; // edx := 0
+ int result = 0; // eax := 0
+ do {
+ int m = x & 0xffffffff; // ecx := edi
+ m &= 1;
+ result += m;
+ x >>= 1;
+ t++;
+ } while (t != n);
+ return result; // ????
+}
+
+int main() {
+ long x;
+ unsigned n;
+ scanf("%ld%u", &x, &n);
+ return puzzle(x, n);
+} \ No newline at end of file
diff --git a/Semestr 4/ask/lista5/zad1.s b/Semestr 4/ask/lista5/zad1.s
new file mode 100644
index 0000000..b935b9e
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad1.s
@@ -0,0 +1,98 @@
+ .file "zad1.c"
+ .text
+ .p2align 4
+ .globl puzzle
+ .type puzzle, @function
+puzzle:
+.LFB23:
+ .cfi_startproc
+ endbr64
+ xorl %r8d, %r8d
+ testl %esi, %esi
+ je .L1
+ xorl %eax, %eax
+ .p2align 4,,10
+ .p2align 3
+.L3:
+ movl %edi, %edx
+ addl $1, %eax
+ sarq %rdi
+ andl $1, %edx
+ addl %edx, %r8d
+ cmpl %esi, %eax
+ jne .L3
+.L1:
+ movl %r8d, %eax
+ ret
+ .cfi_endproc
+.LFE23:
+ .size puzzle, .-puzzle
+ .section .rodata.str1.1,"aMS",@progbits,1
+.LC0:
+ .string "%ld%u"
+ .section .text.startup,"ax",@progbits
+ .p2align 4
+ .globl main
+ .type main, @function
+main:
+.LFB24:
+ .cfi_startproc
+ endbr64
+ subq $40, %rsp
+ .cfi_def_cfa_offset 48
+ leaq .LC0(%rip), %rdi
+ movq %fs:40, %rax
+ movq %rax, 24(%rsp)
+ xorl %eax, %eax
+ leaq 12(%rsp), %rdx
+ leaq 16(%rsp), %rsi
+ call __isoc99_scanf@PLT
+ movl 12(%rsp), %ecx
+ movq 16(%rsp), %rax
+ xorl %r8d, %r8d
+ testl %ecx, %ecx
+ je .L8
+ xorl %edx, %edx
+ .p2align 4,,10
+ .p2align 3
+.L10:
+ movl %eax, %esi
+ addl $1, %edx
+ sarq %rax
+ andl $1, %esi
+ addl %esi, %r8d
+ cmpl %ecx, %edx
+ jne .L10
+.L8:
+ movq 24(%rsp), %rax
+ xorq %fs:40, %rax
+ jne .L16
+ movl %r8d, %eax
+ addq $40, %rsp
+ .cfi_remember_state
+ .cfi_def_cfa_offset 8
+ ret
+.L16:
+ .cfi_restore_state
+ call __stack_chk_fail@PLT
+ .cfi_endproc
+.LFE24:
+ .size main, .-main
+ .ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
+ .section .note.GNU-stack,"",@progbits
+ .section .note.gnu.property,"a"
+ .align 8
+ .long 1f - 0f
+ .long 4f - 1f
+ .long 5
+0:
+ .string "GNU"
+1:
+ .align 8
+ .long 0xc0000002
+ .long 3f - 2f
+2:
+ .long 0x3
+3:
+ .align 8
+4:
diff --git a/Semestr 4/ask/lista5/zad2.asm b/Semestr 4/ask/lista5/zad2.asm
new file mode 100644
index 0000000..7fe4364
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad2.asm
@@ -0,0 +1,15 @@
+puzzle2:
+ movq %rdi, %rax ; rax := rdi
+.L3: movb (%rax), %r9b ; r9b := *rax
+ leaq 1(%rax), %r8 ; r8 := rax + 1
+ movq %rsi, %rdx ; rdx := *rsi
+.L2: movb (%rdx), %cl ; cl := *rdx
+ incq %rdx ; rdx++
+ testb %cl, %cl ; if (cl == 0)
+ je .L4 ; then jump to L4
+ cmpb %cl, %r9b ; if (cl != r9b)
+ jne .L2 ; then jump to L2
+ movq %r8, %rax ; rax := r8
+ jmp .L3 ; jump to L3
+.L4 subq %rdi, %rax ; rax -= rdi
+ ret \ No newline at end of file
diff --git a/Semestr 4/ask/lista5/zad2.c b/Semestr 4/ask/lista5/zad2.c
new file mode 100644
index 0000000..ab9cb54
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad2.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdint.h>
+
+long puzzle2(char *rdi /* rdi */, char *rsi /* rsi */) {
+ char *rax = rdi;
+L3:
+ char r9b = *rax;
+ char *r8 = rax + 1;
+ char *rdx = rsi;
+L2:
+ char cl = *rdx;
+ rdx++;
+ if (cl == 0) {
+ goto L4;
+ }
+ if (cl != r9b) {
+ goto L2;
+ }
+ rax = r8;
+ goto L3;
+L4:
+ return rax - rdi;
+}
+
+
+// funkcja sprawdza jaka jest pierwsza litera z s która nie występuje w d
+// jesli wszystkie występują, to zwróci długość s.
+long puzzle2_decoded(char *s /* rdi */, char *d /* rsi */) {
+ for (char *result = s ; ; result++) {
+ char first = *result;
+ char *crawl = d;
+ for (char *crawl = d; *crawl != first; crawl++) {
+ if (*crawl == 0) {
+ return result - s;
+ }
+ }
+ }
+}
+
+
+
+int main() {
+
+} \ No newline at end of file
diff --git a/Semestr 4/ask/lista5/zad3.c b/Semestr 4/ask/lista5/zad3.c
new file mode 100644
index 0000000..1ce9067
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad3.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+uint32_t puzzle3(uint32_t n, uint32_t d);
+
+// uint32_t divv(uint32_t edi, uint32_t esi) {
+// uint64_t rdi = edi;
+// uint64_t rsi = esi;
+// rsi <<= 32;
+// uint8_t edx = 32;
+// uint32_t ecx = 0x80000000;
+// uint32_t eax = 0;
+// L3:
+// rdi += rdi;
+// int64_t r8 = rdi;
+// r8 -= rsi;
+// if (r8 < 0) {
+// goto L2;
+// }
+// eax |= ecx;
+// rdi = r8;
+// L2:
+// ecx >>= 1;
+// edx--;
+// if (edx != 0) {
+// goto L3;
+// }
+// return eax;
+// }
+
+uint32_t decoded(uint32_t n, uint32_t d) {
+ uint64_t N = n;
+ uint64_t D = (uint64_t)d << 32;
+
+ uint32_t bit = 0x80000000;
+ uint32_t result = 0;
+ for (uint32_t edx = 32; edx > 0; edx--) {
+ N += N;
+ if ((int64_t)(N - D) >= 0) {
+ result |= bit;
+ N -= D;
+ }
+ bit >>= 1;
+ }
+ return result;
+}
+
+int main() {
+ for (uint32_t n=0; n <= 1000; ++n) {
+ for (uint32_t d=1; d <= 1000; ++d) {
+ if (puzzle3(n,d) != decoded(n, d)) {
+ printf("%d %d\n", n, d);
+ printf("%u %u\n", puzzle3(n, d), decoded(n, d));
+ return 0;
+ }
+ }
+ }
+ printf("%d\n", puzzle3(1000000000, 2));
+} \ No newline at end of file
diff --git a/Semestr 4/ask/lista5/zad4.c b/Semestr 4/ask/lista5/zad4.c
new file mode 100644
index 0000000..a6927fb
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad4.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+int puzzle4(long *a, long v, uint64_t s, uint64_t e);
+
+long tab[100];
+
+int assembly_like(long *rdi, long rsi, uint64_t rdx, uint64_t rcx) {
+ uint64_t rax = rcx;
+ rax -= rdx;
+ rax >>= 1;
+ rax += rdx;
+ if (rcx < rdx) {
+ goto L5;
+ }
+ long r8 = *(rdi + rax*8);
+ if (rsi == r8) {
+ goto L10;
+ }
+ if (rsi > r8) {
+ goto L11;
+ }
+ rdx = rax + 1;
+ assembly_like(rdi, rsi, rdx, rcx);
+L10:
+ return (int)rax;
+L11:
+ rcx = rax - 1;
+ assembly_like(rdi, rsi, rdx, rcx);
+ return (int)rax;
+L5:
+ return -1;
+}
+
+
+// binsearch :)
+int decode(long *a, long v, uint64_t s, uint64_t e) {
+ int result = (e + s) / 2;
+ if (e < s) {
+ return -1;
+ }
+ long val = a[result];
+ if (v == val) {
+ return result;
+ }
+ if (v < val) {
+ e = result - 1;
+ }
+ else {
+ s = result + 1;
+ }
+ return decode(a, v, s, e);
+}
+
+int main() {
+ uint64_t n;
+ long v;
+ scanf("%ld %ld", &n, &v);
+ for (int i = 0; i < n; i++) {
+ scanf("%ld", &tab[i]);
+ }
+ printf("%d\n", puzzle4(tab, v, 0, n-1));
+ printf("%d\n", decode(tab, v, 0, n-1));
+
+} \ No newline at end of file