October 12, 2012

I Remember Assembly Language, Part II

Filed under: Main — Tags: — admin @ 12:01 am

Assembly language runs fast. Assembly language produces small programs. But Assembly language takes nearly forever to write. That’s because you pretty much have to do everything from scratch.

Unlike higher-level programming languages — even the C language — Assembly has no libraries. There are some routines you can access in the computer’s ROM or in the operating system, but beyond that you have to code everything. That includes input, output, math — you name it. It’s a lot of work.

The payoff, of course, is that the code runs super fast, especially when compared with the typical BASIC program of the era.

As an example, here is code I wrote in 1988 that displays a message on the screen:

        DOSSEG
        .MODEL small
        .STACK 100h
        .DATA
message db      "This is an .EXE file.$"

        .CODE
start:  mov ax,DGROUP
        mov ds,ax
        mov ah,9
        mov dx,offset message
        int 21h

        mov ah,4Ch              ;leave
        mov al,0                ;zero code
        int 21h

        END     start

The resulting executable file is only 244 bytes long. That size cannot be equalled by any other programming language. Granted, it’s entirely unreadable. Even if I wrote all the necessary comments, few non-Assembly programmers would understand the code itself. But as you can see from my comments above, I probably felt comfortable enough with what I was doing not to write comments.

Generally speaking, most programs do more than display a single line of text. And yet, in Assembly, every little bit of the program must be coded.

Below is a snipped from code I wrote. It takes a string of text stored in memory and converts that text into a value. The value is stored in one of the processor’s registers:

;---------------------------------------;
;  Calculate string si==> into bx       ;
;---------------------------------------;
DECBIN  proc near
        mov bx,0                        ;zero out initial value
Dget_next:
        mov al,byte ptr [si]            ;read value
        sub al,30h                      ;make binary
        cbw                             ;make into a word
        add bx,bx                       ;multiply bx by 10
        push bx                         ;save *2
        add bx,bx                       ;*4
        add bx,bx                       ;*8
        pop dx                          ;get back *2
        add bx,dx                       ;*8 + *2 = *10
        add bx,ax                       ;add new value
        inc si                          ;bump pointer
        cmp byte ptr [si],'0'           ;end of string?
        jl Dleave                       ;go if so
        cmp byte ptr [si],'9'           ;other possible end
        jle Dget_next                   ;if not, continue
Dleave:
        ret
DECBIN  endp

This code snippet is basically what you would call a function in any higher-level language. In Assembly, it’s a procedure. In the C language, you would probably use the atoi() function to accomplish the same thing. In Assembly, you have to write your own code, similar to what’s shown above.

This snippet isn’t the only procedure you’d have to code. For each and every program you wrote in Assembly, you’ve have to write procedures for everything. Eventually you do develop your own procedure library, which you can use over and over. That includes tasks such as converting a string to a value, but also math fucntions, trigonometry, graphics — you name it. That takes time, and it’s a pain.

And I loved it!

After a while, however, I realized how awkward it was to write things in Assembly. I’ve kept all my Assembly code that I wrote on my PCs. The dates range from 1984 through about 1988. That was when I began to code in C, primarily because the development time was so much faster. But I still remember Assembly language. Those were the days.

4 Comments

  1. I think most asm programmers use clibs instead of writing directly to drivers or hardware. I have a feeling that is too dangerous to code like that on a modern desktop and so asm is really only used for embedded programming now. One way to get around this is to get a Raspberry Pi and run Dex BASIC http://www.dex-os.com/DexBasic/DexBasic.htm

    Id really like to see you write a programming book again, I think you could write a really good Perl book since you know the language well. I know a lot of people who want to learn Perl but just cant get their head around it because all the books are meant for experienced programmers who already know how they would utilize the syntax.

    Comment by BradC — October 14, 2012 @ 12:20 pm

  2. Alas, until some other programming book author dies, I won’t be writing any other programming books soon. I get more email urging me to write a C++ programming book than any other email requests I get. No publisher, however, is interested.

    Comment by admin — October 14, 2012 @ 12:32 pm

  3. I strongly urge you to take a look at the D programming language. The syntax is almost exactly the same as C++ but it has garbage collection. If youve ever read a C++ book you will notice that most the book is spent on things like constructors/destructors, delete/new of allocated/pointed memory, generics, basically you have to overuse object orientation in C++ because of lack of garbage collection. But D is more like Java, you just program and dont worry about memory, and you end up with code that is as fast and efficient as C++. There are only two books out on D so you dont have to wait for someone to die to get a publisher interested. I know there are a lot of people clamoring for a good book on D. There are already a ton of books out on Go which is a similar but much inferior to D and is only successful because Ken Thompson and Rob Pike worked on it.

    Comment by BradC — October 14, 2012 @ 12:46 pm

  4. I shall put in a request to the publisher immediately! Thanks!

    Comment by admin — October 14, 2012 @ 12:49 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.


Powered by WordPress