【问题标题】:input a floating point number an output it back to the screen in Assembly?在汇编中输入一个浮点数并将其输出回屏幕?
【发布时间】:2013-02-01 01:35:04
【问题描述】:

我一直在完成这项任务,但无法通过第一部分,这应该很简单。我已经和我的教授讨论过了,但他也想不通……我希望在这里能得到一些帮助。我只想输入一个数字,将其推入st0,然后将其弹出并存储到total_mean中。出于测试目的,我在开头注释掉了代码。

extern printf
extern scanf

segment .data
    prompt_name db "Please enter you name: ", 0
    input_name db "%s %s",0
    ready db "Please have your numberic data ready",10,0
    get_num db "Enter a floating number: ",0
    stringdata db "%s",0

    namedata db "%s ",0
    display_name db "%s",10,0
    floatdata db "%Lf",0
    ask db "Are there more numbers (Y or N)? ", 0

    mean db "The mean of these %d values is %Lf",10,0
    largest db "the largest value is %Lf",10,0
    smallest db "the smallest value is %Lf",10,0

    more db "Do you have more data to analyze (Y or N)? ",0

    bye db "Thank you %s. I hope you enjoyed this program.",10,0


segment .bss
    user_float: resq 1
    large: resq 1
    small: resq 1
    total_mean: resq 2
    first_name: resb 32
    last_name: resq 32

segment .text

    global start_loop

start_loop:

;   mov rax,0
;   mov rdi, stringdata
;   mov rsi, prompt_name
;   call printf
;
;   mov qword rax, 0
;   mov rdi, input_name
;   mov rsi, first_name
;   mov rdx, last_name
;   call scanf
;
;   mov rax, 0
;   mov rdi, namedata
;   mov rsi, first_name
;   call printf
;
;   mov rax, 0
;   mov rdi, display_name
;   mov rsi, last_name
;   call printf
;
;   mov rax, 0
;   mov rdi, stringdata
;   mov rsi, ready
;   call printf
;
;   mov rax, 0
;   mov rdi, stringdata
;   mov rsi, get_num
;   call printf

    mov qword rax, 0
    push qword 0
    push qword 0
    mov rdi, floatdata
    mov rsi, rsp
    call scanf

    push qword 0
    push qword 0

    fld tword [rsp]
    fmul

    fstp tword [total_mean]

    mov qword rax, 0
    mov qword rax,0
    mov rdi, floatdata
    mov rsi, total_mean
    call printf

    pop rax
    pop rax
    pop rax
    pop rax

ret

它只是我试图从我遇到问题的键盘输入的下半部分。当我输入一个数字(例如 12.0)并显示它时,它显示 0.00000。谁能帮我解决这个问题?

【问题讨论】:

  • 解决这类问题的最简单方法是用 C 语言编写程序并向编译器索要 asm。这就是答案。

标签: assembly input output


【解决方案1】:

在 x86-64 中,您在 XMM 寄存器中传递 double 参数,而不是在 80387 fpu 堆栈中。当您在这里使用 SystemV ABI(printfscanf 调用可见)时,只需将第一个 double 参数放入 XMM0,将第二个(如果有的话)放入 XMM1 等等(向上到XMM7)。

【讨论】:

  • 感谢您的回复,但我不太明白,部分原因是我们从未了解过XMM寄存器。那么我应该更改 FLD TWORD [total_mean] 吗?任何我改变它的方式都无法编译。还要如何将其加载到 XMM0 中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多