8 Replies - 3717 Views - Last Post: 09 December 2012 - 04:09 PM

#1 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 08:03 AM

I am building a maze game. I have been having trouble understanding how to get just two bytes out of a file and storing it into an array space, and loop the whole file, until the whole file is loaded. I have the array Maze initialized with values that do work. The idea is to be able to load different values, rather then leaving them hard coded. This will allow me to create more then one file and load new levels into the game.

Here is what my file looks like
3535353535353535353535358835353535353535
3535353535353535000000000035353535008035
3535353535353535003535353535353535003535
3535000000000000003500353535353535003535
3535003535353535353500353535353535003535
3535003535350035353500000000000035003535
3535000000000035353500353535350000003535
3535353535350000000000358000353535000035
3535353535353535353535353500353535350035
3580000000000000003535353500353535000035
3535353535003535000000000000353535003535
3535350000003535003535353500353535000035
3535350035353535003535353500353535350035
3500000035353535000035353500000000350035
3500353500000035353535353535353500350035
3500353500350000353535353580353500350035
3500003500353500353535353500353500000035
3535000000353500000000000000353535003535
3535353580353535353542353535353535803535
3535353535353535353535353535353535353535

I would need to be able to get two number from the file. Right now I am only get 1 number instead of the intend two that I am trying to get to from the file.
For example starting at the top of the file 35,35,35
Instead I am getting only 3,5,3,5


.data

    Maze DWORD     00,00,00,00,00,35,35,35,35,35,35,35,88,35,35,35,35,35,35,35
        rowSize = ($ - Maze)
	DWORD      35,35,35,35,35,35,35,35,00,00,00,00,00,35,35,35,35,00,80,35
	DWORD      35,35,35,35,35,35,35,35,00,35,35,35,35,35,35,35,35,00,35,35
	DWORD      35,35,00,00,00,00,00,00,00,35,00,35,35,35,35,35,35,00,35,35
	DWORD      35,35,00,35,35,35,35,35,35,35,00,35,35,35,35,35,35,00,35,35
	DWORD      35,35,00,35,35,35,00,35,35,35,00,00,00,00,00,00,35,00,35,35
	DWORD      35,35,00,00,00,00,00,35,35,35,00,35,35,35,35,00,00,00,35,35
	DWORD      35,35,35,35,35,35,00,00,00,00,00,35,80,00,35,35,35,00,00,35
	DWORD      35,35,35,35,35,35,35,35,35,35,35,35,35,00,35,35,35,35,00,35
	DWORD      35,80,00,00,00,00,00,00,00,35,35,35,35,00,35,35,35,00,00,35
	DWORD      35,35,35,35,35,00,35,35,00,00,00,00,00,00,35,35,35,00,35,35
	DWORD      35,35,35,00,00,00,35,35,00,35,35,35,35,00,35,35,35,00,00,35
	DWORD      35,35,35,00,35,35,35,35,00,35,35,35,35,00,35,35,35,35,00,35
        DWORD      35,00,00,00,35,35,35,35,00,00,35,35,35,00,00,00,00,35,00,35
	DWORD      35,00,35,35,00,00,00,35,35,35,35,35,35,35,35,35,00,35,00,35
	DWORD      35,00,35,35,00,35,00,00,35,35,35,35,35,80,35,35,00,35,00,35
	DWORD      35,00,00,35,00,35,35,00,35,35,35,35,35,00,35,35,00,00,00,35
	DWORD      35,35,00,00,00,35,35,00,00,00,00,00,00,00,35,35,35,00,35,35
	DWORD      35,35,35,35,80,35,35,35,35,35,42,35,35,35,35,35,35,80,35,35
	DWORD      35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35

    LEFTRIGHT DWORD 4
    BUFFER_SIZE = 5000
    buffer BYTE BUFFER_SIZE DUP(?)
    bytesRead DWORD ?
    Map BYTE "inputfile.txt",0
.code
main PROC
    mov edx, 0				  ;Register to end game
    INVOKE GetTickCount
    mov startTime, eax

GameLoop:
    cmp edx, 99			  ;End game value equals 99
    je EndGame
    call OpenMap
    call TimerScore
    call ReadKey
    jz GameLoop
    cmp ah, 72				  ;Up arrow key
    je Up
    cmp ah, 80				  ;Down arrow key
    je Down
    cmp ah, 75				  ;Left arrow key
    je Left
    cmp ah, 77				  ;Right arrow key
    je Right
Up:
    call UpMove
    call Next
Down:
    call DownMove
    call Next
Left:
    call LeftMove
    call Next
Right:
    call RightMove
    call Next
Next:
    jmp GameLoop
EndGame:
    call TimerCount
    call Crlf
    call WaitMsg
    exit		 
main ENDP

OpenMap PROC

    mov edx, OFFSET Map				 ;Open the file
    call OpenInputFile
    push eax						;Push the register on the stack
    mov edx, OFFSET buffer			;points to the buffer
    mov ecx, BUFFER_SIZE				;max bytes to read
    call ReadFromFile				;read the file
    mov bytesRead, eax				;count of bytes read
    pop eax						;Pop the register off the stack
    call CloseFile					;Close the file

    mov esi, offset buffer			;Point to the buffer
    mov ecx, bytesRead				;Loop counter

LoadFile:
	   mov al, byte ptr [edi]		;get byte at current pointer
	   mov byte ptr [edi], al		;move that decrypted byte back to our buffer at current pointer
	   inc edi					;point to the next byte
	   mov esi, OFFSET Maze
	   mov [esi], al			     ;move the character into the 2D array
	   add esi, LEFTRIGHT
loop Loadfile						;loop					;loop
   
    mov edx, OFFSET buffer			;Display the buffer 
   
    ret
OpenMap ENDP





I decided it would be easyer to read the character rather then the number for the character....

sound now my code looks like this...

OpenMap PROC
    mov edx, OFFSET Map				;Open the file
    call OpenInputFile
    push eax						;Push the register on the stack
    mov edx, OFFSET buffer			;points to the buffer
    mov ecx, BUFFER_SIZE				;max bytes to read
    call ReadFromFile				;read the file
    mov bytesRead, eax				;count of bytes read
    pop eax						;Pop the register off the stack
    call CloseFile					;Close the file

    mov edi, offset buffer			;Point to the buffer
    mov ecx, bytesRead				;Loop counter

LoadFile:
	   mov al, byte ptr [edi]		;get byte at current pointer
	   mov byte ptr [edi], al		;move that decrypted byte back to our buffer at current pointer
	   inc edi					;point to the next byte
	   mov esi, OFFSET Maze
	   mov ebx, [esi]
	   movzx ebx, al				;Move the charater into eax
	   mov [esi], ebx
	   add ebx, 4
loop Loadfile						;loop
   
    mov edx, OFFSET buffer			;Display the buffer
    ;call WriteString
    ret
OpenMap ENDP



and my file looks like this....
I am just testing for the top values but still have no results of printing the area this way in my draw function.

############X########
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#####################
#########*###########
#####################

;=========================================================================

;=========================================================================
Draw PROC
    mov dh, 0						    ;Set maze position X
    mov dl, 0						    ;Set maze position Y
    call Gotoxy					    ;Call Go to X Y
    mov ebx, OFFSET Maze				    ;Move the maze 2D array into ebx
    mov ecx, 0						    ;intialize the counter
PrintLoop:
    mov eax, [ebx]					    ;Move the indirect value of ebx postion 1 into eax
    add ebx, 4						    ;Move to the next offset position
    inc ecx						    ;Increment the counter
    call WriteChar					    ;Write Character
    cmp ecx, 20					    ;Compare for end of row for each 20 positions
    je NextLine
    cmp ecx, 40
    je NextLine
    cmp ecx, 60
    je NextLine
    cmp ecx, 80
    je NextLine
    cmp ecx, 100
    je NextLine
    cmp ecx, 120
    je NextLine
    cmp ecx, 140
    je NextLine
    cmp ecx, 160
    je NextLine
    cmp ecx, 180
    je NextLine
    cmp ecx, 200
    je NextLine
    cmp ecx, 220
    je NextLine
    cmp ecx, 240
    je NextLine
    cmp ecx, 260
    je NextLine
    cmp ecx, 280
    je NextLine
    cmp ecx, 300
    je NextLine
    cmp ecx, 320
    je NextLine
    cmp ecx, 340
    je NextLine
    cmp ecx, 360
    je NextLine
    cmp ecx, 380
    je NextLine
    cmp ecx, 400
    jne PrintLoop
    jmp Print
NextLine:
    call Crlf
    jmp PrintLoop
Print:
    call Crlf
    mov edx,OFFSET MessageDirections
    call WriteString				    ;Call Write String procdure

    ret
Draw ENDP

This post has been edited by Toadill: 09 December 2012 - 10:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: ASM x86, using Irvine32.inc, loading a file into a 2D array

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 09:52 AM

As you haven't posted the complete code, it is missing somethings for me to test. A few observations will have to do:

1. There is a big difference between call and jmp-
call pushes the address of the next instruction onto the stack then jumps to the label and when the code at label is done, flow returns to the instruction after the call.
jmp is just a goto, no return or anything, it just tells the cpu to execute the code at label and continue.

So, in your GameLoop, this:
Up:
    call UpMove
    call Next
Down:
    call DownMove
    call Next
Left:
    call LeftMove
    call Next
Right:
    call RightMove
    call Next
Next:
    jmp GameLoop
should be changes to:
Up:
    call UpMove
    jmp GameLoop
    ; or
    ;jmp Next
Down:
    call DownMove
    jmp GameLoop
    ; or
    ;jmp Next
Left:
    call LeftMove
    jmp GameLoop
    ; or
    ;jmp Next
Right:
    call RightMove
    jmp GameLoop
    ; or
    ;jmp Next
;Next:
;    jmp GameLoop

By using call you are royally messing up the stack.

Going by your maze array in your data section - rowSize = ($ - Maze) is wrong, it is not a byte array but a dword array so change the size equ to rowSize equ ($ - Maze)/4

3535 is how many bytes? you are getting 1 byte at a time, how would you get 2 bytes (WORD) at a time?
Was This Post Helpful? 1
  • +
  • -

#3 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 10:25 AM

I just want to load a file, and then set the char values for that file in the maze array.
It seem like when I try to use code the information does not go into the array like I intend it to do.
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 10:48 AM

Can you at least load the file and display the contents to screen:

Attached Image

Get that code down, then you can display the proper characters.
Was This Post Helpful? 0
  • +
  • -

#5 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 11:35 AM

I had this working this way with the call WriteString
But I want to insert the alpha numeric value for the character
which should then put the this number in the array and work the same way I already had it setup in theory.

This post has been edited by Toadill: 09 December 2012 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 11:37 AM

This is your file converted to characters:
############X#######
########     #### P#
######## ######## ##
##       # ###### ##
## ####### ###### ##
## ### ###      # ##
##     ### ####   ##
######     #P ###  #
############# #### #
#P       #### ###  #
##### ##      ### ##
###   ## #### ###  #
### #### #### #### #
#   ####  ###    # #
# ##   ######### # #
# ## #  #####P## # #
#  # ## ##### ##   #
##   ##       ### ##
####P#####*######P##
####################


can you not just use that? If not, you need to read in 2 bytes and convert to ascii.

Attached Image
Was This Post Helpful? 1
  • +
  • -

#7 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 11:48 AM

My problem now is that it never changes the values in the array. It just uses the initialized values.



;***************************************************************************
;*********************************START**MAIN*******************************
;***************************************************************************

main PROC
    mov edx, 0				  ;Register to end game
    INVOKE GetTickCount
    mov startTime, eax
    call OpenMap
GameLoop:
    cmp edx, 99			  ;End game value equals 99
    je EndGame
    call TimerScore
    call Draw
    call ReadKey
    jz GameLoop

    cmp ah, 72				  ;Up arrow key
    je Up
    cmp ah, 80				  ;Down arrow key
    je Down
    cmp ah, 75				  ;Left arrow key
    je Left
    cmp ah, 77				  ;Right arrow key
    je Right
Up:
    call UpMove
    jmp GameLoop
Down:
    call DownMove
    jmp GameLoop
Left:
    call LeftMove
    jmp GameLoop
Right:
    call RightMove
    jmp GameLoop
       
EndGame:
    call TimerCount
    call Crlf
    call WaitMsg
    exit
main ENDP

;***************************************************************************
;*********************************END**MAIN*********************************
;***************************************************************************



;=========================================================================

;=========================================================================
Draw PROC
    mov dh, 0						    ;Set maze position X
    mov dl, 0						    ;Set maze position Y
    call Gotoxy					    ;Call Go to X Y
    mov ebx, OFFSET Maze				    ;Move the maze 2D array into ebx
    mov ecx, 0						    ;intialize the counter
PrintLoop:
    mov eax, [ebx]					    ;Move the indirect value of ebx postion 1 into eax
    add ebx, 4						    ;Move to the next offset position
    inc ecx						    ;Increment the counter
    call WriteChar					    ;Write Character
    cmp ecx, 20					    ;Compare for end of row for each 20 positions
    je NextLine
    cmp ecx, 40
    je NextLine
    cmp ecx, 60
    je NextLine
    cmp ecx, 80
    je NextLine
    cmp ecx, 100
    je NextLine
    cmp ecx, 120
    je NextLine
    cmp ecx, 140
    je NextLine
    cmp ecx, 160
    je NextLine
    cmp ecx, 180
    je NextLine
    cmp ecx, 200
    je NextLine
    cmp ecx, 220
    je NextLine
    cmp ecx, 240
    je NextLine
    cmp ecx, 260
    je NextLine
    cmp ecx, 280
    je NextLine
    cmp ecx, 300
    je NextLine
    cmp ecx, 320
    je NextLine
    cmp ecx, 340
    je NextLine
    cmp ecx, 360
    je NextLine
    cmp ecx, 380
    je NextLine
    cmp ecx, 400
    jne PrintLoop
    jmp Print
NextLine:
    call Crlf
    jmp PrintLoop
Print:
    call Crlf
    mov edx,OFFSET MessageDirections
    call WriteString				    ;Call Write String procdure
    ret
Draw ENDP



;=========================================================================
;MY PROBLEM IS IN THIS PROC
;=========================================================================
OpenMap PROC
    mov edx, 0
    mov edx, OFFSET Map				;Open the file
    call OpenInputFile
    push eax						;Push the register on the stack
    mov edx, OFFSET buffer			;points to the buffer
    mov ecx, BUFFER_SIZE				;max bytes to read
    call ReadFromFile				;read the file
    mov bytesRead, eax				;count of bytes read
    pop eax						;Pop the register off the stack
    call CloseFile					;Close the file

    mov edi, offset buffer			;Point to the buffer
    mov ecx, bytesRead				;Loop counter
;TRYING TO LOAD THE VALUES FROM THE FILE INTO THE ARRAY HERE
LoadFile:
	   mov al, byte ptr [edi]		;get byte at current pointer
	   mov byte ptr [edi], al		;move that decrypted byte back to our buffer at current pointer
	   inc edi					;point to the next byte

	   mov esi, OFFSET Maze
	   mov eax, [esi]				;move the first spot in the array
	   movzx eax, al				;Convert the byte to a 32 bit value
	   add esi, 4					;mov to the next byte in the array
	   mov [esi], eax				;mov the value in the array				
loop Loadfile						;loop
   
    mov edx, OFFSET buffer			;Display the buffer
    call WriteString
    ret
OpenMap ENDP
;=========================================================================

;=========================================================================


This post has been edited by Toadill: 09 December 2012 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 02:45 PM

What do you mean by

Quote

it never changes the values in the array

Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: ASM x86, using Irvine32.inc, loading a file into a 2D array

Posted 09 December 2012 - 04:09 PM

Kraymerica Seems to be doing something very similar to you here Assembly Language - Mouse in the maze

You need two copies of the data you load from your maze file. The first copy is to check for wall hits, valid moves, end of maze etc. The second copy is what you will update and write to screen.

You don't even need that Maze array in your data section, the file loads just fine with a few things commented out.
;TRYING TO LOAD THE VALUES FROM THE FILE INTO THE ARRAY HERE
;LoadFile:
;	   mov al, byte ptr [edi]		;get byte at current pointer
;	   mov byte ptr [edi], al		;move that decrypted byte back to our buffer at current pointer
;	   inc edi					;point to the next byte
;
;	   mov esi, OFFSET Maze
;	   mov eax, [esi]				;move the first spot in the array
;	   movzx eax, al				;Convert the byte to a 32 bit value
;	   add esi, 4					;mov to the next byte in the array
;	   mov [esi], eax				;mov the value in the array				
;loop LoadFile						;loop

Without this code, the maze seems to load from the file and display properly.

I don't have your code for key presses, but this is 2 ways of doing it.
You have a starting pos, and when a up/down key is pressed, you add/subtract the amount of chars in a row, inc/dec the counter for a left/right move... take this index and lookup that character in your first buffer.. if it is not a valid move, just loop back to get another key press, if it is a valid move, take that index, and write a mouse marker in your second buffer, and write that buffer to screen.

Do we really want to keep writing the whole buffer to the console each key press? This is fine for a small maze (it is even fast enough for a large maze) so the second way is to check for key press as above, keeping track of where th "mouse" is and use GotoXY and print a character to screen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1