Assembly Language for Beginners

(Jeff_L) #1

1.23. LINEAR CONGRUENTIAL GENERATOR


_BSS ENDS


_init$ = 8
_srand PROC
push ebp
mov ebp, esp
mov eax, DWORD PTR _init$[ebp]
mov DWORD PTR _rand_state, eax
pop ebp
ret 0
_srand ENDP


_TEXT SEGMENT
_rand PROC
push ebp
mov ebp, esp
imul eax, DWORD PTR _rand_state, 1664525
mov DWORD PTR _rand_state, eax
mov ecx, DWORD PTR _rand_state
add ecx, 1013904223 ; 3c6ef35fH
mov DWORD PTR _rand_state, ecx
mov eax, DWORD PTR _rand_state
and eax, 32767 ; 00007fffH
pop ebp
ret 0
_rand ENDP


_TEXT ENDS


1.23.2 x64


The x64 version is mostly the same and uses 32-bit registers instead of 64-bit ones (because we are
working withintvalues here).


Butmy_srand()takes its input argument from theECXregister rather than from stack:


Listing 1.320: Optimizing MSVC 2013 x64

_BSS SEGMENT
rand_state DD 01H DUP (?)
_BSS ENDS


init$ = 8
my_srand PROC
; ECX = input argument
mov DWORD PTR rand_state, ecx
ret 0
my_srand ENDP


_TEXT SEGMENT
my_rand PROC
imul eax, DWORD PTR rand_state, 1664525 ; 0019660dH
add eax, 1013904223 ; 3c6ef35fH
mov DWORD PTR rand_state, eax
and eax, 32767 ; 00007fffH
ret 0
my_rand ENDP


_TEXT ENDS


GCC compiler generates mostly the same code.


1.23.3 32-bit ARM.


Listing 1.321: Optimizing Keil 6/2013 (ARM mode)
Free download pdf