>>818433
I don't know alot about C++ , but I just assembled this , and debugged with gdb, and it looks like main has a local variable someString ,which is passed as a pointer to makeString, which calls the C++ runtime to allocate a string at its address directly.
So, it seems this code doesn't 3 times. It copies "Hello" from .data to a string object once in allocating the string, directly where it belongs from makeString().
.file "test.cpp"
.section .ctors,"aw",@progbits
.align 8
.quad _GLOBAL__I__Z10makeStringv
.LC0:
.string "Hello"
.globl _Unwind_Resume
.text
.align 2
.globl _Z10makeStringv
.type _Z10makeStringv, @function
_Z10makeStringv:
.LFB1422:
pushq %rbp
.LCFI14:
movq %rsp, %rbp
.LCFI15:
pushq %rbx
.LCFI16:
subq $40, %rsp #Create locals No Locals! Yikes
.LCFI17:
movq %rdi, -32(%rbp) #save RDI (argument, pointer to local in main) to local
movq -32(%rbp), %rbx #move that same local to %rbx ??
leaq -9(%rbp), %rdi #load local variable address to %rdi (argument to new function)
call _ZNSaIcEC1Ev@PLT
leaq -9(%rbp), %rdx #Load address of another local into %rdx
leaq .LC0(%rip), %rsi #load address of SOMETHING? into %rsi
movq %rbx, %rdi #save old return value(address of someString local) of fn _ZNSaIcEC1Ev@PLT as %rdi (first argument)
.LEHB0:
call _ZNSsC1EPKcRKSaIcE@PLT #This inits string object, now data is contain in string.
#rbx now contains address of string
.LEHE0:
leaq -9(%rbp), %rdi
call _ZNSaIcED1Ev@PLT #what is this? #no return value. takes argument local variable.
jmp .L27
.L30:
movq %rax, -40(%rbp)
.L28:
movq -40(%rbp), %rbx
leaq -9(%rbp), %rdi
call _ZNSaIcED1Ev@PLT
movq %rbx, -40(%rbp)
movq -40(%rbp), %rdi
.LEHB1:
call _Unwind_Resume@PLT
.LEHE1:
.L27:
movq -32(%rbp), %rax
addq $40, %rsp
popq %rbx
leave
ret
.
main:
.LFB1423:
pushq %rbp
.LCFI18:
movq %rsp, %rbp
.LCFI19:
pushq %rbx
.LCFI20:
subq $40, %rsp #create local variables. only local variable is someString!! yikes. 40
.LCFI21:
leaq -16(%rbp), %rdi #load the address of local variable into RDI (argument to makestring)
.LEHB2:
call _Z10makeStringv #call makestring
.LEHE2:
leaq -16(%rbp), %rsi #load address of that local variable into rsi
movq _ZSt4cout@GOTPCREL(%rip), %rdi #some kind of local variable gaurd argument
.LEHB3:
call _ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKSbIS4_S5_T1_E@PLT //call
.LEHE3:
movl $0, %ebx
leaq -16(%rbp), %rdi #load address of local variable into rdi
.LEHB4:
call _ZNSsD1Ev@PLT
.LEHE4:
movl %ebx, -28(%rbp)
jmp .L31
.L34:
movq %rax, -40(%rbp)
.L32:
movq -40(%rbp), %rbx
leaq -16(%rbp), %rdi
call _ZNSsD1Ev@PLT
movq %rbx, -40(%rbp)
movq -40(%rbp), %rdi
.LEHB5:
call _Unwind_Resume@PLT
.LEHE5:
.L31:
movl -28(%rbp), %eax
addq $40, %rsp
popq %rbx
leave
ret