Author Topic: "Hello masm32", not a BOT, new member  (Read 7304 times)

jj2007

  • Member
  • *****
  • Posts: 7557
  • Assembler is fun ;-)
    • MasmBasic
Re: "Hello masm32", not a BOT, new member
« Reply #195 on: April 30, 2017, 05:19:58 PM »
A quick help, please...

I need to iterate through an array of structures. [ebx*Bullet ] wont do it, but [2*Bullet] does. Pre compile thing? How do I do this?
Code: [Select]
xor ecx, ecx
@@:
mov edi, offset man.bullets[ecx*Bullet].active
.IF dword Ptr [edi] == 1
printf (" Bullet %d is active\n", dword Ptr [edi])
.ENDIF
inc ecx
cmp ecx, 100
jnz @B

Marinus is right, ecx gets trashed - MB Print Str$() preserves ecx, but printf() doesn't. Simple solution:

Code: [Select]
.IF dword Ptr [edi] == 1
push ecx
printf (" Bullet %d is active\n", dword Ptr [edi])
pop ecx
.ENDIF

Btw no need for edi - this should work, too (not tested):
Code: [Select]
.IF man.bullets[ecx*Bullet].active == 1
push ecx
printf (" Bullet 1 is active\n")  ; 1 is it's value, for sure, so a constant will do ;-)
pop ecx
.ENDIF

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #196 on: May 01, 2017, 08:07:39 AM »
Thanks Marinus and JJ,

But ecx being trash is not the point. The point is my clause with a register is not building.

A test bed:
Quote
include \masm32\include\masm32rt.inc

        Bullet STRUCT
           active   dd      ?
          speed   dd        ?
          x      dd      ?
          y      dd      ?               
        Bullet ENDS

        Player STRUCT 4
               x          dd    ?
               y          dd    ?
              sprite   dd   ?
               frmx   dd   ?
               frmy   dd   ?
               bullets   Bullet   50 dup (<>)
               kState   dd   ?
        Player ENDS
       
.data
       man Player  <>
.code
start:
       mov   man.bullets[2*Bullet].active, TRUE
       mov    man.bullets[2*Bullet].speed, 100
       mov    man.bullets[2*Bullet].x, 100
       mov    man.bullets[2*Bullet].y, 100
       
       mov eax, 2
       ;mov   man.bullets[2*Bullet].active, FALSE      ; <== THIS WORKS
       mov   man.bullets[eax*Bullet].active, FALSE   ; <== THIS DOESN´T
        
       invoke ExitProcess, eax
end start

This is the problem:
Quote
       ;mov   man.bullets[2*Bullet].active, FALSE      ; <== THIS WORKS
       mov   man.bullets[eax*Bullet].active, FALSE   ; <== THIS DOESN´T

Marinus, thanks for the align trick! Does this automatically align all items in the struct?

Siekmanski

  • Member
  • *****
  • Posts: 1094
Re: "Hello masm32", not a BOT, new member
« Reply #197 on: May 01, 2017, 08:41:55 AM »
Marinus, thanks for the align trick! Does this automatically align all items in the struct?
No, only the struct is aligned, but you can use align xx wihtin a struct or place a dummy byte.

this should work,

Code: [Select]
mov esi,offset man.bullets
 mov ecx,sizeof Bullet

 mov eax,2 ; the bullet number
 mul ecx
 mov dword ptr [esi+eax].Bullet.active,FALSE

edit:

Or in this case because the Bullet struct is 16 bytes,

 mov   esi,offset man.bullets
 mov   eax,2   ; the bullet number
 shl   eax,4
 mov   dword ptr [esi+eax].Bullet.active,FALSE

HSE

  • Member
  • ****
  • Posts: 532
  • <AMD>< 7-32>
Re: "Hello masm32", not a BOT, new member
« Reply #198 on: May 01, 2017, 08:52:06 AM »
Inside brackets you only can multiply by 2,4,8,...

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #199 on: May 01, 2017, 08:54:59 AM »
Inside brackets you only can multiply by 2,4,8,...


But this works fine:
 mov   man.bullets[3*Bullet].active, TRUE         ; <== THIS WORKS

And eax in the test bed was == 2.



edit:
It works fine with a constant. Is this a pre-compiling thing?

Quote
.const
   cINDEX      EQU   3 
...
         mov   man.bullets[cINDEX*Bullet].active, TRUE         ; <== THIS WORKS

Siekmanski

  • Member
  • *****
  • Posts: 1094
Re: "Hello masm32", not a BOT, new member
« Reply #200 on: May 01, 2017, 09:07:41 AM »
Maybe because its an invalid scale value ?

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #201 on: May 01, 2017, 09:08:36 AM »
Marinus,
Quote
No, only the struct is aligned, but you can use align xx wihtin a struct or place a dummy byte.

this should work,

It sure works. However, I wished I could use that syntax, or at least understand why it´s not working

Quote
Maybe because its an invalid scale value ?

Sure, but in this case mov   man.bullets[3*Bullet].active, TRUE shouldn´t work either

Siekmanski

  • Member
  • *****
  • Posts: 1094
Re: "Hello masm32", not a BOT, new member
« Reply #202 on: May 01, 2017, 09:12:46 AM »
does this work ?
 
 mov   eax,2   ; the bullet number
 shl   eax,4
 mov man.bullets[eax].active, FALSE   

what assembler do you use ?

jj2007

  • Member
  • *****
  • Posts: 7557
  • Assembler is fun ;-)
    • MasmBasic
Re: "Hello masm32", not a BOT, new member
« Reply #203 on: May 01, 2017, 09:32:07 AM »
It works fine with a constant. Is this a pre-compiling thing?

Exactly:
At compile time, any constant is fine.
At runtime, HSE's remark is correct: 2, 4 or 8 are the allowed scaling values.

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #204 on: May 01, 2017, 09:49:33 AM »
does this work ?
 
 mov   eax,2   ; the bullet number
 shl   eax,4
 mov man.bullets[eax].active, FALSE   

what assembler do you use ?

That works too.
I´m on HJWasm, since I usually use RichMasm. I try and keep compatibility using qEditor (ML)

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #205 on: May 01, 2017, 09:51:58 AM »
Quote
At compile time, any constant is fine.
At runtime, HSE's remark is correct: 2, 4 or 8 are the allowed scaling values.

I was suspecting that, and the constant test showed it. So, in that syntax the compiler can´t know the value of the register and thus generates an error. Got it.

Thanks everyone, this was instructive.

jj2007

  • Member
  • *****
  • Posts: 7557
  • Assembler is fun ;-)
    • MasmBasic
Re: "Hello masm32", not a BOT, new member
« Reply #206 on: May 01, 2017, 10:07:55 AM »
I´m on HJWasm, since I usually use RichMasm. I try and keep compatibility using qEditor (ML)

This is how I test different assemblers:
; OPT_Assembler ML  ; somewhere in the file forces RichMasm to use \Masm32\bin\ml.exe (or any other exe...)

OPT is case-sensitive, the rest isn't.
Just put an x after the O: OxPT_anyoption to disable an option. If there are several options of the same type, the last one is valid:
...
end start
OPT_Arg1  fileA.txt
OPT_Arg1  fileB.txt

CL$() will find fileB.txt

LordAdef

  • Member
  • ***
  • Posts: 255
Re: "Hello masm32", not a BOT, new member
« Reply #207 on: May 01, 2017, 10:42:15 AM »
NICE ONE!!!!! RM/MB always have a away around things..

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: "Hello masm32", not a BOT, new member
« Reply #208 on: May 01, 2017, 02:20:38 PM »
Something that some folks miss, a "syntax error" with an instruction means that the processor does not have a matching opcode for something in source code that has been fed to it. This applies to the multiplier range of 2, 4 and 8, each of which has a matching opcode, try it with any other number and you are trying to call a non existent opcode. The assembler "should" catch this but they can be a bit sloppy and miss a simple error like this, an error type the is mainly a typo.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: