News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Structures

Started by CCurl, October 16, 2015, 03:00:51 AM

Previous topic - Next topic

CCurl

This has probably been covered before, so ... sorry

In my implementation a string is a DWORD count, followed by a bunch of BYTEs.

How would I define a variable like that?

Right now, I have it like this, but I don't like it ...


cmdBye BYTE 3, 0, 0, 0, "bye", 0

essentially, i'm looking for a way to have it more like this ...

cmdBye DWORD 3, BYTE "bye", 0


thanks

FORTRANS

Hi,

   Well, you could try the following:


String  DD      3
        DB      0, 0, 0, "bye", 0


HTH,

Steve

TouEnMasm

cmdBye      DD 3
                   DB  "bye", 0
Fa is a musical note to play with CL

mabdelouahab

Quote from: CCurl on October 16, 2015, 03:00:51 AM
In my implementation a string is a DWORD count, followed by a bunch of BYTEs.

Are you looking for BSTR?

jj2007

Quote from: CCurl on October 16, 2015, 03:00:51 AMRight now, I have it like this, but I don't like it ...


cmdBye BYTE 3, 0, 0, 0, "bye", 0

essentially, i'm looking for a way to have it more like this ...

cmdBye DWORD 3, BYTE "bye", 0

FORTRAN showed a solution. But you probably want a more flexible way to define strings:
include \masm32\include\masm32rt.inc

CCurl$ STRUCT
  ccPtr dd ? ; first dword holds the pointer
  ccLen dd ? ; second one takes the length
CCurl$ ENDS

.data ; define a string table using this structure
MyStrings CCurl$ <String1, sizeof String1>,
<String2, sizeof String2>,
<String3, sizeof String3>,
<0, 0>

String1 db "This is the first string", 0
String2 db "String #2", 0
String3 db "String Nr. three is a bit longer than the others", 0

.code
start:
  xor ebx, ebx ; index
  mov esi, offset MyStrings ; start of string table
  cc$ equ [8*ebx+esi.CCurl$] ; we use an equate to make the following more readable
  print "len", 9, "string", 13, 10 ; print the header
  .While 1 ; endless loop
mov eax, cc$.ccLen
dec eax
.Break .if Sign? ; if it was a string with zero len, the sign flag is now set
print str$(eax), 9
print cc$.ccPtr, 13, 10
inc ebx
  .Endw
  inkey chr$(13,10, "--- hit any key ---")
  exit

end start


Output:
len     string
24      This is the first string
9       String #2
48      String Nr. three is a bit longer than the others


And mabdelouahab is right: this resembles very much a BSTR.

The above code is plain assembler with a handful of Masm32 macros (print, inkey). The following snippet does BASICally the same, it creates a stringtable with pointers and lengths from a file:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  Recall "\Masm32\include\msvcrt.inc", ms$()      ; create a string table
  Print "Len", Tb$, "String"
  For_ ecx=0 To 30
      Print Str$("\n%i\t", Len(ms$(ecx))), ms$(ecx)
  Next
  Inkey "--- hit any key ---"
EndOfCode

CCurl

Quote from: mabdelouahab on October 16, 2015, 07:22:14 AM
Quote from: CCurl on October 16, 2015, 03:00:51 AM
In my implementation a string is a DWORD count, followed by a bunch of BYTEs.

Are you looking for BSTR?
No, I am implementing my own Forth interpreter. In my implementation, I want to use counted strings with the count as the first element of the string. I also want the string data to be 8-bit CHARs. So the first element of the string (the count) is 32-bit, and the rest of the elements (the characters in the string) are 8-bit.

So it's sort of a hybrid.

CCurl

I swear ... you guys are the best! Thanks again for being so helpful!    :icon14: