scanf run twice instead of one time at assembly x86_64 [duplicate]

為{幸葍}努か 提交于 2019-12-13 20:06:56

问题


I'v tried to run this code, and that what happend -

a. scanf want "epsilon = %lf"

b. for some reason, the program does not continue to print epsilon, but insted it's scan again for "order = %d"

c. print epsilon at that point

d. scanf again for "order = %d"

e. print the first order and exit

I would like to know why the program entered twice to scanf, and how to fix it =]

(there is a terminal img below, to see steps a-e in the actual program)

global main
extern printf 
extern scanf

section .data
    epsilon_formatIN: db "epsilon = %lf",10,0
    epsilon_formatOUT: db "epsilon = %lf",10,0
    order_formatIN: db "order = %d",10,0
    order_formatOUT: db "order = %d",10,0

section .bss
    epsilon: resq 1
    order: resb 1


section .text

   main:
     push rbp
     mov rbp, rsp
     mov rax, 0

     .get_epsilon:
      mov rdi, epsilon_formatIN
      mov rsi, epsilon
      mov rax, 0
      call scanf

    .print_epsilon:
      mov rdi, epsilon_formatOUT
      movsd xmm0, qword [epsilon]
      mov rax, 1    
      call printf


   .get_order:
     mov rdi, order_formatIN
     mov rsi, order
     mov rax, 0
     call scanf

   .print_order:
     mov rdi, order_formatOUT
     mov rsi, [order]
     mov rax, 0
     call printf

   pop rbp
   ret

terminal image after compiling and run this code:

来源:https://stackoverflow.com/questions/49923312/scanf-run-twice-instead-of-one-time-at-assembly-x86-64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!