/* Memory.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-25 File created. 2013-11-25 Last modification. */ #include void print_memory_contents( void* memory_address, int number_of_bytes_to_print ) { unsigned char* a_byte_in_memory = (unsigned char*) memory_address ; int number_of_bytes_on_this_line = 0 ; char bytes_as_characters[ 20 ] ; printf( "\n%08X ", a_byte_in_memory ) ; while ( number_of_bytes_to_print > 0 ) { if ( number_of_bytes_on_this_line == 16 ) { // We'll start a new line on the screen. bytes_as_characters[ number_of_bytes_on_this_line ] = 0 ; printf( " %s\n%08X ", bytes_as_characters ) ; number_of_bytes_on_this_line = 0 ; } printf( " %02X", (int) *a_byte_in_memory ) ; // We'll make sure that bytes_as_characters contains // just printable characters. if ( *a_byte_in_memory > 0x1F && *a_byte_in_memory < 0x7F ) { bytes_as_characters[ number_of_bytes_on_this_line ] = *a_byte_in_memory ; } else { bytes_as_characters[ number_of_bytes_on_this_line ] = ' ' ; } number_of_bytes_on_this_line ++ ; number_of_bytes_to_print -- ; a_byte_in_memory ++ ; } // The last line of memory contents may contain less than // 16 bytes. Now we finish that line. if ( number_of_bytes_on_this_line > 0 ) { while ( number_of_bytes_on_this_line < 16 ) { printf( " " ) ; bytes_as_characters[ number_of_bytes_on_this_line ] = ' ' ; number_of_bytes_on_this_line ++ ; } bytes_as_characters[ number_of_bytes_on_this_line ] = 0 ; printf( " %s", bytes_as_characters ) ; } printf( "\n" ) ; } int main() { int array_of_integers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 65, 66, 67, 68 } ; int some_integer = 0xEE ; int another_integer = 0xCC ; char some_string[] = "abcdefghijklmnopqrstuw 12345678" ; print_memory_contents( array_of_integers, 48 ) ; print_memory_contents( some_string, 32 ) ; print_memory_contents( &another_integer, 16 ) ; strcpy( some_string, "XXXXXYYYYYYzzzzzz" ) ; some_integer = 0x66 ; another_integer = 0x77 ; print_memory_contents( some_string, 32 ) ; print_memory_contents( &another_integer, 10 ) ; print_memory_contents( &some_integer, 4 ) ; print_memory_contents( (void*) print_memory_contents, 32 ) ; } /* Here is a sample execution of this program: 0022FF40 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 00000000 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 00000000 41 00 00 00 42 00 00 00 43 00 00 00 44 00 00 00 A B C D 0022FF00 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 abcdefghijklmnop 00000000 71 72 73 74 75 77 20 20 31 32 33 34 35 36 37 38 qrstuw 12345678 0022FF38 CC 00 00 00 EE 00 00 00 01 00 00 00 02 00 00 00 0022FF00 58 58 58 58 58 59 59 59 59 59 59 7A 7A 7A 7A 7A XXXXXYYYYYYzzzzz 00000000 7A 00 73 74 75 77 20 20 31 32 33 34 35 36 37 38 z stuw 12345678 0022FF38 77 00 00 00 66 00 00 00 01 00 w f 0022FF3C 66 00 00 00 f 00401290 55 89 E5 83 EC 48 8B 45 08 89 45 F4 C7 45 F0 00 U H E E E 00000000 00 00 00 8B 45 F4 89 44 24 04 C7 04 24 00 30 40 E D$ $ 0@ */