c - calling memset from x86_64 assembly -
i'm trying learn x86 assembly, looking @ generated assembly clang. instance, i'd learn how automatic array in c initialized 0's.
int64_t my_array [3000] = {0}; it looks assembly reserving 24000b on stack ( 3000 * 64b / 8b/b ) calling memset. memset's man page, it's signature looks like:
void * memset(void *b, int c, size_t len); so know second argument should passed in %rsi 0 (the value want every byte set to), , third argument (%rdx) $24000, first argument (%rdi)? 2 relevant instructions generated assembly appear be:
leaq -24016(%rbp), %rax movq %rax, %rdi but don't understand why negative 24016 base pointer? why store in %rax move %rdi (maybe because didn't compile optimizations)?
either way, i'm not sure how pass address of first byte of array memset. i'm on osx too, i've had offset stack pointer 8b assemble.
you have allocated my_array on stack (automatic storage), means compiler has decrement stack pointer (the stack grows towards lower addresses) size of local variables plus room save registers , such. %ebp base pointer set point frame pointer of caller (after saving caller's base pointer pushing on stack). part of convention necessary proper stack unwinding. see chapter 9 exception handling , stack unwinding in agner fog's comprehensive calling conventions document
http://www.agner.org/optimize/calling_conventions.pdf.
since %ebp pointing caller's frame, compiler uses negative offset point beginning of my_array, local variable in called function.
i don't have answer why compiler stored address in %rax , copied %rdi, seems have done in 1 step
leaq -24016(%rbp), %rdi
Comments
Post a Comment