程序集x86_64中的循环出错

时间:2018-02-19 03:07:20

标签: loops assembly

我试图在循环中接受多个输入并在此之后显示它们。问题在于循环的第一次迭代。第一次通过循环时它不会等待用户输入,而是直接进入第二次迭代,如图所示:

enter image description here

我的代码在这里用于循环

section .text
computer_info:
    push rbp
    mov rbp, rsp
    sub rsp, 300

    lea rbx, [computers]
    add QWORD [counter], 0
    .input_computers:
        mov rdi, QWORD echo_computer_name
        call print_string
        call print_nl
        call read_string
        mov  rsi, rax
        mov  rdi, rbx
        mov  rcx, 16
        rep movsb
        add  rbx, 16
        add  QWORD [counter], 1
        cmp  QWORD [counter], 2
        jne .input_computers

        mov QWORD [counter], 0
        sub rbx, rbx

    .display_loop:
        mov rdi, computers
        add rdi, rbx
        call print_string
        call print_nl
        add rbx, 16
        add QWORD [counter], 1
        cmp QWORD [counter], 2
        jne .display_loop

    add rsp, 300
    pop rbp
    ret
user_info:
    push rbp
    mov rbp, rsp
    sub rsp, 32

    call main

    add rsp, 32
    pop rbp
    ret
search:
    call main
delete:
    call main

main:
; We have these three lines for compatability only
push rbp
mov rbp, rsp
sub rsp,32

mov rdi, QWORD echo_welcome
call print_string
call print_nl
mov rdi, QWORD echo_computer
call print_string
call print_nl
mov rdi, QWORD echo_user
call print_string
call print_nl
mov rdi, QWORD echo_search
call print_string
call print_nl
mov rdi, QWORD echo_delete
call print_string
call print_nl
call print_nl
mov rdi, QWORD echo_selection
call print_string
call read_int
mov rdi, rax
cmp rdi, 1
je computer_info
cmp rdi, 2
je user_info
cmp rdi, 3
je search
cmp rdi, 4
je delete

; and these lines are for compatability
add rsp, 32
pop rbp

ret

0 个答案:

没有答案