From f4a46ce9f1255489db4f80dea99887b062b6b89a Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Thu, 10 Oct 2024 18:37:22 +0200 Subject: [PATCH] Exercise 8.32 --- chp8_memory/mem/.gitignore | 1 + chp8_memory/mem/Makefile | 11 +++++++++++ chp8_memory/mem/main.c | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 chp8_memory/mem/.gitignore create mode 100644 chp8_memory/mem/Makefile create mode 100644 chp8_memory/mem/main.c diff --git a/chp8_memory/mem/.gitignore b/chp8_memory/mem/.gitignore new file mode 100644 index 0000000..9f5c0b2 --- /dev/null +++ b/chp8_memory/mem/.gitignore @@ -0,0 +1 @@ +mem diff --git a/chp8_memory/mem/Makefile b/chp8_memory/mem/Makefile new file mode 100644 index 0000000..7542387 --- /dev/null +++ b/chp8_memory/mem/Makefile @@ -0,0 +1,11 @@ +CC=gcc +CFLAGS=-Wall -o mem +SRCS=main.c + +build: + $(CC) $(CFLAGS) $(SRCS) +debug: + $(CC) $(CFLAGS) -fsanitize=address -static-libasan -ggdb $(SRCS) + gdb -q mem +clean: + rm -f mem diff --git a/chp8_memory/mem/main.c b/chp8_memory/mem/main.c new file mode 100644 index 0000000..7fcc079 --- /dev/null +++ b/chp8_memory/mem/main.c @@ -0,0 +1,19 @@ +#include +#include + +int main(int argc, char *argv[]) { + if (argc < 2) { + printf("Usage: mem
\n"); + return EXIT_FAILURE; + } + + const unsigned long n = atoi(argv[1]); + const unsigned long page = n >> 12; // equal to n / 2^12 + const unsigned long offset = n - (page << 12); // equal to n - (page * 2^12) + + printf("The address %lu contains:\n", n); + printf("page number = %lu\n", page); + printf("offset = %lu\n", offset); + + return EXIT_SUCCESS; +}