News:

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

Main Menu

Char Array cpp

Started by ragdog, February 21, 2015, 07:41:15 AM

Previous topic - Next topic

ragdog

Hello Again

I have only a little question about this cpp Array
static char str[20][100] = {' ',};

Ist this correct in Masm i try to understand a cpp source

str             db 20, 100 dup(0)


Regards,

dedndave

i am not sure, but it looks like a 20 x 100 array

ragdog

Hello Dave

I also think, and what is {' ',}; first char is a space?

Vortex

Hello ragdog,

That code will produce one space character followed by 1999 NULL characters.

Disassembling the code:


.386
option dotname
.model flat

public _main

_data   SEGMENT DWORD PUBLIC 'DATA'

@8      label byte
        db 20H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        db 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        .
        .
        .
        db 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H

_data   ENDS

dedndave

static char str[20][100] = {' ',};

i believe that creates an array of 20 strings, 100 characters each, and fills them with spaces
i am not a C guy, so i may be wrong
each of the 20 strings might be zero-terminated, too - i don't know

ragdog

Thank you both

Quote

_data   SEGMENT DWORD PUBLIC 'DATA'

@8      label byte
        db 20H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        db 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        .
        .
        .
        db 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H

_data   ENDS

Thats right Vortex i understand the same but gives a syntax for it in Masm

expl:

str   2000 dup (?) <{" "}>   ; i know thats not works ;)

Ok a solution is :
str     db 20h
   db 1999 dup (?)

But i have think it gives a similar solution like cpp


dedndave

i would probably do this
    .DATA?

str db 2000 dup(?)

    .CODE

start:
    mov     edi,offset str
    mov     ecx,sizeof str/4
    mov     eax,20202020h
    rep     stosd

ragdog

Right Dave this is a simply and knowing solution but i have think it is posible in Masm to define in the data section like cpp

dedndave

of course you can
    .DATA

str db 2000 dup(20h)


that makes the EXE almost 2 KB larger though

ragdog

Right

I use now a Allocated memory and add the first char a space

Thanks you,

hutch--

Try this algo.

http://masm32.com/board/index.php?topic=4050.0

mabdelouahab


Vortex

Quote from: mabdelouahab on February 22, 2015, 05:41:52 PM

str db 20*100 dup(" ")


You probably didn't read Dave's posting. Your executable will be bigger because of that data block.

rrr314159

Hello ragdog,

The closest MASM comes to that cpp syntax is this:

str db 20 dup (100 dup (' ',))

This fills all 2000 bytes with 20h. I *think* cpp does the same, the other 1999 are not zero's; but I hate cpp so have a memory block here. The rows are not 0-delimited.

Have a look at this chapter from "The Art of Assembly", Section 5.6.1 Arrays.

Yes, this will increase your .exe by 2K, but that's what cpp does; static arrays are always initialized, with 0's if nothing else.

Well, since you have to deal with it, I hope u like cpp better than I do. Used to love C but after C++ even that's distasteful. Good luck!
I am NaN ;)

ragdog

Thank you all for this infos

QuoteYes, this will increase your .exe by 2K

What is 2 k todays gives programs with many Mbs  :lol:

But my goal was to understand this cpp source