NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: jdubya on December 11, 2013, 01:57:13 AM
-
I'm getting segmentation faults after user input. Here's some code:
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.
-
mov EAX, usrPick
Is moving the address of userPick into eax, I think you meant to do:
movzx EAX, byte [usrPick]
Which will move the contents of userPick into eax...
-
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