32 variables is usually an equivalent of single variable (an array) of size of 32 items.
And instead of using variable name square_3_1_x
you have in mind a "mapping function", for example you address 4x4 squares as 0,1,2.. per row.
so square(row,column)
index is ((row
-1)*4+(column
-1)) for row/column in 1..4 range.
And then either use *2 and +0 for x
/ +1 for y
: 3_1_x
is 16
, 3_2_y
is 19
.
Or +0 for x
and +16 for y
(3_1_x
is 8
, 3_2_y
is 25
).
Or whatever your own fancy mapping you can think of.
So let's say you want to get [x,y] position of disc at row 3 (1..4) and column 2 (1..4) in 32b x86 assembler (NASM syntax) (I like how you didn't specify your platform and syntax, so people answering can choose whatever they wish, and you will then have to understand completely alien syntax to you, and convert it to your final source, very generous from you):
discPositions: times 32 dd 0 ; 16+16 for x+y couples (16 = 4x4).
GetDiscPosition:
; input: row (1..4) in eax, column (1..4) in ebx
; output: x position in eax, y position in ebx
dec eax ; row-1
dec ebx ; column-1
shl eax,2 ; (row-1)*4
add eax,ebx ; (row-1)*4+(column-1)
shl eax,3 ; ((row-1)*4+(column-1))*2*4
; *2 to cover [x,y] pairs, *4 to respect data size (DWORD)
; so here eax is byte offset to [x,y] couple in discPositions array
; it would make sense to have that offset calculation in another
; subroutine, so you can reuse it for SetDiscPosition routine
; Now just return the [x,y] stored in the 128B memory array labeled discPositions
mov ebx,[discPositions+eax+4] ; fetch y position
mov eax,[discPositions+eax+0] ; fetch x position
ret
; ... now somewhere in your code, where you want to get disc's x,y for square(3,4)
mov eax,3
mov ebx,4
call GetDiscPosition
; eax now has x position, ebx has y position
; ...
edit:
Well, since this answer got upvoted twice, it looks like this stuff is maybe helpful to others (thanks to the generous wording of question?).
So I will tackle this from second common perspective. Let's say you really have 32 variables, like for example attributes of an character in RPG, some needing just a byte
(strength), some a qword
(experience), and it would soon become super annoying to remember that health has index 4
, and prestidigitation has index 29
.
Also you often need just several of them for particular part of update, like calculating the character doing damage by sword doesn't need elocution and charisma, but also you need several different characters (player vs enemy) with same stats, so you have still shortage of registers.
In such scenario I often just mimick C-like structs:
; defining "PERSON" structure
PERSON_X equ 0 ;2B
PERSON_Y equ PERSON_X+2 ;2B
PERSON_HP equ PERSON_Y+2 ;4B
PERSON_MANA equ PERSON_HP+4 ;4B
PERSON_LEVEL equ PERSON_MANA+4 ;2B
;...
PERSON_PRESTIDIGITATION equ PERSON_ELOCUTION+1 ;1B
PERSON_SIZE equ PERSON_PRESTIDIGITATION+1
; reserving array for 4 player's characters
playersParty: resb 4*PERSON_SIZE
; ... somewhere in the code:
; teleporting whole party at x,y=(32,64)
MOV ax,32 ; new x
MOV bx,64 ; new y
MOV ecx,4
MOV edi,playersParty
setAllPartyMembers:
MOV [edi+PERSON_X],ax
MOV [edi+PERSON_Y],bx
ADD edi,PERSON_SIZE
LOOP setAllPartyMembers
NASM actually has "struc" macro, which will save you some hassle with calculating the proper size by hand (as I did above):
; defining "PERSON" structure with base offset 13 in NASM
; (but you want particular attributes aligned to their size boundary)
STRUC person, 13
alignb 2
.x: resw 1
.y: resw 1
alignb 4
.hp: resd 1
.mana: resd 1
.level: resw 1
;...
.prestidigitation: resb 1
.size:
ENDSTRUC
; player single person with some basic init (e.g. in .data segment)
SEGMENT .data
playerPerson1: ISTRUC person
AT person.hp, dd 100
AT person.mana, dd 100
AT person.level, dw 1
IEND
; .. somewhere in the code:
; moving player by ax on x, bx on y
; and decreasing hp by 1 (it's a trap!)
MOV edi,playerPerson1
ADD [edi+person.x],ax
ADD [edi+person.y],bx
DEC DWORD [edi+person.hp]
; ...
(I didn't debug my code, so there may be a bug here and there, but hopefully the principle is easy to understand just by reading it)