The MASM Forum

General => The Campus => Topic started by: ragdog on February 21, 2015, 07:41:15 AM

Title: Char Array cpp
Post by: ragdog on February 21, 2015, 07:41:15 AM
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,
Title: Re: Char Array cpp
Post by: dedndave on February 21, 2015, 07:42:25 AM
i am not sure, but it looks like a 20 x 100 array
Title: Re: Char Array cpp
Post by: ragdog on February 21, 2015, 08:00:25 AM
Hello Dave

I also think, and what is {' ',}; first char is a space?
Title: Re: Char Array cpp
Post by: Vortex on February 21, 2015, 08:08:59 AM
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
Title: Re: Char Array cpp
Post by: dedndave on February 21, 2015, 08:33:24 AM
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
Title: Re: Char Array cpp
Post by: ragdog on February 21, 2015, 08:41:39 PM
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

Title: Re: Char Array cpp
Post by: dedndave on February 22, 2015, 01:26:59 AM
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
Title: Re: Char Array cpp
Post by: ragdog on February 22, 2015, 01:31:24 AM
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
Title: Re: Char Array cpp
Post by: dedndave on February 22, 2015, 01:34:36 AM
of course you can
    .DATA

str db 2000 dup(20h)


that makes the EXE almost 2 KB larger though
Title: Re: Char Array cpp
Post by: ragdog on February 22, 2015, 01:59:41 AM
Right

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

Thanks you,
Title: Re: Char Array cpp
Post by: hutch-- on February 22, 2015, 12:48:05 PM
Try this algo.

http://masm32.com/board/index.php?topic=4050.0
Title: Re: Char Array cpp
Post by: mabdelouahab on February 22, 2015, 05:41:52 PM

str db 20*100 dup(" ")
Title: Re: Char Array cpp
Post by: Vortex on February 22, 2015, 08:40:59 PM
Quote from: mabdelouahab on February 22, 2015, 05:41:52 PM

str db 20*100 dup(" ")


You probably didn't read Dave's posting (http://masm32.com/board/index.php?topic=4046.msg42635#msg42635). Your executable will be bigger because of that data block.
Title: Re: Char Array cpp
Post by: rrr314159 on February 23, 2015, 12:33:26 AM
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 (http://courses.engr.illinois.edu/ece390/books/artofasm/CH05/CH05-2.html#HEADING2-16).

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!
Title: Re: Char Array cpp
Post by: ragdog on February 23, 2015, 12:58:11 AM
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