How to use the stack in x86-64 Assembly? -


i'm having bit of trouble how access variables in stack. here i'm doing:

.data .text input:     .string "%ld"   #format input output:     .string "%ld.\n"    #format output  .global main pushq   %rbp                    #push base pointer, known frame pointer movq    %rsp, %rbp              #set base pointer stack pointer, reference off of rbp locals  subq    $8, %rsp                #allocate 16 bytes local use movq    $input, %rdi            #calls scanf in format of input movq    %rsp, %rsi movq    $0, %rax call    scanf   subq    $8, %rsp movq    $input, %rdi            #calls scanf in format of input movq    %rsp, %rsi movq    $0, %rax call    scanf  movq    -8(%rsp), %rbx          #stores first number reg rbx movq    (%rbx),%rbx movq    -16(%rsp), %rcx         #stores second number reg rcx movq    (%rcx),%rcx   movq    $output,%rdi            #prints in format input movq    $0, %rax movq    %rcx, %rsi call    printf  addq $16, %rsp popq %rbp ret 

i read in 2 integers scanf , try store them rbx , rcx registers. however, when try print 1 of them out, prints out random integer.

if follow through own operations on rsp should obvious second number still @ address (%rsp) since nothing has changed , first number @ 8(%rsp) because have subtracted 8 rsp since have read it. also, @eof said, not need indirection:

movq    8(%rsp), %rbx        #stores first number reg rbx movq    (%rsp), %rcx         #stores second number reg rcx 

note calling convention mandates rbx preserved it's bad idea destroy that. x86-64 has plenty of other registers choose ;) (but beware, others need preserved too).


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -