Author Topic: Beginner questions  (Read 7647 times)

nobody

  • Guest
Beginner questions
« on: April 09, 2009, 12:10:28 PM »
How different is assembly on Linux and Windows?

Assembly is the language that computer understands but in a more readable form, right?

I've been interested in game hacking and used program called Cheat Engine(on Windows) a lot. It has "auto assembler" function, can someone tell if it uses same code as nasm?

What is assmebly good for? What do you use it for?

Is it possible to include assembly in C++ code? (It's a bit different format propably)


Thanks in advance
  ~Henry

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Beginner questions
« Reply #1 on: April 09, 2009, 05:13:13 PM »
Hi Henry,

> How different is assembly on Linux and Windows?

Depends on how you look at it, I guess. You're talking to the same CPU, and it understands the same instructions (depending on CPU version - they're always adding new ones, but the old ones still work). Much assembly code - calculating "strlen", for example - doesn't care what OS it's running on. But when you have to interact with the OS, it requires a different sequence of instructions. And you *do* need to interact with the OS, if only to exit your program (a detail HLLs do for you - but it still needs to be done!)

Windows:
push 0 ; "exitcode" or "errorcode", zero usually means "no error"
call ExitProcess ; Windows provides this code

Linux:
mov ebx, 0 ; exitcode, as above
mov eax, 1 ; tell int 80h we want the "exit" function
int 80h ; Linux provides this code

Doing something like printing "hello world" or asking the user for his/her name would differ in a similar way, but moreso. When we get to GUI programming, the differences are even greater. In Windows, the GUI is part of the OS, in Linux, it's a separate program running as a "server". But you're still using the same instructions, and there are "parallels" as well as the differences.

> Assembly is the language that computer understands but in a more readable form,
> right?

Right. For certain values of "readable". :)

> I've been interested in game hacking and used program called Cheat Engine(on
> Windows) a lot. It has "auto assembler" function, can someone tell if it uses
> same code as nasm?

I'm not familiar with it. I doubt if it's the same, but it might be. Can you post a sample?

> What is assmebly good for? What do you use it for?

I personally only use it for "fun". It's "good for" CPU-specific things that C (being portable) doesn't know about. Not too much, these days, actually...

> Is it possible to include assembly in C++ code? (It's a bit different format
> propably)

Yes, a couple of different ways. Most compilers allow "inline assembly". The syntax differs wildly - mostly not too close to Nasm. :( Or you can write a separate module in assembly, assemble it to an .o or .obj file, and link it with C or C++ code that calls it. Michael was even showing me how to link asm code with Fortran! C++ is slightly more complicated in that it "decorates" (mutilates!) function names. You can either learn the naming scheme - which differs between compilers - even versions of the same compiler, I'm told - or you can prototype your function as extern "C" myfunc. The latter is obviously easier, although you might lose some of the advantages of C++(???).

This is probably the most "practical" way to use asm. I'm a "do it yourself" kind of guy, and I like all-asm, self-contained, no libraries, "pure asm". This obviously isn't suitable for "serious" projects (although it can be done!). But writing modules in asm - as few or as many as you like - and "tying them together" with C(++)... we can even get "portability" across Windows/Linux/BSD/MacOS(?)...

In fact, Dr. Paul Carter's tutorial depends on this - lets C deal with the OS, and presents examples as "asm_main", to be called from a C "driver". This allows the same code to run on Windows/Linux/BSD/???. It *does* need to be assembled into linkable object files in different formats, but Nasm's capable of that.

http://www.drpaulcarter.com/pcasm

Take a look at it and see if it's something you're interested in.

Best,
Frank

nobody

  • Guest
Re: Beginner questions
« Reply #2 on: April 10, 2009, 01:38:29 PM »
Here's a auro-disconnect hack for Guild Wars. [Enable] part is run when the hack is enabled and [Disable] when disabling it.

[Enable]
alloc(newmem,2048)
label(returnhere)
label(exit)
label(foxy)

0057f5a8:
jmp newmem
nop
returnhere:

newmem:
cmp [eax+edx],8
je foxy
mov [eax+edx],8

exit:
jmp returnhere

foxy:
mov [eax+edx],0
jmp returnhere

[Disable]
0057f5a8:
mov [eax+edx],bl
inc eax
cmp eax,ecx
dealloc(newmem)



Someone told me assebly will create noticeably smaller programs. Can you give me few examples like size difference between web browsers made in C(++) and asm.


Off-Topic:
I'm currently building a very light Linux distro for my netbook to be a pro assemly, C and C++ coding machine. I'm taking  name suggestions. :P

Jim

  • Guest
Re: Beginner questions
« Reply #3 on: April 10, 2009, 05:59:32 PM »
Once upon a time in a decade not quite so long ago... Compilers weren't as smart - that is, they weren't able to look at the code the produced on a larger scale and optimize it.  Critical routines like image transformation (filter type effects) would be coded in assembly, so that the fewest instructructions were executed in a tight-loop manner.  (modifying each and every dot of an image can take a while).  

Probably noone(!) would write something as complex as a web browser in assembly... (I just did a search, and I like these answers http://answers.yahoo.com/question/index?qid=20090210070022AAQAqlk )

Today, however, gcc, g++ (the compiler used commonly in Linux) is very capable of analyzing the code produced between the translation from C into assembly and making smart use of registers.  The code can be optimized for either size or speed (and they are not nessecarily the same thing), and with such optimizations on, some complex floating point routines I was doing in assembly for matrix transformations was shorter when the compiler generated the code, than the hand-written assembly I had been using.  (Not all compilers are very good at this, lcc-win32 for instance is much more of a macro-replacement scheme where something like ....

float a,b,c,d;
a=b*3;
c=b*4;
d=a*c;

would look in assembl like

mov ax,;
fld ax;
mov ax,3;
fld ax;
fmult
fstor [c]

(repeat for each line above basically)

instead of loading the b variable onto the floating opint stack once, and reusing the value already in the floating point stack.


These days, except the very very lowest components of Linux itself, even all of Linux is written in C.  This provides cross-platform compatibility, that the same code can be targeted to an ARM or PowerPC chip as well as Intel... assembly is limited to a single target platform (and even Intel and AMD can be different, as well as CPU classes, as a Pentium has significatnly more ability than a 386, although, you can use the same common base layer, your assembly program will still work)

Even more modern languages like Java and C# generate a meta code that is compiled just before it is excuted (compiled in that it's meta instructions are transalated into machine-specific operations) I read an article that indicated that the C# engine could, in theory, recompile code multiple times depending on circumstances... producing differently optimized code on-the-fly.

DemoScene (http://en.wikipedia.org/wiki/Demoscene ) these people might be incline to using assembly to producing wicked small programs, well beyond what a compiler could optimize.

I still have some assembly in my libraries for image handling, I have some multi-channel shading that takes advantage of the MMX extensions to compute a pixel's resulting RGBA values in parallel, at once.  There are some compilers that have 'intrinsics' that provide a level of abstraction from the assembly and maintain a C-like interfacing to these, otherwise, unreachable methods... (There's no way in C to say add this 4 value vector with this other 4 value vector and store the result...[r3,b3,g3,a3] = [r1,b1,g1,a1]+[r2,b2,g2,a2])


The newest advances, 64 bit Operating systems, defined an entirely different scheme for passing paramters to sub-routines (based on 2+ decades of shortcomings learned), and, unfortunatly, the mechanisms used for Linux and Windows are different.  (there is a standard published though that details this)... but this makes all the nice MMX code I did for 32 bit compilers not port so well, and basically all the entry/exit and even some of the guts have to be entirely re-written.

Assembly is a wonderful thing to know, if you have a program you've developed, and produced a working version, but stripped all the symbols (all the pretty names like MyFunction() go away, since they don't mean much to the machine, and become a single number (0x5ff3a55)... if you know assembly, and have to, for some reason, debug this released program, it is handy to know what the instructions mean, to be able to identify what function the code is doing.)  But, like Latin, I think that assembly will become a read-only language that is not written.  Although, there will always be that small(miniscule) core group of people developing embedded systems like your phone, or working with some new cpu architecture that will need to know assembly.