Assembly Language for Beginners

(nextflipdebug2) #1

1.18 Replacing arithmetic instructions to other ones


Onboard Software and Satellite Operations: An Introduction, (2011)]. But NOR element isn’t very popular
in computer programming.


So, the NOT operation is implemented here asNOR DST, $ZERO, SRC.


From fundamentals2.2 on page 452we know that bitwise inverting a signed number is the same as
changing its sign and subtracting 1 from the result.


So whatNOTdoes here is to take the value ofstrand transform it into−str− 1. The addition operation that
follows prepares result.


1.17.2 Boundaries of strings.


It’s interesting to note, how parameters are passed into win32GetOpenFileName()function. In order to
call it, one must set list of allowed file extensions:


OPENFILENAME *LPOPENFILENAME;

char * filter = "Text files (*.txt)\0*.txt\0MS Word files (*.doc)\0*.doc\0\0";

LPOPENFILENAME = (OPENFILENAME *)malloc(sizeof(OPENFILENAME));

LPOPENFILENAME->lpstrFilter = filter;

if(GetOpenFileName(LPOPENFILENAME))
{

What happens here is that list of strings are passed intoGetOpenFileName(). It is not a problem to parse
it: whenever you encounter single zero byte, this is an item. Whenever you encounter two zero bytes,
this is end of the list. If you will pass this string intoprintf(), it will treat first item as a single string.


So this is string, or...? It’s better say this is buffer containing several zero-terminated C-strings, which can
be stored and processed as a whole.


Another exmaple isstrtok()function. It takes a string and write zero bytes in the middle of it. It thus
transforms input string into some kind of buffer, which has several zero-terminated C-strings.


1.18 Replacing arithmetic instructions to other ones


In the pursuit of optimization, one instruction may be replaced by another, or even with a group of instruc-
tions. For example,ADDandSUBcan replace each other: line 18 in listing.3.119.


For example, theLEAinstruction is often used for simple arithmetic calculations:.1.6 on page 1028.


1.18.1 Multiplication


Multiplication using addition


Here is a simple example:


unsigned int f(unsigned int a)
{
return a*8;
};


Multiplicationby8isreplacedby3additioninstructions,whichdothesame. Apparently,MSVC’soptimizer
decided that this code can be faster.


Listing 1.188: Optimizing MSVC 2010

_TEXT SEGMENT
_a$ = 8 ; size = 4
_f PROC

Free download pdf