News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

linked list question

Started by ccdcmc421, November 12, 2024, 09:49:49 AM

Previous topic - Next topic

ccdcmc421

Hi folks!

I have been trying to work on creating my own snake game within 16-bit DOS. However, I have been having a hard time with trying to come up with the way that I want the snake to expand as it eats the food.

A concept that I think of when I am doing this is by using a linked list.

The way I would have this work is by:
1) declare a spot in memory for the character that represents the snake as well as the next following items
2) write the address of where it's currently located
3) write the address of the piece of the snake it's "following"

Sounds great to me. However, I have no clue how I would go about this. and on top of that, how I would initialize a new item in the middle of my code if a condition proves true.

Was hoping someone can point me out in the right direction  :cool:

I am using MASM 6.11 and running the code within DOSBox.

If I should attach my code in here, please let me know so I can write out comments in the ASM file.

Appreciate the read.

zedd151

While I myself don't write any 16 bit code, if you google "Snake game source code in assembly" there are several results that you can probably learn from, and might be interested in. I can post some links for you, if you wish, or if you don't get the same results from the search that I did.

I am not saying to copy their code verbatim (clearly a no-no, without permission from respective author), but it might give you some ideas on how to proceeed with your own code.

ccdcmc421

Quote from: zedd151 on November 12, 2024, 10:07:47 AMWhile I myself don't write any 16 bit code, if you google "Snake game source code in assembly" there are several results that you can probably learn from, and might be interested in. I can post some links for you, if you wish, or if you don't get the same results from the search that I did.

I am not saying to copy their code verbatim (clearly a no-no, without permission from respective author), but it might give you some ideas on how to proceeed with your own code.

ahh, yes. I should have thought to do this. I am so sorry!

zedd151

#3
It's not a problem, no need for sorrow.  :smiley:  I just happen to remember that from when I was researching how to write simple games in assembly, I did see some code for the snake game back then.

And there still yet may be, someone on the forum that may be able to assist you directly with your code, so check back from time to time.

Hey, ccdcmc421! Don't forget to come back and let me/us know if you found something helpful elsewhere.  :smiley:

NoCforMe

Here's an example of what you'd need to implement a simple linked list. Mind you, I'm not claiming that this is the only way, but almost any scheme you come up with will probably be similar to this.

The linked list is simply an array of data elements, more than likely an array of structures, which let you store more than one thing in each element. Here, there's just a "value" field where you can stuff a word of data. The other two elements are for traversing the linked list: this list is doubly-linked, meaning that each element has both a forward ("next") and backward ("prev") field, so you can traverse the list in either direction. You might not need the "prev" field; depends on what you're doing with the list.

;===================
; Linked List
;
; Some rudiments.
;===================

; List element structure:

$maxElements EQU 1024 ;# of elements in the list

LISTELEMENT STRUC
  value DW ?
  next DW ?
  prev DW ?
LISTELEMENT ENDS

;***** Uninitialized data:  *****

.data?

; This declares the linked list:
; an array of LISTELEMENT elements.

TheList LISTELEMENT $maxElements DUP(<>)
CurrListPtr DW ? ;Points to current list element.

.code

;============================================================================
; NewListElement (value)
;
; Creates a new list element with the value "value". Sets a pointer to THIS
; element in the previous element's "next" ptr.
;
; "CurrListPtr" is assumed to point to the current list element.
;============================================================================
NewListElement PROC value:WORD

PUSH BX ;Save "sacred" register.
MOV BX, CurrListPtr
MOV DX, BX ;Save ptr. to current element.

; Might want to do some boundary checking in CurrListPtr
; before adding to it, in case the list is full:
ADD BX, SIZEOF LISTELEMENT ;Point to the new element.
MOV CurrListPtr, BX ;Make the new one the current one.

MOV [BX].LISTELEMENT.next, 0 ;Set our "next" ptr. to NULL and
MOV [BX].LISTELEMENT.prev, DX ;  our "prev" pointer to prev. element.

MOV AX, value
MOV [BX].LISTELEMENT.value, AX ;Store new value.
XCHG BX, DX ;Swap ptrs. to this element & previous one.
MOV [BX].LISTELEMENT.next, DX ;Set the previous element's "next" ptr.

POP BX
RET

NewListElement ENDP

The one subroutine here creates a new list element. There's no error checking, so as noted in the code you probably want to put in some code that checks to see if the list is full: if it is, you can either fail with an error, or you can "wrap" the list by starting again at the top. Either way you'll have to write some bookkeeping code, but it won't be that difficult.

So again, the actual form of the linked list depends entirely on your program and what it does. Do you need to store a bunch of attributes in each element? Just add to the LISTELEMENT structure.
Assembly language programming should be fun. That's why I do it.

ccdcmc421

I appreciate the education you've provided to me. I'm going to save what you sent me, I might have figured out a way in my snake game to not even use a linked list. Even though, using a linked list would certainly make my job a lot easier, it's totally cool that there was another way around my original idea.

NoCforMe

Good. If you end up using this in your program let us know here; I'd be curious to see how this might be applied to a game like yours.
Assembly language programming should be fun. That's why I do it.