Assembly Language for Beginners

(Jeff_L) #1

3.11. INLINE FUNCTIONS


strlen()


Listing 3.34: strlen() example

int strlen_test(char *s1)
{
return strlen(s1);
};


Listing 3.35: Optimizing MSVC 2010

_s1$ = 8 ; size = 4
_strlen_test PROC
mov eax, DWORD PTR _s1$[esp-4]
lea edx, DWORD PTR [eax+1]
$LL3@strlen_tes:
mov cl, BYTE PTR [eax]
inc eax
test cl, cl
jne SHORT $LL3@strlen_tes
sub eax, edx
ret 0
_strlen_test ENDP


strcpy()


Listing 3.36: strcpy() example

void strcpy_test(char s1, char outbuf)
{
strcpy(outbuf, s1);
};


Listing 3.37: Optimizing MSVC 2010

_s1$ = 8 ; size = 4
_outbuf$ = 12 ; size = 4
_strcpy_test PROC
mov eax, DWORD PTR _s1$[esp-4]
mov edx, DWORD PTR _outbuf$[esp-4]
sub edx, eax
npad 6 ; align next label
$LL3@strcpy_tes:
mov cl, BYTE PTR [eax]
mov BYTE PTR [edx+eax], cl
inc eax
test cl, cl
jne SHORT $LL3@strcpy_tes
ret 0
_strcpy_test ENDP


memset()


Example#1


Listing 3.38: 32 bytes

#include <stdio.h>


void f(char *out)
{
memset(out, 0, 32);
};


Many compilers don’t generate a call to memset() for short blocks, but rather insert a pack ofMOVs:

Free download pdf