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..113b10dc --- /dev/null +++ b/curs/chap-04-reprezentare/01-data-interp/Makefile @@ -0,0 +1,13 @@ +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 *~ 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..e3fd05d1 --- /dev/null +++ b/curs/chap-04-reprezentare/01-data-interp/data_interp.c @@ -0,0 +1,34 @@ +#include +#include +#include + +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(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(short_array) / sizeof(short_array[0]); j++) + printf(" 0x%04x", short_array[j]); + printf("\n"); + + printf("i:"); + 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_array) / sizeof(ll_array[0]); j++) + printf(" 0x%016llx", ll_array[j]); + printf("\n"); + + return 0; +} 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..ed3c5503 --- /dev/null +++ b/curs/chap-04-reprezentare/02-same-data/Makefile @@ -0,0 +1,13 @@ +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 *~ 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..5af24133 --- /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) + printf("\n"); + printf(" %02x", *((const unsigned char *) start + i)); + } + /* And newline at the end. */ + printf("\n"); +} + + +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; +} 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..b50036c0 --- /dev/null +++ b/curs/chap-04-reprezentare/03-signed-unsigned/Makefile @@ -0,0 +1,13 @@ +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 *~ 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..77a1617a --- /dev/null +++ b/curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c @@ -0,0 +1,18 @@ +#include + +unsigned char unsigned_char = 0xaa; +signed char signed_char = 0xaa; + +short int short_int = -1; +unsigned int unsigned_int; + +int main(void) +{ + printf("%hhu, %hhd\n", unsigned_char, signed_char); + + 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; +} 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 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..60e32886 --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile @@ -0,0 +1,14 @@ +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 *~ $(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..6382e711 --- /dev/null +++ b/curs/chap-04-reprezentare/07-signed-unsigned-ops/cast_op_precedence.c @@ -0,0 +1,23 @@ +#include + +int main(void) +{ + signed int a, b, c, d; + signed char c1 = 100, c2 = 100, c3, c4 = 127, c5 = 127, 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; +}