0

I'm here to ask you a question about kernel development (specific to x86 processors), my problem is this: I want to show the hex address of any pointer.

I tried alot of things, figuring out for myself ofc, but Im hitting this wall.

All print functions I implement by myself and they are all working. Remember: Im coding in a freestanding enviroment (GCC stuff).

Here is what I'm doing until now:

void testPrintHex(void* someptr) {
const char* hexDigits = "0123456789ABCDEF";

uint32_t* ptr = (uint32_t*) someptr;

print("0x");

for(size_t i = 0; i < 32; i+=8) {
    char c = ptr[i];

    char low = c & 0x0F;
    char high = (c >> 4) & 0x0F;

    for(int j = 0; j <= 16; j++) {
        char h = (hexDigits[j] >> 4) & 0x0F;

        if(high == h) {
            printChar(h);
        }
    }

    for(int j = 0; j <= 16; j++) {
        char l = hexDigits[j] & 0x0F;

        if(low == l) {
            printChar(l);
        }
    }
}

print("\n");
}

I expected this to print the memory address of this pointer. What I get? This: 0x

Ow, there's 12 (2 spades, 2 happy faces, 6 squares and 2 hearts) strange symbols that Stack Overflow didn't show.

The Mint Br
  • 71
  • 1
  • 5
  • 1
    You don't need a linear search for the right digit! You can just look it up. In fact, a linear search defeats the purpose because then you'd need to handle the `9` to `A` discontinuity with an `if`. Use something like `printChar(hexDigits[high])` and `printChar(hexDigit[low]);` – Peter Cordes Jan 05 '19 at 00:51
  • 3
    Are you trying to print the address, or the contents? – Phil M Jan 05 '19 at 00:53
  • Also, you should provide a [mcve]. – Phil M Jan 05 '19 at 00:55
  • Are you looking for help understanding why your function is totally broken, or do you want a simple one that works? If the latter, this is a duplicate of another dump-memory question. Note that you're dumping the pointed-to memory, *not* printing the pointer itself. The bugs in your version include printing only the low 4 bits of the ASCII byte, and that the low 4 bits of `A` are the same as the low 4 bits of `1` so your search can't work. – Peter Cordes Jan 05 '19 at 01:07

1 Answers1

2

Assuming you want to print the next 4 bytes the pointer points to, it's much simpler:

void testPrintHex(void* someptr) {
    const char* hexDigits = "0123456789ABCDEF";

    uint8_t* ptr = (uint8_t*) someptr;

    print("0x");

    for(size_t i = 0; i < 4; i++) {
        char c = ptr[i];

        char low = c & 0x0F;
        char high = (c >> 4) & 0x0F;

        printChar(hexDigits[high]);
        printChar(hexDigits[low]);
    }

    print("\n");
}
dbush
  • 205,898
  • 23
  • 218
  • 273