News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to recognize a 8,16 or 32 bit variable

Started by elettronico_79, August 05, 2010, 10:23:49 AM

Previous topic - Next topic

elettronico_79

Hi, I have readed that:
int8, int16, and int32, corresponding to eight-bit (one byte) signed integers, 16-bit (two byte) signed integers, and 32-bit (four byte) signed integers respectively

but after i founded that HLM code:

Program DemoVars;

#include( "stdlib.hhf" );



static

    InitDemo:       int32 := 5;

    NotInitialized: int32;



begin DemoVars;



    // Display the value of the pre-initialized variable:



    stdout.put( "InitDemo's value is ", InitDemo, nl );



    // Input an integer value from the user and display that value:



    stdout.put( "Enter an integer value: " );

    stdin.get( NotInitialized );

    stdout.put( "You entered: ", NotInitialized, nl );



end DemoVars;


my ask is why the variable InitDemo was setting as a 32 bit variable if the number "5" occupy only 1 byte? isn't good that:


InitDemo : int8 := 5;


thanks and sorry my english :)

dedndave

the value of the data (5, in this case) does not presume the size of the data

for example, a 64-bit integer may have a value of 5, as well
the programmer must know the size of the data prior to utilizing it
it isn't enough to be given an address of an integer value
you must know the size of the operand to use it in proper context

elettronico_79

ok, so all of that statements are correct ? :

InitDemo : int8 := 5;

InitDemo : int6 := 5;

InitDemo : int32 := 5;


dedndave

that is correct
also - note that some integers are signed - some are unsigned
it is similar to knowing the size - you must know the type, as well, in order to use it in proper context

elettronico_79

when i run an HLA programs it desappear very quickly, what instruction should i use to stay it in pause until i press any key?
what's the difference betwen a signer or unsigned integer?

dedndave

i dunno about HLA
in the masm32 package, we have a macro called "inkey" that prompts for a keypress
another way to go is to open the console window first, then run the program from there
rather than just clicking on the program

signed/unsigned:
in most cases, "two's compliment" is used for signed values
the uppermost bit is used as the sign bit
for unsigned values, the upper bit is just another value bit

we can use any sized integer to represent signed or unsigned values
let's take 8-bit for example
signed range: -128 to +127
or
unsigned range: 0 to 255
both types are represented with 8 bits

for 8-bit (two's compliment) signed:
00000000 = 0 decimal
01111111 = +127 decimal
10000000 = -128 decimal
11111111 = -1 decimal

for 8-bit unsigned:
00000000 = 0 decimal
01111111 = 127 decimal
10000000 = 128 decimal
11111111 = 255 decimal

MichaelW

Quote from: elettronico_79 on August 05, 2010, 10:23:49 AM
my ask is why the variable InitDemo was setting as a 32 bit variable if the number "5" occupy only 1 byte?

Except for the old 16-bit x86 processors, 32-bit integers are generally preferred over 8-bit or 16-bit integers because the processor can handle them more efficiently.
eschew obfuscation

dedndave

to clarify what Michael says...
the native machine size is most efficient
i.e., if you have a 32-bit processor, 32-bit data is the most efficient size to deal with