Other startpoint to programming with assembly

Develop freeBSD via assembly


  • Total voters
    5
I suggest you start with something simpler, like an Arduino. Then move on to a 6809, 6502 or 8051. 8 bit CPUs or microcontrollers are a lot simpler and thus easier to understand. The programming techniques are mostly the same, so once you've mastered 8 bit it's easy to move to 16 or 32 bit CPUs.

Very few people write code in assembler these days. Very little code (if there's any at all) of FreeBSD is assembler. The "problem" with assembly code is that you're limiting yourself to a single architecture. x86 code doesn't work on MIPS or ARM for example. Hence, FreeBSD is written in a higher level language; C.
 
Why?

There are just about no good reasons to write assembly. As SirDice already explained, there are many good reasons to not write assembly. And there are many reasons he didn't list. But there are still some very small and isolated places for assembly, and/or using very short hand-coded instruction sequences in programs written in higher-level languages.

Are there books about assembly programming? Zillions. I probably have a few myself, in boxes in the basement. They tend to be 30-40 years old; in those days, assembly programming was necessary, because the only alternative on computers hobbyists could afford (like Commodore 2001 or Apple II) was BASIC, which ran too slow, and used too much memory. Many of the books were not actually assembly programming books, but the reference manuals for the CPUs, which explained the architecture (registers, memory model, data flow) and the instruction set. I still have colleagues (seasoned software engineering and computer science professionals) who occasionally have to write a few assembly instructions, or reason about programs at the level of the hardware (for example for lock-free data structures). They don't have assembly programming books; they have processor reference manuals.

If you explain to us exactly why you want to do it, then we may be able to help. But the advice is probably going to be to not do it. If you really want to program at that level, you're probably better of using Verilog or "System C" (which in spite of the "C" in the name is not a programming language), because you should probably be pushing gates (or connections in a programmable chip). Or writing a few assembly instructions in the midst of a larger program (this occasionally happens in performance optimization, even today). Or, as SirDice said, do some low-level programming on an embedded device, like an Arduino or embedded system.
 
They tend to be 30-40 years old; in those days, assembly programming was necessary, because the only alternative on computers hobbyists could afford (like Commodore 2001 or Apple II) was BASIC, which ran too slow, and used too much memory. Many of the books were not actually assembly programming books, but the reference manuals for the CPUs, which explained the architecture (registers, memory model, data flow) and the instruction set.
Yep, that's how I got into assembly too. The C64 was nice, BASIC was fairly simple to learn. I wanted to write games and quickly found out BASIC wasn't fast enough. Read lots of magazines that had programs you could type in. A lot of them consisted of pages upon pages of just numbers. This turned out to be machine code. Never managed to write my own games but I did learn 6502 assembler. This involved many, many visits to the local library. At school I got to the 6809 (which is quite similar to the 6502). And in the meantime I bought an Amiga and thus moved to 68000 assembler to write demos. The switch from 8 bit to 32 bit was very easy. Once you get to understand how registers, program counters and logic works it doesn't really matter if it's 8, 16 or 32 bit. The opcodes may change (MOVE instead of LDA for example) and the registers are bigger (and you typically have more of them) but the basic principles are exactly the same.
 
Thanks, guys for your advises but whole I want book to explain how to programming FreeBSD with assembly line use system call and manipulating kernel and others things.
 
Don't know if it will of some use but in the FreeBSD code, you have some assembly codes in stand/i386/mbr, stand/i386/pmbr and stand/i386/btx among other places. These codes work in real mode which is the base of x86 processors. The last one (btx) is very interresting as it switches the processor in protected mode.

Reading theses codes, you have not only the startup of FreeBSD in BIOS legacy mode but also the essence of the x86 processors. It just lacks the 64 bits mode.

But it's maybe a little hard for a true beginner, I wonder. However, before to play music, it's a good idea to listen music, isn't it?

You have to note that these codes are written with the AT&T syntax used by as. There is also the Intel syntax which is used by numerous assemblers like NASM.

Last thing, if you want to understand these programs, you have to dig into what they do. I mean MBR and GPT schemes in these examples.
 
Don't know if it will of some use but in the FreeBSD code, you have some assembly codes in stand/i386/mbr, stand/i386/pmbr and stand/i386/btx among other places. These codes work in real mode which is the base of x86 processors. The last one (btx) is very interresting as it switches the processor in protected mode.

Reading theses codes, you have not only the startup of FreeBSD in BIOS legacy mode but also the essence of the x86 processors. It just lacks the 64 bits mode.

But it's maybe a little hard for a true beginner, I wonder. However, before to play music, it's a good idea to listen music, isn't it?

You have to note that these codes are written with the AT&T syntax used by as. There is also the Intel syntax which is used by numerous assemblers like NASM.

Last thing, if you want to understand these programs, you have to dig into what they do. I mean MBR and GPT schemes in these examples.
Thank you, i gonna I am going to read it.
 
For what it's worth, I program in assembly on 64-bit FreeBSD and prefer it over C.
The common advice you'll find on the web is to ignore assembly, but I personally ignore that advice.
Compilers are good; better than most humans even, but it's still damn fun IMHO.
Besides the boot loader pointed out above, understanding basic concepts first is where I'd start.
I'd personally start with userland first.

For instance, what is a stack and how do I manipulate it? That would be a good starting point.
Take:

Code:
push rdi

What does this do? It's two operations in one!

A reference to all the intel instructions; assuming you're on a compatible system is here:


Start with the below, compile w/ nasm, and add/remove instructions you read from the manual to learn:

Code:
BITS 64
DEFAULT rel

global _start:function

SECTION .text

_start:

    mov eax, 1    ; Syscall (exit)
    xor edi, edi  ; Return Value
    syscall

Change this. Push a value, call it 'x' onto the stack. Substract the value 'x' on the stack with another constant value 'x - 1'.
Pop the value form the stack back into rdi .... and exit again.

Tinckering(sp) and lots of reading will get ya there.
 
let s start by a book. A book is cool.

This one to start...
DLk4h7eW0AAPoir.jpg
 
Very little code (if there's any at all) of FreeBSD is assembler.
Last I read about this--and it's strange I remember--there are 4400 lines of assembly in the FreeBSD kernel.
There are just about no good reasons to write assembly.
Not on FreeBSD but I'm looking forward to writing assembly for a Linux driver in a few weeks.
Up to 15 years ago, I was actively writing assembly at work. I've done assembly from the 8085 through the 68020.
Assembly is fun! When you feel the need for speed. When you need direct control. I know I don't have to explain these things to you.

I want book to explain how to programming FreeBSD with assembly line use system call and manipulating kernel and others things.

FreeBSD Assembly
 
I suggest x86 or x64 in favor of an Arduino because you can skip the step of uploading your code to the target device. Your rate of iterating / exploring is much faster. Edit -> compile -> run.

There are some decent "hello world" examples out there.
 
Well, I like the picture of the books ;) But they are for a different architecture (m68k).

why actually assembler, *today* ?
Looks like the answer here is "out of interest, for learning".

If that's indeed the case, I'd think about whether I really want to dive into x86. x86 is a very complex (CISC on speed) architecture, designed with compilers in mind, not so much humans writing the code. Of course, it still might be interesting, but be prepared to have to learn a lot before you can actually write some code doing anything useful.

The m68k architecture (used e.g. in older Amigas or Macs) is a lot simpler and actually meant to be "hand programmed". You could go to something even simpler, like the 6502 (which IMHO is really fun to program in assembler), e.g. used in the famous C64, the Apple II, and so on :)
 
Well, I like the picture of the books ;) But they are for a different architecture (m68k).


Looks like the answer here is "out of interest, for learning".

If that's indeed the case, I'd think about whether I really want to dive into x86. x86 is a very complex (CISC on speed) architecture, designed with compilers in mind, not so much humans writing the code. Of course, it still might be interesting, but be prepared to have to learn a lot before you can actually write some code doing anything useful.

The m68k architecture (used e.g. in older Amigas or Macs) is a lot simpler and actually meant to be "hand programmed". You could go to something even simpler, like the 6502 (which IMHO is really fun to program in assembler), e.g. used in the famous C64, the Apple II, and so on :)
I support edu, for sure!

I am just curious, about answer. Sounds great goal - Learning curves!

usually people say, there is openGl, there is python, go for python... ;)
 
Definitely you have to read a book from first to last page. The problem is that instructions are different for each CPU. It is good to learn something practical like 8-bit Atmega. Then you can look 8086, 80386 and x64 instruction set to have notion for complex instruction set (compared to Atmega). Finally, return to C - it is like abstract assembly.
 
I believe it's the pink shirt book or "The Peter Norton Programmer's Guide to the IBM PC" that not only teaches you assembly language but does this by walking you through the writing of a complete and useful (back in the day) application. It's for the old DOS era so you could install DOS in a VM and try it out. It's very basic, 16-bit real-mode assembly language but it's a book and it's a start. I could be mistaken about the book as I have a few books from Peter Norton and I'm not at home so I can't check I'm referring to the correct one.

Other options include Zen of Assembly Language and The Art of Assembly Language though I haven't read them.

Now, for writing assembly language specifically for FreeBSD, I do not believe there are any books but there must be some (or tutorials at the least) about writing assembly language for Linux. Those will give you some directions of how it would be writing for FreeBSD.
 
Is there any books for help me to improve with that.
I really enjoyed Assembly Language Step By Step by Jeff Duntemann. It's an introduction for the 32-bit x86 instruction set so don't expect it to be the last book you read on assembly language. Other books I looked at first out all felt like reference manuals for the x86 instruction set, whereas this book actually teaches programming. The author says it can be used by people without programming experience, though I came to it with experience in a variety of higher level languages including C.

A downside is that to use the software the author uses to teach with you'll need to run Ubuntu 9.10, which hasn't been supported since 2011. I suggest a virtual machine for the job. At an introductory level, it is the concepts that are important, not the tools, specifics of the operating system or even the specifics of the instruction set.

I love coding in assembly but as SirDice said, few people code applications in assembly language for x86 or amd64; it simply takes too long when you could write something portable more quickly in a higher level language. However, learning it is a great education in its own right and really valuable if you are interested in reverse engineering and security research.
 
The Art of Assembly Language
The original book. Not the "High Level Language" newer one. Randy went off the tracks with that one and we had many fierce arguments about that nonsense.

Now, for writing assembly language specifically for FreeBSD
I gave a link for the FreeBSD chapter earlier in this thread.

be prepared to have to learn a lot before you can actually write some code doing anything useful.
Just like C, one can begin with a number of basic instructions and do quite a bit with that. But you also have to have an understanding of the internals of the microprocessor and its interaction with outside components and timing. Assemblers didn't help much with layout in memory but I'm betting they're better now.

You also have a different mindset but, having designed computers from the chip level, it's easier for me than one who doesn't have knowledge of the internals.
 
Definitely you have to read a book from first to last page. The problem is that instructions are different for each CPU. It is good to learn something practical like 8-bit Atmega. Then you can look 8086, 80386 and x64 instruction set to have notion for complex instruction set (compared to Atmega). Finally, return to C - it is like abstract assembly.

C is much more (powerful, low-end, cross platform,...), but it is also much less, but actually it is definitely better to start learning Assembler, rather than anything else.

By the end all goes to machine language.
 
Back
Top