2

This is the code snippet that i am looking

#include <cstdint>
int main() {

    unsigned int i = 0x11111111;
    unsigned int j = 0xFFFFFFFF;

    uint64_t k = 0xFFFFFFFF11111111;
}
0000000140001000  sub         rsp,18h  
0000000140001004  mov         dword ptr [rsp],11111111h  
000000014000100B  mov         dword ptr [rsp+4],0FFFFFFFFh  
0000000140001013  mov         rax,0FFFFFFFF11111111h  
000000014000101D  mov         qword ptr [rsp+8],rax  
0000000140001022  xor         eax,eax  
0000000140001024  add         rsp,18h  
0000000140001028  ret

We are using rax register to carry uint64_t value.

Why we are not using eax or some other 32 bit register for int?

UPinar
  • 1,067
  • 1
  • 2
  • 16
  • 1
    `uint64_t` is a 64-bit number so it uses the 64-bit register RAX to hold it. `int` is a 32-bit values. You don't see the 32-bit it's pass through a register because you don't actually use `i` and `j` in this case. Their 4 byte values are stored to the stack. Because you are not telling GCC to optimize this code there are useless loads and stores to/from the stack. – Michael Petch Jan 18 '23 at 00:29
  • @MichaelPetch i am not using `uint64_t` too and when i optimize it i got nothing but `mov eax,0 ret` – UPinar Jan 18 '23 at 00:32
  • 2
    No you didn't use it but in order for it to store the value on the stack it had to put it in a 64-bit register and then put it on the stack because there is no move 64-bit immediate value to a memory address in the x86-64 instruction set. https://www.felixcloutier.com/x86/mov . The fact that anything goes on the stack at all is because without optimizations the compiler unnecessarily moved values to the stack it never used. – Michael Petch Jan 18 '23 at 00:34
  • 1
    Does [this post](https://stackoverflow.com/questions/62771323/why-we-cant-move-a-64-bit-immediate-value-to-memory) answer what you're asking? It goes into the reasons for not allowing 64-bit immediates, but I'm not sure if that's what you're getting at. – chris Jan 18 '23 at 00:35
  • @chris actually yes – UPinar Jan 18 '23 at 00:38

1 Answers1

0

Since you're not compiling with optimization, it is not using registers to hold any of your variables -- they're all in the stack frame (at rsp, rsp+4,and rsp+8)

rax here is just used here to temporarily hold the constant 0xFFFFFFFF11111111 as there's no single instruction to write a 64 bit value to memory. There is an instruction to write a 32-bit value to memory, so that is used for the unsigned int initializations

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226