Learn some assembly

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Learn some assembly

Post by nikki »

The other day, I decided to learn some assembly programming for the '1337 factor'. It turned out to be pretty cool actually. It taught me quite a bit about registers, memory models (segmented, protected etc.), hex-binary relationship, interrupts, crt0, linking, compiling, loads of other stuff. If you have some free time, I'd suggest you check it out. I'd been programming in high-level languages for quite a while, but never really got 'how it all happened'. I just typed some stuff in an IDE or text editor and hit the 'compile' button. :D However, assembly showed me the wall, where software ends and hardware takes over. End of abstraction.

If you have some free time consider checking it out. Of course, you might have gone through a computer science course or something and learnt it, I'm just learning it as a hobby and so may be I'm not taking the straight road. :)

I'm considering going into the electronic logic-gate stuff soon. This is like one of those 'Aha!' moments, like when I understood how quaternions really work. :)
Last edited by nikki on Thu Feb 12, 2009 10:58 pm, edited 1 time in total.
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58
Contact:

Re: Learn some assembly

Post by betajaen »

I was taught x86 assembly in college, and learned the golden rule that nearly 90% of all assembly consists of "MOV". Later I had a go at 68k assembly on the Amiga, and found it to be much harder due to the co-processors and graphics/audio/disk chips.

Assembly has it's place and all, but in modern times with Python and Ruby even C is looking outdated.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Learn some assembly

Post by lonewolff »

Reminds me of a thread I started back in 2006 (Geez was it that long ago? :roll: )

http://www.ogre3d.org/forums/viewtopic. ... f+assembly

In my first post I gave an example of how to display 'Hello World' in a console in just 24 bytes - not Gigabytes, Megabytes, or even Kilobytes.... Just bytes.

I just tested this under Windows 7 (beta) and it still works! :lol:
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Learn some assembly

Post by Kojack »

Later I had a go at 68k assembly on the Amiga, and found it to be much harder due to the co-processors and graphics/audio/disk chips.
I found 68k far easier than x86. No segment register crap, just a flat 32 bit address space (although only about 24 of those bits worked on the early chips). No freaky named registers with specific uses, just eight 32 bit data and 8 32 bit address registers (and you could use data for addressing and addresses for data, it just had a clock cycle hit).

My assembly order was: 6502 (on a Rockwell Aim65), 6510 (friend's C64. 99.99999% identical to 6502), 6809e (trs80 colour computer 2), 68000 (amiga), Z80 (controller board at college), X86 (also at college), ARM7.

I think all coders should use assembly at some point, it really helps you understand why languages are designed the way they are, and how a computer really works.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Learn some assembly

Post by lonewolff »

I agree. :D

I also started out on 6502 & 6510. The differences were so minimal that I don't even know what the differences were, to he honest. :lol:
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Learn some assembly

Post by Kojack »

I think the 6510 (C64) had a higher clock speed than the 6502 (Vic20). Everybody referred to them both as 6502 though.
:)
Wikipedia time...
Ok, the true difference: the 6510 had an 8 bit io port and a tristate address bus. Otherwise they were the same.
User avatar
volca
Gnome
Posts: 393
Joined: Thu Dec 08, 2005 9:57 pm
x 1
Contact:

Re: Learn some assembly

Post by volca »

Kojack wrote:I think all coders should use assembly at some point, it really helps you understand why languages are designed the way they are, and how a computer really works.
These words should be golden and way bigger. Especially when you care about performance and stability you should really know what the language is doing behind the scenes :)
Image
reptor
Ogre Magi
Posts: 1120
Joined: Wed Nov 15, 2006 7:41 pm
Location: Finland
x 5

Re: Learn some assembly

Post by reptor »

First Linux, then assembly... what next?
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Learn some assembly

Post by jacmoe »

JustBoo wrote:I'll skip the inevitable story of how previously we used rocks and sticks for ones and zero's and an interrupt meant clubbing the guy next to you with a bunch of bits.
:lol:

I used assembly for going into modeX, blitting, polling mouse and keyboard and stuff like that.
Was happy when MS finally made a working DirectX.
No way I'm going back. :)
I agree, though, that programmers ought to know how a processor works.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
jjp
Silver Sponsor
Silver Sponsor
Posts: 597
Joined: Sun Jan 07, 2007 11:55 pm
Location: Cologne, Germany
Contact:

Re: Learn some assembly

Post by jjp »

When one has to do programming with a very strong focus on performance it is useful to be able to read what the compiler produces. Coupled with an understanding of computer architecture on a high level. But I don't subscribe to "every programmer should know some assembly". There are lots of application domains where assembler knowledge does basically nothing for you. And that is a good thing.
Enough is never enough.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Learn some assembly

Post by Kojack »

I bought a $100 assembly cartridge, ah the Good Times
Hehe, I have that. :)
Edtasm+ was cool. I got it on special with the book "TRS-80 Color Computer Assembly Language Programming".
The book is beside me on a shelf, between C How To Program and Amiga Basic.
I need to update my shelves some time.

What the hell? I've got a Mad magazine on my shelf. Where did that come from? I haven't read one for at least a decade, and I didn't really like them then either. What other weird stuff is on here?
- 1984 guiness book of records
- my mother's highschool maths and chemistry textbooks
- a bunch of d&d books (last used about 18 years ago)
- the 1983 doctor who technical manual (hmm, could be handy if I want to model a tardis)
- a return of the jedi / pauls ice cream competition poster (wtf?)
- my old graph book filled with my attempts at sprite art (from the trs-80 days. I should scan some of this.)
- a hundred or so copies of The Elite Against All Odds military magazines (unread, I just looked for cool helicopter pictures)
And a ton more crap.

Ok, sorry, back to assembly. :)
But I don't subscribe to "every programmer should know some assembly"
Then you really aren't going to like it when I say every programmer should know some digital electronics (it makes assembly language make more sense). :)

ARM assembly is pretty freaky. It's both big and little endian at the same time. If you read a 16 or 32 bit number from an even address, it's one style (can't remember which), if you read from an odd address it goes backwards in memory and reads it as the opposite style. If you jump to an even address, it runs in 16 bit Thumb mode, if you jump to an odd address it masks it to an even address and switches to 32 bit ARM mode. This made it very easy to mix arm and thumb code in one program, no explicit mode change code is needed, the address of a function determines which mode to use to execute it. ANother cool feature is that almost all instructions can have conditional execution. They can test (and even change) the condition flags, the instruction only executes if the flags match the requested state.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Learn some assembly

Post by lonewolff »

Kojack wrote:Then you really aren't going to like it when I say every programmer should know some digital electronics (it makes assembly language make more sense). :)
I could not agree more! :wink:

I did Electronics for three years at Melbourne's (Australia) leading university (toppped every year, too 8) ). I learnt assembly (or should say I was taught - coz I knew assembly before then) on Zilog Z80 (similar in many ways to 6502/10). Looking at the circuit diagrams for this device made perfect sense of what was going on in the programming side. I/O ports were on a ceratin address and you could look at the circuit diagram and behold, the actuall physical port was tied to those same address lines.

All you had to do was send a byte or two to that address and the port would do something and the circuit diagram would agree that this should happen. It was awesome.

I even started designing my own computer at circuit level. I was going to make a console. 8)

But, like everything else - got put on the back burner and never got done. :roll:
jjp
Silver Sponsor
Silver Sponsor
Posts: 597
Joined: Sun Jan 07, 2007 11:55 pm
Location: Cologne, Germany
Contact:

Re: Learn some assembly

Post by jjp »

Kojack wrote:Then you really aren't going to like it when I say every programmer should know some digital electronics (it makes assembly language make more sense). :)
No, I couldn't agree less ;) I am 100% positive you can program great software without knowing any of it, and there is more valuable stuff to practice and learn for most people. It doesn't hurt to to know some of it and someone who consideres himself a well-educated computer scientist probably should know a bit of that field. But not every programmer and software designer.
Enough is never enough.
User avatar
spookyboo
Silver Sponsor
Silver Sponsor
Posts: 1141
Joined: Tue Jul 06, 2004 5:57 am
x 151
Contact:

Re: Learn some assembly

Post by spookyboo »

Ha, this brings back some memories. I still hear the cassette tape sound that was hooked up on my Zx spectrum. Seeing the yellow/blue and red/blue lines on the screen and waiting for the start of Jet Set Willy. My first assembly attempts where on the Z80a. Man, what a vingertwister that machine was, with those small rubber keys and something like 5 different functions under one key, while pressing some other keys at the same time.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Learn some assembly

Post by Kojack »

I did Electronics for three years at Melbourne's (Australia) leading university (toppped every year, too 8) ).
Hehe, I did the same at TAFE (for overseas people, TAFE is a country wide government run college system, kind of).
We used Z80 chips on an open board (led display, hex keypad), and hooked oscilloscopes up to the cpu pins to watch the signals. We also had a class on designing and implementing our own 4bit cpu using just logic gates in a circuit simulator program.

Damn that was fun (even though I already knew 3 kinds of assembly language by then).

No, I couldn't agree less ;) I am 100% positive you can program great software without knowing any of it
True, for most high level code (try doing hardware drivers, firmware or similar without assembly knowledge or digital theory). But it's kind of sad that so many otherwise skilled programmers have no more knowledge of how their software is actually executed by a pc than a secretary has knowledge of how Office is executed.

From personal experience, learning assembly increased my ability to quickly pick up other languages and understand performance limitations, by providing the common building blocks which all languages eventually become.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Learn some assembly

Post by nikki »

Asembly is fun. I just wrote a fibonacci sequence thing. It's hella fast. The only problem is overflow. That should be easy though. Use more than one register, add the carry flag. I believe there's already an 'adc' instruction for that.

gcc inline asm sucks though (AT&T syntax? Yuck!).

EDIT: Seems inline intel is possible:-

Code: Select all

#include <string.h>

char str[] = "This is from ASM!\n";
int len;

int main()
{
    len = strlen(str);

    asm (
            ".intel_syntax noprefix\n"
            "mov eax,4\n"
            "mov ebx,1\n"
            "mov ecx,offset str\n"
            "mov edx,len\n"
            "int 0x80\n"
            ".att_syntax\n"
        );

    return 0;
}
User avatar
Karan
Halfling
Posts: 62
Joined: Wed Sep 17, 2008 1:59 pm
Location: Berlin, Germany

Re: Learn some assembly

Post by Karan »

When you want to understand how a program is executed it is also important to know something about compiler construction I think. Assembly and digital electronics is all fine, but there is already SO much going on between your programming language and the assembly code (especially the many many possible optimizations). Just had a lecture with a little project about it :)
reptor
Ogre Magi
Posts: 1120
Joined: Wed Nov 15, 2006 7:41 pm
Location: Finland
x 5

Re: Learn some assembly

Post by reptor »

I did some assembly learning using NASM on Linux and on FreeBSD.

It was fun to learn some basics but I did quickly understand that I am probably never going to create a useful program using just assembly. I'm not interested in programming the sort of devices which would need assembly programming.

On desktop computers I can get away with using C/C++ for performance-critical applications. The size isn't a problem.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Learn some assembly

Post by nikki »

reptor wrote:It was fun to learn some basics but I did quickly understand that I am probably never going to create a useful program using just assembly. I'm not interested in programming the sort of devices which would need assembly programming.
Of course not. Sure is a great way to understand the foundations though. Some times you can also inline some assembly into your C++ for quick hax. :roll:
reptor
Ogre Magi
Posts: 1120
Joined: Wed Nov 15, 2006 7:41 pm
Location: Finland
x 5

Re: Learn some assembly

Post by reptor »

Yes.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Learn some assembly

Post by jacmoe »

nikki wrote:Some times you can also inline some assembly into your C++ for quick hax. :roll:
I trust my optimising compiler more than I trust hand-written inline assembly. :wink:

<edit>
But that doesn't rule out the *usefullness* of learning a bit of assembly magic. For educational purposes. And fun.
</edit>
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Nauk
Gnoll
Posts: 653
Joined: Thu May 11, 2006 9:12 pm
Location: Bavaria
x 36
Contact:

Re: Learn some assembly

Post by Nauk »

Oh man, C64, Amiga & Co, I just this week talked with a friend about how much it sucked the Amiga universe stopped thanks to Commodores brilliant mis-management back then and from that point on everyone was stuck in the 10 years behind in time Microsoft universe with 16 lousy colours and no multitasking at all - I so loved that handsigned machine with the bootable OS :). More on the nostalgia flash, I also remember typing gazillion of pages of basic data / assembly into my C64 from computer magazines, THE horror when you had a typo after 1 week of work and couldn't find it and the game wouldn't run - meh ~v~.

Back on topic I too started brushing up on assembly again not long ago, I think it can come in quite handy in certain situations, and I always found it intriguing how much you can do with how little code. :)
User avatar
Nauk
Gnoll
Posts: 653
Joined: Thu May 11, 2006 9:12 pm
Location: Bavaria
x 36
Contact:

Re: Learn some assembly

Post by Nauk »

Oh and more derail: ZX81 anyone with the whooping 1024 bytes ram including the video memory? :D
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Re: Learn some assembly

Post by nullsquared »

I think assembly is useless unless you've mastered it and a profiler shows you that some particular routine is guaranteed to be slow unless you manually code up an assembly version of it. I put my trust in good programming practices combined with an optimizing compiler.

Edit: Apparently jacmoe had the same idea before me :D
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Learn some assembly

Post by nikki »

You gotta just be clever then. ;)

Anyway, I was stressing more on the learning and fun aspects.
Post Reply