Computer Science Canada

So confused about assembly?

Author:  bill_ion_boi [ Thu Jul 14, 2005 1:57 pm ]
Post subject:  So confused about assembly?

I have started looking up on assembly and have becoem very confused, coould anyone spare a minute to help please?
1- There are assembly tutorials for programming on linux and others for programming on windows, so my question is "What kind of assembly is used in the source code for simple operating systems i see once in a while on the net?"
2-I've seen assembly for intel cpus, so does that mean there are different assembly languages for different cpus or is it that they are all the same languages just programmed in a different manner for different cpus?
3- In operatign systems if they use assembly, what type would they use because people could gave many types of cpus, (amd, intel etc)

then what is mine intel

Author:  wtd [ Thu Jul 14, 2005 2:23 pm ]
Post subject: 

Assembly is completely non-portable.

This is because each type of CPU has its own instruction set; its own language.

Prominent instructions sets include things like m68k, x86, PowerPC, Sparc, MIPS, ARM, etc.

But, even within these families, there are features only certain processors support.

Further, each operating system is coded differently, and so writing assembly programs, for instance for Windows on a 3.2GHz Pentium 4 is going to be rather different from writing assembly programs for Linux on a 3.2GHz Pentium 4.

A very small percentage of programmers work directly with assembly, and they're generally well-paid for their pain.

Assembly makes an exceedingly poor introduction to computer programming, especially on the scale you're talking about.

Author:  [Gandalf] [ Thu Jul 14, 2005 2:24 pm ]
Post subject:  Re: So confused about assembly?

bill_ion_boi wrote:
1- There are assembly tutorials for programming on linux and others for programming on windows, so my question is "What kind of assembly is used in the source code for simple operating systems i see once in a while on the net?"
2-I've seen assembly for intel cpus, so does that mean there are different assembly languages for different cpus or is it that they are all the same languages just programmed in a different manner for different cpus?
3- In operatign systems if they use assembly, what type would they use because people could gave many types of cpus, (amd, intel etc)

Well, the answer to all three of your questions is very similar. There are many variations of assembly, for each type of processor, mattering on where it's used. I don't really know much assembly, except 'push' and 'pop' but if you know one, I think it's safe to say that you can learn another in a much shorter time. If you want to program an operating system, you could probably do one for windows and maybe linux simply in C++. If you really want to make it on assembly though, then you will need to choose which processor its going to run on.
Disclaimer: Some of what I have told you could possibly be wrong Surprised

*edit* Ah, just listen to wtd, he knows best Smile.

Author:  md [ Thu Jul 14, 2005 7:24 pm ]
Post subject: 

amd and intel both produce chips which are based on the x86 instruction set. The real problem with assembly is not only that there are different instruction sets for different processors, but also that there are different assembler syntaxes for some processors.

Assembly, while very close to machine language, still needs to be compiled into machine code. This means that there are always going to be people who try and write an assembler that takes a different (supposedly better) syntax just because they can. Fortunalty in recent times syntax has become relatively standard so there isn't much to worry about.

If you are new to programming I'd recomending staying well away from assembly, but if you know what your getting into I'd recomend getting a MIPS emulator and learnings MIPS assembly. It's fairly strait forward, and you don't need to worry about things like operating systems if your writing for an emulated CPU as it's just the CPU and some memory. You should not try and write an assembly program for windows OR linux; it would be FAR to complex to be doable unless you have many years experience. A better plan is to write a program in say C or C++ (or even pascal, but it has to be a native language) and then rewrite some of your functions in assembly. That way you can deal with the OS in an easier high level language, but still do what you want in assembly.

Author:  wtd [ Thu Jul 14, 2005 7:29 pm ]
Post subject: 

Or try writing very simple C programs, and compiling them with GCC's "-S" option.

code:
$ cat > testasm.c
int main()
{

   return 0;
}
$ gcc -S testasm.c
$ ls testasm.*
testasm.c  testasm.s
$ cat testasm.s
        .file   "testasm.c"
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.3.5 (Debian 1:3.3.5-8ubuntu2)"

Author:  [Gandalf] [ Thu Jul 14, 2005 7:58 pm ]
Post subject: 

Yep, you can always just write a C or C++ program, and then convert it into assembly (not manually) and learn like that. Just look at the commented C code, and see how you get the assembly from there. Most of the simple assembly code is just overhead which, once you get past, will leave you with the actual code:
assembly:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movl    $0, %eax
        leave

Author:  wtd [ Thu Jul 14, 2005 9:30 pm ]
Post subject: 

It should be noted that the code I posted was using AT&T style syntax, rather than Intel style, which you'll see used by most who write it manually.

Author:  bill_ion_boi [ Fri Jul 15, 2005 10:51 am ]
Post subject: 

Wow your answers were very imformative, i was thinking of making a simple "helo world" kernel in assembly-i have seen some examples on the internet.
So how is it than windows runs on any cpu, is it because it is not written in assembly?-and if i were to write a kernel in assembly my best choice would be x86 seeing as intel and amd (the most popular personal cpus) are based on its instuction set?

Author:  md [ Fri Jul 15, 2005 1:41 pm ]
Post subject: 

windows doesn't run on any CPU it only runs on x86 based CPUs (amd, amd64, and intel x86 + pentium chips). There are other windows versions for specific processors (like windows server for itaniums) but these will only run on those specific CPUs and not on x86 CPUs.

As for what to write for; if you actually want to run it then you'll need to write it for a CPU you can emulate, or one you have. x86 is probably a good choice there because unless your using a mac that's what you're currently using Razz Jumping into writing a hello world kernel might be a lot to start off with, instead try writing functions in assembly, then work your way up.

Again; MIPS is a very easy system to learn on. Sure it's not intel, but there is less to worry about when you are just starting to learn assembly, and you can actually get MIPS based devices if you want to try your hand with some hardware once you get things working on a simulator (MIPS emulators are a dime a dozen, so you get your pick).

Author:  Martin [ Fri Jul 15, 2005 4:25 pm ]
Post subject: 

Assembly's not too tough when you get the hang of it. It's just...very sequential. It's neat to learn about how it all works on a hardware level (in CS251 we learnt how to impliment a simple multicycle CPU on the hardware side).

It's definitely not a necissary skill to have as a programmer, but it's nice to look into. Gives you a new appretiation for recursion.

Author:  wtd [ Fri Jul 15, 2005 4:41 pm ]
Post subject: 

Martin wrote:
Gives you a new appretiation for recursion.


So does Haskell. Wink


: