Nasm:不使用scanf扫描输入

时间:2018-04-21 17:59:04

标签: assembly scanf nasm x86-64

我有以下代码:

section .data
    ;scans
    scan_epsilon:       db  "%*7s%*1s%lf", 0
    scan_order:         db  10,"%*5s%*1s%i", 0
    scan_coeff:         db  10,"%*5s%i%*1s%lf%lf\n", 0
    scan_initial:       db  10,"initial = %lf %lf", 0

    print_epsilon:      db  "epsilon = %lf", 10, 0
    print_order:        db  "order = %i", 10, 0
    print_complex:      db  "%lf + %lfi", 10, 0

SECTION .bss
    epsilon:        resq 1
    order:          resq 1
    initial:        resq 1
    arr_size:       resq 1
    poly_real:      resq 1
    poly_img:       resq 1
    loop_counter:   resq 1
    coeff_pos:      resq 1
    temp:           resq 2

section .text
    extern printf
    extern scanf
    extern malloc
    extern free

    global _start

_start:
    ;;epsilon
    mov     rdi, scan_epsilon
    mov     rsi, epsilon
    mov     rax, 0
    call    scanf

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

    ;order
    mov     rdi, scan_order
    mov     rsi, order
    mov     rax, 0
    call    scanf

    mov     rdi, print_order
    mov     rsi, qword[order]
    mov     rax, 1
    call    printf

    mov     r10, qword[order]
    mov     qword[arr_size], r10
    add     qword[arr_size], 1
    ;allocating memory for polyinomial
    pushad
    mov     rdi, qword[arr_size]
    call    malloc
    mov     qword[poly_real], rax

    mov     rdi, qword[arr_size]
    call    malloc
    mov     qword[poly_img], rax
    popad

    ;initializing the loop counter
    mov     qword[loop_counter], 0

    .coeff_loop:
        mov     r10, qword[arr_size]
        cmp     r10, qword[loop_counter]
        je      .continue

        ;reading the coeff
        mov     rsi, scan_coeff
        mov     rdi, coeff_pos
        mov     rdx, temp
        mov     rcx, temp + 8
        mov     rax, 0
        call    scanf

        mov     rdi, print_double
        movsd   xmm0, [temp]
        mov     rax, 1
        call    printf

        ;calculating the position of the given coeff
        mov     rax, qword[coeff_pos]
        mov     rbx, 8
        mul     rbx

        ;putting the coeff at coeff_pos at it's coresponding position
        mov     r10, qword[temp]
        mov     qword[poly_real + rax], r10
        mov     r10, qword[temp + 8]
        mov     qword[poly_img + rax], r10

        add     qword[loop_counter], 1
        jmp     .coeff_loop

    .continue:
    ;initial
    mov     rdi, scan_initial
    mov     rsi, initial        ;pointer to the real part of the initial number
    mov     rdx, initial + 8    ;pointer to rhe imaganary part of the initial number
    mov     rax, 0
    call    scanf

    mov     rdi, print_complex
    movsd   xmm0, [initial]
    movsd   xmm1, [initial + 8]
    mov     rax, 2
    call    printf

    ;freeing the allocating memory
    mov     rdi, qword[poly_real]
    call    free
    mov     rdi, qword[poly_img]
    call    free

    mov     rax, 60             ;exit
    syscall

输入应该如下所示:

  

epsilon = 2

     

order = 1

     

coeff 0 = 1.1 2.1

     

coeff 1 = -1.1 2.1

     

initial = 1.1 1.1

但在我读完订单后程序退出。 当我试图看到我是否进入循环(通过在循环中打印某些东西)时,我发现我正在进入它,但是scanf没有扫描任何系数的值。

我不明白为什么会发生这种情况。

0 个答案:

没有答案