-1

I was asking about this yesterday. Had a chance to sit with the professor today and we couldn't figure this out. When running in the debugger, I get the following after calling scanf in read_int_new.

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.

He says it appears to be set up correctly. I've been stuck on this for a while.

the code:

        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
user3866044
  • 181
  • 6
  • 20
  • Doesnt crash after changing it to this ;mov rdi, LC5 ;lea rsi, [value] mov rax, [value] ;mov eax,0 call scanf can't find the input though – user3866044 Jul 23 '15 at 00:48
  • Please inlcude only the code you're actually running. All those commented-out instructions make the code a lot harder to read. – Michael Jul 23 '15 at 05:50

1 Answers1

0

I don't know what it is happening in your code (too much comments) but you can call scanf this way if you don't mind having 64 bit absolute addresses

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

or you can use

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

Don't get confused by the use of the square brackets, they are used with a lea instruction, no memory is accessed.

In contrast to print the value with printf you can use

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

The use with the absolute addresses is clearer (to me) but waste a lot of bytes.


Note that "%d" reads an int (which is still 32 bit).

  • I had tried similar setups before. Trying to use these, I still get the same error when running it in the debugger. Crashes after getting an input. Changed %d to %ld, still no luck. Error is 1057 vfscanf.c: No such file or directory. (1057 now instead of 1857). I'm going to skip this one for now after working on it for about two weeks. If I finish everything else I'll come back to it. Thanks! – user3866044 Jul 23 '15 at 14:50
  • @user3866044 I have tested the code above and worked, maybe it is something else. –  Jul 23 '15 at 15:16
  • Might be time to try another machine! Thanks. – user3866044 Jul 23 '15 at 15:24