nasm汇编程序中的scanf问题

时间:2015-07-22 23:16:23

标签: assembly nasm scanf

我昨天在问这个问题。今天有机会与教授坐在一起,我们无法解决这个问题。在调试器中运行时,我在read_int_new中调用scanf后得到以下内容。

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6c742 in _IO_vfscanf_internal (s=<optimized out>, 
    format=<optimized out>, argptr=argptr@entry=0x7fffffffdde0, 
    errp=errp@entry=0x0) at vfscanf.c:1857
1857    vfscanf.c: No such file or directory.

他说它似乎设置正确。我已经坚持了一段时间。

代码:

        bits 64
        global  main
        extern  puts
        extern  printf
        extern  scanf   
        extern  get_kb



      section.data

errormsg:   db  'Invalid Input. Enter N,F, or X',0x0D,0x0a,0
numequalsmsg:   db  'Number equals: '
LC2:    db  "%d",0
menuprompt: db  0x0D,0x0a,'Enter N to enter an integer from 0 to 20',0x0D,0x0a,'Enter F to display the first N+1 numbers (beginning with zero) on the console',0x0D,0x0a,'Enter X to quit the program',0x0D,0x0a,0
choicemsg:  db  "Your Choice: ",0
LC5:    db  "%d",0
enterintmsg:    db  "Enter and integer 0-20: ",0
enternummsg:    db  'Enter a valid number between 0 and 20',0x0D,0x0a,0
LC8:    db  " , ",0
LC9:    db  'Success!',0x0D,0x0a,0
LC10:   db  'In L10!',0x0D,0x0a,0       
LC11:   db  'In L12!',0x0D,0x0a,0 
LC13:   db  'In compare to zero section',0x0D,0
value:  dq  0



.code
main:

menu:
    ;print menu 
    mov edi, menuprompt
    call    puts            ;display menu
    mov edi,choicemsg
    ;mov    eax, 0
    ;call   printf          ;display "Your choice:" 
    call puts
    call    get_kb
    mov bl, al
    cmp bl, 'N' ;N
    je  read_int_new
    cmp bl, 'F' ;F
    je  fib
    cmp bl, 'X' ;X
    je  correct     
    ;else
    jmp menu

;print success!! for debugging purposes
correct:
    mov edi, LC9
    mov eax,0
    call    printf
    jmp     menu

entered_n:
    call    read_int_new
    jmp menu 


read_int_new:
    mov edi, enterintmsg    ;display "Enter an integer 0-20: "
    mov eax, 0
    call    printf

    ;lea    rax, [value]
    ;mov    rsi, rax
    ;mov    rax, value  
    ;mov    rdi, LC5
    ;mov    eax, 0
    ;call   scanf               ;get user input 

    mov rdi, LC5
    ;lea    rsi, [value]    
    mov rax, [value]    
    ;mov    eax,0
    call    scanf


    ;ERROR OCCURS HERE!!!!!!!!!!!!!!!!!!!

    mov edi, LC9            ;test to see if it got here
    mov eax, 0
    call    printf  


    ;test   ebx, ebx            ;compare to 0 (eax-eax=0)
    ;js L9
    ;mov    edi, LC9            ;test to see if it got here
    ;mov    eax, 0
    ;call   printf  

    ;mov    ebx, DWORD [rbp-4]
    ;cmp    ebx, 20             ;jump if greater than 20
    ;jg L9
    ;mov    edi, LC9            ;test to see if it got here
    ;mov    eax, 0
    ;call   printf      

    ;mov    ebx, DWORD [rbp-4]      ;else, jump to L10
    ;mov    edi, LC9            ;test to see if it got here
    ;mov    eax, 0
    ;call   printf      
    ;jmp    L10
    ;leave  
    ;ret
    jmp menu




fib:

    ;mov esi, [value]   
    mov edi, LC9 

    mov eax,0   
    ;mov eax, LC5
    ;push [eax] 
    ;push value
    ;push LC5   
    call printf
    jmp menu

1 个答案:

答案 0 :(得分:0)

我不知道你的代码中发生了什么(评论太多)但如果你不介意拥有64位绝对地址,你可以用这种方式调用scanf

mov rdi, formatString         ;Absolute address!!
mov rsi, intVarible           ;Absolute address!!
xor rax, rax
call scanf

或者您可以使用

lea rdi, [rel formatString]
lea rsi, [rel intVarible]
xor rax, rax
call scanf

不要因使用方括号而感到困惑,它们与lea指令一起使用,不会访问内存。

与使用printf打印值相比,您可以使用

lea rdi, [rel formatOutputString]   ;No mem access
mov rsi, QWORD [rel intVarible]     ;Real memory access
xor rax, rax
call printf

使用绝对地址对我来说更清楚(但对我而言)但浪费了很多字节。

注意“%d”读取int(仍为32位)。