从键盘输入一个数字,并在汇编代码中将其显示回屏幕

时间:2013-02-14 04:27:46

标签: assembly input

我参加了汇编语言课程,但教学方法只是将示例代码交给我们自己学习,但我仍然很挣扎。我想编写这个示例程序只是为了更好地了解I / O,但它一直在崩溃,有人可以向我解释原因吗?

这是我的.asm

extern printf
extern scanf


segment .data

    prompt db "Enter a number: ",0

    stringdata db "%s",0
    inputdata db "%Lf",0

segment .bss

    x:  resq 1


segment .text
global ints

ints:


;=========== Back up the base pointer =============================================================

       push       rbp                           ;Save a copy of the stack base pointer


;=========== Save registers ======================================================================

       push       rdi
       push       rsi

;===============prompt user for a number====
    mov rax, 0
    mov rdi, stringdata
    mov rsi, prompt
    call printf

    finit

;==================grab input from keyboard
    mov rax,0
    mov rdi, inputdata
    push qword 0
    push qword 0
    mov rsi, rsp
    call scanf

    fld tword [rsp]     ;store into st0

    fld st0         ;place another copy into the stack
    fmul st0        ;multiply st0 and st1
    fstp qword [x]      ;store st0 into x


    mov rax,    0
    mov rdi,    inputdata
    push qword 0
    push qword 0

    mov rsi, x
    call printf


;============ Restore all registers =================================================================================================

       pop       rsi                                        ;Restore the original value to rsi
       pop       rdi                                        ;Restore the original value to rdi
       pop       rbp                                        ;Restore the base pointer

       mov qword rax, 0                                     ;Return 0 to the caller.  0 indicates successful termination.
       ret                                                  ;ret pops the stack taking away 8 bytes; all addresses contain 8 bytes

继承C文件,虽然不是很多..

#include <stdio.h>
#include <stdint.h>


extern unsigned long int ints();

int main()
{
        unsigned long int result = -999;
        result = ints();

        return result;

}

0 个答案:

没有答案