Sign in to follow this  
ordered_disorder

what languages force you to declare your variables in a data segment

Recommended Posts

I'm learning assembly, and very unlike c++ I can't just declare variables anywhere in my code, well I can but it will cause all sorts of weird crashes, which I don't really understand. I heard if your using a c only compiler, all your variables need to be declared at the top of a code block segment, is this true? I'm curious to what other languages force you to declare your variables in a special section of your code.

Share this post


Link to post
Share on other sites
Some versions of C (depending which revision) force you to declare your data at the beginning of an instruction block (not necesarly at the beginning of a function).
Later C revisions will let you declare it allover, but I personally think that is a bad practice.

As for ASM, depending on the CPU you are using and the OS, the data doesn't have to be in the data segment, it can be in any segment. That's how you can write self modifying code, or how you load a program from a file to execute.

Share this post


Link to post
Share on other sites
masm forces u to declare your varibles in the .data area.
whereas fasm does not you can declare them wherever you want.

jmp end_Data
; data here
end_Data:

that way fasm doesn't try to use my varibles as instructions.
i haven't done inline asm yet. so i don't know how that goes.

i don't like declaring varibles all over the place because, if one varible holds a bad value. or gets modified incorrectly, you then have to hunt that varible down within god knows how many lines of code.
its cleaner and easier to declare them in an include file.
or at the beginning of functions/ start of the program.

but each to their own.

Share this post


Link to post
Share on other sites
In C or C++, (global and static) variables are physically placed in the data section regardless of where you declare them.

In many other languages, variables are created dynamically.

Assembly is not like this.

In assembly, you don't really declare a variable, you're just reserving some memory and declaring a label. A label is not intrinstically a variable, rather just a pointer to somewhere in memory, which may be code or data (or anything else). A label is only a variable if you use it as one.

Mark

Share this post


Link to post
Share on other sites
Quote:

Later C revisions will let you declare it allover, but I personally think that is a bad practice.

Quote:

Thanks raduprv, could you please elaborate on why you think its bad practice for c to be able to declare its variables anywhere?

Some programmers think that the code becomes harder to read if the variables declaration are spread along a function, while having them all grouped in the same place is easier. As for me, I'm half-half: I group the variables declaration into logical units in a function, and put them at the beginning of their unit.
But I find usefull to let the programmer choose where to declare the variables:

void foo(int *buffer)
{
if(!buffer) return;

SlowConstructor a(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0));
SlowConstructor b(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0));
SlowConstructor c(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0));
.
.
.



In this case, it would be bad (IMO) to declare a,b,c befor checking the pointer, and whenever the utilization of a var depends from a condition (which is not said that uses a code block, like in this example), it is better to declare the variables after the condition. In addition, often one needs many vars in a function, and I find annoying to have 20 vars in the head of a function.

Share this post


Link to post
Share on other sites
There are two reasons why I like them at the beginning of the function:

1. It is easier to see, at a glance, what variables are used in the function.
2. Makes it a little bit harder to define a variable in two places in the same function. For example:

function_blah()
{
//stuff
int i;
//use i for some stuff
//later on:
while(blah)
{
int i=something;
}
}

Share this post


Link to post
Share on other sites
In NASM, you can do something like

[section .TEXT]
; code here
[section .DATA]
dd variable
[section .TEXT]
; more code


and nasm sorts them out. You could code a macro to simplifly the process.
DISCLAIMER: I don't know if this is correct NASM syntax.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Iirc, COBOL and Fortran force you to declare variables in special sections of code. Visual Basic does too.


COBOL yes. Fortran, I don't know. VB: Not since I've started using it (6.0).

Share this post


Link to post
Share on other sites
Quote:
Original post by jfclavette
Quote:
Original post by LessBread
Iirc, COBOL and Fortran force you to declare variables in special sections of code. Visual Basic does too.


COBOL yes. Fortran, I don't know. VB: Not since I've started using it (6.0).


Fortran required it back in 1986 when I had to study it. I don't know if it still does. VB does require declaring variables at the beginning of a function, in similar fashion as C89.

For example

Dim hProcessAs Long
Dim PID As Long
Dim RemDevPath As String
Dim FileNameAs String

I'm sure you're familiar with this.

Share this post


Link to post
Share on other sites
Recent revisions of Fortran do not require all variables to be declared up front. None of the recent versions of VB require variables to be declared at the top of a function either.

Share this post


Link to post
Share on other sites
Delphi/Pascal is an obvious example. Stack variables have to be declared in a VAR section before the first procedural statement.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this