Archives for September 2016

Happy 50th Anniversary, Star Trek!

We take a break from writing about computers to pay tribute to a fantastic TV show that has inspired generations of scientists and engineers because their imaginations were sparked by Star Trek. On this day 50 years ago, the first episode of Star Trek aired in the United States. Curiously it wasn’t the pilot that aired first, but the third episode entitled The Man Trap (it’s the one with the shape-shifting salt monster). When the series first aired, we didn’t have cell phones or iPads or Google. In 1966, we hadn’t even landed on the moon yet. However we saw it all first imagined on Star Trek. Thanks for the fun and inspiration, Gene Roddenberry and the cast, crew, writers and producers of all the Star Trek shows.

USS Enterprise And Nur

In 2013 I saw the original USS Enterprise model at the Smithsonian Air and Space Museum in DC.

No Comments

Quick And Dirty Memory Inspection

Ever wish you could inspect N number of bytes of memory starting from a certain address? Back in the day when I as a kid and was programming in BASIC on an Apple II, we had two BASIC instructions called PEEK and POKE, which seemed like witchcraft at the time; you used PEEK and POKE to load a magic number and stuff happened! Looking back on it now, they were just instructions to inspect and set values at addresses in memory.

Nowadays, using C, we can write out memory using printf which has a conversion character for hexadecimal, and usually people dump the more compact hex values. However, occasionally we want a bitwise dump of each byte and there’s no printf conversion for that. Therefore I find myself rewriting this over and over every time I need it, so I coded up a quick one and stuck it here for my own future reference. It’s really simple but you’re welcome to use it if you like, I hereby declare it public domain code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>

void print_byte(const uint8_t x)
{
    size_t i;
    uint8_t mask = 0x01;

    for(i=0;i<8;i++) {
        printf("%u", (x & (mask << (7-i)))!=0);
        mask = 0x01;
    }
}

void dump_memory(const void * mem, const size_t length)
{
    uint8_t * byte_ptr;
    size_t i;

    for(i=0;i<length;i++) {
        byte_ptr = (uint8_t *)mem;
        byte_ptr+=i;
        printf("%p:\t", (void *)byte_ptr);
        print_byte(*byte_ptr);
        printf("\n");
    }        
}

int main()
{
    char sentence[] = "Hello World";

    dump_memory((void *)sentence, 12);

    return 0;
}

Before you go about peeking at addresses in memory, you need to make sure they’re mapped; otherwise you’d get a fault.

No Comments