Programming assembly

Code:
        segment .text
        global  main
        extern  malloc, printf, scanf

main:
.n      equ     0           ; degree of the polynomial
.a      equ     8
.b      equ     16
.p      equ     24          ; polynomial
.pp     equ     32          ; p' = derivative
.P      equ     40          ; Integral
.i      equ     48          ; loop counter
        push    rbp
        mov     rbp, rsp
        sub     rsp, 64


;       prompt for and read n
        segment .data
.n_prompt:
        db      "Enter the degree of the polynomial: ",0

        segment .text
        lea     rdi, [.n_prompt]
        xor     eax, eax
        call    printf

        segment .data
.n_scanf:
        db      "%ld",0

        segment .text
        lea     rdi, [.n_scanf]        ;scanf("%ld", &n)
        lea     rsi, [rsp+.n]
        xor     eax, eax
        call    scanf

;       allocate pp, p and P
        mov     rdi, [rsp+.n]        ;double pp[n]
        call    create
    mov     [rsp+.pp], rax

        mov     rdi, [rsp+.n]        ;double p[n]
        inc     rdi
        call    create
    mov     [rsp+.p], rax

        mov     rdi, [rsp+.n]        ;double P[n]
        add     rdi, 2
        call    create
    mov     [rsp+.P], rax

;       prompt for and read p
        segment .data
.p_prompt:
        db      "Enter %ld numbers for p: ",0

        segment .text
        lea     rdi, [.p_prompt]
        mov     rsi, [rsp+.n]
        inc     rsi
        xor     eax, eax
        call    printf            ;printf("Enter %ld numbers for p: ", n++)

;       for ( i = 0; i <= n; i++ ) {
        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
.for    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for
;           scanf ( "%lf", p+i );

            segment .data
.p_scanf:   db      "%lf",0

            segment .text
            lea     rdi, [.p_scanf]
            mov     rsi, [rsp+.p]
            lea     rsi, [rsi+8*rcx]
            xor     eax, eax
            call    scanf
            mov     rcx, [rsp+.i]
            inc     rcx
            mov     [rsp+.i], rcx
            jmp     .for
;       }
.end_for:
;       prompt for and read a and b

    segment .data
.a_prompt:
    db "Enter a: ", 0

    segment .text
    lea     rdi, [.a_prompt]
    xor    eax, eax
    call    printf

        segment .data
.a_scanf:
        db      "%lf",0

        segment .text
        lea     rdi, [.a_scanf]        ;scanf("%ld", &a)
        lea     rsi, [rsp+.a]
        xor     eax, eax
        call    scanf

    segment .data
.b_prompt:
    db "Enter b: ", 0

    segment .text
    lea     rdi, [.b_prompt]
    xor    eax, eax
    call    printf

        segment .data
.b_scanf:
        db      "%lf",0

        segment .text
        lea     rdi, [.b_scanf]        ;scanf("%ld", &b)
        lea     rsi, [rsp+.b]
        xor     eax, eax
        call    scanf

;       compute p prime from p

;for(i = 1; i<=n; i++)
;{
        xor     ecx, ecx                ; i = 1
    inc    rcx
        mov     [rsp+.i], rcx
.for2  
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n 
        jg      .end_for2

;    double element = p[i];
    mov    rax, [rsp+.p]
    mov    r8, [rsp+.pp]
    movsd    xmm0, [rax+8*rcx]
;    int i2 = i;
    mov    rbx, rcx
;    i2--;
    dec    rbx
;    pp[i2] = element*i;
    cvtsi2sd    xmm1, rcx
    mulsd    xmm1, xmm0
    movsd    [r8+8*rbx], xmm1
        mov     rcx, [rsp+.i]
        inc     rcx
        mov     [rsp+.i], rcx
    jmp     .for2
.end_for2

;       compute P from p

;for(i = 0; i<=n; i++)
;{
        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
.for3
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for3
;    double element = p[i];
    mov    rax, [rsp+.p]
    mov    r8, [rsp+.P]
    movsd    xmm0, [rax+8*rcx]
;    P[i] = element/i;nn
    cvtsi2sd    [r8+8*rcx], xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for3
;}

.end_for3

;       compute and print p(a) and p(b)

movsd    xmm0, [rsp+.a]
mov    rax, [rsp+.p]
movsd    xmm1, [rax]
movsd    xmm2, 0

;for(i=0; i<=n; i++)
;{

        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
.for4
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for4

;    mul = a * p[i];

    movsd    xmm1, [rax+8*rcx]
    mulsd    xmm1, xmm0

;    acc += mul;

    addsd    xmm2, xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for4
;}
.end_for4

    segment .data
.a_out    db    "p(a): %lf", 0

    lea    rdi, [.a_out]
    movsd    xmm0, xmm2
    xor    eax, eax
    call    printf

movsd    xmm0, [rsp+.b]
mov    rax, [rsp+.p]
movsd    xmm1, [rax]
movsd    xmm2, 0

;for(i=1; i<=n; i++)
;{

        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
.for5
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for5

;    mul = a * p[i];

    movsd    xmm1, [rax+8*rcx]
    mulsd    xmm1, xmm0

;    acc += mul;

    addsd    xmm2, xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for5
;}
.end_for5

    segment .data
.b_out    db    "p(b): %lf", 0

    segment    .text
    lea    rdi, [.b_out]
    movsd    xmm0, xmm2
    xor    eax, eax
    call    printf

;       compute and print pp(a) and pp(b)

movsd    xmm0, [rsp+.a]
mov    rax, [rsp+.pp]
movsd    xmm1, [rax]
movsd    xmm2, 0

;for(i=1; i<=n; i++)
;{

        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
    inc    rcx
.for6
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for6

;    mul = a * pp[i];

    movsd    xmm1, [rax+8*rcx]
    mulsd    xmm1, xmm0

;    acc += mul;

    addsd    xmm2, xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for6
;}
.end_for6

    segment .data
.da_out    db    "p'(a): %lf", 0

    segment .text
    lea    rdi, [.da_out]
    movsd    xmm0, xmm2
    xor    eax, eax
    call    printf

movsd    xmm0, [rsp+.b]
mov    rax, [rsp+.pp]
movsd    xmm1, [rax]
movsd    xmm2, 0

;for(i=0; i<n; i++)
;{

        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
.for7
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n 
        jg      .end_for7

;    mul = b * p'[i];

    movsd    xmm1, [rax+8*rcx]
    mulsd    xmm1, xmm0

;    acc += mul;

    addsd    xmm2, xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for7
;}
.end_for7

    segment .data
.db_out    db    "p'(b): %lf", 0

    segment    .text
    lea    rdi, [.b_out]
    movsd    xmm0, xmm2
    xor    eax, eax
    call    printf

;       compute and print p(b)-p(a)

movsd    xmm0, [rsp+.b]
movsd    xmm4, [rsp+.a]
mov    rax, [rsp+.P]
movsd    xmm2, 0
movsd    xmm5, 0

;for(i=0; i<n; i++)
;{

        xor     ecx, ecx                ; i = 0
        mov     [rsp+.i], rcx
    inc    rcx
.for8
    mov     rdx, [rsp+.n]
        cmp     rcx, rdx                ; i <= n ?
        jg      .end_for7

;    mul = b * P[i];

    movsd    xmm1, [rax+8*rcx]
    mulsd    xmm1, xmm0

;    acc += mul;

    addsd    xmm2, xmm1
    mov    rcx, [rsp+.i]
    inc    rcx
    mov    [rsp+.i], rcx
    jmp    .for8
;}
.end_for8

    segment .data
.P_out    db    "P(b) - P(a): %lf", 0

    segment    .text
    lea    rdi, [.P_out]
    movsd    xmm0, xmm2
    xor    eax, eax
    call    printf


        leave
        ret

create:
        push    rbp
        mov     rbp, rsp
        shl     rdi, 3
        call    malloc
        leave
        ret

Ah, this is quite lovely, is it not?

Woeful lack of annotation and hard to read.
 
Woeful lack of annotation and hard to read.

How is assembly supposed to be easy to read? You could write C code and literally compile it line by line like the most idiotic compiler in the universe, but there'd be no point in writing assembly if you did so. If you look later on at the parts I wrote, however, that's actually what I'm doing, being a very, very stupid C compiler. The most unintelligible, unnannotated parts are actually the work of my esteemed professor, who enjoys writing in that way in order to torture us. For this project, his starter code was actually better annotated than usual. Also, some parts of my code follow in his lead, because I tortuously read through his code to understand it and thought I may as well write in that manner.

I am not seriously recommending that anyone read this. That would be embarrassing. This is terrible, fucking terrible, horrible code.

I fucking hate assembly, it's a pointless language that no one should ever write in or learn unless they have a gun pointed to their head. Besides in a few practical applications, even an excellent assembly programmer is usually going to write slower code than a C compiler would spit out, because modern compilers are just that damn good. And it takes much more work to write. Why on Earth? Why would you do so much work to be slower?
 
Last edited:
I think that the most embarrassing code I ever wrote was a Java class. I was rushed, so I didn't test it. Whether or not one method or another was called actually depended on whether or not you passed it an integer (3) or a double (3.0). I also didn't do another half of the project.

I got a 75 on it. Still not sure about how that worked, since I didn't do half of it, and the other half was a load of shit.
 
How is assembly supposed to be easy to read? You could write C code and literally compile it line by line like the most idiotic compiler in the universe, but there'd be no point in writing assembly if you did so. If you look later on at the parts I wrote, however, that's actually what I'm doing. The parts that are unintelligible are actually the work of my esteemed professor, who enjoys writing in that way in order to torture us. Also, some parts of my code follow in his lead, because I tortuously read through his code to understand it and thought I may as well write in that manner.

I am not seriously recommending that anyone read this. That would be embarrassing. This is terrible, fucking terrible, horrible code*. Where it is readable, it is slow, where it isn't slow, it's unreadable. That's the only way assembly code works. I fucking hate assembly, it's a pointless language that no one should ever write in or learn unless they have a gun pointed to their head. Besides in a few practical applications, even an excellent assembly programmer is usually going to write slower code than a C compiler would spit out. And it takes much more work to write. Why on Earth? Why would you do so much work to be slower?

* It doesn't even fucking work right now, I haven't tested it, there's no point in reading it you idiot, that's not the fucking point.

I wrote reams of assembler code back in the day, it was used primarily as memory was vastly more expensive in those days and processors were a hell of a lot slower. It doesn't matter what language you are using, from the point of view of maintenance, it is always good practice to document fully including comments that describe what the code is doing.

I believe that drivers and interrupt handlers are still written using assembler as the code is much more optimised than even the best C compilers are capable of doing.
 
I wrote reams of assembler code back in the day, it was used primarily as memory was vastly more expensive in those days and processors were a hell of a lot slower.

And today, because compilers are so good, assembly code is usually slower and takes more memory. Back in the day, compilers weren't as good as they are now, and assembly was a lot faster than anything high-language. Today, assembly only has a few niches, because it's entirely unreadable on its own and only actually faster than compilers with a great deal of work that could better be spent elsewhere.

It doesn't matter what language you are using, from the point of view of maintenance, it is always good practice to document fully including comments that describe what the code is doing.

Pro-tip: this code isn't going to be maintained. If I were writing something that I intended someone to look at, it wouldn't be so stupid. If you can read actually read it, I'm going to delete it. If you could actually understand it, it's so dumb that I think it could make a meme. In this way, it's lack of annotation is a good thing. It is a shield, my friend.
 
I think that the most embarrassing code I ever wrote was a Java class. I was rushed, so I didn't test it. Whether or not one method or another was called actually depended on whether or not you passed it an integer (3) or a double (3.0). I also didn't do another half of the project.

I got a 75 on it. Still not sure about how that worked, since I didn't do half of it, and the other half was a load of shit.
Obviously the prof was concerned about your lack of self esteem.
 
And today, because compilers are so good, assembly code is usually slower and takes more memory. Back in the day, compilers weren't as good as they are now, and assembly was a lot faster than anything high-language. Today, assembly only has a few niches, because it's entirely unreadable on its own and only actually faster than compilers with a great deal of work that could better be spent elsewhere.



Pro-tip: this code isn't going to be maintained. If I were writing something that I intended someone to look at, it wouldn't be so stupid. If you can read actually read it, I'm going to delete it. If you could actually understand it, it's so dumb that I think it could make a meme. In this way, it's lack of annotation is a good thing. It is a shield, my friend.

Many compilers actually convert the code into assembler anyway, hence if assembler is written well and optimised then it's hard to see how a high level compiler can be better.
 
Many compilers actually convert the code into assembler anyway, hence if assembler is written well and optimised then it's hard to see how a high level compiler can be better.

The only compilers that don't compile into assembly would probably be compilers that compile to some kind of runtime environment bytecode, Java being the most famous example. Of course, bytecode is really assembly for the Java environment, and the JVE does the actual compiling to assembly on the fly. Deep down assembly is really just a more readable version of machine code, with perhaps a couple of shortcuts added in.

Anyway, modern compilers are often able to beat hand-made assembly because they apply very good optimizations. Of course, theoretically, you could beat the compiler. It's just that today the compilers are so damn good that it's usually not worth the effort - there are better uses of your time. Talk to anyone who has experience in modern assembly programming, and they would probably agree with me on this.

Also, this might not be as true for fancy microprocessor architectures that don't have compilers as advanced as the ones available for x86.
 
That's what I found many moons ago. I majored in Chemistry but after one job in the big bad world working for Ciba Geigy, I moved into the IT industry.
I think someone with a Chemistry degree will do well in the real world cause it is one of the hardest degrees to earn and a person who can earn such a degree obviously has ability. It's just there's not a big market for Chemist. One things for sure. If you can learn Chemistry you can learn just about anything.
 
I wrote reams of assembler code back in the day, it was used primarily as memory was vastly more expensive in those days and processors were a hell of a lot slower. It doesn't matter what language you are using, from the point of view of maintenance, it is always good practice to document fully including comments that describe what the code is doing.

I believe that drivers and interrupt handlers are still written using assembler as the code is much more optimised than even the best C compilers are capable of doing.

People who understand computers are a strange breed indeed. Listening to them is rather like listening to the guy who can name every player who has ever played for Leicester City, his previous half dozen clubs, his transfer fees and the name of his bloody dog.
I respect such depths of knowledge but prefer to stand at the other end of the bar.
I did learn Basic many years ago and felt second only to whoever it was that invented the thing. The first computer I ever used was the size of a fridge and consumed paper tape punched with little holes. A roll of diameter about two inches would hold enough information to make the golf ball in the corner clack out 'H E L L O'.
I also learned how to adjust a carburetta. Neither skill has been worth a dime since.
 
Better to build shit then play with test tubes.
If by that you mean you can make more as a production or R/D chemist then as an analytical chemist. I agree. As a production chemist I enjoyed my collaboration with engineerss. I would figure out the process and steps to manufacture a product and they would build it. I made good money doing that.

I worked as an analytical/qa/qc chemist the first two years of my career and didn't make shit. Analytical chemistry, as a field, sucks, and I know this will sound sexist, cause a lot of women with chemistry degrees graduate from college, start a family and then in their 30's go back to work and will work for half of what most men would accept. It completely undermined that job market.

When I worked for a analytical lab I was one of two men working with 8 women. With in two years both of us were gone to better paying jobs but the ladies all stayed. I never understood their attitudes. This was the late 80's and the job paid $10/hour and I didn't go to college and earn a science degree to make $10/hour. I walked after 18 months when they refused to give me a raise. Took a job with an environmental company making $18.50/hr (this was 1987) entry level. Some of those gals had been there for years making shit wages and most had went to good schools too. OSU, Miami, UD, Otterbein, etc. I never understood it.
 
People who understand computers are a strange breed indeed. Listening to them is rather like listening to the guy who can name every player who has ever played for Leicester City, his previous half dozen clubs, his transfer fees and the name of his bloody dog.
I respect such depths of knowledge but prefer to stand at the other end of the bar.
I did learn Basic many years ago and felt second only to whoever it was that invented the thing. The first computer I ever used was the size of a fridge and consumed paper tape punched with little holes. A roll of diameter about two inches would hold enough information to make the golf ball in the corner clack out 'H E L L O'.
I also learned how to adjust a carburetta. Neither skill has been worth a dime since.
LOL Basic was the only computer language I ever learned too. I bought my first PC back in 1980 and I'm pretty sure I was the only person in town who owned a PC. It was a Sinclair 1000 with 1K of ram. It was about the size of a modern lap top and had a keyboard. You had to hook it up to a tv and could do some basic programing. I figured out real quick that programming was not my thing. It's like accounting. To freaken tedious for me. I'm to hyperactive too. I couldn't stand to sit down for that long.
 
If by that you mean you can make more as a production or R/D chemist then as an analytical chemist. I agree. As a production chemist I enjoyed my collaboration with engineerss. I would figure out the process and steps to manufacture a product and they would build it. I made good money doing that.

I worked as an analytical/qa/qc chemist the first two years of my career and didn't make shit. Analytical chemistry, as a field, sucks, and I know this will sound sexist, cause a lot of women with chemistry degrees graduate from college, start a family and then in their 30's go back to work and will work for half of what most men would accept. It completely undermined that job market.

When I worked for a analytical lab I was one of two men working with 8 women. With in two years both of us were gone to better paying jobs but the ladies all stayed. I never understood their attitudes. This was the late 80's and the job paid $10/hour and I didn't go to college and earn a science degree to make $10/hour. I walked after 18 months when they refused to give me a raise. Took a job with an environmental company making $18.50/hr (this was 1987) entry level. Some of those gals had been there for years making shit wages and most had went to good schools too. OSU, Miami, UD, Otterbein, etc. I never understood it.
I know a gal with a PhD in biochem and the most she made in that field was $45k. I don't understand it either. She was out of work for nearly two years before she gave up and is now selling BMWs.
 
I know a gal with a PhD in biochem and the most she made in that field was $45k. I don't understand it either. She was out of work for nearly two years before she gave up and is now selling BMWs.
That's why I'm glad I majored in Biology instead of chemistry. I have a lot more options. If the environmental field hadn't of panned out for me I could always go into the medical technology field.
 
Oh yeah, and drivers are definitely still written in assembly. As I said, it still has applications. It's just not really something used for general development, for a number of reasons. I just thought it was interesting that one of those reasons was that it's often slower, due to the difficulty of developing to the level of optimizations provided by modern compilers. I suppose it isn't as surprising with C as it would be with other languages - C is fast because, deep down, it actually mirrors the hardware of the most common type of computer, the Von Neumann machine, very well. You're unlikely to come across a faster language for this kind of hardware.
 
People who understand computers are a strange breed indeed. Listening to them is rather like listening to the guy who can name every player who has ever played for Leicester City, his previous half dozen clubs, his transfer fees and the name of his bloody dog.
I respect such depths of knowledge but prefer to stand at the other end of the bar.

Except I'm going to use this to make money, whereas that guy's knowledge is mostly trivial. Plus I kind of suck at it.

If you would like subjects for which I have no excuse for having a disturbing amount of knowledge on, that would ancient Chinese history, electoral methods, and microtonal music theory.
 
I know a gal with a PhD in biochem and the most she made in that field was $45k. I don't understand it either. She was out of work for nearly two years before she gave up and is now selling BMWs.

It might've been the PhD, actually. I think a PhD scares a lot of employers, oddly making the person less employable.
 
Back
Top