Author Topic: nasm segmentation fault  (Read 9980 times)

Offline jdubya

  • Jr. Member
  • *
  • Posts: 7
nasm segmentation fault
« on: December 11, 2013, 01:57:13 AM »
I'm getting segmentation faults after user input. Here's some code:
Code: [Select]
SECTION .data
formatd       db "%d",0

        array               db 0,1,2
array1       db 3,4,5
array2       db 6,7,8

SECTION .bss
usrPick:  resb 1

SECTION .text
push usrPick ; get users pick from input
push formatd
call scanf
add ESP, 8

mov EAX, usrPick
cmp EAX, byte 2 ;see if usr picked from row1
jg l2 ; if not jump

; ---usr picked from row1---
mov esi, eax ; put usrPick in esi
cmp byte[array + ESI], 'X' ; chk if X is there
je invalid ; if X then invalid
jne xmov ; is not X   jump

xmov: ; ---row1---
cmp byte[array + ESI], 'O' ; chk if O is there
je invalid ; if O then invalid
mov byte[array + ESI], 'X' ; put X in row1 array
jmp px ; jmp
The user enters input "0-8" to choose a position on the board.  Then I put an "X" in that position and print the array to the screen.  It seg faults when user input is entered. Yes, it is tic tac toe.

 

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: nasm segmentation fault
« Reply #1 on: December 11, 2013, 02:15:34 AM »
Code: [Select]
mov EAX, usrPickIs moving the address of userPick into eax, I think you meant to do:

Code: [Select]
movzx EAX, byte [usrPick]
Which will move the contents of userPick into eax...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: nasm segmentation fault
« Reply #2 on: December 11, 2013, 03:07:53 AM »
Right. I'm thinking "scanf" is going to put a dword "integer" into "usrPick" though, so "usrPick" should probably be "resd 1", not "resb". To be clear, the address of "usrPick" is the correct parameter for "scanf", but to use the result, "[usrPick]" - the "[contents]" of memory.

Best,
Frank