osx - Mac OS x86 Assembly: Why does the initialized memory amount change? -
i started learning assembly week or ago, , when debugging program, came across strange memory usage. following code (see end of post) broken 2 files reason.
if compile , run with
gcc main.s ./a.out
with code block 1 running (code block 2 commented out), program prints "8", meaning right when program starts, mac os automatically puts 8 bytes worth of stuff on stack, leaves program thing.
however, if compile , run with
gcc main.s print.s ./a.out
with code block 2 running (code block 1 commented out), program prints "16", meaning mac os putting 16 bytes on stack instead of 8. when happens, offsets applied rsp
achieve 16-byte alignment remain same, meaning start of stack being offset 8 bytes whenever outside function called.
i tried putting _printnum
function in same file main.s
, discrepancy persisted. thing tried add format string , use later on in program see if format string using memory, made no difference.
what think going on mac os pushing instruction pointer next instruction execute when program terminates onto stack, pushing old base stack pointer onto stack, both 32-bit, total of 8 bytes. when include function call (either local or external main file), seems assembler decides use 64-bit addresses instead of 32-bit addresses, doubling memory used, , hence 16 bytes used.
why happening, , if wrong, mac os doing stack? of stack used of value me? computer doing else instead of switching 32-bit 64-bit addressing? thanks.
main program (main.s):
.cstring _format: .asciz "%d\n" .text .globl _main _main: movq %rbp, %rax # put stack base pointer in rax subq %rsp, %rax # subtract stack pointer total memory used subq $8, %rsp # 16-byte alignment #--------------------------------------------------------- # code block 1 - prints rax manually #--------------------------------------------------------- movq %rax, %rsi # value print needs in rsi lea _format(%rip), %rdi # address of format string goes in rdi # don't know "_format(%rip)" does, # works (any info handy) call _printf #--------------------------------------------------------- # code block 2 - prints rax via function call #--------------------------------------------------------- call _printnum # prints value of rax #--------------------------------------------------------- # stack cleanup , return #--------------------------------------------------------- addq $8, %rsp # account previous -8 rsp ret # end program
printing function (print.s):
.cstring _format: .asciz "%d\n" .text .globl _printnum # assumes 16-byte aligned when called # prints value of rax register _printnum: push %rbp # save %rbp - previous stack base movq %rsp, %rbp # update stack base push %rsi # save %rsi - register push %rdi # save %rdi - register # print - 16 byte aligned (rip , 3 values 32 bytes) movq %rax, %rsi # load value print lea _format(%rip), %rdi # load format string call _printf # restore registers popq %rdi popq %rsi popq %rbp # return ret
Comments
Post a Comment