| View previous topic :: View next topic |
| Author |
Message |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Mar 25, 2016 7:06 pm Post subject: Translating Lua to ASM. |
|
|
Please take a look at the following Lua code:
| Code: |
math.randomseed(os.time())
local x = math.random(0,10)
local y = math.random(0,10)
while x == y do y = math.random(0,10) end
|
How to write a piece of ASM code that can achieve the same result? I am really new to ASM language (X86).
Thanks in advance. |
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4709
|
Posted: Fri Mar 25, 2016 9:43 pm Post subject: |
|
|
Assuming the process has the msvcrt library loaded, you can call srand and rand from that dll. Both should use the cdecl calling convention. You can call GetTickCount from kernel32 to pass to srand. Use the div instruction and get the remainder from EDX to limit it to an integer from 0 to 10.
Google around for examples. Wikipedia has examples on the cdecl calling convention. There should be a few CE topics dealing with rand (here is one you made). _________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Mar 25, 2016 10:32 pm Post subject: |
|
|
| ParkourPenguin wrote: | Assuming the process has the msvcrt library loaded, you can call srand and rand from that dll. Both should use the cdecl calling convention. You can call GetTickCount from kernel32 to pass to srand. Use the div instruction and get the remainder from EDX to limit it to an integer from 0 to 10.
Google around for examples. Wikipedia has examples on the cdecl calling convention. There should be a few CE topics dealing with rand (here is one you made). |
Penguin, it's you again, my friend. Thank you as usual. I have two problem today, one is about declaring variables, the other one is the use of srand. You helped me on both issues. Thanks again.
BTW, I have found this:
| Code: |
//Only once in a module {
call getTickCount
push eax
call srand
//}
// as much as required
call rand
// a new random number in eax
call rand
// a new random number in eax
....
// div (max-min)
// add edx,min
// now min < edx < max
|
Is this what you meant? I can define the range by giving max and min values, right? |
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4709
|
Posted: Fri Mar 25, 2016 11:08 pm Post subject: |
|
|
I give a brief explanation and an example of this here. Look up what the div instruction does to get a better understanding of why that code works (link to a reference). If you need more help, just ask. _________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Mar 26, 2016 6:55 am Post subject: |
|
|
| ParkourPenguin wrote: | | I give a brief explanation and an example of this here. Look up what the div instruction does to get a better understanding of why that code works (link to a reference). If you need more help, just ask. |
Oh, I didn't understanding back then, but kinda understand now. I will give it a try. Thanks, ParkourPenguin.  |
|
| Back to top |
|
 |
|