Assembly Language for Beginners

(Jeff_L) #1

3.27. OPENMP


free(min); free(max);
};


Thecheck_nonce()function just adds a number to the string, hashes it with the SHA512 algorithm and
checks for 3 zero bytes in the result.


A very important part of the code is:


#pragma omp parallel for
for (i=0; i<INT32_MAX; i++)
check_nonce (i);

Yes, that simple, without#pragmawe just callcheck_nonce()for each number from 0 toINT32_MAX
(0x7fffffffor 2147483647). With#pragma, the compiler adds some special code which slices the loop
interval into smaller ones, to run them on allCPUcores available^57.


The example can be compiled^58 in MSVC 2012:


cl openmp_example.c sha512.obj /openmp /O1 /Zi /Faopenmp_example.asm


Or in GCC:


gcc -fopenmp 2.c sha512.c -S -masm=intel


3.27.1 MSVC


Now this is how MSVC 2012 generates the main loop:


Listing 3.122: MSVC 2012
push OFFSET _main$omp$1
push 0
push 1
call __vcomp_fork
add esp, 16

All functions prefixed byvcompare OpenMP-related and are stored in the vcomp*.dll file. So here a group
of threads is started.


Let’s take a look on_main$omp$1:


Listing 3.123: MSVC 2012

$T1 = -8 ; size = 4
$T2 = -4 ; size = 4
_main$omp$1 PROC
push ebp
mov ebp, esp
push ecx
push ecx
push esi
lea eax, DWORD PTR $T2[ebp]
push eax
lea eax, DWORD PTR $T1[ebp]
push eax
push 1
push 1
push 2147483646 ; 7ffffffeH
push 0
call __vcomp_for_static_simple_init
mov esi, DWORD PTR $T1[ebp]
add esp, 24
jmp SHORT $LN6@main$omp$1
$LL2@main$omp$1:
push esi
call _check_nonce


(^57) N.B.: This is intentionally simplest possible example, but in practice, the usage of OpenMP can be harder and more complex
(^58) sha512.(c|h) and u64.h files can be taken from the OpenSSL library:http://go.yurichev.com/17324

Free download pdf