News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

JEAXNZ ??

Started by mariø, February 07, 2005, 04:50:55 PM

Previous topic - Next topic

Nilrem

That's all well and good but what if I had to fill more variables then there are registers? What are the FPU registers btw?

dsouza123

The FPU, floating point unit, has 8 registers ST0 - ST7 on a stack
that can deal with 32 or 64 bit integers (among other types)

FILD    loads integer into FPU
FISTP  stores integer into memory or CPU register
FXCH   swaps values between FPU registers.

There are some help files that come with MASM32 that explain about the FPU instructions and libraries.

Some processors have MMX and SSE,SSE2 registers that can hold multiple integer (32 bit) vars in parallel.

Nilrem

Still doesn't solve what I feel to be a flaw. How come there is no function like this? Can do it in every other (hll) language.

Tedd

I'm not sure if I entirely understand what your problem is.. (if this isn't right, post a hll example of what you actually want to achieve)

So, before calling another function/procedure, you should save any registers you need the values of (that the function is going to mess up otherwise) - on the stack (usually) - and then get them back after returning.
Largely, you don't need to worry about saving the values of variables as long as they're LOCAL (defined inside the function) since any calls to other (or the same) functions wil lbe allocated a new set of variables, so they won't mess each other up.
For global varibles (.data/.data? sections), then it can cause problems. But this is exactly the same case as global variables in hlls.

So, as far as I understand your example:


.data?
  var1 dd ?
  var2 dd ?

.code
xor ecx,ecx
call someproc
;ecx is the return value - (conventionally we use eax, but it's no real matter)
.
.
.


someproc proc
  inc ecx
  if ecx = 1 - mov var,var2
  if ecx = 2, mov var,var1

  push ecx                      ;<----- save ecx - so "anotherproc" doesn't mess it up for us
  call anotherproc
  pop ecx                       ;<----- get 'our' ecx back

  push ecx
  push edx                     ;<----- save both of these, because "manipulate" use both (tho we don't care about edx in this instace, but just as example)
  manipulate var1            ;macro
  pop edx
  pop ecx

  inc ecx
  ret
someproc endp

No snowflake in an avalanche feels responsible.

sluggy

Quote from: Nilrem on February 21, 2005, 01:06:04 PM
It won't work because in my manipulating variable procedure I have to push and pop most of the registers.
So what? Why is this an issue? You can PUSH and POP as much as you want as long as you leave the stack balanced.

Quote from: Nilrem on February 21, 2005, 01:06:04 PMI need to move 3 variables to three seperate registers and push/pop them but cannot. Is there no way at all to move something to a variable? I just do not understand why I cannot fill an initialised variable? There must be some way around this problem as I can see it occuring a lot?
What exactly ARE you talking about? You have been given all the different methods of moving values between variables and registers, but you are still complaining. The only thing you cannot do is move a value directly from one variable (memory location) to another variable (memory location), you must effect the transfer via a register or the stack.

It seems to me that the issue is in the design of your code, not any imagined limitations of the instruction set or CPU!




AeroASM


.data?

var dd ?

.code

start:

mov var,50
ret

end start

Nilrem

Apologies (Sluggy) if I came across as insulting, that was not my intention. All I'm saying is it is all well and good for me to save the registers, but in my subroutine (proc) I actually need to save them (so I'd be overwriting what I saved?). If I tell you exactly what I want to do maybe this will help us all? Once again apologies I am not one to want to offend, especially users of such a brilliant language.

I want to do this:


if ecx = 1
mov var,var1 (which I cannot actually do, so how to get around this?)
.elsif ecx = 2
mov var, var2
.endif

pbrennick

#22
Nilrem,
Use the m2m macro from the macro library.  If you use it you can do this:

m2m var,var1

To get it to work, add this line in your include section:

include \masm32\macros\macros.asm

Paul

Nilrem

I've already got that included, but because people were saying there was no such feature I thought they meant with hutch's package not the actual default language. That will help tremendously (if it works as I hope it will). Whoever coded that snippet of code, thankyou! Any rules or restrictions on it (I'm at school right now, can't test it)?

AeroASM

It does exist in the default language:


push var2
pop var


The macro just makes it possible to do it on one line.

Mark Jones

#25
Nilrem, when you get home, open up the masm32\macros\macros.asm and search for m2m. Very handy, some of those macros. :)

Note you might have to include the MASM32.inc and .lib also for some of the macros to work.

It is confusing at first that "variables" are not really "variables" in assembler. It might be better to think that there are no variables in assembler at all, just memory locations (offsets) and memory contents (values.) If you create some data called myVar:


.data
myVar DB "Hello there!",0


The actual value of "myVar" may be 0x0041001E (or some other number, every var is a different number.) This is the OFFSET or pointer to the string of data in memory... the string actually looks like this:


410010: 00 00 00 00 00 00 00 00   00 00 00 00 00 00 48 65
410020: 6C 6C 6F 20 54 68 65 72   65 21 00 00 00 00 00 00


So myVar is actually just a pointer to the start of "flat" data, terminated with a 00h. That is why mov x,x can't directly copy one string to another-- a string can be much bigger than any register (a double-word value at best) and mnemonics such as MOV only deal with data that can fit in a single register. Now you CAN copy the string's offset (0x0041001E) to another "string" or DD value and make it seem like the string was actually copied when in fact, you've only duplicated the string's OFFSET. i.e.,


.data
  szTitle DB "Testing!",0
  myVar DB "Hello There!",0
  szTemp DD 0
.code
  lea eax, myVar ; copy myVar's OFFSET into EAX
  mov szTemp, eax ; copy EAX to szTemp value, making it same number as myVar
  invoke MessageBox, hWnd, dword ptr szTemp, addr szTitle, MB_OK  ; display strings


To learn more about Dword Ptr and dereferencing, see the ASMINTRO.HLP file.

To actually copy the contents of one string value into another string, try this:


.data
  szTitle DB "Testing!",0
  myString DB "Hello there!",0
  szTemp DB 128 dup(0) ; make destination bigger than the copied string
.code
  invoke lstrcpy, addr szTemp, addr myString  ; copies contents of myString into szTemp
  invoke MessageBox, hWnd, addr szTitle, addr szTemp, MB_OK  ; display strings


The lstrcpy function copies each byte from one string offset to another string offset. You must make certain the destination string is big enough to hold anything put into it, else you could overwrite other "strings." Have fun!
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

pbrennick

Nilrem,
Let us know how you make out.
Paul

Nilrem

Thanks guys, thanks on that explanation Mark, really helps since I think as asm as a slightly different version of C (I use the hll way, please no flames). I'll try it out now.

Nilrem

Not working, so I tried push and pop and when popping it said this:
Quoteimmediate operand not allowed

the var I am pushing already has values in it and is db. My var that I am popping it too is:
org_val      db   128   dup(?)

I am guessing this is where the problem lies, but since they are both db I don't understand why.

pbrennick

Nilrem,
I am attaching a small project that uses m2m in seven different places.  Learn from it, it is a cool example of how to show a message for a specific interval and then close.  The first 2 boxes close themselves, the third you must vlose yourself.  It is not my code, I wish I could remember who wrte it so I can credit it properly.  Examine the code and then have fun.

Paul


[attachment deleted by admin]