News:

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

Main Menu

array index ???

Started by requiem31, May 15, 2014, 06:09:49 AM

Previous topic - Next topic

requiem31

So I am trying to work with arrays in masm32 (came from c++) and its really getting annoying. I proto typed what I want in c++ and it looks like this: void randomplaceShip()
{
int toPlace = 3;
srand(time(0));

while (toPlace > 0)
{
int v1 = rand() % 12;
int v2 = rand() % 12;

if (dArray[v1][v2] != 'X' && v1 < 9)
{
for (int i=0; i<3; i++)
{
dArray[v1+i][v2] = 'X';
}
toPlace--;
}
}
}

It places 3 ships on a 12x12 game board each 1 wide and 3 long. So far in assembly I store my array address in the esi register, and get a random number between 1-9 that I store in rVal. I am having trouble with storing an "X" at the right spot because theres no indexing like there is in c++ =(. The array I am using in masm32 is a byte array with 144 slots (for the 12x12 board).

lea esi, dArray
        mov eax, 11
       
        .while eax > 10
            invoke crt_rand
        .endw
        mov rVal1, eax

        mov eax, 11
        .while eax > 10
            invoke crt_rand
        .endw
        mov rVal2, eax
       
        movzx eax, byte ptr [esi+1*rVal1]    ;The problem
        mov eax, "x"                        ;IS HERE =(

requiem31

wait I don't need 2 random values just 1 that is 144 because I do not have a multi dimensional array lol. Does not solve the overall problem but it helps

qWord

Quote from: requiem31 on May 15, 2014, 06:09:49 AM
        mov eax, 11
        .while eax > 10
            invoke crt_rand
        .endw
        mov rVal2, eax
You should reconsider that loop! You might use floating point math to convert values in interval [0, RAND_MAX] to [0,10].

Quote from: requiem31 on May 15, 2014, 06:09:49 AM        movzx eax, byte ptr [esi+1*rVal1]
You can't use a memory operand as part of an other memory operand (-> use a register). Did you read any book or tutorial on x86 Assembler?
MREAL macros - when you need floating point arithmetic while assembling!

requiem31

Nope our teacher has never lectured. He just hands out worksheets and thats all we do in class LOL. But he does expect me to write the game battleship for my final project.

requiem31

Alright I changed it up like you suggested and did not use a memory operand. Although the things just crashes the second I try to place a random ship because I think I am outside of my array. It makes sense to me though :P, I have the address of the array +1-144 , since each memory location will only be 1 wide in a byte array that "should" be fine.
        lea esi, dArray
        mov eax, 150
       
        .while eax > 144
            invoke crt_rand
        .endw
        mov edx, eax
       
        movzx eax, byte ptr [esi+edx]    ;The problem
        mov eax, "x"                        ;IS HERE =(

MichaelW

This won't help you with your memory operands, but it does show a simple way to use a random number generator to generate random numbers in a given interval.

;==============================================================================
    include \masm32\include\masm32rt.inc
;==============================================================================
    .data
        min dd 0ffffffffh
        max dd 0
    .code
;==============================================================================
; This proc returns a value in the interval [lbound,ubound]
rand proc lbound:DWORD, ubound:DWORD
    ; Use INVOKE here so ML will generate code to remove parameters from stack.
    invoke crt_rand
    mov ecx, ubound
    sub ecx, lbound
    inc ecx           ; ECX = ubound - lbound + 1
    cdq               ; zero extend EAX into EDX to avoid integer overflow
    div ecx           ; rand / (ubound - lbound + 1) EAX=quotient EDX=remainder
    mov eax, edx      ; EAX now has (rand % (ubound - lbound + 1))
    add eax, lbound   ; adjust for lbound
    ret
rand endp
;==============================================================================
start:
;==============================================================================
    REPEAT 1000
    invoke rand, 1, 9
    .IF eax < min
        mov min, eax
    .ENDIF
    .IF eax > max
        mov max, eax
    .ENDIF
    ENDM
    printf("%d\t%d\n\n", min, max)
    inkey "Press any key to exit..."
    exit
;==============================================================================
end start

Well Microsoft, here's another nice mess you've gotten us into.

dedndave

sort of like the Battleship game ?
i take it the ships can't be on top of each other   :P

surprisingly, converting your C code to ASM isn't as hard as you might think
a few differences in syntax, and you have to manage your own counts
you can calculate indexes into an array fairly easily

a good place to start might be the MASM version 6.1 programmer's guide, chapter 7

there are numerous places to download the PDF
i remember someone posting a CHM version of it, as well

MASM supports .if/.else/.elseif/.endif, .repeat, .while/.wend, and so on
and - you can use the flags as operands
    sub     eax,edx
    .if CARRY?
        ;do stuff
    .else
        ;do other stuff
    .endif

FORTRANS

Quote from: dedndave on May 15, 2014, 09:47:57 PM
a good place to start might be the MASM version 6.1 programmer's guide, chapter 7

there are numerous places to download the PDF
i remember someone posting a CHM version of it, as well

Hi Dave,

   FWIW, I seem to have a copy in *.DOC format as well.

Cheers,

Steve N.

dedndave

hi Steve
i think you can download a DOCX version directly from MS
is that what you're refering to ?

dedndave

i am going to leave these on my site for a short time

the PDF version is high-quality and includes all the manuals for masm 6.1
compliments of qWord....

http://dedndave.x10.mx/files/MASM61GuidePDF.zip

the CHM version is compliments of GregL...

http://dedndave.x10.mx/files/MASM61GuideCHM.zip

FORTRANS

Quote from: dedndave on May 15, 2014, 10:52:58 PM
hi Steve
i think you can download a DOCX version directly from MS
is that what you're refering to ?

Hi,

   Haven't the foggiest idea where it came from.  Probably Microsoft
though.  I actually have three related documents.  See below.

Directory of C:\temp

9-07-01   9:06a    271204           0  MasmReference.zip
        1 file(s)     271204 bytes used

Searching ZIP: MASM.ZIP

Length  Method   Size  Ratio    Date     Time    CRC-32  Attr  Name
------  ------   ----- -----    ----     ----   -------- ----  ----
  39936  DeflatX   8454  79%  05-21-2000  12:40  f77f4c05 --wa  TitleAndCopyright.doc
  50011  DeflatX  17475  66%  02-16-1994  18:22  e5961843 --w-  Chap_02.doc
  36420  DeflatX  12502  66%  02-16-1994  18:22  939165c6 --w-  Chap_03.doc
308760  DeflatX  77143  76%  02-16-1994  18:22  e845db29 --w-  Chap_04.doc
169344  DeflatX  36598  79%  02-16-1994  18:22  0c92dbf2 --w-  Chap_05.doc
  19662  DeflatX   7213  64%  02-16-1994  18:22  81cb7ba0 --w-  Chap_06.doc
446688  DeflatX  67939  85%  02-16-1994  18:23  b9e6f29d --w-  Chap_07.doc
  34489  DeflatX   8931  75%  02-16-1994  18:23  96cf6d4e --w-  Introduction.doc
  73547  DeflatX  24582  67%  02-16-1994  18:22  25b13997 --w-  Chap_01.doc
  24516  DeflatX   9333  62%  02-16-1994  18:23  979a6e80 --w-  Contents.doc
------          ------  ---                                    -------
1203373          270170  78%                                         10

Reference
Microsoft® MASM
Assembly-Language Development System
Version 6.1
For MS-DOS® and Windows™ Operating System
Microsoft Corporation


Directory of C:\temp

9-07-01   9:13a   1669785           0  EnvironTools.zip
        1 file(s)    1669785 bytes used

Length  Method   Size  Ratio    Date     Time    CRC-32  Attr  Name
------  ------   ----- -----    ----     ----   -------- ----  ----
642418  DeflatX 102334  85%  02-16-1994  18:15  838152da --w-  Chap_02a.doc
  20466  DeflatX   7919  62%  02-16-1994  18:16  da7d5e4a --w-  Chap_01.doc
422673  DeflatX  69101  84%  02-16-1994  18:16  7725075c --w-  Chap_04.doc
615144  DeflatX 154433  75%  02-16-1994  18:17  d57d9b25 --w-  Chap_07.doc
225383  DeflatX  56874  75%  02-16-1994  18:17  bf48320d --w-  Chap_09.doc
  80983  DeflatX  25217  69%  02-16-1994  18:17  c8a8da94 --w-  Chap_11.doc
142055  DeflatX  42282  71%  02-16-1994  18:17  86808c0f --w-  Chap_13.doc
  83938  DeflatX  22162  74%  02-16-1994  18:17  15ec5b6b --w-  Chap_15.doc
  58708  DeflatX  18674  69%  02-16-1994  18:17  f27b0b4a --w-  Chap_17.doc
  64488  DeflatX  20075  69%  02-16-1994  18:17  4aad28d0 --w-  Chap_19.doc
480057  DeflatX  82908  83%  02-16-1994  18:17  cf0d52be --w-  Chap_21.doc
211065  DeflatX  64955  70%  02-16-1994  18:18  21680557 --w-  Index.doc
713188  DeflatX 111028  85%  02-16-1994  18:16  2ee35b7c --w-  Chap_05a.doc
  18644  DeflatX   5366  72%  02-16-1994  18:18  186605d1 --w-  Part_4.doc
  13248  DeflatX   4755  65%  02-16-1994  18:18  0e4dfc5f --w-  Part_5.doc
  49831  DeflatX  15081  70%  02-16-1994  18:18  f41d470b --w-  Contents.doc
  32256  DeflatX   5522  83%  05-21-2000  13:22  6b43c6f5 --wa  TitlePage.doc
740004  DeflatX 127421  83%  02-16-1994  18:15  879d4d7e --w-  Chap_02b.doc
127434  DeflatX  20950  84%  02-16-1994  18:16  1c24c42d --w-  Chap_05b.doc
  12920  DeflatX   4559  65%  02-16-1994  18:18  c4059bab --w-  Part_3.doc
  12920  DeflatX   4567  65%  02-16-1994  18:18  352cd7ae --w-  Part_2.doc
  12920  DeflatX   4585  65%  02-16-1994  18:18  5caabb47 --w-  Part_1.doc
765531  DeflatX 126812  84%  02-16-1994  18:16  18553574 --w-  Chap_03.doc
498024  DeflatX  81013  84%  02-16-1994  18:16  30197881 --w-  Chap_06.doc
119990  DeflatX  35944  71%  02-16-1994  18:17  3aec34cf --w-  Chap_08.doc
107548  DeflatX  31966  71%  02-16-1994  18:17  f7154d05 --w-  Chap_10.doc
256474  DeflatX  73466  72%  02-16-1994  18:17  7cf3a358 --w-  Chap_12.doc
108789  DeflatX  30346  73%  02-16-1994  18:17  e3c374ac --w-  Chap_14.doc
225671  DeflatX  65405  72%  02-16-1994  18:17  88cc90cb --w-  Chap_16.doc
103588  DeflatX  31182  70%  02-16-1994  18:17  ebbdab85 --w-  Chap_18.doc
105553  DeflatX  33204  69%  02-16-1994  18:17  df212c2c --w-  Chap_20.doc
116288  DeflatX  35495  70%  02-16-1994  18:17  715b52c4 --w-  Glossary.doc
  47133  DeflatX  15492  68%  02-16-1994  18:17  f9f3053e --w-  Introduction.doc
395636  DeflatX 111037  72%  02-16-1994  18:16  6a0431fc --w-  Appendix_A.doc
  70893  DeflatX  19379  73%  02-16-1994  18:16  386662f0 --w-  Appendix_B.doc
  13025  DeflatX   4696  64%  02-16-1994  18:16  f3bd030a --w-  Appendixes.doc
------          ------  ---                                    -------
7714886         1666205  79%                                         36

Environment and Tools
Microsoft® MASM
Assembly-Language Development System
Version 6.1
For MS-DOS® and Windows™ Operating Systems
Microsoft Corporation


Directory of C:\temp

9-07-01   8:20a    878681           0  ProgrammersGuide.zip


Length  Method   Size  Ratio    Date     Time    CRC-32  Attr  Name
------  ------   ----- -----    ----     ----   -------- ----  ----
  24163  DeflatX   7819  68%  02-16-1994  18:08  817b3429 --r-  Appendix_E.doc
  94018  DeflatX  30212  68%  02-16-1994  18:08  85b00785 --r-  Chap_02.doc
164886  DeflatX  42210  75%  02-16-1994  18:08  3c234576 --r-  Chap_04.doc
335343  DeflatX  54057  84%  02-16-1994  18:09  a7bfa75c --r-  Chap_06.doc
127368  DeflatX  27126  79%  02-16-1994  18:09  d492d137 --r-  Chap_08.doc
  60677  DeflatX  20687  66%  02-16-1994  18:09  d4fb7a24 --r-  Chap_10.doc
233475  DeflatX  47705  80%  02-16-1994  18:09  b4da80cd --r-  Chap_12.doc
125952  DeflatX  27711  78%  05-21-2000  12:37  cbc2e0f4 --ra  Index.doc
  66048  DeflatX  13049  81%  05-21-2000  12:35  e96f7b1d --ra  Title_and_Contents.doc
  38945  DeflatX  14158  64%  02-16-1994  18:09  ad480525 --r-  Introduction.doc
124777  DeflatX  40919  68%  02-16-1994  18:08  284765a1 --r-  Appendix_A.doc
  88655  DeflatX  27088  70%  02-16-1994  18:08  1af75de9 --r-  Appendix_B.doc
314115  DeflatX  73064  77%  02-16-1994  18:08  fc1c16a0 --r-  Chap_01.doc
  37754  DeflatX  13198  66%  02-16-1994  18:08  5e92e544 --r-  Appendix_C.doc
  38220  DeflatX  13032  66%  02-16-1994  18:08  f82d43b2 --r-  Appendix_D.doc
140974  DeflatX  42159  71%  02-16-1994  18:08  af63aa70 --r-  Chap_03.doc
238691  DeflatX  46168  81%  02-16-1994  18:09  6ec5e7f6 --r-  Chap_05.doc
341316  DeflatX  76578  78%  02-16-1994  18:09  af63b507 --r-  Chap_07.doc
  97167  DeflatX  32558  67%  02-16-1994  18:09  97afe85e --r-  Chap_09.doc
293731  DeflatX  68797  77%  02-16-1994  18:09  1ad87f33 --r-  Chap_11.doc
  55462  DeflatX  15729  72%  02-16-1994  18:09  68a52e00 --r-  Chap_13.doc
  73071  DeflatX  24788  67%  02-16-1994  18:09  8ea02d21 --r-  Glossary.doc
395264  DeflatX 117519  71%  05-21-2000  12:32  5449983c --wa  GettingStarted.doc
------          ------  ---                                    -------
3510072          876331  76%                                         23

Programmer's Guide
Microsoft® MASM
Assembly-Language Development System
Version 6.1
For MS-DOS® and Windows™ Operating Systems
Microsoft Corporation


   On a related topic (??), what is the last version that will run
on a 16-bit system with MS-DOS.  And how would I get a copy?
I have not had any luck with Microsoft.Com.  (Probably pilot error
if it is there.)  TIA.

Regards,

Steve N.

dedndave

you may be able to find a version 6, earlier than 6.1
i don't really know if that's 16-bit, but i think it is

otherwise, i have version 5.10, which i used for a long time prior to win32
it includes SYMDEB, CODEVIEW, and other tools

dedndave

hmmmm - i guess it doesn't have SYMDEB
CODEVIEW replaced it in v 5.10

dedndave

windows 2.0 SDK has some stuff  ;)  (including SYMDEB/MAPSYM)
you might look at other SDK's