News:

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

Main Menu

Question regarding initializing variables in .data

Started by Sieg, April 19, 2015, 09:37:48 AM

Previous topic - Next topic

Sieg

Hi! I've learned that we have DB, DW, DD and a few other options to choose from when initializing variables in the .data section.

I understand the binary/hexadecimal systems and the size of bits, nibbles (4 bits), bytes (8 bits), words (16 bits), and double words (32 bits).

Basically, my question is how do I determine how many bits are necessary to store a given variable?

In the example provided (https://www.tortall.net/projects/yasm/manual/html/nasm-pseudop.html):


     db      0x55                ; just the byte 0x55
        db      0x55,0x56,0x57      ; three bytes in succession
        db      'a',0x55            ; character constants are OK
        db      'hello',13,10,'$'   ; so are string constants
        dw      0x1234              ; 0x34 0x12
        dw      'a'                 ; 0x41 0x00 (it's just a number)
        dw      'ab'                ; 0x41 0x42 (character constant)
        dw      'abc'               ; 0x41 0x42 0x43 0x00 (string)
        dd      0x12345678          ; 0x78 0x56 0x34 0x12
        dq      0x1122334455667788  ; 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11
        ddq     0x112233445566778899aabbccddeeff00
        ; 0x00 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0x99
        ; 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11
        do     0x112233445566778899aabbccddeeff00 ; same as previous
        dd      1.234567e20         ; floating-point constant
        dq      1.234567e20         ; double-precision float
        dt      1.234567e20         ; extended-precision float


0x55 - How do I know this will only need the directive db?
0x1234 - How do I know this will need to have the directive dw?

And why does a string, say for instance


.data
myStr    db "fdskjfhsdfsd fhsdf ksdhfsd fhsdkf ksdhf sdhfsdf khfjksdh fkjsdhf kjsdhfsd kjfshdf sdkjfhsd kfhsdk fkjsdhf ksjdfhsdkj jfsf",0


Only need the directive db? Seems like it would take up more than a byte!

Thanks, and I hope this isn't a silly question.

rrr314159

#1
Hi Sieg,

The numbers you're looking at, 0x55 and 0x1234, are hex numbers. Each digit goes not from 0 to 9, but 0 to 15, so each digit takes one nibble, 4 bits. Therefore two digits needs 1 byte, and 4 digits needs 1 word, 2 bytes.

When defining a string of words like 0x1234, 0x1ab4, 0x0001, you may ask: the last one only takes 1 digit - in fact, just one bit - so why use a whole word to store it? It would be far too much trouble to specifically use a byte for that "0x1", both in writing the data statement and, later, handling it in code. And saving a couple bytes is absolutely meaningless with today's storage. In general, when in doubt, use the larger size. For instance I often define numbers as dwords, 32 bits, even though the largest one might be only 100 (or whatever), just for convenience.

It's worth noting the decimal equivalents: 1 byte, 2 nibbles, goes up to 255 decimal; 2 bytes, 1 word, to 65535 decimal; and so forth.

When a string is defined like db "fdskjfhsdfsd" it means that each character takes a byte. the total storage would be, in this case, 12 bytes.

BTW with MASM instead of 0x55 you would write 55h.

have fun , !
I am NaN ;)

dedndave

Quote from: Sieg on April 19, 2015, 09:37:48 AM
Basically, my question is how do I determine how many bits are necessary to store a given variable?

quite often, the required range will determine the size used
even though a value may now hold 1234h, for example - it may later hold some other value(s)

sometimes, however, i assign a DWORD size because it's more convenient in the code
if i want to initialize a DWORD sized register, it's better to initialize a DWORD variable,
even though the used range of values might fit into a smaller data item
it saves me from having to use special instructions or zeroing out the upper bits

dwValue dd 5
.
.
.
    mov     eax,dwValue


Quote from: Sieg on April 19, 2015, 09:37:48 AM
And why does a string, say for instance...
...Only need the directive db? Seems like it would take up more than a byte!

it's an array of bytes
if UNICODE strings are supported, it might be an array of words, though   :P

Sieg