13 Replies - 2783 Views - Last Post: 03 December 2012 - 07:02 PM

#1 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Assembly Language - Mouse in the maze program

Posted 29 November 2012 - 07:09 PM

Hello All,
Working on a program that loads a maze.txt file and then the user tries to find the cheese or 'C' and can't get through any walls 'X'. Haven't even gotten to the finding the cheese part as I can't get it to stop and go back if they hit a wall. Can anyone take a look and give me a kick in the right direction? I am not sure if it is even loading my text file, and like I said, cannot get it to stop and go back if they move to a space containing a wall.

Here is what I have so far:

Maze.txt contents:
XXXXXXXXXXXX
X X
X X
XXXXXXX X
X X
X X
X XXXXXXX
X X
X C X
X X
XXXXXXXXXXXX

TITLE Maze
INCLUDE Irvine32.inc

.data
line BYTE 0dh, 0ah,0
screen BYTE 200 DUP(' ')
fileName BYTE 100 DUP(0)
buffer BYTE 5000 DUP(?)
ARRAY_SIZE = 144
pArray DWORD ?
FILL_VAL EQU ' '

.code
main PROC
	call	allocate_array
	jnc	arrayOk		; failed (CF = 1)?
	call	WriteWindowsMsg
	call	Crlf
	jmp	quit
	
	arrayOk:

	    mov edx, OFFSET fileName
	    call GetCommandTail
	    add edx, 1
	    call OpenInputFile
	    push eax ; EAX Has file handle
	    mov edx, OFFSET buffer
	    mov ecx, 5000
	    call ReadFromFile
	    mov ecx, eax
	    mov	esi,pArray
	    mov edi, OFFSET buffer

	    call writeScreen; write to the screen first time
	    mov ecx, 40; number of moves to make
	    mov edx, 13; starting location

	READTOP:
		mov bl, screen[edx]
		mov screen[edx], '.';previous location
		call ReadChar; get the key press
		
		AA:	 cmp al, 'd'
			 jne BB
			 inc edx
			 jmp WALL

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp WALL

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp WALL

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp WALL

		DON:
		
		WALL: 	cmp screen[edx], 'X'
				jne BOT
				mov screen[ebx], bl
				jmp READTOP

		BOT:mov screen[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP
    quit:
		exit
main ENDP

allocate_array PROC USES eax
	
	.IF eax == NULL
	   stc				; return with CF = 1
	.ELSE
	   mov  pArray,eax		; save the pointer
	   clc				; return with CF = 0
	.ENDIF

	ret
allocate_array ENDP

	writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 200
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [screen + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP
END main



Sorry, here is my maze file in tags

XXXXXXXXXXXX
X          X
X          X
XXXXXXX    X
X          X
X          X
X    XXXXXXX
X          X
X       C  X
X          X
XXXXXXXXXXXX



Is This A Good Question/Topic? 0
  • +

Replies To: Assembly Language - Mouse in the maze program

#2 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 29 November 2012 - 09:27 PM

Apparently I was doing it wrong. We are actually supposed to print the entire maze, cheese and all, and just have the user move around within the maze on screen, then when they run into the cheese, end the program.

Here is my updated code, which prints the maze, but immediately crashes :(
TITLE
INCLUDE Irvine32.inc

.data
line BYTE 0dh, 0ah,0
screen BYTE 200 DUP(' ')
fileName BYTE 200 DUP(0)
buffer BYTE 5000 DUP(?)
ARRAY_SIZE = 144
pArray DWORD ?
FILL_VAL EQU ' '

.code
main PROC
	call	allocate_array
	jnc	arrayOk		; failed (CF = 1)?
	call	WriteWindowsMsg
	call	Crlf
	jmp	quit
	
	arrayOk:

	    call load_array

	    call	display_array 
	    call	Crlf

	    call writeScreen; write to the screen first time
	    mov ecx, 40; number of moves to make
	    mov edx, 13; starting location

	READTOP:
		;mov edx, ebx
		mov screen[edx], '.';previous location
		call ReadChar; get the key press
		
		AA:	 cmp al, 'd'
			 jne BB
			 inc edx
			 jmp WALL

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp WALL

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp WALL

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp WALL

		DON:
		
		WALL: 	cmp screen[edx], 'X'
				jne BOT
				;mov ebx, edx
				je READTOP

		BOT:mov screen[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP
    quit:
		exit
main ENDP

allocate_array PROC USES eax
	
	.IF eax == NULL
	   stc				; return with CF = 1
	.ELSE
	   mov  pArray,eax		; save the pointer
	   clc				; return with CF = 0
	.ENDIF

	ret
allocate_array ENDP

load_array PROC USES ecx edx esi
	mov ecx, ARRAY_SIZE
	mov esi, pArray	; point to the array
	
	mov edx, OFFSET fileName
	call GetCommandTail
	mov edx, OFFSET fileName + 1
	call OpenInputFile
	push eax 
	mov edx, OFFSET buffer
	mov ecx, 5000
	call ReadFromFile 
	mov ecx, eax
	mov esi, 0
	L2: 
		mov al, buffer[esi] 
		call WriteChar
		inc esi
		loop L2
     ret
load_array ENDP


display_array PROC USES eax ecx esi

	mov	ecx,ARRAY_SIZE	; loop counter
	mov	esi,pArray	; point to the array
	
L1:	mov	al,[esi]		; get a byte
	call	WriteChar		; display it
	inc	esi			; next location
	loop	L1

	ret
display_array ENDP

	writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 200
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [screen + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP
END main


Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Assembly Language - Mouse in the maze program

Posted 01 December 2012 - 09:42 PM

Er, Um, where do you allocate an array in your allocate_array proc?
allocate_array PROC USES eax
	
	.IF eax == NULL
	   stc				; return with CF = 1
	.ELSE
	   mov  pArray,eax		; save the pointer
	   clc				; return with CF = 0
	.ENDIF

	ret
allocate_array ENDP

Setting and clearing the carry flag does not allocate an array. Your just checking to see if eax == 0, which 99% of the time, eax will not be 0 on program startup, which is where your calling allocate_array

So, your code will always jump to arrayOK which then you call load_array:
load_array PROC USES ecx edx esi
	mov ecx, ARRAY_SIZE
	mov esi, pArray	; point to the array

Which you then try to use a non-existent array pointer!

Or am I missing something not obvious here?
Was This Post Helpful? 0
  • +
  • -

#4 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 01 December 2012 - 09:47 PM

I haven't learned enough about array allocation to answer you clearly, and that part in my code is probably due to my copy / pasting it from a sample program I had from my book. The original looks like this



main PROC
	INVOKE GetProcessHeap		; get handle to prog's heap
	.IF eax == NULL			; failed?
	call	WriteWindowsMsg
	jmp	quit
	.ELSE
	mov	hHeap,eax		; success
	.ENDIF

	call	allocate_array
	jnc	arrayOk		; failed (CF = 1)?
	call	WriteWindowsMsg
	call	Crlf
	jmp	quit

;... misc code

; free the array
	INVOKE HeapFree, hHeap, 0, pArray
	
quit:
	exit
main ENDP

allocate_array PROC USES eax
;
; Dynamically allocates space for the array.
; Receives: nothing
; Returns: CF = 0 if allocation succeeds.
;--------------------------------------------------------
	INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, ARRAY_SIZE
	
	.IF eax == NULL
	   stc				; return with CF = 1
	.ELSE
	   mov  pArray,eax		; save the pointer
	   clc				; return with CF = 0
	.ENDIF

	ret
allocate_array ENDP


Was This Post Helpful? 0
  • +
  • -

#5 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 02 December 2012 - 07:57 AM

How about this, is there a way I can load the text file into a buffer, and then move around with WASD within the buffer that contains the contents of the text file?

This code loads the file and then lets me move around, but not in the same place.

TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib. 

INCLUDE Irvine32.inc
INCLUDE macros.inc

BUFFER_SIZE = 200

.data
line BYTE 0dh, 0ah,0
buffer BYTE BUFFER_SIZE DUP(?)
filename    BYTE 200 DUP(0)
fileHandle  HANDLE ?
screen BYTE 200 DUP(' ')

.code
main PROC

; Let user input a filename.
	mWrite "Enter an input filename: "
	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	ReadString

; Open the file for input.
	mov	edx,OFFSET filename
	call	OpenInputFile
	mov	fileHandle,eax

; Check for errors.
	cmp	eax,INVALID_HANDLE_VALUE		; error opening file?
	jne	file_ok					; no: skip
	mWrite <"Cannot open file",0dh,0ah>
	jmp	quit						; and quit
file_ok:

; Read the file into a buffer.
	mov	edx,OFFSET buffer
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile
	jnc	check_buffer_size			; error reading?
	mWrite "Error reading file. "		; yes: show error message
	call	WriteWindowsMsg
	jmp	close_file
	
check_buffer_size:
	cmp	eax,BUFFER_SIZE			; buffer large enough?
	jb	buf_size_ok				; yes
	mWrite <"Error: Buffer too small for the file",0dh,0ah>
	jmp	quit						; and quit
	
buf_size_ok:	
	mov	buffer[eax],0		; insert null terminator
	;mWrite "File size: "
	;call	WriteDec			; display file size
	call	Crlf

; Display the buffer.
	mWrite <"Buffer:",0dh,0ah,0dh,0ah>
	mov	edx,OFFSET buffer	; display the buffer
	call	WriteString
	call	Crlf

	;call writeScreen; write to the screen first time
	mov ecx, 40; number of moves to make
	mov edx, 13; starting location

	READTOP:
		mov screen[edx], '.';previous location
		call ReadChar; get the key press
		cmp al, 'd'
		jne BB
		inc edx
		jmp BOT

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp BOT

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp BOT

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp DON

		DON:

		BOT:mov screen[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP

close_file:
	mov	eax,fileHandle
	call	CloseFile


quit:
	exit
main ENDP

writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 200
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [screen + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP

END main


Was This Post Helpful? 0
  • +
  • -

#6 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 02 December 2012 - 11:03 AM

Getting closer to something, perhaps. When I run this I can move around, but it rewrites my screen and throws the whole maze off...

TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib. 

INCLUDE Irvine32.inc
INCLUDE macros.inc

BUFFER_SIZE = 200

.data
line BYTE 0dh, 0ah,0
buffer BYTE BUFFER_SIZE DUP(?)
filename    BYTE 200 DUP(0)
fileHandle  HANDLE ?
screen BYTE 200 DUP(' ')

.code
main PROC

; Let user input a filename.
	mWrite "Enter an input filename: "
	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	ReadString

; Open the file for input.
	mov	edx,OFFSET filename
	call	OpenInputFile
	mov	fileHandle,eax

; Check for errors.
	cmp	eax,INVALID_HANDLE_VALUE		; error opening file?
	jne	file_ok					; no: skip
	mWrite <"Cannot open file",0dh,0ah>
	jmp	quit						; and quit
file_ok:

; Read the file into a buffer.
	mov	edx,OFFSET buffer
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile
	jnc	check_buffer_size			; error reading?
	mWrite "Error reading file. "		; yes: show error message
	call	WriteWindowsMsg
	jmp	close_file
	
check_buffer_size:
	cmp	eax,BUFFER_SIZE			; buffer large enough?
	jb	buf_size_ok				; yes
	mWrite <"Error: Buffer too small for the file",0dh,0ah>
	jmp	quit						; and quit
	
buf_size_ok:	
	mov	buffer[eax],0		; insert null terminator
	;mWrite "File size: "
	;call	WriteDec			; display file size
	call	Crlf

; Display the buffer.
	mWrite <"Buffer:",0dh,0ah,0dh,0ah>
	mov	edx,OFFSET buffer	; display the buffer
	call	WriteString
	call	Crlf

	;call writeScreen; write to the screen first time
	mov ecx, 40; number of moves to make
	mov edx, 13; starting location

	READTOP:
		;mov buffer[edx], '.';previous location
		call ReadChar; get the key press
		cmp al, 'd'
		jne BB
		inc edx
		jmp BOT

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp BOT

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp BOT

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp DON

		DON:

		BOT:mov buffer[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP

close_file:
	mov	eax,fileHandle
	call	CloseFile


quit:
	exit
main ENDP

writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 200
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [buffer + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP

END main


Was This Post Helpful? 0
  • +
  • -

#7 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Assembly Language - Mouse in the maze program

Posted 02 December 2012 - 11:28 AM

I think the problem is with the writescreen function somewhere.. I will have a look in a bit.
Was This Post Helpful? 0
  • +
  • -

#8 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 08:46 AM

Well I took out all the extra checks and writing to the screen thinking that may have something to do with it, but still the same problem, so I am guessing you are right Gunner in it being something with the WriteScreen PROC. My brain is much after working on this for so long, I need to get some fresh eyes on it.

TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib. 

INCLUDE Irvine32.inc
INCLUDE macros.inc

BUFFER_SIZE = 200

.data
line BYTE 0dh, 0ah,0
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 'maze1.txt',0
fileHandle  HANDLE ?
screen BYTE 200 DUP(' ')

.code
main PROC

	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	OpenInputFile
	mov	fileHandle,eax
	
; Read the file into a buffer.
	mov	edx,OFFSET buffer
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile
		
	mov	buffer[eax],0		; insert null terminator
	
; Display the buffer.
	mov	edx,OFFSET buffer	; display the buffer
	call	WriteString
	call	Crlf

	mov ecx, 40; number of moves to make
	mov edx, 14; starting location

	READTOP:
		mov buffer[edx], '.';previous location
		call ReadChar; get the key press
		cmp al, 'd'
		jne BB
		inc edx
		jmp BOT

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp BOT

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp BOT

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp DON

		DON:

		BOT:mov buffer[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP

close_file:
	mov	eax,fileHandle
	call	CloseFile


quit:
	exit
main ENDP

writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 166
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [buffer + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP

END main


Was This Post Helpful? 0
  • +
  • -

#9 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 01:16 PM

Hey Gunner, can you take a look at this. The problem was actually in my text file that I load. It had a bunch of hidden CRLF's in it that was messing up the writeScreen PROC.

Now I just need to figure out how to check to see if they try to go through a wall, and if so, not allow it and move them back.

Can I do something like this

READTOP:
;copy the current location to BH
mov bh, buffer[edx]

;all the WASD movement code...

;Check to see if they hit a wall
WALL: cmp buffer[edx], 'X'
	jne WIN ;code to see if they hit the cheese
	mov buffer[edx], bh ;restore the old location since they ran into a wall
	je READTOP



Was This Post Helpful? 0
  • +
  • -

#10 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 04:40 PM

There is a problem when I press a key, the maze gets all screwy.

MP4 of BadMaze
Was This Post Helpful? 0
  • +
  • -

#11 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 05:10 PM

Nice video. Try this updated text file and code.

I have it working for the most part, it alerts when you hit the cheese, but I just need to get it working for if they try running in to a wall.

Replace your maze1.txt file with this:
XXXXXXXXXXXXX          XX          XXXXXXXXX   XX          XX          XX          XX   XXXXXXXXX          XX       C  XX          XXXXXXXXXXXXX



TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib. 

INCLUDE Irvine32.inc
INCLUDE macros.inc

BUFFER_SIZE = 200

.data
line BYTE 0dh, 0ah,0
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 'maze1.txt',0
fileHandle  HANDLE ?
screen BYTE 200 DUP(' ')

.code
main PROC

	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	OpenInputFile
	mov	fileHandle,eax
	
; Read the file into a buffer.
	mov	edx,OFFSET buffer
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile
		
	mov	buffer[eax],0		; insert null terminator
	
; Display the buffer.
	mov	edx,OFFSET buffer	; display the buffer
	call	writeScreen
	;call	Crlf

	mov ecx, 50; number of moves to make
	mov edx, 13; starting location

	READTOP:
		mov ebx, edx
		mov buffer[edx], '.';previous location
		call ReadChar; get the key press
		cmp al, 'd'
		jne BB
		inc edx
		jmp WALL

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp WALL

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp WALL

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp DON

		DON:

		WALL: cmp buffer[edx], 'X'
				jne WIN
				mov ebx, edx
				je READTOP

		WIN:cmp buffer[edx], 'C'
			jne BOT 
			mWrite "You Win!"
			jmp quit

		BOT:mov buffer[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		

		loop READTOP
		

close_file:
	mov	eax,fileHandle
	call	CloseFile


quit:
	exit
main ENDP

writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 166
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [buffer + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP

END main


Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 05:56 PM

You need to learn to debug... add code to your code to print numbers and characters.

I don't use Irvine's stuff, he doesn't teach things right, his material is old. I use straight Windows API and MASM. I have it to run code from users like yourself.

Add this then run your code:
	READTOP:
	    push    edx         ; this is added
	    mov     eax, edx    ; press a key
	    call    WriteInt    ; and take notice
	    pop     edx         ; as to what it prints each key press
	    
		mov buffer[edx], '.';previous location


Great, now you see the numerical position of the "mouse". What can you do with that number? Well, you need to check for an "X" right? You would use that number as an index into your buffer to check for a wall.

You will need to use 2 buffers:
1 that holds the file contents, and another that is a copy of it that you write to. You have 2 in your data section (buffer and screen) but you keep overwriting the buffer with a dot or mouse. Use the screen buffer to write to screen, and use buffer with edx as an index and check for an x.
Was This Post Helpful? 1
  • +
  • -

#13 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 06:06 PM

Ok, try this:
TITLE Reading a File                      (ReadFile.asm)

; Opens, reads, and displays a text file using
; procedures from Irvine32.lib. 
INCLUDE D:\Irvine32\Irvine32.inc
includelib D:\Irvine32\kernel32.lib
includelib D:\Irvine32\user32.lib
includelib D:\Irvine32\irvine32.lib
;TITLE
;INCLUDE macros.inc

BUFFER_SIZE = 200

.data
line BYTE 0dh, 0ah,0
filename BYTE 'maze2.txt',0

szSpace     db  "SPCE", 0
szWall      db  "WALL", 0

.data?
fileHandle  HANDLE ?
screen BYTE BUFFER_SIZE DUP(?)
buffer BYTE BUFFER_SIZE DUP(?)
.code
main PROC

	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	OpenInputFile
	mov	fileHandle,eax
	
 ;Read the file into a buffer.
	mov	edx,OFFSET buffer
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile		
	mov	buffer[eax],0		; insert null terminator
	mov	eax,fileHandle
	call	CloseFile

	mov	edx,OFFSET filename
	mov	ecx,SIZEOF filename
	call	OpenInputFile
	mov	fileHandle,eax
			
	;This is our buffer to write to screen
    mov	edx,OFFSET screen
	mov	ecx,BUFFER_SIZE
	call	ReadFromFile		
	mov	screen[eax],0		; insert null terminator
	call writeScreen;rewrite the screen
	
	mov	eax,fileHandle
	call	CloseFile

	mov ecx, 40; number of moves to make
	mov edx, 14; starting location

	READTOP:
	    push    edx

	    mov     al, byte ptr buffer[edx]    ; press a key
	    cmp     al, "X"
	    jne     NotWall
	    
	    mov     edx, offset szWall
        call    WriteString

	    jmp     CheckDone
	    
	NotWall:
	    mov     edx, offset szSpace
        call    WriteString
        
	CheckDone:
	    pop     edx         ; as to what it prints each key press
	    
		mov screen[edx], '.';previous location
		call ReadChar; get the key press
		cmp al, 'd'
		jne BB
		inc edx
		jmp BOT

		BB: cmp al, 'a'
			jne CC
			dec edx
			jmp BOT

		CC: cmp al, 's'
			jne DD1
			add edx, 12
			jmp BOT

		DD1: cmp al, 'w'
			jne DON
			sub edx, 12
			jmp DON

		DON:

		BOT:mov screen[edx], 0B0h; character for the mouse
			call writeScreen;rewrite the screen

		loop READTOP

close_file:
	mov	eax,fileHandle
	call	CloseFile


quit:
	exit
main ENDP

writeScreen PROC; write the screen to the terminal
	push eax;save used registers
	push ebx
	push ecx
	push edx
	push esi
	mov dl, 0
	mov dh, 0
	call Gotoxy; mov to 0,0 which uses dl and dh
	mov ecx, 166
	mov edi, 0; char to write to screen
	mov edx, 0; newline counter

	TOP:
		mov al, [screen + edi];get the char from screen
		call WriteChar;write character to console
		cmp edx, 11;check for newlines
		jne CC1
		call Crlf
		mov edx, 0
		jmp EE

		CC1:inc edx

		EE: inc edi; increment array index
	loop TOP
	
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	ret
	writeScreen ENDP

END main


You have to change the include stuff... Now, it is up to you to write the code to handle running into a wall from what I gave you
Was This Post Helpful? 1
  • +
  • -

#14 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Mouse in the maze program

Posted 03 December 2012 - 07:02 PM

Thanks Gunner. I'll work on it some more with what you gave me there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1