ice_cracked
New Member
- Joined
- Jul 22, 2016
- Messages
- 17
- Office Version
- 2016
- Platform
- Windows
Hi I am trying to write code in C to mimic assembly code and am stuck on getting a random number.
The string I am using is First.Second
which is 12 characters long
The assembly code is as below
mov eax, [esp+78h] ; strlen of characters for the srand call
mov [esp], eax ; Seed value
call _srand
So I am guessing that the value of strlen is being passed to the _srand call
and I am using the below in my C code which seems to work and does not generate any errors
length = strlen(string);
srand(length);
further assembly code is as below
mov eax, [esp+7Ch] ; loop counter
lea edx, ds:0[eax*4] ; loop count = 4-8-C into EDX
mov eax, [esp+74h] ; Pointer to allocated memory which I have created using malloc()
lea ebx, [edx+eax]
call _rand ; first random number call returns 4D 2nd call returns 15FC 3rd call returns 1858 etc.
cdq ; Sign extend EAX into EDX:EAX
idiv dword ptr [esp+78h] ; random number divided by strlen chars entered string
mov ecx, edx ; Save the mod value for later use
mov eax, [esp+78h] ; strlen chars entered string
mov edx, eax ; Count of chars entered string
shr edx, 1Fh ; strlen shr 31 I don't understand what this is for as it always results in = 0
add eax, edx ; result of shr + strlen string so strlen is always unchanged and still 12
sar eax, 1 ; divide strlen string by 2 now = 6
neg eax ; multiply by -1
add eax, ecx ; EAX now 1st time FFFFFFFF 2nd time FFFFFFFA 3rd time FFFFFFFE etc. through the string
these values are then stored in an array created earlier with malloc()
At the moment I am playing with the below C code but it is not working ?
srand(length);
for (i = 0; i < length; ++i) {
myRand = rand();
myBuf += myRand % length - length / 2;
Sorry if this is just a basic question but just starting to learn C and you have to start somewhere.
I appreciate your time in reading this and any suggestions are MOST welcome.
The string I am using is First.Second
which is 12 characters long
The assembly code is as below
mov eax, [esp+78h] ; strlen of characters for the srand call
mov [esp], eax ; Seed value
call _srand
So I am guessing that the value of strlen is being passed to the _srand call
and I am using the below in my C code which seems to work and does not generate any errors
length = strlen(string);
srand(length);
further assembly code is as below
mov eax, [esp+7Ch] ; loop counter
lea edx, ds:0[eax*4] ; loop count = 4-8-C into EDX
mov eax, [esp+74h] ; Pointer to allocated memory which I have created using malloc()
lea ebx, [edx+eax]
call _rand ; first random number call returns 4D 2nd call returns 15FC 3rd call returns 1858 etc.
cdq ; Sign extend EAX into EDX:EAX
idiv dword ptr [esp+78h] ; random number divided by strlen chars entered string
mov ecx, edx ; Save the mod value for later use
mov eax, [esp+78h] ; strlen chars entered string
mov edx, eax ; Count of chars entered string
shr edx, 1Fh ; strlen shr 31 I don't understand what this is for as it always results in = 0
add eax, edx ; result of shr + strlen string so strlen is always unchanged and still 12
sar eax, 1 ; divide strlen string by 2 now = 6
neg eax ; multiply by -1
add eax, ecx ; EAX now 1st time FFFFFFFF 2nd time FFFFFFFA 3rd time FFFFFFFE etc. through the string
these values are then stored in an array created earlier with malloc()
At the moment I am playing with the below C code but it is not working ?
srand(length);
for (i = 0; i < length; ++i) {
myRand = rand();
myBuf += myRand % length - length / 2;
Sorry if this is just a basic question but just starting to learn C and you have to start somewhere.
I appreciate your time in reading this and any suggestions are MOST welcome.