Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming in Assembly Language

  1. Dec 16, 2011 #1
    Hi,

    I've never programmed before but I recently downloaded the book "Programming from the Ground Up" and I plan to start studying it in 2012.

    I've heard that many programmers who learn to program in Assembly don't see a need to use anything else besides this programming language. Is this true? Why?

    Also, I'd like to know one other thing, how do I actually get started with programming in Assembly? What do I need to download onto my Ubuntu OS etc? (the book doesn't say)

    Thanks.
     
  2. jcsd
  3. Dec 17, 2011 #2

    Borek

    User Avatar

    Staff: Mentor

    Programming in assembler is programming on the knees - it takes much longer to get there than in other languages. So no, I don't think it is true people are switching to assembly only, as in most cases programming in assembly is a waste of time.
     
  4. Dec 17, 2011 #3
    I doubt any seasoned or unseasoned programmer will recommend assembly to a novice. Its true the individual instructions are about as easy to understand as any other language, but the simplicity dissolves when it comes time to do anything more than a little integer math. For example, it might take you a half hour of googling and debugging just to print a message on the screen, write data to a file, or show a simple message box. And writing a simple floating-point math operation would take even more study! Assembly is only used by more seasoned programmers, and only in sections of their project that need a little more speed than a low level language like C/C++ can muster. A novice programmer should start with something like Python. Its free, easy to install, very easy to learn, powerful, doesn't lose its usefulness as you become more seasoned, and there are tons of documentation and examples and free code libraries. Don't try to get an elaborate development environment set up at first--that will take too long. Just download and install Python, create a python script with a text editor, and run it from the command line. Also, get some bare-bones (several lines of code) examples and experiment with making small changes to them to see what happens. Once you've warmed up a little, you might install a nice development environment with a debugger. For Python that would be Eclipse with the PyDev addon. All that stuff is free. The only drawback of Python is that its interpreted. So in the unlikely event that you can't find the number crunching you need in one of the many science libraries available for python (which Do run fast), you wouldn't want to write a number cruncher in Python code because it would be dog slow. its about 1/10th as fast as a compiled language for solid number crunching (depending on what kind of crunching and what kind of data), but you won't notice much slowness in anything else you are doing, especially input/output operations like GUI operations. For fast number crunching in Python, use a free library or write the cruncher in C/C++ and call it from Python. That's what all the speed-intensive libraries do--and there are a LOT of libraries (all free) available for Python. For example check out PyGame and SciPy, to name a couple. BTW Python is migrating over a major revision. (2.x to 3.x). I have been using 3.x without much problem except i've come across a few libraries that hadn't been ported yet.
     
    Last edited: Dec 17, 2011
  5. Dec 17, 2011 #4
    Most people like to use Assembly language to program Embedded systems where memory size is limited and precious. But most would argue that C/C++ would be a better choice to program an Embedded System. If I were you I would first do C/C++ and when I know that a particular thing cannot be done with C/C++, I will shift to Assembly language. I don't think people will use Java at all. Because if the memory size in the Embedded system is small there is no question of fitting a JVM software into the memory. I am not very experienced in this. So, I am not going to talk about things like cross-compilers and stuff. Maybe, an expert can share more on this subject. You choose your language according to your requirement.
     
  6. Dec 17, 2011 #5
    Hello Kevin,
    Start with higher-level languages, maybe Basic then C and C++. Fortran even maybe. They still have that? Assembly is for people that like understanding how computers work and how they interface to other devices.

    I'd have to disagree with who that was up there that said assembly is a waste of time? Anyway, I use to work in industry and several times assembly solved a difficult problem: Once we had an op sys upgrade. But that upgrade did something to a 16-bit limit on tape storage that prevented us from retrieving archived data . We, me, had to modify the tape driver which was written in assembly to accomodate the new op system. Another time on a Vax system, our current file copying mechanism had problems. At the time I was taking Vax assembly. I wrote an assembly language routine to resolve that problem. Another time we purchased new lab equipment which required modifying the data acquisition routine which interfaced equipment to the data acquisition sytsem. Again, that was assembly.

    Based on my vast experience in this matter, I recommend you not try to learn assembly on your own but rather I suggest you first learn a few higher-level languages, then if you still want to learn assembly, do not try to learn it on your own as you will be met with terrible frustrations but rather take a class at a university and even that, it's still will be difficult unless you really, really like getting down to the fundamental level of programming on a computer.

    Also, I cannot imagine programmers in general would rather assembly if a higher-level language could accomplish the task except perhaps if there is a performance issue and even that, my understanding is C++ often is as efficient as assembly.
     
    Last edited: Dec 17, 2011
  7. Dec 17, 2011 #6
    Programming in assembly language is difficult and I do not recommend if for a beginner, or for anyone else who has a choice in the matter.
     
  8. Dec 17, 2011 #7
    And I think the evolution of the languages is something like this:
    Basic---> D ----> C ----->C ++ ----> Java and others. Your Assembler is somewhere between Basic and C? And there are several other languages which were influenced by these.
     
  9. Dec 17, 2011 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Assembly is something you should learn only after you've mastered a high level language, if ever. It is not a particularly useful skill in scientific programming. Most programmers don't know and do not need to know how to program at this low a level. (I'm assuming that you are posting at PhysicsForums because you have an interest in scientific programming.) Another problem is that assembly programming is not a very marketable skill. Here's a graph of job demand for assembly programmers in the UK as a percentage of all programming jobs in the UK:

    permanent-demand-trend.aspx?s=assembly+language&l=uk.png


    Yet another problem is efficiency. While the resultant code might be very efficient (no guarantee), the programming is so very, very inefficient. Assembly programmers are just about as efficient as high-level programmers in terms of lines of code per hour. The problem is that each line of high level code is equivalent to several lines of assembly code. If a problem can be solved in a high level language, the high level programmer will solve that problem, and the next, and the next, in the time it takes an assembly programmer to even get started.

    Assembly programming was a lot more prevalent 40 years ago when computers were slow and memory was tight. Machine time was valued much more than programmer time back then. That equation changed decades ago.
     
  10. Dec 17, 2011 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    A couple of people have suggested Basic. Don't learn Basic.
     
  11. Dec 17, 2011 #10

    rcgldr

    User Avatar
    Homework Helper

    The book seems to be OK to use as a learning tool. The main issue with assembly programs is that you'll be writing code with minimal functionality compared to what you would start off with in a higher level language. However I don't see any harm in following a few of the examples shown in that book.

    Side note - the syntax for the GCC assembler reverses the destination and source operands versus Intel and Microsoft assemblers, which have the destination first: opcode ... destination,source. Also the operand size is specifed by the opcode with the GCC assembler, while it's specified in the operands for Intel and Microsoft assemblers. Microsoft also added some extensions to assembly language in version 6 (first released in 1992), that include some higher language type directives such as .if / .else / .endif, .repeat / .until, .break / .if, ... . I don't know if the current Intel assemblers have the version 6 extensions added by Microsoft.

    Unless you can get a hold of an old set of Microsoft tools that run in a dos console window or perhaps MSDOS installed on a virtual PC under Windows, you would need to use Microsoft Visual C++ express (it's free), which includes ml.exe, the current Microsft assembler. You have to manually create a "custom build step" for the .asm source file in a VS project to tell Visual Studio to run ml.exe to create .obj files from .asm files.

    The books states to get the GCC tool set, which apparently includes the assembler. Do a web search for "GCC", and you should find a link where you can download the toolset. Someone here may be able to give you a direct link.

    This isn't true any more. There may have been a time when there were no decent high level languages for some computer types, but I doubt this is true for anything other than a classroom type environment.

    As far as higher level languages go, C / C++ is probably the most popular (if you consider all the embeded software / firmware in consumer devices or computer peripherals). I'm not familiar with Python, but it seems to be more popular in some (or most?) schools. Java is also popular. Fortran is still used in some scientific environments, and Cobol is still used in business environments.

    Assembly is mostly used for portions of operating systems and device drivers that have to deal with context (task / thread) switching, low level hardware I/O, interrupt handling, and other hardware related tasks, such as setting up virtual memory. The rest of many operating systems is written in C / C++.
     
    Last edited: Dec 17, 2011
  12. Dec 17, 2011 #11
    kevin
    here's 'hello world' routine in assembler
    .MODEL Small
    .STACK 100h
    .DATA
    db msg 'Hello, world!$'
    .CODE
    start:
    mov ah, 09h
    lea dx, msg ; or mov dx, offset msg
    int 21h
    mov ax,4C00h
    int 21h
    end start
    nothing to be afraid of
    you can get assembler program
    at linusquestions.org
     
  13. Dec 17, 2011 #12

    phinds

    User Avatar
    Gold Member
    2016 Award

    Borek, I usually find your posts quite intelligent, so was surprised to see this. I certainly agree w/ you that people aren't switching to assembler and I agree w/ all the posts that tell the OP not to waste his time by starting out in assembler, BUT

    Really? Have you ever done device drivers? How about embedded code on some specialized system where memory is REALLY at a premium?

    Assembler is just another specialized tool that has its place. To call it a waste of time is inappropriate.
     
  14. Dec 17, 2011 #13

    rcgldr

    User Avatar
    Homework Helper

    For some people first learning how to program, it may help to spend a relatively small amount of time (a few weeks) to get some idea of how computers function with a few simple assembly language examples, before moving onto higher level languages, which I think is the point of that book.

    The other situation where knowledge of assembly would be helpful is in the case where a debugger lacks full source level debugging, which is the case with some embedded software (fimrware) development enviornment.
     
  15. Dec 17, 2011 #14

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    My judgement would be that today, a professional quality cross-compiler running on a PC will quite likely generate code that is both smaller and faster than what you would achieve by hand, unless the target system is very simple. You can't optimise machine code by looking at a one-page chart of "machine cycles per instruction" any more, because modern CPUs don't work that way. You need software tools that can rearrange a string of sequential instructions to maximize the number of overlapping clock cycles in the CPU, get the best "speculative execution" lookahead before a branch instruction, etc. Humans are no good at handling that sort of detail.

    I agree there is a sometimes a use for embedding short sequences of machine code instructions in a high level language program, but writing a whole application in assembler is history IMO.

    Considering how cheap flash ROM is these days, the economic choice between shaving bytes from the size of the machine code, or bungiing an extra Mb (or even Gb) of memory into the design, is often a no-brainer.
     
  16. Dec 17, 2011 #15

    rcgldr

    User Avatar
    Homework Helper

    The point of the book in the OP is a low level introduction to programming to provide some understanding of how computers work. I doubt that book promotes programming in assembly as a means to optimize code.

    A bit off topic but in response to previous posts: a few years ago, I wrote a simple multi-tasking OS for the ARM cpu, mostly in assembly with some C, because most of that code involved specialized instructions for context switching and interrupt handling. Some of the specialized math routines for the ARM were also written in assembly code, in this case for the purpose of speed optimization, but the ARM is a "simple target".
     
  17. Dec 18, 2011 #16
    Thanks for all the info, everyone!

    I've decided to start with Java. I saw some great tutorials on Youtube.
     
  18. Dec 18, 2011 #17

    Borek

    User Avatar

    Staff: Mentor

    Perhaps too much cutting corners on my side. I meant - it is a waste of time in general programming, as that's what the original question was about. I did some assembly programming when it was necessary, but these were rare and highly specialized cases, and these were not full applications, just small parts of a critical code. I rest my case, perhaps just slightly rewording it - in 99.99% of cases using assembler for programming is a waste of time, as higher level languages will do much better job much faster.
     
  19. Dec 18, 2011 #18

    rcgldr

    User Avatar
    Homework Helper

    Starting with Java will be fine. If you're curious about assembly language, it's always something you can explore later, and it appears you have a decent reference with that book you downloaded.

    Yes, but the book mentioned in the OP is using a small amount of assembly programming as part of a overall learning experience, not as a practical means for writing signficant code.
     
  20. Dec 26, 2011 #19

    phinds

    User Avatar
    Gold Member
    2016 Award

    Don't know I'd go 99.99, but in general I agree w/ you. My experience is colored by having done a lot of assembler programming under conditions where it was literally impossible to get the job done any other way.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Programming in Assembly Language
  1. Assembly language (Replies: 5)

Loading...