Permalink
Browse files

HW5: Part 1 done.

  • Loading branch information...
1 parent ead86fc commit 34022ae9a6e7a2bdd048cd66902eb2a71925440e @oblique63 committed Apr 12, 2012
Showing with 42 additions and 6 deletions.
  1. +42 −6 hw5/unix_address.c
View
@@ -1,13 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-#define PAGE_SIZE 32000
+#define PAGE_SIZE 32768
#define PAGE_NUMBER_BITS 49
#define OFFSET_BITS 15
+#define BYTES 8
typedef unsigned long long int INT64;
-const INT64 page_number_mask = 0xffffffffffff8000;
+// Bit mask to extract the OFFSET_BITS from an address
const INT64 offset_mask = 0x7fff;
void check(int expression, char *message) {
@@ -17,16 +19,50 @@ void check(int expression, char *message) {
}
}
+void decimal_to_octal(int decimal, char* octal) {
+ int ascii_offset = 48;
+ int octal_digit;
+ int digit_count;
+
+ int i;
+ char temp;
+
+ memset(octal, 0, BYTES);
+
+ // Calculate Octal number, from least to most significant digit
+ for (i = 0; decimal > 0 && i < BYTES; i++) {
+ octal_digit = decimal % 8;
+ octal[i] = octal_digit + ascii_offset;
+ decimal /= 8;
+ }
+ digit_count = i - 1;
+
+ // Reverse Octal string
+ for (i = 0; i <= digit_count/2; i++) {
+ temp = octal[i];
+ octal[i] = octal[digit_count-i];
+ octal[digit_count-i] = temp;
+ }
+}
int main(int argc, char *argv[]) {
check(argc > 1, "Needs an address");
- INT64 decimal_address = atoll(argv[1]);
+ INT64 address = atoll(argv[1]);
+ char octal_address[BYTES];
+ decimal_to_octal(address, octal_address);
+
+ INT64 page_number = address >> OFFSET_BITS;
+ char octal_page_number[BYTES];
+ decimal_to_octal(page_number, octal_page_number);
- INT64 page_number = (decimal_address & page_number_mask) >> OFFSET_BITS;
- INT64 offset = decimal_address & offset_mask;
+ INT64 offset = address & offset_mask;
+ char octal_offset[BYTES];
+ decimal_to_octal(offset, octal_offset);
- printf("Page Number: %ld \nOffset: %ld \n", page_number, offset);
+ printf("Address: \n Decimal: %ld \n Octal: %s\n", address, octal_address);
+ printf("Page Number: \n Decimal: %ld \n Octal: %s\n", page_number, octal_page_number);
+ printf("Offset: \n Decimal: %ld \n Octal: %s\n", offset, octal_offset);
return 0;
}

0 comments on commit 34022ae

Please sign in to comment.