Exercise 8.32
This commit is contained in:
1
chp8_memory/mem/.gitignore
vendored
Normal file
1
chp8_memory/mem/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
mem
|
||||||
11
chp8_memory/mem/Makefile
Normal file
11
chp8_memory/mem/Makefile
Normal file
@@ -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
|
||||||
19
chp8_memory/mem/main.c
Normal file
19
chp8_memory/mem/main.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Usage: mem <ADDRESS>\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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user