Sign in to follow this  

labeling local variables in assembly

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm learning assembly to write faster game code(not to write games in assembly, no no, I'm not crazy, but I DO want to understand what's going on behind the scenes for WAY easier and more knowledgable debugging, and knowing what to optimize, which WILL save me time), and even though assembly is quite awesome, at times it can be very tedious, I guess that's the price for power. I am trying to figure out a way to give give my local variables a mnemonic like I would in c++, so that I don't have to remember the variable's effective address(what esp points too) everytime I want to mess with a local variable's value. In c++ I can just say
int foo;
foo = 0x01; // if foo was at 40100h, instead of saying mov 40100h, 1 
// i can just say mov foo, 1; this is because foo is mnemonic for 00040100h

In the assembly source below, [ebp-4h] is the effective address of where foo is located in the stack, how can I give [esp-4h] the mnemonic of foo, and just start using that, so that windows will understand foo as [esp-4h] when it's executing my program instructions in memory. I know there has to be a way, when I debug vc++ and watch the assemblage, I see windows use a rep stos function to set up what I believe is the mnemonic! But when I google, google gives me assembly tutorials from the 80s which are too smart for me, back then people had to do stuff with segmented 16 bit dos memory(damn genius programmers) :[.
foofunc PROC
	; going to create a local variable
	push ebp 
	mov ebp, esp ; set up framework, save current esi into ebp
	sub esp, 04h ; make room for a DWORD , cause DWORD = 4 bytes
	; done, normal function code below
	
        ; I want to label this current esp using std portable IA32 instructions
        ; I want esp which is [ebp-4h] to be called foo!
	mov DWORD PTR [esp], 2h
	add DWORD PTR [esp], 2h
	
	mov ebx, [esp];
	call DumpRegs ; show me the registers and verify I'm not crazy, and that this function worked
	
	; restore stack
	mov esp, ebp
	pop ebp
	ret
foofunc ENDP


Share this post


Link to post
Share on other sites
rep stos isn't a function and doesn't set up a mnemonic. The quick of it is that the rep stos instruction fills the memory at the address stored in edi with the value stored in eax repeatedly until ecx is decremented to zero. There are variations on the instruction depending on whether a byte, a word or a double word are transfered. It's very likely that what you're observing is the local variable being set to zero.

Perhaps you can lable local variables using special assembler specific syntax - maybe HLA or MASM provides for this - but in general it's up to the programmer to keep track of the local variables and their purpose.

I recommend getting a copy of the IA32 manuals from Intel. They will even send you free hard copy versions of the manual. Unfortunately, I can't find the url!

Share this post


Link to post
Share on other sites
Thanks LessBread, I can now see why programmers of old are so good at paying attention to detail, they had to remember all those address and offsets! I did a google and found the intel manuals at [a href="http://developer.intel.com/design/pentium/manuals/]intel manuals[/a]. I'm going to ask them to send me hardcopies of them all. Looks like I've got more reading to do!

I found some MASM directives that let me have mnemonics for local variables, and IF statements, and pretty much any other High Level language feature, which reminded me of why I dislike directives in assembly, because with those I'm no longer programming low level, i'm pretty much back to programming c. I was looking for intel instruction code to let me have the mnenomics, but oh well, that is life, back to the coding...
*type type type*

Share this post


Link to post
Share on other sites
Yeah, I was going to say, if you want local variable names in asm, you might as well just use C. If you can't find the page for ordering the free hard copies from Intel, start a new thread asking for the url, someone will likely provide it.

Share this post


Link to post
Share on other sites
Quote:

I found some MASM directives that let me have mnemonics for local variables, and IF statements, and pretty much any other High Level language feature, which reminded me of why I dislike directives in assembly, because with those I'm no longer programming low level, i'm pretty much back to programming c. I was looking for intel instruction code to let me have the mnenomics, but oh well, that is life, back to the coding...


Mnemonics are higher level anyway. It doesn't even make sense to have an assembly instruction that would set up mnemonics. Instructions are executed when the program runs, how could they setup something that you use when you write your code in your editor? Think about it, what kind of "instruction" could handle such a thing? One that would create a dictionary in run-time, create a string named "foo",add the item named "foo" and the value [esp-4h], and then... forget it, as I said it doesn't make sense. Do you think that "foofunc PROC" for example is something that intel instructions understand? The only type of operand the instruction "call" takes is an address or a register that contains an address. "Foofunc" is just a mnemonic setup by MASM. Use MASM directives whenever you can, the jist of writing in assembly isn't having to type 8 pushes in order to call a procedure. If you think that using directives is what makes the difference between C code and assembly, then why did you start learning it in the first place? So you can write:

push a
push b
push c
call fooproc

instead of:

invoke fooproc,a,b,c?

Do you think either of them is going to have any impact in performance? The only thing you'll achieve by writing in "purist" mode is more bugs in your code.

[Edited by - mikeman on January 26, 2006 4:39:39 AM]

Share this post


Link to post
Share on other sites
Those are great points mikeman, I didn't think mnemonics where possible, but the vc++ debugger confused my little brain. I guess the debugger in assembly mode still isn't pure assembly. I agree that using a few directives for specific purposes is smart coding, but at this point in my learning of assembly, I want every single gruesome detail, and sometimes I will get confused, but it's worth it in the end if I feel like I know what my c++ is really doing. That is why i decided to walk the road of assembly, not to learn how to take shortcuts, but to learn how long the journey really is, and than I know I will be truely greatful to my c++ compiler :) I LOVE YOU VC!!

Share this post


Link to post
Share on other sites

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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