hi,
I am wondering when would it be more suitable to write a part of program in assembly language instead of high-level languages (such as C++)...????
any help would be appreciated =)
Hi my friend,
Assembly language is a sports car designed mainly for speed purposes, it is not comfortable like a GS 300 Lexus, the designers of the assembler don’t really car of you have a fast and easy way to listen to you CD in the car or not, if you want you can build your own player [function].
Also you have to know that programming with any high level language allow you to do whatever the designers of the compiler permit you to do but in assembly you can do what ever the microprocessor can do.
So the answer for you question is, when the speed is a very important factor and/or no ready function to do what you want to do.
Common examples for this kind of applications are, device drivers, embedded systems etc.
But, be aware that the evaluation of the speed factor I meant is professional evaluation, meaning, all of us [as programmers] want our programs to run with a sky rocket speed but a lot of factors control the software production cycle, like the dead line etc..
So, if your company made the decision that this software worth the time and effort to be written with assembly, so go ahead do it. If they need a program up and running as fast as you can [may be for demo or such] the assembly is not your option unless you can write really fast other wise you risking your job security, believe it or not for good for bad, having software up and running has a higher priority than the performance anywhere in the world even though you read something else in the companies web sites, frustrating ha, but it is fact.
What I said is applicable on the professional level of programming [primarily to make money]. If you want to write assembly for your own fun and increase you programming skills
You can write any thing with assembly even a phone book program.
Good luck Walid Magd
Engwam@Hotmail.com
Specially, for C/C++ it is hard to state any limitations. I didn’t use Assembly because of any limitation of C/C++ if I am writing application for the PC.
But I am an Electric engineer specialized in the processor-based control systems, for a specific processor, you may not find any C compiler so it is faster and safer to write Assembly code. For you as a student, my advice to you is to through behind your back any frustrating opinions about Assembly language like no one use it now, very hard to find a job etc..
All of this is not true by any means. In the most basic level we can say, whoever know how to write Assembly, he knows how to do any thing.
This doesn’t mean to assign 100% of your time to it and forget C/C++ for instance, but just keep reading and learning Assembly this will elevate your value as a programmer specially you are student now and have time to invest, in the future this will incredibly reward you.
You will be more than welcomed to work for any great company even to program with VB if you can prove to them you can master Assembly because this is simply means that you can master the PC.
Good luck
what Walid said is great! I am programming using delphi,c,php,etc. now however i often delve into assembly as a hobby.
I feel the assembly help me to understand hardware and high level language better.For example by disassemble the delphi coutput i can know what this compiler does and how it optimize the code...
In a word mastering assembly is a big help for your programming life!
Regards! zallen@cmmail.com
ICQ:101229409
Long live of freedom!
thanks Walid for your help =)
I came across a part in my text which is pretty interesting,but I don't understand some concepts behind it...
it basically says that writing a code in assembly language might not obtain the highest performace.
For a human coder to compete with current compilers he/she has to be very famaliar with the concepts of processor pipelining and memory hierarchy(which I have no idea what they are...pls help Walid...=P )
On the other hand,C programmers can also hint the compiler about which variables should be kept in registers rather than spilled in memories.(I guess that means using pointers?)
let me know if I got any of the concepts wrong?thanks
Well, compiler writers have done a lot of research on which instructions run fastest in given situations, and write their compilers to generate this "optimized" code. If you don't know the optimization tricks, and write assembly, you could potentialy (even likely) write slower code than comprable compiled code.
It's written in the same language, but you just (probably) don't know all of the tricks and shortcuts that the people that wrote the compiler do.
This is probably not always the case, but it happens enough of the time that you can't beat a *Good* compiler very often
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
Well, my friend mbaranski ,( you can't cross a bridge before you reach it first, meaning, to be familiar with the tricks of optimization of any language not just assembly, you have to familiarize your self with the basics of the language first. Specially for the assembly language and low level C programming you may need to familiarize your self with some computer design architecture to be able to understand what does it mean pip line and parallel processing etc.
But, if you remember your problems memorizing the multiplication table when you was a kid, and if you are going to laugh now if I asked you what is the result of 12*12, you well get my point, for example on my first day in the engineering collage I didn’t know how to design an AND gate with MOSFT transistors, 5 years later my graduation project was designing a micro controller from scratch not using any micro processor [use your own micro processor] and write the software for it to run. It just needs time.
For you my friend J22, don’t even believe that your are not able to improve (let’s leave the word BEAT to the marketing people) or build on top of some body’s else researches, if you going to count what we still need to do, you will find it thousands of time more than we accomplished already. Biometric recognition technologies (Sound recognition, hand writing recognition, etc..)
Improving some tools we already use (and suffer from) like Microsoft MFC and more.
Just go a head and start, one thousand mile trip has to begin with one step.
Regarding your question about the register variable, it is not the same as using pointers.
It is just an instruction to put the value of this variable in the register not in memory.
Think about register just like a memory location but inside the micro processor it self which make any operation on this variable very fast. Use it wisely, as an engineer you never waste a water tank just to drink a glass of water. If you have time go to
and search for the book Assembly Language Primer for the IBM PC and XT by robbert laufor.
It is more than fun to read this book, don’t let XT scare you.
I can recommend another books in the hardware design too, but I don’t know what kind of study you practice now, and what is your schedule.
Feel free to contact me if you have any question.
Thanks
Hey,
Whenever u have to directly speak with hardware inorder
to get fast performance ( thereby avoiding library call
overheads etc..), use assembly. Moreover there some special
registers can be accessed only in assembly ( For exp,
CR3 in 386, i.e the PageTableBaseRegister. ofcourse,u need
to access CR3 iff u r writing an OS or sthg near to it )
In most of the games u play, the screen refreshing
routine would have been written in assembly.
U can always consider coding a bottleneck routine in
assembly, provided u c any scope for performance.
regards,
sarnath
Do not rejoice that ur code works.
it might be a special case of an error :-(
I've got to put my two cents in here. I'm a semi-pro programmer (What's that joke about an unemployed programmer?) who mainly works in Pascal, though I am slowly picking up C++. I love to program in assembly though, because the flexibility lets me do some things so much easier than Pascal or C++ would. For instance, say you want to test for the existance of an extended bios drive capability, and based upon that result, call proc A or Proc B. In Pascal or C++, this would be fairly difficult to do, but in ASM, you just store the address of the desired proc in a DWORD and push it on the stack and return to get there. Yes, there are times when I discover why my father, who was a programmer from the pre-HL Language era, lost all his hair, but the satisfaction of watching an assembler program step through it's execution as planned and watching the expected results come up is ten times greater than just sitting down and hacking out some code in Pascal.
Hi my friend Prattaratt
My son is about 50 days old now. From the comments this Japanese lady telling me lately when she cuts my hair, I finally found out why my father -God bless his soul - lost all his hair.
It has nothing to do with Assembly language believe me.
A common misconception is that the average assembly language programmer cannot beat an optimizing compiler. This is inaccurate. It should say that a COMPILER written by the average assembly language programmer cannot beat an optimizing compiler compiling a well-designed program. A good person can out-think a machine any time. There are just a few tips to optimize any program, whatever the language:
1. Check the most likely conditions first. Example, you get a number in the range 1-100, all numbers greater than 80 go to this func, all numbers less than 50 go to this other func, else go to some other func. Which is more likely? Put that decision first. So check the numbers less than 50, then check if it's more than 80, then go to else condition. Or if you're processing commands, check the most common command first, e.g. if you want to rewrite COMMAND.COM check for DIR first, then CD, etc.
2. Be as straightforward as possible. If you need to put A in C, try not to put it in B unless there is absolutely no other way.
3. Unwind loops. For example, you need to loop through something 100 times, each time checking for 2 different things (odd loops do this, even loops do that), don't loop through 100 times, loop through 50 times, each time doing the odd and even procedures.
These are optimizations no compiler invented has ever done. They may seem simple, but considering that you can loop through a lot of stuff thousands of times, just reducing by a few comparisons can give big boosts in speed.
Additional:
4. Know about bottlenecks in the system, and avoid them as much as possible. For example, for a long time accessing the video on PC's was a slow task which could severely cripple even the fastest 486's. As many as four wait states might be needed. So when they developed functions to draw, say, horizontal lines, they didn't plot it one pixel at a time - because of the way the video memory is stored, they plotted it one byte - 8 pixels - at a time. Loop unwinding (3) frequently considers bottlenecks in the system so that the unwound loop is more efficient.
5. Check the program for errors first, then apply optimizations. So what if it runs fast but does things all wrong? If you optimize before you debug, you may be forced to undo your optimization and end up being slower. Of course, this excludes optimizations at the design level; you have to "debug" the design while it's all in your head.
6. As much as possible you want to make optimizations in your DESIGN.
7. Avoid having to recalculate and recalculate and recalculate. If you need a set of values, say, sines for 1 to 90 degrees and you know you want them in increments of one degree, have your init calculate them and put them in a table. Better yet, get a calculator and enter the values yourself. Programming is hard work. Or as a better example, consider the basic algorithm for SPLINE:
'This is in QBasic
FOR t = 0 TO 1 STEP tStep
xp = t * t * t * x1 + t * t * (1 - t) * x2 + t * (1 - t) * (1 - t) * x3 + (1 - t) * (1 - t) * (1 - t) * x4
yp = t * t * t * y1 + t * t * (1 - t) * y2 + t * (1 - t) * (1 - t) * y3 + (1 - t) * (1 - t) * (1 - t) * y4
PSET xp, yp
NEXT t
'You can optimize it to:
FOR t = 0 TO 1 STEP tStep
C1 = t * t * t
C2 = t * t * (1 - t)
C3 = t * (1 - t) * (1 - t)
C4 = (1 - t) * (1 - t) * (1 - t)
xp = C1 * x1 + C2 * x2 + C3 * x3 + C4 * x4
yp = C1 * y1 + C2 * y2 + C3 * y3 + C4 * y4
PSET xp, yp
NEXT t
'example ends
All the constants are calculated at the start of the loop, instead of calculating them twice for x and y. So you can be reasonably sure it will almost double your speed. And what if you were drawing in three dimensions? You'd get triple the speed.
You HLL lovers must realize that your languages, which are inherently inefficient since they try to make the computer think like a human instead of vice-versa, should train you to be more efficient, because the language your using isn't. And yet the only people I find who keep talking about efficiency are virtually ASM programmers. I really don't understand why. Maybe the language just attracts that sort of person.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.