From 409914a75ea7e4168e4daeef926804df6306c6b9 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 24 Dec 2021 12:41:10 +0200 Subject: [PATCH 01/11] chap-04: Reindex demos Signed-off-by: Razvan Deaconescu --- .../.gitignore | 0 .../{01-endianness => 04-endianess}/Makefile | 0 curs/chap-04-reprezentare/04-endianess/README | 24 +++++++++++++++++++ .../{01-endianness => 04-endianess}/README.md | 0 .../bin-oct-hex.c | 0 .../endian.asm | 0 .../{01-endianness => 04-endianess}/mem.c | 0 .../print_code.c | 0 .../signed-unsigned.c | 0 .../{02-endianness => 05-endianess}/Makefile | 0 .../endianess.c | 0 .../endianess2.c | 0 curs/chap-04-reprezentare/06-C2/.gitignore | 6 +++++ .../{03-C2 => 06-C2}/Makefile | 0 .../{03-C2 => 06-C2}/README.md | 0 .../{03-C2 => 06-C2}/abs.cpp | 0 .../{03-C2 => 06-C2}/asymmetrical.c | 0 .../{03-C2 => 06-C2}/do_not_mix.c | 0 .../{03-C2 => 06-C2}/flags.asm | 0 .../{03-C2 => 06-C2}/print_flags.c | 0 20 files changed, 30 insertions(+) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/.gitignore (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/Makefile (100%) create mode 100644 curs/chap-04-reprezentare/04-endianess/README rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/README.md (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/bin-oct-hex.c (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/endian.asm (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/mem.c (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/print_code.c (100%) rename curs/chap-04-reprezentare/{01-endianness => 04-endianess}/signed-unsigned.c (100%) rename curs/chap-04-reprezentare/{02-endianness => 05-endianess}/Makefile (100%) rename curs/chap-04-reprezentare/{02-endianness => 05-endianess}/endianess.c (100%) rename curs/chap-04-reprezentare/{02-endianness => 05-endianess}/endianess2.c (100%) create mode 100644 curs/chap-04-reprezentare/06-C2/.gitignore rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/Makefile (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/README.md (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/abs.cpp (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/asymmetrical.c (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/do_not_mix.c (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/flags.asm (100%) rename curs/chap-04-reprezentare/{03-C2 => 06-C2}/print_flags.c (100%) diff --git a/curs/chap-04-reprezentare/01-endianness/.gitignore b/curs/chap-04-reprezentare/04-endianess/.gitignore similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/.gitignore rename to curs/chap-04-reprezentare/04-endianess/.gitignore diff --git a/curs/chap-04-reprezentare/01-endianness/Makefile b/curs/chap-04-reprezentare/04-endianess/Makefile similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/Makefile rename to curs/chap-04-reprezentare/04-endianess/Makefile diff --git a/curs/chap-04-reprezentare/04-endianess/README b/curs/chap-04-reprezentare/04-endianess/README new file mode 100644 index 00000000..4c1bac37 --- /dev/null +++ b/curs/chap-04-reprezentare/04-endianess/README @@ -0,0 +1,24 @@ +# curs-04-demo + + +1. Laborator 1, exercițiu 5, mem.c +- se printează octet cu octet conținutul fiecărei variabile +- observați adresele de început pentru variabile +- observati adresele fiecărui octet component +- reconstituiți valoarea variabilei folosind octeții si aritmetica în baza 16 + +2. endian.asm +- compilați si încărcati în gdb + make + gdb endian + b main + r +- examinați variabilele a, b, c +- octet cu octet cu comanda în gdb: x/20xb &a +- short cu short cu comanda în gdb: x/20xh &a +- word cu word cu comanda în gdb: x/20xw &a +- idem pentru varianbilele b, c + + + + diff --git a/curs/chap-04-reprezentare/01-endianness/README.md b/curs/chap-04-reprezentare/04-endianess/README.md similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/README.md rename to curs/chap-04-reprezentare/04-endianess/README.md diff --git a/curs/chap-04-reprezentare/01-endianness/bin-oct-hex.c b/curs/chap-04-reprezentare/04-endianess/bin-oct-hex.c similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/bin-oct-hex.c rename to curs/chap-04-reprezentare/04-endianess/bin-oct-hex.c diff --git a/curs/chap-04-reprezentare/01-endianness/endian.asm b/curs/chap-04-reprezentare/04-endianess/endian.asm similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/endian.asm rename to curs/chap-04-reprezentare/04-endianess/endian.asm diff --git a/curs/chap-04-reprezentare/01-endianness/mem.c b/curs/chap-04-reprezentare/04-endianess/mem.c similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/mem.c rename to curs/chap-04-reprezentare/04-endianess/mem.c diff --git a/curs/chap-04-reprezentare/01-endianness/print_code.c b/curs/chap-04-reprezentare/04-endianess/print_code.c similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/print_code.c rename to curs/chap-04-reprezentare/04-endianess/print_code.c diff --git a/curs/chap-04-reprezentare/01-endianness/signed-unsigned.c b/curs/chap-04-reprezentare/04-endianess/signed-unsigned.c similarity index 100% rename from curs/chap-04-reprezentare/01-endianness/signed-unsigned.c rename to curs/chap-04-reprezentare/04-endianess/signed-unsigned.c diff --git a/curs/chap-04-reprezentare/02-endianness/Makefile b/curs/chap-04-reprezentare/05-endianess/Makefile similarity index 100% rename from curs/chap-04-reprezentare/02-endianness/Makefile rename to curs/chap-04-reprezentare/05-endianess/Makefile diff --git a/curs/chap-04-reprezentare/02-endianness/endianess.c b/curs/chap-04-reprezentare/05-endianess/endianess.c similarity index 100% rename from curs/chap-04-reprezentare/02-endianness/endianess.c rename to curs/chap-04-reprezentare/05-endianess/endianess.c diff --git a/curs/chap-04-reprezentare/02-endianness/endianess2.c b/curs/chap-04-reprezentare/05-endianess/endianess2.c similarity index 100% rename from curs/chap-04-reprezentare/02-endianness/endianess2.c rename to curs/chap-04-reprezentare/05-endianess/endianess2.c diff --git a/curs/chap-04-reprezentare/06-C2/.gitignore b/curs/chap-04-reprezentare/06-C2/.gitignore new file mode 100644 index 00000000..0eeecc13 --- /dev/null +++ b/curs/chap-04-reprezentare/06-C2/.gitignore @@ -0,0 +1,6 @@ +/f.dat +/g.dat +/endianess +/endianess2 +/print_flags +/flags diff --git a/curs/chap-04-reprezentare/03-C2/Makefile b/curs/chap-04-reprezentare/06-C2/Makefile similarity index 100% rename from curs/chap-04-reprezentare/03-C2/Makefile rename to curs/chap-04-reprezentare/06-C2/Makefile diff --git a/curs/chap-04-reprezentare/03-C2/README.md b/curs/chap-04-reprezentare/06-C2/README.md similarity index 100% rename from curs/chap-04-reprezentare/03-C2/README.md rename to curs/chap-04-reprezentare/06-C2/README.md diff --git a/curs/chap-04-reprezentare/03-C2/abs.cpp b/curs/chap-04-reprezentare/06-C2/abs.cpp similarity index 100% rename from curs/chap-04-reprezentare/03-C2/abs.cpp rename to curs/chap-04-reprezentare/06-C2/abs.cpp diff --git a/curs/chap-04-reprezentare/03-C2/asymmetrical.c b/curs/chap-04-reprezentare/06-C2/asymmetrical.c similarity index 100% rename from curs/chap-04-reprezentare/03-C2/asymmetrical.c rename to curs/chap-04-reprezentare/06-C2/asymmetrical.c diff --git a/curs/chap-04-reprezentare/03-C2/do_not_mix.c b/curs/chap-04-reprezentare/06-C2/do_not_mix.c similarity index 100% rename from curs/chap-04-reprezentare/03-C2/do_not_mix.c rename to curs/chap-04-reprezentare/06-C2/do_not_mix.c diff --git a/curs/chap-04-reprezentare/03-C2/flags.asm b/curs/chap-04-reprezentare/06-C2/flags.asm similarity index 100% rename from curs/chap-04-reprezentare/03-C2/flags.asm rename to curs/chap-04-reprezentare/06-C2/flags.asm diff --git a/curs/chap-04-reprezentare/03-C2/print_flags.c b/curs/chap-04-reprezentare/06-C2/print_flags.c similarity index 100% rename from curs/chap-04-reprezentare/03-C2/print_flags.c rename to curs/chap-04-reprezentare/06-C2/print_flags.c From 80015251e538eada0920c0fe43c79066b25ea405 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 24 Dec 2021 12:47:15 +0200 Subject: [PATCH 02/11] chap-04: Add demo showing data interpretation depending on endianess Use different data types to interpret memory contents. Signed-off-by: Razvan Deaconescu --- .../01-data-interp/.gitignore | 1 + .../01-data-interp/Makefile | 14 ++++++++ .../01-data-interp/data_interp.c | 34 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 curs/chap-04-reprezentare/01-data-interp/.gitignore create mode 100644 curs/chap-04-reprezentare/01-data-interp/Makefile create mode 100644 curs/chap-04-reprezentare/01-data-interp/data_interp.c diff --git a/curs/chap-04-reprezentare/01-data-interp/.gitignore b/curs/chap-04-reprezentare/01-data-interp/.gitignore new file mode 100644 index 00000000..7d08180b --- /dev/null +++ b/curs/chap-04-reprezentare/01-data-interp/.gitignore @@ -0,0 +1 @@ +/data_interp diff --git a/curs/chap-04-reprezentare/01-data-interp/Makefile b/curs/chap-04-reprezentare/01-data-interp/Makefile new file mode 100644 index 00000000..355af108 --- /dev/null +++ b/curs/chap-04-reprezentare/01-data-interp/Makefile @@ -0,0 +1,14 @@ +CFLAGS = -m32 -fno-PIC -Wall +LDFLAGS = -m32 -no-pie + +.PHONY: all clean + +all: data_interp + +data_interp: data_interp.o + +data_interp.o: data_interp.c + +clean: + -rm -f *~ + -rm -f data_interp.o data_interp diff --git a/curs/chap-04-reprezentare/01-data-interp/data_interp.c b/curs/chap-04-reprezentare/01-data-interp/data_interp.c new file mode 100644 index 00000000..284e963c --- /dev/null +++ b/curs/chap-04-reprezentare/01-data-interp/data_interp.c @@ -0,0 +1,34 @@ +#include +#include +#include + +unsigned char c[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; +unsigned short s[4]; +unsigned int i[2]; +unsigned long long ll[1]; + +int main(void) +{ + size_t j; + + memcpy(s, c, sizeof(c)); + memcpy(i, c, sizeof(c)); + memcpy(ll, c, sizeof(c)); + + printf("s:"); + for (j = 0; j < sizeof(s) / sizeof(s[0]); j++) + printf(" 0x%04x", s[j]); + printf("\n"); + + printf("i:"); + for (j = 0; j < sizeof(i) / sizeof(i[0]); j++) + printf(" 0x%08x", i[j]); + printf("\n"); + + printf("ll:"); + for (j = 0; j < sizeof(ll) / sizeof(ll[0]); j++) + printf(" 0x%016llx", ll[j]); + printf("\n"); + + return 0; +} From 5cb134286474a5832a57194baa865517cef02ade Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 24 Dec 2021 12:48:48 +0200 Subject: [PATCH 03/11] chap-04: Add demo to show different data types represented the same in memory Signed-off-by: Razvan Deaconescu --- .../02-same-data/.gitignore | 1 + .../02-same-data/Makefile | 14 +++++++++ .../02-same-data/same_data.c | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 curs/chap-04-reprezentare/02-same-data/.gitignore create mode 100644 curs/chap-04-reprezentare/02-same-data/Makefile create mode 100644 curs/chap-04-reprezentare/02-same-data/same_data.c diff --git a/curs/chap-04-reprezentare/02-same-data/.gitignore b/curs/chap-04-reprezentare/02-same-data/.gitignore new file mode 100644 index 00000000..297dbd6d --- /dev/null +++ b/curs/chap-04-reprezentare/02-same-data/.gitignore @@ -0,0 +1 @@ +/same_data diff --git a/curs/chap-04-reprezentare/02-same-data/Makefile b/curs/chap-04-reprezentare/02-same-data/Makefile new file mode 100644 index 00000000..cfa0170a --- /dev/null +++ b/curs/chap-04-reprezentare/02-same-data/Makefile @@ -0,0 +1,14 @@ +CFLAGS = -m32 -fno-PIC -Wall +LDFLAGS = -m32 -no-pie + +.PHONY: all clean + +all: same_data + +same_data: same_data.o + +same_data.o: same_data.c + +clean: + -rm -f *~ + -rm -f same_data.o same_data diff --git a/curs/chap-04-reprezentare/02-same-data/same_data.c b/curs/chap-04-reprezentare/02-same-data/same_data.c new file mode 100644 index 00000000..b1bca023 --- /dev/null +++ b/curs/chap-04-reprezentare/02-same-data/same_data.c @@ -0,0 +1,30 @@ +#include + +unsigned int i = 0x12345678; +unsigned short int s[2] = {0x5678, 0x1234}; +unsigned char c[4] = {0x78, 0x56, 0x34, 0x12}; + +static void dump(const void *start, size_t len, const char *id) +{ + unsigned int i; + + printf("\nDumping %s from address %p (%zu bytes):\n", id, start, len); + for (i = 0; i < len; i++) { + /* Add a newline every 8 bytes. */ + if (i % 8 == 0) + puts(""); + printf(" %02x", *((const unsigned char *) start + i)); + } + /* And newline at the end. */ + puts(""); +} + + +int main(void) +{ + dump(&i, sizeof(i), "i (unsigned int)"); + dump(&s, sizeof(s), "s (unsigned short int)"); + dump(&c, sizeof(c), "c (unsigned char)"); + + return 0; +} From 0d8dd127c49b0f5aecacc29630f4fa04338a497a Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 24 Dec 2021 12:49:39 +0200 Subject: [PATCH 04/11] chap-04: Add demo showing signed / unsigned representation and intepretation Signed-off-by: Razvan Deaconescu --- .../03-signed-unsigned/.gitignore | 1 + .../03-signed-unsigned/Makefile | 14 ++++++++++++++ .../03-signed-unsigned/signed_unsigned.c | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 curs/chap-04-reprezentare/03-signed-unsigned/.gitignore create mode 100644 curs/chap-04-reprezentare/03-signed-unsigned/Makefile create mode 100644 curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c diff --git a/curs/chap-04-reprezentare/03-signed-unsigned/.gitignore b/curs/chap-04-reprezentare/03-signed-unsigned/.gitignore new file mode 100644 index 00000000..483a55ac --- /dev/null +++ b/curs/chap-04-reprezentare/03-signed-unsigned/.gitignore @@ -0,0 +1 @@ +/signed_unsigned diff --git a/curs/chap-04-reprezentare/03-signed-unsigned/Makefile b/curs/chap-04-reprezentare/03-signed-unsigned/Makefile new file mode 100644 index 00000000..d2e13e30 --- /dev/null +++ b/curs/chap-04-reprezentare/03-signed-unsigned/Makefile @@ -0,0 +1,14 @@ +CFLAGS = -m32 -fno-PIC -Wall +LDFLAGS = -m32 -no-pie + +.PHONY: all clean + +all: signed_unsigned + +signed_unsigned: signed_unsigned.o + +signed_unsigned.o: signed_unsigned.c + +clean: + -rm -f *~ + -rm -f signed_unsigned.o signed_unsigned diff --git a/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c b/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c new file mode 100644 index 00000000..abbc69cf --- /dev/null +++ b/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c @@ -0,0 +1,18 @@ +#include + +unsigned char uc = 0xaa; +signed char sc = 0xaa; + +short int si = -1; +unsigned int ui; + +int main(void) +{ + printf("%hhu, %hhd\n", uc, sc); + + ui = si; + printf("si (%p): 0x%08x, si: %u, si: %d\n", &si, si, si, si); + printf("ui (%p): 0x%08x, ui: %u, ui: %d\n", &ui, ui, ui, ui); + + return 0; +} From d859ac8bec59a1a509e93caea29e11abb0c2b7c5 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 24 Dec 2021 18:47:50 +0200 Subject: [PATCH 05/11] chap-04: Add demos with signed-unsigned operations Signed-off-by: Razvan Deaconescu --- .../07-signed-unsigned-ops/.gitignore | 4 ++ .../07-signed-unsigned-ops/Makefile | 15 ++++++++ .../cast_op_precedence.c | 31 +++++++++++++++ .../negative_overflow.c | 12 ++++++ .../signed_unsigned_char_int.c | 38 +++++++++++++++++++ .../signed_unsigned_print.c | 35 +++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/negative_overflow.c create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_char_int.c create mode 100644 curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_print.c diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore b/curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore new file mode 100644 index 00000000..22f41a0a --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore @@ -0,0 +1,4 @@ +/cast_op_precedence +/negative_overflow +/signed_unsigned_print +/signed_unsigned_char_int diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile b/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile new file mode 100644 index 00000000..b1ab145c --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile @@ -0,0 +1,15 @@ +CFLAGS = -m32 -fno-PIC -Wall +LDFLAGS = -m32 -no-pie +BINARIES = cast_op_precedence signed_unsigned_char_int signed_unsigned_print negative_overflow + +.PHONY: all clean + +all: $(BINARIES) + +$(BINARIES): %: %.o + +: %.o: %.c + +clean: + -rm -f *~ + -rm -f $(BINARIES) $(BINARIES:=.o) diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c b/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c new file mode 100644 index 00000000..a5f5d767 --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c @@ -0,0 +1,31 @@ +#include + +int main(void) +{ + signed int a; + signed int b; + signed int c; + signed int d; + signed char c1 = 100; + signed char c2 = 100; + signed char c3; + signed char c4 = 127; + signed char c5 = 127; + signed char c6; + + c3 = c1 + c2; + a = c3; + b = c1 + c2; + + printf("a: 0x%08x\n", a); + printf("b: 0x%08x\n", b); + + c6 = ++c4; + c = c6; + d = ++c5; + + printf("c: 0x%08x\n", c); + printf("d: 0x%08x\n", d); + + return 0; +} diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/negative_overflow.c b/curs/chap-04-reprezentare/07-signed-unsigned-ops/negative_overflow.c new file mode 100644 index 00000000..07aa17fc --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/negative_overflow.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + signed char c = -120; + unsigned char cu = c; + + printf("0x%02x\n", c); + printf("0x%02x\n", cu); + + return 0; +} diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_char_int.c b/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_char_int.c new file mode 100644 index 00000000..f03e83f3 --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_char_int.c @@ -0,0 +1,38 @@ +#include + +int main(void) +{ + signed char c1 = -9; + unsigned char c2 = c1; + signed int i1; + unsigned int i2; + + i1 = c1; + printf("0x%08x\n", i1); + i1 = c2; + printf("0x%08x\n", i1); + i1 = c1 + c2; + printf("0x%08x\n", i1); + + i2 = c1; + printf("0x%08x\n", i2); + i2 = c2; + printf("0x%08x\n", i2); + i2 = c1 + c2; + printf("0x%08x\n", i2); + + i1 = -9; + i2 = i1; + + c1 = i1; + printf("0x%02x\n", c1); + c1 = i2; + printf("0x%02x\n", c1); + + c2 = i1; + printf("0x%02x\n", c2); + c2 = i2; + printf("0x%02x\n", c2); + + return 0; +} diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_print.c b/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_print.c new file mode 100644 index 00000000..9a23faa2 --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/signed_unsigned_print.c @@ -0,0 +1,35 @@ +#include + +int v1 = -1; +unsigned int v2 = -2; + +int sum1; +unsigned int sum2; + +int main(void) +{ + printf("v1 (%%d): %d, v1 (%%u): %u, v1 (%%08x): 0x:%08x\n", v1, v1, v1); + printf("v2 (%%d): %d, v2 (%%u): %u, v2 (%%08x): 0x:%08x\n", v2, v2, v2); + printf("-1 (%%d): %d, -1 (%%u): %u, -1 (%%08x): 0x:%08x\n", -1, -1, -1); + + sum1 = v1 + 20; + sum2 = v2 + 20; + + printf("sum1 (%%d): %d, sum1 (%%u): %u, sum1 (%%08x): 0x:%08x\n", sum1, sum1, sum1); + printf("sum2 (%%d): %d, sum2 (%%u): %u, sum2 (%%08x): 0x:%08x\n", sum2, sum2, sum2); + + // MAXINT MAXINT-1 + if (v1 < v2) + printf("-1 < -2\n"); + else + printf("-1 > -2\n"); + + //10 MAXINT-1 + v1 = 10; + if (v1 < v2) + printf("10 < -2\n"); + else + printf("10 > -2\n"); + + return 0; +} From 7878899b8c23208ca438cdbb0d5fb7d682c3af97 Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:44:42 +0200 Subject: [PATCH 06/11] chap-04: Update Makefile Signed-off-by: Gabriel Mocanu --- curs/chap-04-reprezentare/01-data-interp/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/curs/chap-04-reprezentare/01-data-interp/Makefile b/curs/chap-04-reprezentare/01-data-interp/Makefile index 355af108..113b10dc 100644 --- a/curs/chap-04-reprezentare/01-data-interp/Makefile +++ b/curs/chap-04-reprezentare/01-data-interp/Makefile @@ -10,5 +10,4 @@ data_interp: data_interp.o data_interp.o: data_interp.c clean: - -rm -f *~ - -rm -f data_interp.o data_interp + -rm -f *~ data_interp.o data_interp From eba0525c27842762e6a2f99029f3b52a6ccbfaff Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:45:09 +0200 Subject: [PATCH 07/11] task: Add descriptive variable names Signed-off-by: Gabriel Mocanu --- .../01-data-interp/data_interp.c | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/curs/chap-04-reprezentare/01-data-interp/data_interp.c b/curs/chap-04-reprezentare/01-data-interp/data_interp.c index 284e963c..e3fd05d1 100644 --- a/curs/chap-04-reprezentare/01-data-interp/data_interp.c +++ b/curs/chap-04-reprezentare/01-data-interp/data_interp.c @@ -2,32 +2,32 @@ #include #include -unsigned char c[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; -unsigned short s[4]; -unsigned int i[2]; -unsigned long long ll[1]; +unsigned char char_array[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; +unsigned short short_array[4]; +unsigned int int_array[2]; +unsigned long long ll_array[1]; int main(void) { size_t j; - memcpy(s, c, sizeof(c)); - memcpy(i, c, sizeof(c)); - memcpy(ll, c, sizeof(c)); + memcpy(short_array, char_array, sizeof(char_array)); + memcpy(int_array, char_array, sizeof(char_array)); + memcpy(ll_array, char_array, sizeof(char_array)); printf("s:"); - for (j = 0; j < sizeof(s) / sizeof(s[0]); j++) - printf(" 0x%04x", s[j]); + for (j = 0; j < sizeof(short_array) / sizeof(short_array[0]); j++) + printf(" 0x%04x", short_array[j]); printf("\n"); printf("i:"); - for (j = 0; j < sizeof(i) / sizeof(i[0]); j++) - printf(" 0x%08x", i[j]); + for (j = 0; j < sizeof(int_array) / sizeof(int_array[0]); j++) + printf(" 0x%08x", int_array[j]); printf("\n"); printf("ll:"); - for (j = 0; j < sizeof(ll) / sizeof(ll[0]); j++) - printf(" 0x%016llx", ll[j]); + for (j = 0; j < sizeof(ll_array) / sizeof(ll_array[0]); j++) + printf(" 0x%016llx", ll_array[j]); printf("\n"); return 0; From 4a30c3d9ac2d4b1e7a53e7e675db5d6bbd86ae2c Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:51:00 +0200 Subject: [PATCH 08/11] task: Update all Makefiles Signed-off-by: Gabriel Mocanu --- curs/chap-04-reprezentare/02-same-data/Makefile | 3 +-- curs/chap-04-reprezentare/03-signed-unsigned/Makefile | 3 +-- curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/curs/chap-04-reprezentare/02-same-data/Makefile b/curs/chap-04-reprezentare/02-same-data/Makefile index cfa0170a..ed3c5503 100644 --- a/curs/chap-04-reprezentare/02-same-data/Makefile +++ b/curs/chap-04-reprezentare/02-same-data/Makefile @@ -10,5 +10,4 @@ same_data: same_data.o same_data.o: same_data.c clean: - -rm -f *~ - -rm -f same_data.o same_data + -rm -f *~ same_data.o same_data diff --git a/curs/chap-04-reprezentare/03-signed-unsigned/Makefile b/curs/chap-04-reprezentare/03-signed-unsigned/Makefile index d2e13e30..b50036c0 100644 --- a/curs/chap-04-reprezentare/03-signed-unsigned/Makefile +++ b/curs/chap-04-reprezentare/03-signed-unsigned/Makefile @@ -10,5 +10,4 @@ signed_unsigned: signed_unsigned.o signed_unsigned.o: signed_unsigned.c clean: - -rm -f *~ - -rm -f signed_unsigned.o signed_unsigned + -rm -f *~ signed_unsigned.o signed_unsigned diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile b/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile index b1ab145c..60e32886 100644 --- a/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile @@ -11,5 +11,4 @@ $(BINARIES): %: %.o : %.o: %.c clean: - -rm -f *~ - -rm -f $(BINARIES) $(BINARIES:=.o) + -rm -f *~ $(BINARIES) $(BINARIES:=.o) From 575a8bc24ae00e3e322b3828f9ce14535d37b466 Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:53:04 +0200 Subject: [PATCH 09/11] task: Declare on single line Signed-off-by: Gabriel Mocanu --- .../07-signed-unsigned-ops/cast_op_precedence.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c b/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c index a5f5d767..6382e711 100644 --- a/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c @@ -2,16 +2,8 @@ int main(void) { - signed int a; - signed int b; - signed int c; - signed int d; - signed char c1 = 100; - signed char c2 = 100; - signed char c3; - signed char c4 = 127; - signed char c5 = 127; - signed char c6; + signed int a, b, c, d; + signed char c1 = 100, c2 = 100, c3, c4 = 127, c5 = 127, c6; c3 = c1 + c2; a = c3; From 142d64b26a2b68fc9aa717c2f2a80e715000ea09 Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:55:19 +0200 Subject: [PATCH 10/11] task: Replace puts with printf Signed-off-by: Gabriel Mocanu --- curs/chap-04-reprezentare/02-same-data/same_data.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/curs/chap-04-reprezentare/02-same-data/same_data.c b/curs/chap-04-reprezentare/02-same-data/same_data.c index b1bca023..5af24133 100644 --- a/curs/chap-04-reprezentare/02-same-data/same_data.c +++ b/curs/chap-04-reprezentare/02-same-data/same_data.c @@ -12,11 +12,11 @@ static void dump(const void *start, size_t len, const char *id) for (i = 0; i < len; i++) { /* Add a newline every 8 bytes. */ if (i % 8 == 0) - puts(""); + printf("\n"); printf(" %02x", *((const unsigned char *) start + i)); } /* And newline at the end. */ - puts(""); + printf("\n"); } From 009383be6844f1d91c40c78f43d59553ad6fe15a Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Sat, 30 Mar 2024 16:58:02 +0200 Subject: [PATCH 11/11] task: Rename variables Signed-off-by: Gabriel Mocanu --- .../03-signed-unsigned/signed_unsigned.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c b/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c index abbc69cf..77a1617a 100644 --- a/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c +++ b/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c @@ -1,18 +1,18 @@ #include -unsigned char uc = 0xaa; -signed char sc = 0xaa; +unsigned char unsigned_char = 0xaa; +signed char signed_char = 0xaa; -short int si = -1; -unsigned int ui; +short int short_int = -1; +unsigned int unsigned_int; int main(void) { - printf("%hhu, %hhd\n", uc, sc); + printf("%hhu, %hhd\n", unsigned_char, signed_char); - ui = si; - printf("si (%p): 0x%08x, si: %u, si: %d\n", &si, si, si, si); - printf("ui (%p): 0x%08x, ui: %u, ui: %d\n", &ui, ui, ui, ui); + unsigned_int = short_int; + printf("short_int (%p): 0x%08x, short_int: %u, short_int: %d\n", &short_int, short_int, short_int, short_int); + printf("unsigned_int (%p): 0x%08x, unsigned_int: %u, unsigned_int: %d\n", &unsigned_int, unsigned_int, unsigned_int, unsigned_int); return 0; }