News:

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

Main Menu

DefStruct, Data, Read and Rewind

Started by jj2007, December 08, 2023, 10:49:25 AM

Previous topic - Next topic

jj2007

Version 8 December has some more goodies:

- DefStruct defines a structure:

include \masm32\MasmBasic\MasmBasic.inc
DefStruct PETS: puppyname, gender: BYTE, cuteness:BYTE, baseprice, DogOrCatProc
Data "Cooper", 0, 33, 100, Dog, "Yorky", 0, 99, 101, Dog
Data "Bella", 1, 77, 128, Cat, "Daisy", 1, 60, 100, Cat
Data "Charlie", 1, 55, 90, Dog, @, 1, 2, 3, "a string"    ; @ separates pets section from the following data
  Init
  Dim pets() As PETS
  Read pets()
  For_ ecx=0 To eax-1
    xCall pets(ecx, DogOrCatProc), ecx    ; pass the registered function and the index
    PrintLine Str$("\t(cuteness %i", pets(ecx, cuteness)), Str$(" x base %i)", pets(ecx, baseprice))
  Next
  Inkey "done"
  Exit
Dog proc inx
  movzx eax, pets(inx, cuteness)
  push eax
  fild pets(inx, baseprice)
  fimul stack
  fistp stack
  Print pets(inx, puppyname), cfm$("\tis a dog and costs ")
  pop eax
  Print Str$("%i euros\t", eax)
  ret
Dog endp
Cat proc inx
  movzx eax, pets(inx, cuteness)
  push eax
  fild pets(inx, baseprice)
  fimul stack
  fistp stack
  Print pets(inx, puppyname), cfm$("\tis a cat and costs ")
  pop eax
  Print Str$("%i euros\t", eax)
  ret
Cat endp
EndOfCode

Output:
Cooper  is a dog and costs 3300 euros          (cuteness 33 x base 100)
Yorky  is a dog and costs 9999 euros          (cuteness 99 x base 101)
Bella  is a cat and costs 9856 euros          (cuteness 77 x base 128)
Daisy  is a cat and costs 6000 euros          (cuteness 60 x base 100)
Charlie is a dog and costs 4950 euros          (cuteness 55 x base 90

- The Rewind macro sets the Data pointer back by n items (no arg, one item):
Data 123, "a string"
Read eax, ecx    ; eax=123, ecx="a string"
Rewind 2
Read var1, var2  ; var1=123, var2="a string"

As always, since this code assembles just fine with Masm (6.15 ... 15 or so - attention, the latest ones are buggy), UAsm, AsmC and JWasm, it is purest Assembly :biggrin: