views:

39

answers:

1

Is it possible to allocate locally-scoped memory in assembly?

For example, consider the following (completely contrived) situation:

I have two macros, one of which is dependent on the other. The first is:

minimum MACRO dest, num1, num2
; Finds the minimum of two unsigned numbers, stores the result in dest

And the second is:

tripMin MACRO dest, num1, num2, num3
; Finds the minimum of three unsigned numbers, stores the result in dest

minimum firstMin, num1, num2
minimum secondMin, num2, num3
minimum dest, firstMin, secondMin

(I know that this isn't a realistic example for a variety of reasons, but bear with me.)

Assuming that all the registers are otherwise occupied, is there any way to declare firstMin and secondMin locally within the macro?

Or am I just better off freeing a register by pushing its value onto the stack and popping it back when I'm done?

+1  A: 

locally-scoped memory is a notion that only applies to scoped languages, not assembly.

If you were to write the same thing as a function in, say C, your temporaries would be stored on the stack (or available registers if there are some, depending on ABI). You can mimic the same thing. How to push/pop values from the stack is dependent on which assembly language you are using though.

However, this starts to look awfully like a function call.

Bahbar
Ah ok, that makes sense. I think what I was really driving for was the difference between macros and functions; your response was well-explained. Thanks!
dcmoebius