• Welcome to Valhalla Legends Archive.
 

Calling API from assembler?

Started by brew, October 02, 2007, 08:19 PM

Previous topic - Next topic

brew

I intend to eventually make a bot in asm. Now to find out how to access a struct's members...
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Warrior

whoas man a bot in asm! cool guy.

On a serious note:

What makes you think you can out-optimize the compiler? You'd be wasting your time to be honest.
There is little or no practicality in writing a Client in ASM.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Hell-Lord

Quote from: Warrior on October 04, 2007, 09:42 PM
whoas man a bot in asm! cool guy.

On a serious note:

What makes you think you can out-optimize the compiler? You'd be wasting your time to be honest.
There is little or no practicality in writing a Client in ASM.

Indeed, why waste your own time writing a bot in ASM when a well written bot in even VB6 can do the job.

Banana fanna fo fanna

There's a HUGE amount you can learn by doing some work in assembler. With that said, it's unlikely you'll be able to write a bot in all ASM without killing yourself first.

And there's no practical reason to, either :)

Camel

For everything you could do in C that you do in asm, expect the following:
* The development time will be ten times longer than if you used C
* The code will run no more than 10% faster

If you're just trying to write a lightweight efficient bot, you're wasting your time. If you're trying to learn about computers and assembly, then it's a worthy cause.

No matter how complex a bot is (within reason), it always spends almost all of its time yielding to other threads.

brew

It may be. I don't intend to have the entire thing in assembler..... (i'll use char string##[] for constant strings) but I will make most of it with asm. Of course it's not going to be practical, I'm just doing it because I would learn THAT much more. And trust me, it's not that hard. 95% of code for battle.net bots is calling win32 api. Even if it does only run 10% faster then with C, wouldn't it be worth it? The only problem would be the development time (and i have a lot of spare time)
@Warrior: Yeah, I can out optimize the compiler quite easily. IIRC in an older topic Yegg and I were discussing about how the MSVC6 compiler actually un-optimizes some code.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

warz

Quote from: brew on October 05, 2007, 02:29 PM@Warrior: Yeah, I can out optimize the compiler quite easily. IIRC in an older topic Yegg and I were discussing about how the MSVC6 compiler actually un-optimizes some code.

You still use visual studio 6? Wow, guy, get with the times.

Warrior

Quote from: Camel on October 05, 2007, 09:27 AM
* The code will run no more than 10% faster

I think speed might actually be worse if he's not a wiz at ASM. Considering the heavy optimization that compilers do.
I'm pretty sure the whole "ASM is faster" myth is under ideal situations with expert programmers.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Camel

Quote from: Warrior on October 05, 2007, 03:06 PM
Quote from: Camel on October 05, 2007, 09:27 AM
* The code will run no more than 10% faster

I think speed might actually be worse if he's not a wiz at ASM. Considering the heavy optimization that compilers do.
I'm pretty sure the whole "ASM is faster" myth is under ideal situations with expert programmers.

The compiler doesn't always know what's best for each situation; it knows what it's trying to accomplish according to what each C instruction tells it to do. Heuristic optimizations are not nearly as effective as you might think. Sure, it'll usually pick up silly things like repetitive adds to the same register, but it can't look at a segment of code, figure out what the goal is, and re-write it entirely.

I would say 10% is the least amount of speed increase you would get, as long as the person writing the asm is familiar with every instruction and has at least a basic understanding of the tradeoffs of each one.

Win32

Quote
It also pops my stack for me, I don't have to add 16 to esp manually. Say, msdn said that you're supposed to pop it to ebx 4 times,
but is adding 16 to esp a good idea? (will it mess something up in the long run?) Doing that seems like i'm just making a workaround to
stop the _chkesp error box and nothing more.
It depends on the calling convention, all conventions I know of require the callee to pop the parameters off the stack. The only
exception (I'm aware of) is those conventions that accept a dynamic number of arguments.

Quote
why does the following code crash when it calls sprintf? :/
I'm too tired to bother trying to read that (might want to clean it), but I'd say it's something to do with the fact sprintf is a
cdecl (dynamic param count) call: You need to pop the given parameters, who knows what the function does (then again, you can just
walk through it with a disassembler)


Quote
I intend to eventually make a bot in asm. Now to find out how to access a struct's members...
There's no reason you cannot use identifiers in asm.


Quote
Indeed, why waste your own time writing a bot in ASM when a well written bot in even VB6 can do the job.
Naughty naughty.


--
Personally, Brew, I think you may want to spend some time reading into processor architecture. (Intel.com, "IA Manuals") and aquire
a little more knowledge before you attempt a task such as a chat client.

I've been working with assembly for several years now, it's an extremely enlightening learning experience, yet the practical applications
assembly has is minimal. Even so, most of my projects are written in a 40:60 Asm to C ratio, mainly due to I'm a performance nazi and have
enough experience to far outperform any compiler. Still, not even I would bother writing a whole chat client in pure assembly, the
amount of time you'll spend debugging (especially since you're a relative novice) is going to consume project time 10 fold.

At present, you are not going to be outperforming the compiler, not even Borland, so here's a few pointers for when working with Asm:

1. Do not use half-registers, (ie, WORD), there's a good possibility of a stall depending on how many write's are in flight.
2. Structure your code so a branch is only taken on the least-likely outcome.
3. Avoid branches whenever possible, if possible, substitute with conditional instructions (CMOV, SETCC, ect...)
4. Align loops on 16-byte boundaries (use the ALIGN directive)
5. If you plan to use a spin-wait loop (I presume you'll be multithreading), then insert a PAUSE instruction to avoid cache thrashing on exit.
6. Align data on their natural size boundaries (I'm sure you know this)
7. Use the addressing modes supported by the processor, instead of calculating address yourself (such as scaled index). LEA is your friend also.
8. MUL/DIV are two of the most expensive instructions, (~30 clocks (Processor-dependent)), use shifts (SHL/SHR/ect) when possible.
9. Serialize data access, perform write's in chunks instead of patterns like: read/read/write/read/write.

A few other things to keep in mind:

* EAX, ECX, EDX are considered volatile registers in C++, thus you are free to use them in your own assembly routines without corrupting
  the caller's state (important when mixing Asm and C).

* Try using the naked (__declspec(naked)) modifier when writing assembly routines, this enables (and requires) you to write the prologue and epilogue
  allowing you to also use EBP (Personally, I never use EBP for "stack frames", it's worthless) (Note: EBP _is_ volatile)

* If you use registers in your assembly routines other than EAX,EDX,ECX, you must preserve the caller's value of these register (this is the prologue/epilogue)


Anything else, feel free to ask.

Yegg

I thought I'd throw this in here because I feel it's pretty interesting. If anybody remembers or knows of the NBA Jam game for Sega Genesis from years and years ago, the entire game was written by one man entirely in assembly.

Warrior

Quote from: Yegg on October 25, 2007, 11:07 AM
I thought I'd throw this in here because I feel it's pretty interesting. If anybody remembers or knows of the NBA Jam game for Sega Genesis from years and years ago, the entire game was written by one man entirely in assembly.

I'm 100% sure that the Chatter Bots of today are infinitely more complex than anything ever conjured on the Sega Genesis.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Barabajagal

Hey man, don't dis the genesis. It gave us Zero Wing, and where would our bases be if not for that game? ;D

MyndFyre

Quote from: Andy on October 25, 2007, 05:34 PM
Hey man, don't dis the genesis. It gave us Zero Wing, and where would our bases be if not for that game? ;D
Also Kid Chameleon and Altered Beast!
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Barabajagal

Wasn't Sonic Adventure on the genesis, too? The one where the first level is "snowboarding" on the street chased by a giant Semi?

|