Note that there are some explanatory texts on larger screens.

plurals
  1. POprinting pointer address using printf in 64 bit machine
    primarykey
    data
    text
    <p>I have written my own code for printf. <strong>I am using a 64 bit machine</strong>. I am trying to print pointer address. For some reason I am getting only the lower 32 bits address. Can you help me why I am not getting the full address? My findings so far.</p> <pre><code>Breakpoint 1, kprintf (fmt=0xffffffff00201687 "%p") at sys/main.c:249 249 write_string(0x1F,"0x"); (gdb) p /x addr $1 = 0xffffffff002050f4 </code></pre> <p>This is my corresponding code.</p> <pre><code>247 case 'p': addr = va_arg(arg_p, uint64_t); // Print Address of the pointer 248 //addr = (uint64_t) v; 249 write_string(0x1F,"0x"); 250 write_string(0x1F,convert(addr,16)); 251 break; </code></pre> <p>Strangely I found that the argument to convert function is <strong>actually passed through %edi</strong> register which is why I am getting a 32 bit value. But I am wondering why is not getting passed in %rdi register. This is my objdump / disassembly</p> <pre><code> 658 ffffffff00200a34: 48 89 44 24 08 mov %rax,0x8(%rsp) 659 ffffffff00200a39: be 0a 00 00 00 mov $0xa,%esi 660 ffffffff00200a3e: 8b 39 mov (%rcx),%edi 661 ffffffff00200a40: e8 c3 01 00 00 callq ffffffff00200c08 &lt;convert&gt; </code></pre> <p>This is how I receive arguments in convert function.</p> <pre><code> 13 char *convert(uint64_t num, int base) 14 { </code></pre> <p>Can someone please tell me the problem here?</p> <p>Edit: Adding more code</p> <pre><code>int kprintf(char *fmt,...){ char *p; uint64_t addr; int i; // integer argument char *s; // string argument va_list arg_p; // pointer to the variable argument list va_start(arg_p, fmt); for(p=fmt; *p ; p++){ if(*p != '%'){ write_char(0x1F,*p); continue; } switch(*++p){ case 'p': addr = va_arg(arg_p, uint64_t); // Print Address of the pointer //addr = (uint64_t) v; write_string(0x1F,"0x"); write_string(0x1F,convert(addr,16)); break; } </code></pre> <p>My va_list</p> <pre><code>1 #define va_start(v,l) __builtin_va_start(v,l) 2 #define va_arg(v,l) __builtin_va_arg(v,l) 3 #define va_end(v) __builtin_va_end(v) 4 #define va_copy(d,s) __builtin_va_copy(d,s) 5 typedef __builtin_va_list va_list; </code></pre> <p>I am calling my krpintf as follows</p> <pre><code>kprintf("%p",(uint64_t)&amp;i); </code></pre> <p>Finally, my convert function is</p> <pre><code> 13 char *convert(uint64_t num, int base) 14 { 15 static char buf[65]; 16 char *ptr; 17 18 ptr=&amp;buf[sizeof(buf)-1]; 19 *ptr='\0'; 20 do 21 { 22 *--ptr="0123456789abcdef"[num%base]; 23 num/=base; 24 }while(num!=0); 25 return(ptr); 26 } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload