PDA

View Full Version : Understanding ASM



kuphryn
07-11-2002, 12:24 PM
Hi.

I have close to one year C++ experience and six months MFC experience. I love programming using C++ and enjoy the simplicity MFC brings to Windows programming.

I will begin my senior year in college this coming fall semester and hopefully, I will fullfil all required courses to graduate spring 2003 and work as a programmer. I am looking at various job descriptions for entry-level programmers especially for C++ programmers. Anyways, I often see ASM in the description. I have no experience with ASM. I will take a required ASM course this fall. Nonetheless, I would like to know some fundamentals of ASM.

First, I hear about the power of ASM because anything is possible. I hear about ASM being "clean" coding. I know some Java, but have not done any real projects using Java and I do not plan on it either. I think C++ is elite! In real projects including commercial software, games, OS, and the like, how does ASM fit into the equation?

Basically, I just would like to know at what point do programmers prefer ASM over C/C++? Second, I hear that it is possible to incorporate multiple language when developing one software. How is that possible (especial with C/C++ and ASM)?

Again, please take into considering that I have no knowledge of ASM and very limited experience with software engineering.

Thanks,
Kuphryn

adrianxw
07-11-2002, 12:52 PM
>>> at what point do programmers prefer ASM over C/C++?

No point. Assembler has/had it's uses. People who usually claim assembler has "n" advantages over a modern optimising compiler are living in a golden passed. Unless you actually have to use assembler for a task, you are better off not. Period. Punktum. Full Stop. Assembler programming is always more difficult to understand, more prone to, (disasterous), errors, has big maintenance problems, and tends to be platform specific.

>>> possible to incorporate multiple language when developing one software.

With any true compiler, the high level code you write is converted to "assembler", and then onward processed. The intermediate assembler code, (often simply called "object"), is language independent. Thus, a module wriiten and compiled with a C compiler, and a module written with a FORTRAN compiler can be linked together transparently as long as the "calling" convention is agreed in advance. No magic there.

Shiro
07-11-2002, 01:24 PM
From a software engineering point of view, ASM is not recommended. You can't write platform independent software or develop easy to maintain and flexible software. And it will take you a long time to code when the project is large. Since ASM software is harder to debug and more sensitive for errors, also the test time will probably be long.

But it is useful to understand ASM, since it gives you lot of insight in how microprocessors work and it makes you understanding computer architecture.

In commercial software, I can only speak for embedded systems, there still is a role for ASM. Some very low-level things can only be done in ASM. But modern compilers are so good, that it is usually unnecessary to use ASM for the other things. Those other things are in a lot of cases written in C, C++ or Java or a combination.

For example, software for a mobile phone: the very low level software, below the OS level and some parts of the OS, are written in assembly. Most of the OS-level is written in C or C++. The applications are written in C++, but more and more applications these days are written in Java. So a Java virtual machine is layed upon the OS level and the applications run on this virtual machine.

>Basically, I just would like to know at what point do >programmers prefer ASM over C/C++?

Personally I never prefer ASM over C/C++, but sometimes I can't use C/C++.

VBprogrammer
07-11-2002, 01:27 PM
I propose a deal with all you C/C++ programmers, we all know you are to scared to learn assembly so stop posting a load of bull**** about assembly as a cover-up (not specifically you adrianxw but there are some people on this board who believe that C is the be-all and end-all of computer programming).

kuphryn assembly most definatly has its advantages, it is increably fast and produces small tight code. It is ideal in games programming where the use of tight loops is nessisary. However it has uses in any algorythm which must exicute at high speed or very often.

The first assembly comand most people learn is:

mov dest, source
This is pretty easy to understand, dest and source can be anyone of the processors registers or a memory location. There are some very good assembly tutorial on the net so try a google search.

Shiro
07-11-2002, 01:37 PM
>assembly most definatly has its advantages, it is increably fast
>and produces small tight code.

Sure, it has some advantages, but for commercial software it is not suitable.

In the mobile phones market, the size and speed of the software isn't the most important thing. More important are things like speed of development, maintainability, flexibility, reusability etc. If your opponents in the market can develop software in a faster and more efficient way and get their product earlier on the market, then you can stop the project. I've been at a project which was stopped after a year because an opponent released its product of the same type two weaks earlier than we planned to do. But because of good software architecture, we're able to reuse the architecture and software components for new products.

VBprogrammer
07-11-2002, 01:45 PM
Shiro - why? What makes assembly so different from every other programming language. If you need part of your program to run fast then code that in assembly, thats pretty simple. I wouldn't recommend assembly for the whole project but for those time critical parts it is unbeatable.The portability of C is always over played as an advantage, in practace changing between different compilers can be pretty difficult.

Enmeduranki
07-11-2002, 01:46 PM
>But it is useful to understand ASM, since it gives you lot of insight in how microprocessors work and it makes you understanding computer architecture.<

This depends on the processor. Learning x86 asm gives limited insight as to how a modern processor using the x86 ISA actually works.

VBprogrammer
07-11-2002, 01:52 PM
Im not really sure what you mean by that Enmeduranki however it easily gives a better insight into how the processor works that C would.

P.S. I dont really want to turn this thread into yet another C vs ASM argument, all i want to do is help kuphryn understand what ASM is all about.

Shiro
07-11-2002, 02:14 PM
>What makes assembly so different from every other >programming language.

The purpose of the language. Most language are written for some special purpose. Assembly is meant to make low-level programming possible. Object oriented languages are meant to make developing object oriented software easier. In my opinion a programmer should use the language which fits the needs. And therefore you should use assembly only to speed-up specific portions of the code or to do some ow-level things.

>If you need part of your program to run fast then code that in
>assembly, thats pretty simple.

If there is a need for speed in parts of the program, one could consider to use assembly. That's correct. I know that a lot of space-, medical- and defense-applications have their time-critical portions of code written in assembly.

>I wouldn't recommend assembly for the whole project but for
>those time critical parts it is unbeatable.

I agree. Note that this is also done, but also note that the size of the critical parts in a project is very small. There usually are just a few places in the software which are time-criticial. But this is usually measured when the code is implemented in C or C++.

The measurements will show which parts take most of the time and which parts are time-critical. Some research should show that assembly is useful here, in a lot of cases the code an optimizing compiler gives, isn't slower or just less slower than C-code.

Since hardware is so fast these days and compilers are getting better, these differences are getting smaller and smaller.

>The portability of C is always over played as an advantage, in
>practace changing between different compilers can be pretty
>difficult.

Also changing assemblers can be real difficult, just compare assembly for Motorola processors with assembly for Intel processors.

Most C compilers are not compatible and most of them even aren't conform the ANSI C standard. But an advantage of C is that it is more platform independant then assembly. If you use C, then you can more easily switch to a different platform than using assembly, especially when the project is large.

>This depends on the processor. Learning x86 asm gives limited
>insight as to how a modern processor using the x86 ISA actually
>works.

That's true. Also ASM is different for several processors. But understanding ASM and how a certain processor works, gives you the basic knowledge to study other ASM variants and other processors.


>P.S. I dont really want to turn this thread into yet another C vs
>ASM argument,

I also don't want to argument using C/C++ vs ASM, I'm just explaining why, in my opinion, one should combine the advantages of languages to produce software.

Enmeduranki
07-11-2002, 02:20 PM
>Im not really sure what you mean by that Enmeduranki however it easily gives a better insight into how the processor works that C would.<

True, but there is alot going on that you never see (register renaming, instruction translation, instruction rescheduling, etc). So your x86 asm program may not actually bear that much resemblance to the finished article. That's not to say there's no benefit though.

Shiro
07-11-2002, 02:27 PM
>True, but there is alot going on that you never see (register renaming, instruction translation, instruction rescheduling, etc).

That's correct, but I wonder if the average programmer needs to know about processors in that detail. I think the level of detail a programmer gets when learning assembly is enough for most tasks. By "know how a processor works", I meant from a software developer point of view, not from a hardware or embedded developer point of view.

Fordy
07-11-2002, 02:59 PM
Originally posted by VBprogrammer
I propose a deal with all you C/C++ programmers, we all know you are to scared to learn assembly so stop posting a load of bull**** about assembly as a cover-up (not specifically you adrianxw but there are some people on this board who believe that C is the be-all and end-all of computer programming).

adrianxw assembly most definatly has its advantages, it is increably fast and produces small tight code. It is ideal in games programming where the use of tight loops is nessisary. However it has uses in any algorythm which must exicute at high speed or very often.


::WINCE::

No offecne VB, but from what little I know of Adrian, he was programming in different forms of assembler while you were still collecting teddy bears......

VBprogrammer
07-11-2002, 03:10 PM
Yeah, i figured that out pretty quick (though i dont think i ever collected teddybears so does that make Adrian god?), however i felt it nessisary to give a balanced point of view on the subject of assembly programming. As kuphryn mentioned there are plenty of jobs where experience in assembly ranges from being an advantage to a job requirement.

Re-reading Adrians post i cant help thinking that those years of programming experience have turned him into the fountain of useless information commonly known as an "Old Fart". Come on, i know that he probably has more computer knowlage in his big toe than i can muster from every last inch of my body, but why doesnt that show in his post?

kuphryn
07-11-2002, 04:07 PM
Thanks everyone.

From all responses and my limited experience, I believe every programming language has/had its imporantance in the world of software design and implementation. I believe C/C++ is currently the most dynamic, most extensible programming language.

I have seen ASM code of small projects. ASM programming seems redundant when you code certain code many times. Without ASM experience, I am not able to determine what the programmer is doing at each part of the code. I hope to be able to read a ASM source and know exactly what the programmer wants to do. I believe programmers should know a little ASM.

Oh, this is somewhat off topic, but do software engineers use ASM to develope compilers especially C and C++ compilers?

Kuphryn

VBprogrammer
07-11-2002, 04:14 PM
Yes and No, it depends on when the C compiler was developed. Obviously if it was developed very early on it will have been developed in ASM (though it may have been developed in Machine Code however this is unlikely). However if it was a modern compiler it is quite possible that the magority of the code was actually developed on another C compiler with ASM being used to speed parts of the code. It is quite possible to develop a compiler in any language but some dont really suit this purpose i.e. writing a C compiler in VB!

kuphryn
07-11-2002, 04:21 PM
"writing a C compiler in VB!"

hahahhah

Kuphryn

VBprogrammer
07-11-2002, 04:30 PM
Possible but no, not a good idea!

Garfield
07-11-2002, 05:59 PM
Summed up (I just had to get in on this conversation ;))...

::drum roll::

No language is perfect. Every language has their ups and their downs. You choose which language suits your ups, and the downs are take-able (able to be delt with). It's not hard to see the ups and downs of Assembly. It's quick, it's efficient, the programmer has all control. And, there are the downs: it's hard to debug, hard to make use of resources and just plainly hard to read. It's hard to code. It takes long. But, as a programmer and with my "department" of programming, I choose Assembly because I think the ups outweigh the downs. Some differ, because they do different types or programming. Both C and Assembly and very noble and great languages. They are my two favorites, actually! :D

ggs
07-11-2002, 06:08 PM
www.hugi.de/compo
^^ great assembly size opt competition, several per year..

[/hijack]

Garfield
07-11-2002, 07:54 PM
Thanks! I think I'll enter, maybe...

Nick
07-11-2002, 09:49 PM
This page is interesting
http://www.azillionmonkeys.com/qed/optimize.html

adrianxw
07-11-2002, 11:00 PM
VBprogrammer:

>>> it is increably fast and produces small tight code.

No. It can be incredibly fast and it can produce small tight code, the results are only as good as the author. It is easier to do the same with a modern optimising compiler.

Unless you really know your hardware, writing a routine in assembler can be counter productive. One needs to consider how cacheing, instruction pipelining, etc. etc., are performed in the chip. The developers of the compilers optimising software have done this for you.

>>> the fountain of useless information commonly known as an "Old Fart".

The characteristic of an old fart is that they live in the past. Surely, by advocating the newer technology over the old, I am exhibiting the the opposite trend. However, I, of course, don't give a damn about what you think about me, why should I?

>>> but why doesnt that show in his post?

I think my post gave clear and concise opinions/answers to the questioners question. What is wrong with that?

General:

There are very few general applications for assembler these days. Where they exist, they are in small niches. (I routinely write assembler for one particular customer - they use Fairchild F8 microprocessors - a dead chip with no reliable compiler - niche). Otherwise it is speed of development/time to market and maintenance that rule.

Learning some assembler, (maybe at the conceptual level), may give an insight to general processor operations, however, with modern chips, (and the ever growing rate of chip development), there is little real point unless you are aiming at one of the niche markets.

kuphryn
07-12-2002, 08:24 AM
Okay. Thanks.

One last subtopic. As I mentioned, I have seen simple ASM code and for some reason, I believe ASM can be fun. Again, maybe I feel that way because I have absolutely no knowledge of ASM. Nonetheless, I find incorporating ASM into a C++ program seems fun, interesting, and even innovative.

At what point do you learn to incorporate ASM code into C/C++? For example, do ASM books teaches the procedure or it is sort of like an individual discovery type of thing?

I understand completely that to incorporate ASM into C/C++ one must understand ASM well.

Kuphryn

Nutshell
07-12-2002, 08:29 AM
yea i want to know what kind of books would show how to implement inline asm kind of stuff?

Shiro
07-12-2002, 08:39 AM
>At what point do you learn to incorporate ASM code into C/C++?
>For example, do ASM books teaches the procedure or it is sort of like an individual discovery type of thing?

I do not know exactly what you mean with incorporating ASM into C/C++. There are two ways to use ASM in combination with C/C++.
1. Using inline assembly, if available.
2. Assembling external assembly-routines and link them with your C/C++ routines.

In most books, when learning C or C++, you don't learn assembly. Only books on subjects like assembly, computer architecture or related treat using mixed assembly and C.

A lot of compiler manuals on C and C++ compilers also have some chapters on mixed code.

>yea i want to know what kind of books would show how to implement inline asm kind of stuff?

Most compilers I've used, each had its own way of implementing inline asm. So I would suggest to check out the manuals of your compiler, you could also visit their site, if there is one.

Enmeduranki
07-12-2002, 05:58 PM
>Some differ, because they do different types or programming.<


What type of programming do you do?

Garfield
07-12-2002, 07:39 PM
>> No. It can be incredibly fast and it can produce small tight code, the results are only as good as the author. <<

This is true. A good and experienced C programmer will write a faster program than a sloppy Assembly programmer. This is a very big, if not the biggest, aspect of how the program will turn out...