From a11df2c963520c64768158c2753237152a1eae3d Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Sat, 17 Apr 2021 17:31:46 +0200 Subject: Update --- Semestr 4/aisd/pracownia4/rozw2.cpp | 135 +++++++++++++ Semestr 4/ask/lista6/puzzle.c | 28 +++ Semestr 4/ask/lista6/puzzle.s | 22 +++ Semestr 4/ask/lista6/puzzle7.s | 29 +++ Semestr 4/ask/lista6/puzzle8.c | 25 +++ Semestr 4/ask/lista6/puzzle8.s | 30 +++ Semestr 4/ask/lista6/test.c | 9 + Semestr 4/ask/lista6/zad7.c | 18 ++ .../ask/materia\305\202y/x86-64-psABI-1.0.pdf" | Bin 0 -> 496280 bytes .../pstwo/kol1/FranciszekMalinkaKolokwium1.pdf | Bin 0 -> 3170096 bytes Semestr 4/sieci/pracownia2/router.c | 212 +++++++++++++++++++++ Semestr 4/sieci/pracownia2/udp_client.c | 33 ++++ Semestr 4/sieci/pracownia2/udp_server.c | 59 ++++++ Semestr 4/sieci/pracownia3/p3.pdf | Bin 0 -> 95110 bytes Semestr 4/sieci/wyklady/lec3.pdf | Bin 0 -> 8701267 bytes Semestr 4/sieci/wyklady/lec4.pdf | Bin 0 -> 5658130 bytes Semestr 4/sieci/wyklady/lec5.pdf | Bin 0 -> 4572488 bytes Semestr 4/sieci/wyklady/lec6.pdf | Bin 0 -> 3339096 bytes 18 files changed, 600 insertions(+) create mode 100644 Semestr 4/aisd/pracownia4/rozw2.cpp create mode 100644 Semestr 4/ask/lista6/puzzle.c create mode 100644 Semestr 4/ask/lista6/puzzle.s create mode 100644 Semestr 4/ask/lista6/puzzle7.s create mode 100644 Semestr 4/ask/lista6/puzzle8.c create mode 100644 Semestr 4/ask/lista6/puzzle8.s create mode 100644 Semestr 4/ask/lista6/test.c create mode 100644 Semestr 4/ask/lista6/zad7.c create mode 100644 "Semestr 4/ask/materia\305\202y/x86-64-psABI-1.0.pdf" create mode 100644 Semestr 4/pstwo/kol1/FranciszekMalinkaKolokwium1.pdf create mode 100644 Semestr 4/sieci/pracownia2/router.c create mode 100755 Semestr 4/sieci/pracownia2/udp_client.c create mode 100755 Semestr 4/sieci/pracownia2/udp_server.c create mode 100644 Semestr 4/sieci/pracownia3/p3.pdf create mode 100644 Semestr 4/sieci/wyklady/lec3.pdf create mode 100644 Semestr 4/sieci/wyklady/lec4.pdf create mode 100644 Semestr 4/sieci/wyklady/lec5.pdf create mode 100644 Semestr 4/sieci/wyklady/lec6.pdf diff --git a/Semestr 4/aisd/pracownia4/rozw2.cpp b/Semestr 4/aisd/pracownia4/rozw2.cpp new file mode 100644 index 0000000..17562a9 --- /dev/null +++ b/Semestr 4/aisd/pracownia4/rozw2.cpp @@ -0,0 +1,135 @@ +#include +using namespace std; + +struct treap { + typedef long long T; + treap *left = nullptr; + treap *right = nullptr; + int rank, items = 1; + T value; + T sum; + bool rev = false; + + treap(T val = T()) : rank(rand()), value(val), sum(val) {} + + inline void update() { + if (rev) { + swap(left, right); + if (left) left->rev = !left->rev; + if (right) right->rev = !right->rev; + rev = false; + } + } +}; + +inline int items(treap *t) { return t ? t->items : 0; } +inline int sum(treap *t) { return t ? t->sum : 0; } +inline void recalc(treap *t) { + t->items = items(t->left) + items(t->right) + 1; + t->sum = sum(t->left) + sum(t->right) + t->value; +} + +pair split(treap *t, int k) { + if (!t) return {nullptr, nullptr}; + t->update(); + if (items(t->left) < k) { + auto p = split(t->right, k - items(t->left) - 1); + t->right = p.first; + recalc(t); + return {t, p.second}; + } + else { + auto p = split(t->left, k); + t->right = p.first; + recalc(t); + return {p.first, t}; + } +} + +treap* merge(treap *a, treap *b) { + if (!a) return b; + if (!b) return a; + a->update(); + b->update(); + if (a->rank > b->rank) { + a->right = merge(a->right, b); + recalc(a); + return a; + } + else { + b->left = merge(a, b->left); + recalc(b); + return b; + } +} + +treap::T select(treap *t, int k) { + if (!t) return treap::T(); + t->update(); + int i = items(t->left); + if (i == k) return t->value; + else if (i > k) return select(t->left, k); + else return select(t->right, k - i - 1); +} + +treap *insert (treap *t, treap::T val, int k) { + auto p = split(t, k); + return merge(merge(p.first, new treap(val)), p.second); +} + +treap *erase(treap *t, int k) { + auto p1 = split(t, k); + auto p2 = split(p1.second, 1); + return merge(p1.first, p2.second); +} + +treap::T sum(treap *t, int l, int r) { + auto p1 = split(t, r); + auto p2 = split(p1.first, l); + return sum(p2.second); +} + +void write(treap *t) { + if (!t) return; + t->update(); + write(t->left); + cout << t->value << " "; + write(t->right); +} +void destroy(treap *t) { + if (!t) return; + destroy(t->left); + destroy(t->right); + delete t; +} + +treap *t = 0; + +int main() { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + char c; + scanf(" %c", &c); + if (c == 'I') { + int p, x; + scanf("%d%d", &p, &x); + t = insert(t, x, p); + } + if (c == 'D') { + int p; + scanf("%d", &p); + t = erase(t, p - 1); + } + if (c == 'S') { + int l, r; + scanf("%d%d", &l, &r); + printf("ans: %lld\n", sum(t, l, r)); + } + cout << "Treap:\n"; + write(t); + cout << "\n"; + } + + destroy(t); +} \ No newline at end of file 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 + +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 +#include + +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 +#include + +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\305\202y/x86-64-psABI-1.0.pdf" "b/Semestr 4/ask/materia\305\202y/x86-64-psABI-1.0.pdf" new file mode 100644 index 0000000..2a5a923 Binary files /dev/null and "b/Semestr 4/ask/materia\305\202y/x86-64-psABI-1.0.pdf" differ diff --git a/Semestr 4/pstwo/kol1/FranciszekMalinkaKolokwium1.pdf b/Semestr 4/pstwo/kol1/FranciszekMalinkaKolokwium1.pdf new file mode 100644 index 0000000..82c38f7 Binary files /dev/null and b/Semestr 4/pstwo/kol1/FranciszekMalinkaKolokwium1.pdf differ diff --git a/Semestr 4/sieci/pracownia2/router.c b/Semestr 4/sieci/pracownia2/router.c new file mode 100644 index 0000000..79754b3 --- /dev/null +++ b/Semestr 4/sieci/pracownia2/router.c @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SERVER_PORT 54321 +#define TURN_LEN_S 5 +#define TURN_LEN_MS (1000 * TURN_LEN_S) +#define TURN_LEN_US (1000000 * TURN_LEN_S) +#define NS_TO_MS(X) ((long)(X) / (long)1000000) +#define S_TO_MS(X) ((long)(X) * (long)1000) + +struct router_addr { + struct in_addr addr; + uint16_t distance; + uint8_t netmask; +}; + +struct in_addr get_broadcast_address(struct router_addr ra) { + struct in_addr result = ra.addr; + /* bitshift by more than 31 is UB */ + if (ra.netmask < 32) { + result.s_addr |= ~((1< +#include +#include +#include +#include +#include +#include + +int main() +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + struct sockaddr_in server_address; + bzero (&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_port = htons(54321); + inet_pton(AF_INET, "192.168.1.2", &server_address.sin_addr); + + char* message = "Hello server!"; + ssize_t message_len = strlen(message); + if (sendto(sockfd, message, message_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)) != message_len) { + fprintf(stderr, "sendto error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + close (sockfd); + return EXIT_SUCCESS; +} + diff --git a/Semestr 4/sieci/pracownia2/udp_server.c b/Semestr 4/sieci/pracownia2/udp_server.c new file mode 100755 index 0000000..b5ae6d1 --- /dev/null +++ b/Semestr 4/sieci/pracownia2/udp_server.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + struct sockaddr_in server_address; + bzero (&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_port = htons(54321); + server_address.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind (sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { + fprintf(stderr, "bind error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + for (;;) { + + struct sockaddr_in sender; + socklen_t sender_len = sizeof(sender); + u_int8_t buffer[IP_MAXPACKET+1]; + + ssize_t datagram_len = recvfrom (sockfd, buffer, IP_MAXPACKET, 0, (struct sockaddr*)&sender, &sender_len); + if (datagram_len < 0) { + fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + char sender_ip_str[20]; + inet_ntop(AF_INET, &(sender.sin_addr), sender_ip_str, sizeof(sender_ip_str)); + printf ("Received UDP packet from IP address: %s, port: %d\n", sender_ip_str, ntohs(sender.sin_port)); + + buffer[datagram_len] = 0; + printf ("%ld-byte message: +%s+\n", datagram_len, buffer); + + char* reply = "Thank you!"; + ssize_t reply_len = strlen(reply); + if (sendto(sockfd, reply, reply_len, 0, (struct sockaddr*)&sender, sender_len) != reply_len) { + fprintf(stderr, "sendto error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + fflush(stdout); + } + + close (sockfd); + return EXIT_SUCCESS; +} + diff --git a/Semestr 4/sieci/pracownia3/p3.pdf b/Semestr 4/sieci/pracownia3/p3.pdf new file mode 100644 index 0000000..14f1261 Binary files /dev/null and b/Semestr 4/sieci/pracownia3/p3.pdf differ diff --git a/Semestr 4/sieci/wyklady/lec3.pdf b/Semestr 4/sieci/wyklady/lec3.pdf new file mode 100644 index 0000000..ae48ec2 Binary files /dev/null and b/Semestr 4/sieci/wyklady/lec3.pdf differ diff --git a/Semestr 4/sieci/wyklady/lec4.pdf b/Semestr 4/sieci/wyklady/lec4.pdf new file mode 100644 index 0000000..352b71d Binary files /dev/null and b/Semestr 4/sieci/wyklady/lec4.pdf differ diff --git a/Semestr 4/sieci/wyklady/lec5.pdf b/Semestr 4/sieci/wyklady/lec5.pdf new file mode 100644 index 0000000..3a7b39b Binary files /dev/null and b/Semestr 4/sieci/wyklady/lec5.pdf differ diff --git a/Semestr 4/sieci/wyklady/lec6.pdf b/Semestr 4/sieci/wyklady/lec6.pdf new file mode 100644 index 0000000..04975ce Binary files /dev/null and b/Semestr 4/sieci/wyklady/lec6.pdf differ -- cgit v1.2.3