Previous page Select page Next page

The MOV mnemonic

This (finally) is where we learn to use our first assembler mnemomic - MOV. As you can imagine from the name, it's purpose is to move a value from one place to another. At the present state of our project, we have the width stored in the string "clientDimensions" but now we want to append the comma and space - ", ". We can actually move these two characters directly into one of our 32-bit registers. In fact, we could move 4 characters at a time, if we wanted, since each character occupies 8 bits.

The question is - which register should we use? We might be tempted to use the EAX register. It's a fairly popular one but it's currently holding some information which is very important to us - can you remember what it is? When we converted the width to a string, the EAX register returned from the String function with the number of characters making up the new string. We need this information because we don't want to put the ", " at the beginning of clientDimensions. We want to "skip" into the string by the length of the width string and place it there. OK. Let's use the EBX register. The MOV mnemonic requires us to specify first the target and then the source of the data, separated by a comma. To move the string ", " into EBX, all we have to say is:

	Mov Ebx, ", "

Easy or what?

But now comes the hard part. We need to move what's in EBX to the location in memory where clientDimension is stored plus whatever is in EAX (the width of the string generated earlier). Since we are storing it in a box, we should use the square brackets. This is how we wouold express it:

	Mov [clientDimensions + Eax], Ebx

So you can see that we can perform a little arithmetic inside the square brackets to indicate an "offset" from the base address of the "variable". Place these two lines after your "Invoke String" instruction and recompile. You should now see your width in the caption bar, followed by a comma. The code so far looks like this:

	OnResize:
		UseData winMainProcedure

		Invoke GetClientRect, [hWnd], Addr clientRect

		Invoke String, [clientRect.right], Addr clientDimensions, ecDecimal

		Mov Ebx, ", "

		Mov [clientDimensions + Eax], Ebx

		Invoke SetText, [hWnd], Addr clientDimensions

		Return (TRUE)
	EndU
	; End OnResize

To complete our handler, all that remains is to calculate where the end of the string now is and then to convert the height of the client area to string and then append it at that location within clientDimensions. One way to work out the new location for the next string is to take what we already have - EAX (holding the width of the first string) - add the address of the clientDimensions variable and then add a further 2 places to take care of the ", ". To do this, we use another mnemonic - ADD.

The ADD mnemonic

This instruction requires us first to specify the "store" where the result is to be held. This may currently hold a value and, if so, this will be included in the total. Note that if the result is greater than the "store" can hold, the overflow flag may be set as we discussed earlier in the chapter. But this is not likely to happen here and, even if it did, we do not have the knowledge yet to deal with it. Let's just ignore that possibility for now. The second thing we specify is the value we want to add to the "store". Our "store" right now is the EAX register and we want to add the address of clientDimensions + 2 (for the comma and space):

	Add Eax, Addr clientDimensions + 2

Now all we have to do is to call the String function again, passing it the "bottom" compartment of the clientRect (the height of the client area) and EAX as the new pointer to the string location:

	Invoke String, [clientRect.bottom], Eax, ecDecimal

And that's it. Compile it and you should now have a program that constantly updates the caption bar of the window with a comma-separated pair of dimensions. Here's the full text of the handler, complete with comments:

	OnResize:
		; Get the event loop data (to get hWnd)
		UseData winMainProcedure

		; Get the dimensions of winMain's client rectangle
		Invoke GetClientRect, [hWnd], Addr clientRect

		; Convert the width into a string and store it in clientDimensions
		Invoke String, [clientRect.right], Addr clientDimensions, ecDecimal

		; Append a comma and space to the string "clientDimensions"
		Mov Ebx, ", "
		Mov [clientDimensions + Eax], Ebx

		; Calculate where in the string the height should be stored
		Add Eax, Addr clientDimensions + 2

		; Convert the width into a string and append it to clientDimensions
		Invoke String, [clientRect.bottom], Eax, ecDecimal

		; Now set the caption of winMain to clientDimensions
		Invoke SetText, [hWnd], Addr clientDimensions

		; All done - return to the main event loop
		Return (TRUE)
	EndU
	; End OnResize

Previous page Select page Next page