I want to know how can I print the address of a pointer to screen and get this address and convert it to a string and send it to my serial port. Plz help me dudes, I'm dumb

Code: Select all
const uint16_t digits[] = {
u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'a', u'b', u'c', u'd', u'e', u'f'
};
void printHexDigit(uint32_t n) {
char buffer[9];
char *p = buffer;
for (int i = 7; i >= 0; --i) {
uint8_t nibble = (n & (0xf << (i*4))) >> (i*4);
*p++ = digits[nibble];
}
*p = 0;
writeStr(SERIAL_COM1, buffer);
}
void kmain(void) {
Console *console = NULL;
ttyInitialize(console);
writeStr(SERIAL_COM1, "Iniciando log...\n");
writeStr(SERIAL_COM1, "Aether Kernel v0.01 BUILD 0001, estágio 1 iniciado!\n\n");
uint8_t c[] = {'O', 'i'};
uint32_t *p = (uint32_t*) c;
printHexDigit(p[0]);
printHexDigit(p[1]);
println(console, "Aether Kernel v0.01 BUILD 0001");
}
Code: Select all
SomeType *p = ...;
writeHex(*p);
You probably don't want uint16_t for your implementation. In this case I was working with UEFI, which uses wide characters. Unless you're also in UEFI-land, you most likely want just char. (Which also means you don't need the u prefix on your character literals.AwfulMint wrote:Code: Select all
const uint16_t digits[] = { u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'a', u'b', u'c', u'd', u'e', u'f' };
Take a step back and think about what you're asking the compiler for with the data types here. Remember than in C, a pointer and an array are not very different. So you're telling the compiler to allocate two bytes on the stack for 'Oi', and c points to those two bytes. You then make p point to the same address, and then ask it to read 4 bytes starting at p as a uint32_t, and then the next 4 bytes after that as a uint32_t, when you only allocated 2 bytes.AwfulMint wrote:Code: Select all
uint8_t c[] = {'O', 'i'}; uint32_t *p = (uint32_t*) c; printHexDigit(p[0]); printHexDigit(p[1]);