HACKER Q&A
📣 Claude_Shannon

How to begin learning assembler for university course?


Next semester I'll have a subject about computer architecture. During it, we'll be learning assembler (TASM, if the website's correct).

Could I ask for your advice, how to approach that topic? I'm not going to lie, task of learning assembler seems to me quite daunting and stresses me out. Do you have any tips from your personal experience? Do you have any TASM books recommendations?


  👤 xorcist Accepted Answer ✓
The notion that assembly language is somehow difficult is just silly exceptionalism.

It's a lot of work to do complex things, that's for sure, but it isn't in any way hard to understand. There will be a reference guide for your processor which will be exact and succinct.

It's the easiest language there is to learn. It's hardly even a language. Even if modern processors have layers of abstraction to them, you still get to get to program a processor directly.

Don't be surprised if you also get a much better understanding of how some common data structures work when you have to code them yourself. I remember being baffled by pointers but after coding assembly a few times it suddenly seemed completely natural. It was a few years ago though.


👤 junon
ASM is ridiculously shallow, but broad. You don't need to understand a whole lot to understand the basic 'mechanics'. You just need a really good reference for the actual instructions.

First, realize what a CPU actually does. Realize what machine code does. Learn the parts of the CPU (roughly speaking) - instructions, pointers, registers, flags, etc. Learn some basic EE concepts such as what "high" and "low" mean, and what a "line" is, for example. It's a prerequisite to reading some of the meatier bits of documentation.

Secondly, reading the CPU guides for whatever ISA (Google "AMD programmer manual volume 3" for x86_64 platforms for example) is a great start. They're full of useful information, and AMD's in particular is very easy to read, albeit large. Skim through it, especially the first bits that explain what the registers are.

Then use NASM to write a simple program that exits 42. Get there first. Learn what the different directives mean (e.g. `.text` for ELF output).

Then start to add instructions and play with registers. Learn about calling conventions. Write some C code and emit S files (google it) to see how it compiles down to machine code (turn of optimizations when you're starting out).

Get familiar with Godbolt.org, it'll help with the last thing on that list.

Any book is going to help, though pay attention to the syntax it uses (avoid GAS syntax, in my opinion) and the platform it targets (make sure you have access to that platform, e.g. don't pick up an ARM book if you're on x86). It'll make things very hard to follow if you don't.

These are just tips, but maybe they help just a bit.


👤 Someone
Why would it stress you out?

If the university is any good at teaching, it will pace the course in a way that at least ⅔ of the students will be able to follow the course. If you have time _this_ semester to read a book about _next_ semester’s stuff, you aren’t struggling to keep up, so I would expect you to be in that group.

Also, assembler breaks down stuff into smaller steps, but that doesn’t mean it’s hard. It ‘just’ makes it more work to write large programs.

If you have done some C programming, assembler is a bit like having a program where you declare a fixed number of integers (say A, B, and C) and a fixed set of double's (say D and E), a huge array of bytes called MEMORY, functions for reading bytes and integers from, and writing them to it, and then limit your program to.

- simple expressions (e.g. A=B+C is fine, A=B+C+D isn’t. You would have to do A=B+C; A=A+D; instead). The exact set of allowed things varies by CPU. Some allow you to do “A=B + MEMORY[C], some don’t, etc)

- not using while, for

- if is only allowed as if A op 0 goto FOO; (where op is ==, !=, <, etc)

The exact set of int's and double's and the set of allowed expressions varies by processor. also, assembler syntax is different. you'd write something like (this is different per processor, and even between assemblers) "ADD A,B,C" for "A=B+C", for example.

You could start with a small C program and see whether you can change it to adhere to some of these restrictions.

Sample programs could be

- computing the GCD of what’s in A and B

- counting the number of times each byte value occurs in MEMORY between offsets 10,000 and 20,000.

- checking whether the memory region between offsets A and B contains a range of values equal to that given by offset C and number of bytes D.

The result won’t look like assembly, but I think it would give you the right mindset.


👤 hinoki
Like other posters said, don’t sweat learning it early, just do the assignments.

That said, Compiler Explorer [0] is very useful to see what modern compilers actually do. If you want to learn how simple programs are converted to assembly, make sure you turn off the optimisations with the ‘-O0’ compiler flag.

[0] https://godbolt.org/


👤 foxfluff
I think the idea of a course is that you will be taught. Why worry about it? Do you not trust your uni to provide the necessary course materials and exercises?

Did someone tell you assembly is hard? I will tell you that assembly is easy, very easy. It's easy because instructions are simple and stupid. They do one little thing, and maybe set a few flags. And while modern ISAs have grown a ton of cruft, you get pretty far with a small set of core instructions (loads & stores, register-register and immediate-register moves, arithmetic, testing flags, branching). Given that it's a computer architecture course, they'll probably want you to learn something about ABI and calling conventions too. Not a big deal, it just illustrates the (somewhat arbitrary) convention of what to pass in registers vs what to pass on stack, who saves registers (which registers?) and so on. Simple stuff that you just need to have a convention for if you want to make it easy to interface with prebuilt binaries.

With very basic knowledge, you could disassemble a program and just look up instructions as you go to figure out what it does. That is how I "learned" assembly. I put learned in quotes because for me learning is on-demand and is never fully done. I learn more as I need, including last week when I did some ARM assembly programming.

I think assembly has an undeserved reputation for being difficult, because building large programs in assembly is a lot of work. Also because fear of the unknown: few people learn assembly, and even fewer use it enough to be proficient with it, so in their mind it's the devil they don't know. I doubt the course has you building large applications, it'll be some rather basic things. And that's how a lot of assembly usage is in real life: you just have to do some basic bits in early stage bootloaders, OS kernels, crt, etc. in assembly. But only enough to get the code in a higher level language going. (The other significant use is in optimization, especially SIMD, but I doubt a computer architecture course would focus on that too much).

Have no fear and just dive in. That's how you approach the topic.

Knowing C and checking the compiler output is very helpful. You can do it interactively on godbolt.org, it'll even hilight lines and the corresponding machine instructions.


👤 cturner
Some things that were high value to me,

1. Play through the first page of levels of TIS-100. This is a game you can buy on Steam. After spending a few hours working on problems in this, I felt a hard-to-describe mental shift that removed the intimidation of assembly.

2. Work through the first half of /Programming Boot Sector Games/ by Toledo.

3. Designing and building a simple instruction architecture, implementing it as a VM, writing an assembler for it.

The ChibiAkamus tutorials on youtube may be worth a look, also.

If you try for TIS-100, give yourself a decent chance to get into it. Print out the manual, calmly read through it with a biro, don't let yourself give up easily on the first few problems.


👤 jakuboboza
Isn't TASM "turbo assembler" ? that is 16/32 bit. What I would do is to get hold of someone from previous year and ask about the classes. If it is turbo assembler than figure out how it is setup. If it is vm etc... and once You know this you can pick probably a book that fits.

When I was in the uni we had a forum for all years where we shared info on subjects, lecturers and info needed in general. I don't know how students do it nowadays probably via messenger, whatsapp and gdrvie links.


👤 flohofwoe
8bitworkshop is great:

https://8bitworkshop.com/v3.9.0/?platform=c64&file=hello.das...

It's mostly vintage CPUs like the 6502 or Z80, but those have the advantage that they're trivial to learn, and the basic concepts still apply to modern CPU assembly.

There's also a DOSBox wrapper for x86 assembly though (sadly nothing 68000 based, because this was by far the nicest CPU for assembly coding).


👤 ajb
Speaking as someone who has spent probably 10 years writing assembler:

Assembler was difficult in the old days because you would load your code, and it would crash, giving no information on what went wrong. These days you can run stuff in a simulator that will print out what changed at every tick. Yes, your attempts will crash a lot and it will take you a long time to write a program that hardly does anything, but that will be true of all the people on the course.


👤 fwsgonzo
I don't know if I'm the right guy to answer this, but I've written a lot of assembly on the side as part of VM and emulation projects. I think the first time I really understood how simple some things can be was when I wrote a simple function that did nothing but returned back to the caller.

So, how about starting out with a C program that enters main, calls an extern assembly function and then exits? And this would be the example C code:

    int main() {
        extern void my_assembly_function();
        my_assembly_function();
    }
Now you have to create your assembly file, and then compile it to an object file that can be linked with your program:

    gcc -c myassembly.S -o myassembly.o
OR

    nasm -f elf64 myassembly.asm -o myassembly.o
See: https://www.ele.uva.es/~jesman/BigSeti/seti1/nasm/nasmdoc2.h...

I've never used TASM before and I couldn't really find anyone using it. But I guess the course will cover that. Is this programming on Windows or Linux?


👤 t-3
If a "modern" high level language is like a shop with machines full of safety features and refinements, lower-level languages are similar but the bandsaw won't try to stop before cutting your fingers off, and assembly is like building by hand (some platforms might give you an electric drill and 50 different saws, others, just a chisel). In many ways, it's easier to think about, but it also lacks the easy guide-rails and labor-saving machinery that make high level attractive.

Assembly will give you the context to really appreciate why higher-level languages work how they do, especially the older ones like C.


👤 fouc
I highly recommend starting with the amazing book "Assembly Language: Step-By-Step by Jeff Duntemann" It's 90% about architecture and 10% about actual syntax/coding. Covers both TASM/MASM. There's a few different editions - the earlier ones include DOS specific info.

It's the type of book you can almost read in one sitting. It's that enjoyable.

It's basically the absolute best "beginners" book. Read this first, THEN get into a proper assembly book (that can be more of a reference to the instructions, etc).


👤 tremon
What's your background? Obviously, if you're studying marketing or sociology, learning computer assembly is going to be a challenge.

At uni, we didn't learn to write assembler until we'd already been taught about Boolean logic, finite automata and RISC processor design. So we were already familiar with basic processor operation before writing any code for it. But I studied electrical engineering, not computer science.

Knowing your current background/field of study is going to be essential to give useful recommendations, I think.


👤 lithiumhead

👤 fmax30
I think TASM, isn't that difficult. I remember learning this in a university course and I actually quite enjoyed it. There are youtube videos out there.

But to be honest, just approach it like any other language.

- Learn the basic constructs (control structure patterns, loop patterns and instructions etc)

- Use it to solve easy problems (something like the easy problems in project euler)

- Just read source code to see how people use it in c/c++ programs.

In general TASM is pretty limited, but the knowledge you gain there can be used in other places which makes it worth it.

Edit: Formatting


👤 forinti
My Uni had this weird notion of teaching assembly with made up processors. It was some professor's hobby to write emulators with a semi-graphical environment that showed the registers, and so on.

I didn't buy it. We should have used real processors or, at least, those fantasy processors should have been reflections of real ones. I thought then that we should have started with 8-bit processors.

Anyway, later we had a chance to do some work with real 8086 assembly and that was a lot more exciting.


👤 trotFunky
I have dabbled with quite a few assembly languages (8051, MIPS, A64 and a bit of x86), the first and second of which I learned in school. To be honest if your teacher is good and the class is well prepared, it should be pretty nice ! As fmax says in their comment it's not that different than learning any other new language : there are some key concepts and ideas which you'll need and the mnemonics, which are the assembly keywords like `ADD` (better to have a searchable reference handy). But all those things can be acquired by experimenting with it and writing simple programs. On top of that, you say the course is on computer architecture so I bet you'll have a very useful overview of all those concepts that are useful for assembly writing ! peterkelly's link looks very useful for that purpose as well.

For that you'll need to setup some sort of environment where you can write, assemble and run your assembly code. jakuboboza has some good ideas to check with previous students to know what can of environment you need.

Eventually you can check an even simpler assembly like the 8051 that has very simple emulators and IDEs available. Or, if you want something a bit more game-y you can look into Zachtronics' puzzle games[0] that often have an assembly-like language and come with a manual, which can help you become familiar with the basic principles. TIS-100 is only assembly writing, Shenzen I/O has a bit of wiring/"electronics" on top and EXAPUNKS makes you "hack" systems using little bots written with this assembly.

I found that knowing a bit of assembly is quite useful and interesting to learn, I hope you'll have fun with it !

[0] : https://www.zachtronics.com/


👤 threecoins
https://www.youtube.com/c/BenEater

This is a very good channel that will help you. He goes through assembly language in various videos as part of demonstrating different topics and I'm not gonna lie but he make it seem very simple and obvious.


👤 sokoloff
I agree with the bulk of posters who are advising you to stop stressing and that the course has taught thousands of students before you who didn't specifically prepare for it.

What I'd probably do instead is to take a language like C and learn how the compiler translates C to assembly with optimizations turned off and targeting a simple processor (maybe an MSP430 or an AVR). If you already know C, this exercise will teach you why I consider C to be "portable assembly language".

Or, you could enjoy the time between now and then doing whatever you like and show up to the course well-rested and ready to learn. It's hard/expensive to make large, complex programs in assembly. It's not hard to learn the essentials of it for a computer architecture course purposes.


👤 zoenolan
A little outside what you asked but https://www.nand2tetris.org/ is a good introduction to computer architecture and assembly.

For something a little more complex you could have a look at a 68000 course https://www.youtube.com/watch?v=UpNS-IVsBew&list=PL-i3KPjyWo...


👤 protomyth
Don't stress on assembler. This should be a fun class for you. If they are using TASM then it's early x86, so not the funnest (like 68000 or 6809), but not especially hard.

I would get ahold of the TASM manual early and give it a read through, then take your x86 instruction reference and write each instruction on a 3x5 card in the proper format for the assembler and what registers the instruction changes (directly and via conditions). You'll be fine.

I'm pretty sure there is no equivalent of the IBM 360/370 banana book.


👤 psyc
Go download Exapunks, Shenzhen IO, and TIS-100. I didn’t have that option when I was in your position, so I read Intel’s pentium programming manual and made a Winamp viz plugin instead.

👤 0xbadc0de5
A practical approach is sometimes useful. Write a simple "Hello World" in C and compile it with full debug symbols. Then open it in GDB and step through. Or alternatively, open it in Ghidra and follow the instructions to see how they map to higher-level code. Try the same on increasingly complex programs. Try the same with C++ instead of C, or Rust to see how different languages produce different ASM. Try compiling to ARM versus X86 to observe the differences in architectures.


👤 jonpalmisc
If you already know C or C++, I’d recommend checking out Compiler Explorer [1]. It helped me tremendously when learning assembly as I could see how source code translated to assembly, which helped me learn common patterns in assembly, etc.

[1] https://godbolt.org/

Note: It supports more languages than C and C++, but with the exception of Zig, the others will typically have more complicated assembly output.


👤 vitiral
I recommend"Programming from the Ground Up"

https://www.google.com/url?sa=t&source=web&rct=j&url=https:/...


👤 lbriner
Any decent book or course on programming microcontrollers. Ideally, if you can get a cheap microcontroller test board, you can quickly see and appreciate what your instructions do. That said, using assembly in a PC is quite a different abstraction and it depends realistically how much you will use it on your course.

If it is just a module, I wouldn't worry too much about it and just look at the college's recommendations.


👤 pferdone
I would be sooo excited if I were you. I don't know if you're the type of person who likes to break stuff and figure out inner workings of programs. But the moment I fell in love with assembler was, when I was able to DISassemble a program and understand it bit by bit. Read process memory from another process, inject and modify code. IDK to me it was the first moment I felt 'in control' :)

👤 teeray
I think I learned more from Ben Eater’s “Building an 8-bit breadboard computer” series [0] than my intro to computer architecture course at university. Highly recommended.

[0] https://www.youtube.com/watch?v=HyznrdDSSGM&list=PLowKtXNTBy...


👤 ultra_nick
The basics of assembly aren't bad, but there's a lot of bad tutorials out there. Look for the ones that start by explaining what a processor register is.

https://www.cs.virginia.edu/~evans/cs216/guides/x86.html


👤 mordae

👤 sircastor
I can’t speak for TASM, but I learned 6502 ASM for my senior project. It can be daunting, and for me the trickiest part was reducing down to what I wanted to do. I suspect it’s more complicated for larger addresses and more features, but still it’s ANDing, ORing, and simple math. You’ll get it.

👤 tony_codes
I wrote a guide that may help -- it enables gdb debugging within consistent containerised environment: https://blog.devgenius.io/getting-started-writing-assembly-l...

👤 miked85
I've never heard of prepping for a university course - the whole point of taking the course is to learn.

👤 electronstudio
I just learnt assembly last week, and I made a list of all the resources I used:

https://loughton.me.uk/2022/01/01/assembly-language.html


👤 megapoliss
I liked this book for TASM. However, it's pretty much outdated now

https://www.amazon.com/Mastering-Turbo-Assembler-Tom-Swan/dp...


👤 artificialLimbs
It's quite the luxury to be at a University, with knowledgeable professors at your fingertips. Ask them for help if you get stuck and can't do a web search to find what you need. Classmates can be helpful too if you think they have some competence.

👤 rodney24
Have a look at this: https://hackaday.io/course/178733-raspberry-pi-pico-and-rp20...

His youtube videos are interesting as well.


👤 mhitza
40HEX and 29a computer virus ezines. I can also recommend Ralf Browns Interrupt List for a great reference for BIOS/DOS interrupts (procedures).

Definitely not along the lines of what you're going to be taught, but fun.


👤 e19293001
I highly recommend Assembly Language and Computer Architecture using C++ and Java by Anthony Dos Reis. This book will teach you a lot of stuff in assembler.

👤 racnid
It gets posted every so often here but:

https://asmtutor.com/

Is a good intro to writing x86_64.


👤 brudgers
Why not MIX? Find an old edition of Fundamental Algorithms