diff options
| author | Mike Vink <mike@pionative.com> | 2024-06-14 09:23:32 +0200 |
|---|---|---|
| committer | Mike Vink <mike@pionative.com> | 2024-06-14 09:23:32 +0200 |
| commit | 8092f4c334db547ced59d6f439b558dad35e1ab2 (patch) | |
| tree | fa462fa885efea1ec6095d015286998d632c2c3d /3/9_data_structures | |
| parent | b424517a33bf61aedff29eed74a665402ab496ba (diff) | |
commit for once
Diffstat (limited to '3/9_data_structures')
| -rw-r--r-- | 3/9_data_structures/41_struct_test.c | 30 | ||||
| -rw-r--r-- | 3/9_data_structures/42_ace_structure.c | 23 | ||||
| -rw-r--r-- | 3/9_data_structures/43_dest.c | 43 | ||||
| -rw-r--r-- | 3/9_data_structures/44_gaps.c | 86 | ||||
| -rw-r--r-- | 3/9_data_structures/45_rec.c | 42 |
5 files changed, 224 insertions, 0 deletions
diff --git a/3/9_data_structures/41_struct_test.c b/3/9_data_structures/41_struct_test.c new file mode 100644 index 0000000..1816b31 --- /dev/null +++ b/3/9_data_structures/41_struct_test.c @@ -0,0 +1,30 @@ +struct test { + short *p; + struct { + short x; + short y; + } s; + struct test *next; +} + +// A. +// p: 0, it's at the beginning of the struct +// s.x: (length p) <- 8 +// s.y: (length p) + (length s.x) <- 10 +// next: (length p) + (length s.x) + (length s.y) <- 12 +// B. +// total bytes should be 20. pointer + short + short + pointer +// C. +// st in %rdi +// st_init: +// movl 8(%rdi), %eax +// movl %eax, 10(%rdi) +// leaq 10(%rdi), %rax +// movq %rax, (%rdi) +// movq %rdi, 12(%rdi) +// ret +void st_init(struct test *st) { + st->s.y = st->s.x; + st->p = &(st->s.y); + st->next = st; +} diff --git a/3/9_data_structures/42_ace_structure.c b/3/9_data_structures/42_ace_structure.c new file mode 100644 index 0000000..8b4e92d --- /dev/null +++ b/3/9_data_structures/42_ace_structure.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +struct ACE { + short v; + struct ACE *p; +}; + +// B. it's a linked list of short values that are multiplied. +short test(struct ACE *ptr) { + short v = 1; + while (ptr) { + v *= ptr->v; + ptr = ptr->p; + } + return v; +} + +int main(void) { + struct ACE a = {2, NULL}; + struct ACE b = {3, NULL}; + a.p = &b; + printf("%d\n", test(&a)); +} diff --git a/3/9_data_structures/43_dest.c b/3/9_data_structures/43_dest.c new file mode 100644 index 0000000..67bb630 --- /dev/null +++ b/3/9_data_structures/43_dest.c @@ -0,0 +1,43 @@ +#define type long; + +typedef union { + struct { + long u; + short v; + char w; + } t1; + struct { + int a[2]; + char *p; + } t2; +} u_type; + +// Kinda confused when I'm moving a memory address +// Forgot it was a union +// Also forgot I had to move to dest +// +// up and dest in rdi and rsi +// +// expr type code +// +// up->t1.u long movq (%rdi), %rax +// movq %rax, (%rsi) +// +// up->t1.v short movw 8(%rdi), %ax +// movw %ax, (%rsi) +// +// &up->t1.w char* addq 10, %rdi +// movq %rdi, (%rsi) +// +// up->t2.a int * movq %rdi, (%rsi) +// +// up->t2.a[up->t1.u] int movq (%rdi), %rax // What's interesting: rdi is the t2.a int* and the t1.u long, and they are being used in the same expression! +// movl (%rdi, %rax, 4), %eax +// movl %eax, (%rsi) +// +// *up->t2.p char movq 8(%rdi), %rax +// movb (%rax), %al // Remember, you cannot have Memory -- Memory moves! +// movb %al, (%rsi) +void get(u_type *up, type *dest) { + *dest = ___; +} diff --git a/3/9_data_structures/44_gaps.c b/3/9_data_structures/44_gaps.c new file mode 100644 index 0000000..1162895 --- /dev/null +++ b/3/9_data_structures/44_gaps.c @@ -0,0 +1,86 @@ +// A. +struct P1 { + int i; + char c; + int j; + char d; +}; +// Largest primitive is 4, so the local alignment value must be 4 +// |int 4| char 1|gap 3 |int 4 |char 1|gap 3| +// |-----|--------------|------|------------| +// 4 4 4 4 +// i 0 +// c 4 +// j 8 +// d 12 +// +// total 16 +// +// B. +struct P2 { + int i; + char c; + char d; + long j; +}; +// Largest primitive is 8, so the local alignment value must be 8 +// |int 4|char 1| char 1|gap 2|long 8| +// |--------------------------|------| +// 8 8 +// i 0 +// c 4 +// d 5 +// j 8 +// +// total 16 +// +// C. +struct P3 { + short w[3]; + char c[3]; +}; +// Largest primitive is 2, so the local alignment value must be 2 +// |short 2|short 2|short 2|char 1|char 1|char 1|gap 1| +// |-------|-------|-------|-------------|------------| +// 2 2 2 2 2 +// +// w[0] 0 +// w[1] 2 +// w[2] 4 +// c[0] 6 +// c[1] 7 +// c[2] 8 +// +// total 10 +// +// D. +struct P4 { + short w[5]; + char *c[3]; +}; +// Largest primitive is 8, so the local alignment value must be 8 +// |short 2|short 2|short 2|short 2|short 2|gap 6|char* 8|char* 8|char* 8| +// |-------------------------------|-------------|-------|-------|-------| +// 8 8 8 8 8 +// +// w[0] 0 +// w[1] 2 +// w[2] 4 +// w[3] 6 +// w[4] 8 +// c[0] 16 +// c[1] 24 +// c[2] 32 +// +// total 40 +// E. +struct P5 { + struct P3 a[2]; + struct P2 t; +}; +// Largest primitive is 8, so the local alignment value must be 8 +// |P3 10|P3 10|gap 4|P2 16| +// +// a[0] 0 +// a[1] 10 +// t 24 diff --git a/3/9_data_structures/45_rec.c b/3/9_data_structures/45_rec.c new file mode 100644 index 0000000..f04ac0e --- /dev/null +++ b/3/9_data_structures/45_rec.c @@ -0,0 +1,42 @@ +struct { + char *a; + short b; + double c; + char d; + float e; + char f; + long g; + int h; +} rec; +// A. +// |char* 8|short 2|gap 6|double 8|char 1|float 4|char 1|gap 2|long 8|int 4|gap 4| +// |-------|-------------|--------|---------------------------|------|-----------| +// 8 8 8 8 8 8 +// +// a 0 +// b 8 +// c 16 +// d 24 +// e 25 +// f 29 +// g 32 +// h 40 +// +// B. +// 48 +// +// C. +// |char* 8|short 2|char 1|float 4|char 1|double 8|long 8|int 4|gap 4| +// |-------|-----------------------------|--------|------|-----------| +// 8 8 8 8 8 +// +// a 0 +// b 8 +// c 16 +// d 10 +// e 11 +// f 15 +// g 24 +// h 32 +// +// total 40 |
