7.3 Statement Labels


7.3 Statement Labels

HLA low level control structures make extensive use of labels within your code. A low level control structure usually transfers control from one point in your program to another point in your program. You typically specify the destination of such a transfer using a statement label. A statement label consists of a valid (unique) HLA identifier and a colon, e.g.,

 aLabel: 

Of course, like procedure, variable, and constant identifiers, you should attempt to choose descriptive and meaningful names for your labels. The example identifier above, aLabel, is hardly descriptive or meaningful.

Statement labels have one important attribute that differentiates them from most other identifiers in HLA: you don't have to declare a label before you use it. This is important, because low level control structures must often transfer control to a label at some point later in the code, therefore the label may not be defined at the point you reference it.

You can do three things with labels: transfer control to a label via a jmp(GOTO) instruction, call a label via the call instruction, and take the address of a label. There is very little else you can directly do with a label (of course, there is very little else you would want to do with a label, so this is hardly a restriction). The program in Listing 7-1 demonstrates two ways to take the address of a label in your program and print out the address (using the lea instruction and using the "&" address-of operator):

Listing 7-1: Displaying the Address of Statement Labels in a Program.

start example
 program labelDemo; #include( "stdlib.hhf" ); begin labelDemo;     lbl1:         lea( ebx, lbl1 );     mov( &lbl2, eax );         stdout.put( "&lbl1=$", ebx, "&lbl2=", eax, nl );     lbl2: end labelDemo; 
end example

HLA also allows you to initialize double word variables with the addresses of statement labels. However, there are some restrictions on labels that appear in the initialization portions of variable declarations. The most important restriction is that you must define the statement label at the same lex level as the variable declaration. That is, if you reference a statement label in the initializer of a variable declaration appearing in the main program, the statement label must also be in the main program. Conversely, if you take the address of a statement label in a local variable declaration, that symbol must appear in the same procedure as the local variable. Listing 7-2 demonstrates the use of statement labels in variable initialization:

Listing 7-2: Initializing DWORD Variables with the Address of Statement Labels.

start example
 program labelArrays; #include( "stdlib.hhf" ); static     labels:dword[2] := [ &lbl1, &lbl2 ];     procedure hasLabels;     static         stmtLbls: dword[2] := [ &label1, &label2 ];     begin hasLabels;         label1:             stdout.put             (                 "stmtLbls[0]= $", stmtLbls[0], nl,                 "stmtLbls[1]= $", stmtLbls[4], nl             );         label2:     end hasLabels; begin labelArrays;     hasLabels();     lbl1:         stdout.put( "labels[0]= $", labels[0], " labels[1]=", labels[4], nl );     lbl2: end labelArrays; 
end example

Once in a while, you'll need to refer to a label that is not within the current procedure. The need for this is sufficiently rare that this text will not describe all the details. However, you can look up the details on HLA's label declaration section in the HLA documentation, should the need to do this ever arise.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net