Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review chapter 04 #61

Merged
merged 11 commits into from
Mar 30, 2024
1 change: 1 addition & 0 deletions curs/chap-04-reprezentare/01-data-interp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/data_interp
13 changes: 13 additions & 0 deletions curs/chap-04-reprezentare/01-data-interp/Makefile
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions curs/chap-04-reprezentare/01-data-interp/data_interp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>

Check failure on line 1 in curs/chap-04-reprezentare/01-data-interp/data_interp.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1
#include <stdlib.h>
#include <string.h>

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++)

Check failure on line 19 in curs/chap-04-reprezentare/01-data-interp/data_interp.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Prefer ARRAY_SIZE(s)
printf(" 0x%04x", short_array[j]);
printf("\n");

printf("i:");
for (j = 0; j < sizeof(int_array) / sizeof(int_array[0]); j++)

Check failure on line 24 in curs/chap-04-reprezentare/01-data-interp/data_interp.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Prefer ARRAY_SIZE(i)
printf(" 0x%08x", int_array[j]);
printf("\n");

printf("ll:");
for (j = 0; j < sizeof(ll_array) / sizeof(ll_array[0]); j++)

Check failure on line 29 in curs/chap-04-reprezentare/01-data-interp/data_interp.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Prefer ARRAY_SIZE(ll)
printf(" 0x%016llx", ll_array[j]);
printf("\n");

return 0;
}
1 change: 1 addition & 0 deletions curs/chap-04-reprezentare/02-same-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/same_data
13 changes: 13 additions & 0 deletions curs/chap-04-reprezentare/02-same-data/Makefile
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions curs/chap-04-reprezentare/02-same-data/same_data.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>

Check failure on line 1 in curs/chap-04-reprezentare/02-same-data/same_data.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1

unsigned int i = 0x12345678;
unsigned short int s[2] = {0x5678, 0x1234};
unsigned char c[4] = {0x78, 0x56, 0x34, 0x12};
gabrielmocanu marked this conversation as resolved.
Show resolved Hide resolved

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;
}
1 change: 1 addition & 0 deletions curs/chap-04-reprezentare/03-signed-unsigned/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/signed_unsigned
13 changes: 13 additions & 0 deletions curs/chap-04-reprezentare/03-signed-unsigned/Makefile
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions curs/chap-04-reprezentare/03-signed-unsigned/signed_unsigned.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>

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;
}
24 changes: 24 additions & 0 deletions curs/chap-04-reprezentare/04-endianess/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# curs-04-demo


1. Laborator 1, exercițiu 5, mem.c

Check failure on line 4 in curs/chap-04-reprezentare/04-endianess/README

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: trailing whitespace
- 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




6 changes: 6 additions & 0 deletions curs/chap-04-reprezentare/06-C2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/f.dat
/g.dat
/endianess

Check failure on line 3 in curs/chap-04-reprezentare/06-C2/.gitignore

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: 'endianess' may be misspelled - perhaps 'endianness'?
/endianess2
/print_flags
/flags
4 changes: 4 additions & 0 deletions curs/chap-04-reprezentare/07-signed-unsigned-ops/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/cast_op_precedence
/negative_overflow
/signed_unsigned_print
/signed_unsigned_char_int
14 changes: 14 additions & 0 deletions curs/chap-04-reprezentare/07-signed-unsigned-ops/Makefile
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

int main(void)
{
signed char c = -120;
unsigned char cu = c;

printf("0x%02x\n", c);
printf("0x%02x\n", cu);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>

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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>

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;
}
Loading