Assembly

Started by
10 comments, last by LessBread 17 years, 3 months ago
Hiya I was wondering if anyone who knew about assmebly could give me some advice. I want to see how a computer works in depth, right up to BIOS and how the computer works that (this isn't for any crackpot scheme like writing an OS btw). So would it be advantagous for me to try to learn assmebly (I know its no easy task, and I probably would be struggling to get the basics for about a year) and if so is there any decent IDE, tutorials or books you could direct me to? Any help would be much appreciated.
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Advertisement
Check out The Art of Assembly.

As for what you can do with it, unless you're working in your own OS, or making device drivers, you will be limited on what exactly you can and cannot do.

As for advantages, as you stated, you'll learn how the computer works. You learn certain things about instruction order, and memory alignment, and other tricks that may not seem straightforward, but the computer needs to run in order to not crash.

As far as adventageous, perhaps if you have a bare understanding, but I really don't think that you really need to have a full blown grasp on the subject unless you're working on machines with limited memory or speed. It's good to get an understanding, but past that, that's why there are high level languages and optimizing compilers.
I'd say learning assembly gave me a bit more insight into how the computer works - I did indeed tie it into a crackpot scheme of writing an OS (but with rather more realistic expectations than the term implies (and I actually got something that was quite rewarding for what little there was of it) - there is actually a small hobbyist community writing their own OSes, but it tends to become bogged down in theory rather than actual results) as the aim was to learn a little more about how it all fitted together.

A few handy resources:
The Intel IA-32 architecture software development manuals are invaluable if you are really aiming to gain an insight into how everything works at a low level - plus they provide a list of opcodes with mnemonics alongside (once used this to hand compile an exceptionally small project in C - very educational, although utterly futile).

The Art of Assembly (e-book) is highly recommended, although I only skimmed it. Counterintuitively, you're probably looking for the DOS/16-bit version at first.

A good debugger can also come in handy - I recommend OllyDbg.

I personally use NASM (Netwide ASeMbler) [and it's counterpart the NASM disassembler) - one thing to remember here is that if you compile to a 32-bit PE executable, you want to skip the first 60 bytes when disassembling because that's just the header (check the -h message to see what the argument is - I forget, sorry).

I wouldn't go so far as to say you'll be struggling with the basics for a year - the basics are pretty simple, really, assuming you've got a decent reference for the mnemonics that you haven't remembered. I spent a few full (~8 hours) days at it, straight, and had moderately unimpressive results at the end - but they worked.

EDIT: Too slow by several minutes.
[TheUnbeliever]
Also: learning assembly is just 20% of your goal. Understanding what an OS does is learned by studying an OS... I learned a lot from books that describe the kernels. Just pick up a book about the kernel of some *NIX or Windows (even very old versions).

Cheers
Thanks a lot.

Would I go about learning assembly like I did C++, making little applications like "Hello world", a calculator, or would that be considerably harder and so should I try to concentrate on the theory?

EDIT: @TheUnbeliever: That sounds very interesting can you please give me a link?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Quote:Original post by Peter Conn
Thanks a lot.

Would I go about learning assembly like I did C++, making little applications like "Hello world", a calculator, or would that be considerably harder and so should I try to concentrate on the theory?


It's not that much harder, to be honest. I would continue with little programs - don't waste time trying to do huge things in assembly language though. I'd say it's best to understand the idea of how things work - so even a little program that has a couple functions that adhere to, say, the C calling convention and make a little program to print the fibonacci sequence.

Another thing that's occured to me as being genuinely invaluable is Ralf Brown's Interrupt List (RBIL). It details the various functions accessible from various interrupts under different environments. Under Windows, a handy interrupt to look at is INT21 - various DOS subsystem functions are here (such as printing text to the console).

EDIT:
Quote:EDIT: @TheUnbeliever: That sounds very interesting can you please give me a link?


To what, sorry?
[TheUnbeliever]
I find the best way to learn how something works is to go about and play at its lowest level. Instead of working with x86 architecture, you could try picking up a uC or an HC11. Both have compilers available to them for your standard langauges, and both can be very easily programmed in machine code. It may cost more to go about this as you need to buy the hardware, but you learn first hand how to work with registers, interrupts, etc.

Have Fun!
Thanks again, I mean to the community based on making OSs.
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Quote:Original post by Peter Conn
Thanks again, I mean to the community based on making OSs.


www.osdev.org - It's pretty tiny, and not anything like as professionally conducted as most discussions here, although there are a few people that migrated from an older community (which I was part of and merged with OSDev.org) that really know their stuff.
www.osdever.net - Doesn't seem to have been updated in a while, but has a fair few handy resources to start you off. Of course, OS development is not really a tutorial sort of thing - as I said, a large part of the discussion is in theoretical terms (although there are a couple who are quite far along with implementations of these)
[TheUnbeliever]
Well after learning VB,C,C++,Java,etc I finally got around to learning assembly not long ago by reading Peter Norton's guide to assembly language.
It took me about 2 weeks to get through book so I don't think it'll take you a year to get the basics.
The book doesn't cover everything about ASM but walks you through building a fairly large disk editing program in assembly by the end of the book so you'll have pretty good idea what it's all about.
I also have a newer assembly language book by kip irvine that covers 32bit asm and even windows programming with assembly and just started working on it and it looks like there has been quite a bit of changes since 16bit asm which is what the norton book used.
Looking back assembly is quite helpful now that it makes programming in C or just about any other language look so easy in comparison!
I mean here is an example program I wrote in assembly using Norton book that prints the letters a to j on the screen:
.model small.codeprint_a_j proc  mov dl,'A'       ;Start with the A character  mov cx,10        ;Print 10 characters, starting with Aprint_loop:  call write_char  ; Print the character  inc dl           ;Move to the next char in the alphabet  loop print_loop  ;Continue for 10 characters  int 20h          ;return to DOSprint_a_j endpwrite_char proc  mov ah,2         ;set function code for character output  int 21h          ;print the character already in dl  retwrite_char endpend print_a_j      ;need to designate main procedure

Notice unlike C or just about any other language there isn't even a builtin function like cout,printf,etc to print even a single character to the screen you have to do it all yourself!
But once you get the basics down it's not to bad since once you learn how to print a character to the screen then you can figure out how to print an entire string to the screen by checking for 0 at end or whatever you want to use
	PUBLIC WRITE_STRING;-----------------------------------------------------------------------;; This procedure writes a string of characters to the screen. The       ;; string must end with 	DB 0											;;																		;; On Entry:	DS:DX	Address of string									;; Uses:		WRITE_CHAR													;;-----------------------------------------------------------------------;WRITE_STRING	PROC	PUSH	AX	PUSH	DX	PUSH	SI	PUSHF				;Save direction flag	CLD					;Set direction flag forward	MOV	SI,DX			;Place address into SI for LODSBSTRING_LOOP:	LODSB				;Get char into AL register	OR	AL,AL			;Have we found 0 yet?	JZ	END_OF_STRING	;Yes, we are done with string	MOV	DL,AL			;No, write char	CALL    WRITE_CHAR	JMP	STRING_LOOPEND_OF_STRING:	POPF				;Restore direction flag	POP	SI	POP	DX	POP	AX	RETWRITE_STRING	ENDP

So after you get all your basic input/output functions built put them into separate asm file and link them with your main asm file and away you go!
So all this work pays off in end helping you appreciate how much work <iostream> if you using C++ or <stdio.h> if you are using C is doing for you when you write programs using higher level languages.
And if you care more for the hardware aspects of how computers work I suggest getting this to work on although if you start on it before learning assembly you might have a rough time when you get to actually trying to communicate with the device you built since it has to be done in assembly!

p.s. If you have great attention to detail and easily spot typos you should have no problem with assembly since you have to type so much just to get anything done 1 little typo is a pain in the butt to find. For example the book I mentioned have a typo where they had bx instead of [bx] and it took me about an hour to figure out why my program wasn't working. Oh and the bad thing with assembly is that just about everything is a pointer and you are always dealing directly with memory addresses,etc and you know that can only lead to trouble since all them buffer overflow attacks you hear hackers using are due to a hard to catch pointer error. Even norton himself admitted to a hard to find bug in program he wrote in book mentioned due to fact the he typed a DW instead of DB in one line of the program.

[Edited by - daviangel on January 15, 2007 4:33:55 PM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement