From c5fcf7179a83ef65c86c6a4a390029149e518649 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Tue, 5 Oct 2021 21:49:54 +0200 Subject: Duzy commit ze smieciami --- semestr-4/ask/lista5/ask21_lista_5.pdf | Bin 0 -> 91135 bytes semestr-4/ask/lista5/puzzle3.s | 19 +++++++ semestr-4/ask/lista5/puzzle4.s | 24 ++++++++ semestr-4/ask/lista5/puzzle5.s | 24 ++++++++ semestr-4/ask/lista5/zad1.asm | 15 +++++ semestr-4/ask/lista5/zad1.c | 25 +++++++++ semestr-4/ask/lista5/zad1.s | 98 +++++++++++++++++++++++++++++++++ semestr-4/ask/lista5/zad2.asm | 15 +++++ semestr-4/ask/lista5/zad2.c | 44 +++++++++++++++ semestr-4/ask/lista5/zad3.c | 60 ++++++++++++++++++++ semestr-4/ask/lista5/zad4.c | 66 ++++++++++++++++++++++ semestr-4/ask/lista5/zad5.c | 54 ++++++++++++++++++ 12 files changed, 444 insertions(+) create mode 100644 semestr-4/ask/lista5/ask21_lista_5.pdf create mode 100644 semestr-4/ask/lista5/puzzle3.s create mode 100644 semestr-4/ask/lista5/puzzle4.s create mode 100644 semestr-4/ask/lista5/puzzle5.s create mode 100644 semestr-4/ask/lista5/zad1.asm create mode 100644 semestr-4/ask/lista5/zad1.c create mode 100644 semestr-4/ask/lista5/zad1.s create mode 100644 semestr-4/ask/lista5/zad2.asm create mode 100644 semestr-4/ask/lista5/zad2.c create mode 100644 semestr-4/ask/lista5/zad3.c create mode 100644 semestr-4/ask/lista5/zad4.c create mode 100644 semestr-4/ask/lista5/zad5.c (limited to 'semestr-4/ask/lista5') diff --git a/semestr-4/ask/lista5/ask21_lista_5.pdf b/semestr-4/ask/lista5/ask21_lista_5.pdf new file mode 100644 index 0000000..b8c1977 Binary files /dev/null and b/semestr-4/ask/lista5/ask21_lista_5.pdf differ diff --git a/semestr-4/ask/lista5/puzzle3.s b/semestr-4/ask/lista5/puzzle3.s new file mode 100644 index 0000000..f625752 --- /dev/null +++ b/semestr-4/ask/lista5/puzzle3.s @@ -0,0 +1,19 @@ + .global puzzle3 + + .text +puzzle3: # rdi: n; rsi: d + movl %edi, %edi # zeruje 32 starsze bity rdi + salq $32, %rsi # rsi <<= 32 + movl $32, %edx # edx := 32 + movl $0x80000000, %ecx # ecx := MIN_INT + xorl %eax, %eax # eax := 0 +.L3: addq %rdi, %rdi # rdi *= 2 + movq %rdi, %r8 # r8 := rdi + subq %rsi, %r8 # r8 -= rsi + js .L2 # if r8 < 0 then jump to L2 + orl %ecx, %eax # eax |= ecx + movq %r8, %rdi # rdi := r8 +.L2: shrl %ecx # ecx >>= 1 + decl %edx # edx-- + jne .L3 # if (edx != 0) then jump to L3 + ret diff --git a/semestr-4/ask/lista5/puzzle4.s b/semestr-4/ask/lista5/puzzle4.s new file mode 100644 index 0000000..16ca386 --- /dev/null +++ b/semestr-4/ask/lista5/puzzle4.s @@ -0,0 +1,24 @@ +.global puzzle4 + +.text +puzzle4: # argumenty: rdi - long*, rsi - long, rdx - uint64, rcx - uint64 + movq %rcx, %rax # rax := rcx + subq %rdx, %rax # rax -= rdx + shrq %rax # rax >>= 1; // ale logiczne! + addq %rdx, %rax # rax += rdx; + cmpq %rdx, %rcx # if rcx < rdx + jb .L5 # then jump to L5 + movq (%rdi,%rax,8), %r8 # r8 = *(rdi + 8*rax) + cmpq %rsi, %r8 # if rsi == r8 + je .L10 # then jump to L10 + cmpq %rsi, %r8 # if (r8 - rsi < 0) <=> r8 < rsi + jg .L11 # then jump to L11 + leaq 1(%rax), %rdx # rdx := rax + 1 + call puzzle4 # call recursively puzzle4 +.L10: ret # return rax +.L11: leaq -1(%rax), %rcx # rcx := rax - 1 + call puzzle4 # call recursively puzzle4 + ret # return rax +.L5: movl $-1, %eax # eax := -1 + ret # return rax + diff --git a/semestr-4/ask/lista5/puzzle5.s b/semestr-4/ask/lista5/puzzle5.s new file mode 100644 index 0000000..02c3aea --- /dev/null +++ b/semestr-4/ask/lista5/puzzle5.s @@ -0,0 +1,24 @@ +400590 : +400590: 48 83 subq $0x3c, %rsi # rsi -= 0x3c +400594: 48 83 fe 0 cmpq $0x5, %rsi # if rsi > 0x5 +400598: 77 29 ja *0x4005c3 # then jump to line 16 +40059a: ff 24 f5 f8 06 40 00 jmpq *0x4006f8(,%rsi,8) # jump to 19 + rsi +4005a1: 48 8d 04 fd 00 00 00 00 lea 0x0(,%rdi,8),%rax # rax := rdi * 8 +4005a9: c3 retq # return rax +4005aa: 48 89 f8 movq %rdi,%rax # rax := rdi +4005ad: 48 c1 f8 03 sarq $0x3,%rax # rax >>= 3 (arithmetic) +4005b1: c3 retq # return rax +4005b2: 48 89 f8 movq %rdi,%rax # rax := rdi +4005b5: 48 c1 e0 04 shlq $0x4,%rax # rax <<= 4 (logic) +4005b9: 48 29 f8 subq %rdi,%rax # rax -= rdi +4005bc: 48 89 c7 movq %rax,%rdi # rdi := rax +4005bf: 48 0f af ff imulq %rdi,%rdi # rdi *= rdi +4005c3: 48 8d 47 4b leaq 0x4b(%rdi),%rax # rax := 0x4b + rdi +4005c7: c3 retq # return rax + +0x4006f8: 0x4005a1 # line 6 +0x400700: 0x4005a1 # line 6 +0x400708: 0x4005b2 # line 11 +0x400710: 0x4005c3 # line 16 +0x400718: 0x4005aa # line 8 +0x400720: 0x4005bf # line 15 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 +#include + +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 +#include + +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 +#include +#include + +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 +#include +#include + +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 diff --git a/semestr-4/ask/lista5/zad5.c b/semestr-4/ask/lista5/zad5.c new file mode 100644 index 0000000..c1fe957 --- /dev/null +++ b/semestr-4/ask/lista5/zad5.c @@ -0,0 +1,54 @@ +#include +#include + +long switch_prob(long rdi, long rsi) { + static void *array[] = { &&L1, &&L1, &&L2, &&L3, &&L4, &&L5 }; + long rax; + rsi -= 0x3c; + if (rsi > 0x5) { + goto L3; + } + goto *array[rsi]; +L1: + rax = rdi * 8; + return rax; +L4: + rax = rdi; + rax >>= 3; + retun rax; +L2: + rax = rdi; + rax <<= 4; + rax -= rdi; + rdi = rax; +L5: + rdi *= rdi; +L3: + rax = 0x4b + rdi; + return rax; +} + +long decode(long x, long n) { + long result; + n -= 0x3c; + switch (n) + { + case 0: + case 1: + return x + 8; + case 4: + return x >> 3; + case 2: + result = (x << 4) - x; + x = result; + case 5: + x *= x; + case 3: + default: + return 0x4b + x; + } +} + +int main() { + +} \ No newline at end of file -- cgit v1.2.3