wei@princeton.UUCP (P Wei) (11/19/85)
Is the following peiec going to work? code segment assume cs:code routine proc near jmp begin variable dw ? begin: . . . mov variable,ax . . My final goal is to set a 'static local variable' (like in C) such that on second entry into this routine I can still get the value of variable set last time. From the TURBO reference manual, the local variable is stored in stack segment. So I think the above routine is not going to work. Can anyone tell me a workable procedure ? Thanks. HP Wei (wei@princeton)
cc100jr@gitpyr.UUCP (Joel M. Rives) (11/21/85)
In article <1165@princeton.UUCP> wei@princeton.UUCP (P Wei) writes: > >My final goal is to set a 'static local variable' (like in C) such that on >second entry into this routine I can still get the value of variable set last >time. From the TURBO reference manual, the local variable is stored in stack >segment. So I think the above routine is not going to work. Can anyone tell >me a workable procedure ? >Thanks. >HP Wei (wei@princeton) ------------------------------------------------------------------------------ One way to ensure the integrity of a local variable in Turbo Pascal is to take advantage of the "absolute" declaration. Start by generating space for the variable (or table of variables if you need) either in the main program or through dynamic allocation (the later method is not suggested, however, as the results are unpredictable should you release the heap at any other point in the program). In your procedure, declare the variable as "absolute" over the global variable already established. This bit of gymnastics is absurd, though, sense you could simply use the global variable directly. If you consider the method that Pascal uses for providing space for variables, you will realize that use of global references are the only viable means of doing what you want to do. Every time you enter a procedure or function, a fresh (theoretically undefined) section of memory is set aside for all of the local variables. This allocation of space is dynamic in the sense that it is provided upon demand. Therefore, the second time that you enter a procedure, it is very unlikely that it will be mapped directly over the exact same spot in memory that it occupied the first time. It is possible to ensure that such a thing happens by forcing the program to load at a higher address than it would normally. By doing so, you will have left a chunk of memory (between the address in memory where the loader would have put the program and the address where you instructed it to load the program). This chunk of memory is totally transparent to the program in that it will perform no memory management upon that section of memory. You can then reference that location either through "absolute" declarations or the Mem(csect:address) function. In any case, use of global references - where neccissary - is not an evil. the never-present whisper spirit Joel Rives Georgia Insitute of Technology, Atlanta Georgia, 30332 ...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!cc100jr "Remember, no matter where you go, there you are!" << Buckaroo Banzai >>
jkr@gitpyr.UUCP (John Kenneth Riviere) (11/26/85)
In article <1165@princeton.UUCP> wei@princeton.UUCP (P Wei) writes: > >My final goal is to set a 'static local variable' (like in C) such that on >second entry into this routine I can still get the value of variable set last >time. From the TURBO reference manual, the local variable is stored in stack >segment. So I think the above routine is not going to work. Can anyone tell >me a workable procedure ? >Thanks. >HP Wei (wei@princeton) I have not responded earlier in hopes that someone else will point this out for me, but as I have not seen this solution suggested yet I guess I will add my $.02. It is not obvious from the Turbo Pascal manual unless you read it carefully, but it is possible to declare what amounts to a static local variable within a procedure or function by declaring it as a typed constant. These are the initialized constants that Turbo allows and the space for them is allocated in the code segment which will not change from one recursion to another (except as the procedure's code changes it). They are not really constants since it is possible to change their values, but they seem to be frequently overlooked since their declaration indicates that they are constants. I haven't tried using them with inline code, but in Chapter 20 of my Turbo 3.0 manual it specifically mentions typed constants as being accessible through the CS register when doing inline code. Note that if the procedure in question is being reloaded from an overlay file then the initial value will be reloaded along with the rest of the code so it is not static in this case, but other- wise it should be OK. Hope this helps. -- J. Kenneth Riviere (JoKeR) Georgia Institute of Technology, Atlanta Georgia, 30332 ...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!jkr