0

I'm trying to get it to display the menu, have the user enter N, then have the user enter a number. Using the gdb debugger, I get the following error after entering an integer:

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.
(gdb) backtrace 
#0  0x00007ffff7a6c742 in _IO_vfscanf_internal (s=<optimized out>, 
    format=<optimized out>, argptr=argptr@entry=0x7fffffffdde0, 
    errp=errp@entry=0x0) at vfscanf.c:1857
#1  0x00007ffff7a721eb in __scanf (format=<optimized out>) at scanf.c:33
#2  0x0000000000400a95 in read_int_new ()
#3  0x00007ffff7a36ec5 in __libc_start_main (main=0x400a27 <value.code>, 
argc=3, argv=0x7fffffffdf98, init=<optimized out>, fini=<optimized out>, 
rtld_fini=<optimized out>, stack_end=0x7fffffffdf88) at libc-start.c:287
#4  0x00000000004007f9 in _start ()
(gdb) print value
$1 = {<text variable, no debug info>} 0x400a1f <value>

I've been working on this program for a while and searching for that error has been no help. I've been told that the program is set up correctly. It's getting pretty frustrating.

The program:

    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  "%lld",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


    lea rdi, [LC2]
    lea rsi, [value]
    xor eax, eax
    call    scanf               ;get user input


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

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


fib:

    mov esi, value  
    mov edi, esi  

    mov eax,0   
    ;mov eax, LC5
    ;push [eax] 
    ;push value
    ;push LC5   
    call printf
    jmp menu
user3866044
  • 181
  • 6
  • 20
  • 1
    You should ask gdb to show the faulting instruction by doing `x/i $rip`. I assume it will be an aligned SSE instruction which blows up because you messed up stack alignment. – Jester Jul 27 '15 at 10:06
  • Gave the following line, which isn't in my code. (gdb) x/i $rip => 0x7ffff7a6c742 <_IO_vfscanf_internal+12370>: mov DWORD PTR [rdx],eax – user3866044 Jul 27 '15 at 12:53
  • 1
    Hm, that's not an alignment problem. Check `section.data` because that's missing a space and should be `section .data` Otherwise the stuff might not go into the data section and thus not be writable. – Jester Jul 27 '15 at 13:17
  • Was indeed the space in section .data – user3866044 Jul 27 '15 at 14:05
  • Getting this now: => 0x6011f6: add BYTE PTR [rax],al running disassemble shows it as the first line in the menu function. – user3866044 Jul 27 '15 at 18:08
  • 1
    That's the disassembly for zero bytes, you probably jumped somewhere you shouldn't have, or messed up a return address. Step through the code to see where it goes wrong. – Jester Jul 27 '15 at 18:21
  • It's happening after printing "success" and when jumping back to menu. It shows it as the first line of menu, like that, but my code for that line is mov edi, menuprompt. – user3866044 Jul 27 '15 at 18:30
  • 2
    Change `.code` to `section .text`. – Jester Jul 27 '15 at 18:32
  • Give us a minimal compilable example. This does not compile because get_kb is undefined. And is not minimal. – Ciro Santilli OurBigBook.com Jul 28 '15 at 06:21

0 Answers0