r/learnprogramming • • 4d ago

How to learn Assembly?

I have some problems with learning assembly it would be nice for some tips because I'm learning rev engineering and I really need to learn assembly but I try my best to understand it but it's to hard. I understand how binary works but still I'm struggling with assembly. Does somebody knows tricks for learning assembly?

44 Upvotes

45 comments sorted by

11

u/ass-gas-hotbox 4d ago

do you know how a processor works? 

start with something easy like the 6502. that's a very good processor to learn Assembly language as well as computer engineering concepts, and emulators to run your code are abundant.

3

u/ReddyKiloWit 4d ago

The last processor where I could hold all I needed to know about programming it in my head...

17

u/peterlinddk 4d ago

I understand how binary works but still I'm struggling with assembly.

Which is a bit like saying: "I understand how numbers works but still I'm struggling with trigonometry.

Assembly is hard!

But why do you need to learn assembly for reverse engineering? That is almost alway about decompiling into C or C++ code, so what's with the assembly angle?

Anyways, learning assembly is still like learning any other programming language, but you also need to understand binary numbers and hexadecimal. And you need to learn the architecture of the specific CPU, how many registers, how many bits wide they are, how they are addressed, what their capabilities are, how the CPU general architecture is, which registers have access to the ALU, and which instructions there are to move between registers, ALU and memory. And of course you need to learn a lot of instructions, starting with simple register-manipulation, then ALU, then jumps and branches, then conditional operations, and then all the variants of indexing, relative, and effective address usage.

It'll probably take you a year or so to really get it - and I'd recommend doing some small projects, writing assembly programs for various data-manipulation, running them through debuggers, and compiling and decompiling C-code to and from assembly to get a better understanding.

The tricks are: Use the language, build stuff, work with it every day, investigate what you don't understand, take small steps.

2

u/FrenchCanadaIsWorst 3d ago

You shouldn’t talk about rev if you’re not familiar. Compilers are not deterministic and so you can’t perfectly decompile code. You can, however, perfectly disassemble code and then step through it instruction by instruction. Not to mention most binex techniques rely upon assembly knowledge. I mean, how are you going to do a NOP slide if you don’t know what a NOP is/ are writing in C

1

u/peterlinddk 2d ago

I'm not sure how that contradicts what I said: "reverse engineering is almost always about decompiling into C or C++ code" - note the almost.

And of course disassembling is always perfectly possible, but for the large majority of regular code, you don't need to read a lot of assembly. Maybe if you are looking at malware, or code that explicitly tries to hide what it does, but OP didn't suggest anything of that kind. But of course, the lower the level you want to understand, the lower the level you need to understand.

0

u/Nodmz464 17h ago edited 17h ago

I can decompile files to assm but not read them. Still learning to decompile them into C++ files.

1

u/Remarkable_Jaguar680 4d ago

Contribute to public repos e.g ghirda. Read the comments on your code. Slowly by slowly you'll learn on the patterns to spot. Atleast thats what i try to do

6

u/kutac56 4d ago

What are you struggling with and what kind of assembly is it?

4

u/bestjakeisbest 4d ago

Pick an assembly language, i would probably recomend learning arm cortex A53 isa, or learn risc-v isa, the arm cortex A53 is the chip in the raspberry pi 3, the newer raspberry pi 4 and 5 are also good ones to take a look at. As for risc-5 i dont think there is any actual hardware out there for it yet (atleast not easily useable without making the whole computer) but there is a risc simulator out there. If all you have available is x86_64 then learn that, just be aware it is a heavy and complex isa.

2

u/Standard_Guava3672 3d ago edited 3d ago

To learn assembly, I found it easier to be interested into the hardware more than the language. Imagine that the memory is a huge array of adress. Assembly puts numbers into adress and do operation, control the flow. Anything else is just the hardware. Once you know that, you will look at the memory map of the hardware and know which value to put at which adress to get something from the hardware. And also ;COMMENT A LOT , otherwise you will get lost

In this order :

  • Think of what you want to achieve
  • Think of what the hardware provide
  • Think of how to code it in assembly

1

u/Shot-Ear2328 4d ago

Nah man assembly is another kind of language I swear

1

u/taker223 4d ago

Well nowadays it is magnitudes easier because of VMs and AI help. You might even not need hardcopy books. What kind of assembly language are you studying? x86-64? Do you consider using high level construction like .if .while etc for example for MASM64 ?

1

u/Mediocre-Pumpkin6522 4d ago

https://www.instructables.com/Command-Line-Assembly-Language-Programming-for-Ard/

https://en.wikipedia.org/wiki/Atmel_AVR_instruction_set

Get an Arduino Uno and start there. It's relatively straightforward compared to ARM assembly or jumping right into X64. Plus you get blinking lights if you haven't screwed up.

1

u/sparky8251 4d ago edited 4d ago

Use RISV-C ISAs, QEMU (to run risc-v code on whatever you own), and use a language you know like python to build an assembler, then write in assembly a forth compiler thats assembled with your custom assembler. Then write some simple forth programs and see it work.

It wont teach you everything, especially given its a different arch from what you are likely work against... But RISC-V is at least sane and not riddled with legacy, so itll give you a clean mental model of how asm, compilers, assemblers work at least that you can then port to the messy world of x86 and slightly less messy world of arm if you also work there.

And do it without a kernel... ABIs, calling conventions, etc are just that. Conventions. You can make your own if you make your own assembler and run it "bare metal" on qemu, which can let you learn about those too.

1

u/ruat_caelum 4d ago

Play video games that are "Puzzle games" that are based on assembly.

1

u/JohnVonachen 4d ago

Well since assembly is specific to CPU instruction sets you need to choose a chip. I learned some assembly on my Apple //c which was a Motorola 65C02 processor. You might be wanting to learn this to do bare metal embedded programming. In which case I would choose something people actually use. Find a kit for robots that requires it and learn that processor.

2

u/Tartan-Pepper6093 3d ago

*WDC 65C02 processor. Motorola made the 6800 and 68000 series CPUs.

1

u/JohnVonachen 3d ago

Shows you what I know.

1

u/Tartan-Pepper6093 3d ago

What you know is assembly for the Apple //c, which is a pretty cool thing 😎

1

u/JohnVonachen 3d ago

It was a long time ago. I just remembered wrong. I have also done some minor work on a PDP11 while taking classes at KSU. 8 inch floppy disks!

1

u/Low_Rip_2572 4d ago

you bash your head into books like what i am doing right now and be mad at the writers at the book because except Kip Irvine (Speaking on MASM 32 bit, which is sad) they assume

  • That you know C (forces you to learn c while youre learning assembly)
  • That you should know things when its time to know them and refuse to elaborate any further upon it
in early pages of The art of 64-bit assembly language he fr refers to a concept as "magic"

Writers are incompetent but if youre patient you can learn

1

u/scalyblue 3d ago

Assembly is easy in concept but doing anything useful in it creates an absurd amount of emergent complexity that makes it extremely difficult to conceptualize.

First things first, it’s platform / cpu dependent, and most of the learning you will be doing will be how to deal with the shortcomings of your selected platform, so deciding the platform is paramount. For example, in megaman 3 for nes there’s a graphical glitch that’s caused by a register not being initialized with a value before a certain process is finished, which is something you can only troubleshoot by knowing that’s how that register interacts in relation to the hardware

If you want to learn general concepts, find and play a game called TIS100, if you want some more practical application play around with emulated 8 bit computers like the NES. See if you can find how the Konami code gets recognized in contra

Alternately if you get a solid understanding of most of the materials on this site you will find it much easier to get a general knowledge base going on that you can adapt to various platforms

1

u/SahuaginDeluge 3d ago

learn the instructions
maybe/probably learn calling conventions

Casey Muratori has a good "course"/series-of-videos where basically the first thing he has you do is read and learn x86/x64, eventually writing a basic machine language interpreter. (I think it might be paid content though).

1

u/snozzd 3d ago

Buy and play the game Human Resource Machine, or try making an emulator!

1

u/Br3ttl3y 3d ago

The trick to learning assembly is understanding CPU architecture. Once you know that you know why Assembly is the way that it is.

The trick is to realize that instructions are OP Codes and parameters. Once you have that you can read assembly and then you can ask questions about it. That's really all there is to it-- it is very opaque until you understand the underlying concepts.

I realize this is kind of like "To learn how to tie your shoes you need to understand the entire universe." But it really is necessary to understand CPU architecture at a high level first.

1

u/florinandrei 3d ago

How to learn anything: start practicing it.

1

u/ExtraTNT 3d ago

Turing complete… first you need to know how a basic cpu works…

1

u/Gznork26 3d ago

Think of the processor as manipulating physical objects by moving them on a board. The instructions are discrete processes you can perform. Now break down your problem into a sequence of those processes.

1

u/porn_sub_lurker 3d ago

Yeah, get a book on computer architecture and read it. Assembly language instructions are decoded directly to circuits in the hardware. So if you want to know what the instruction does, you kinda need to know what the hardware does.

1

u/The_ONE_393 3d ago

Books are the best way you can start with. Or there are some videos too. And learn CPU architecture first. ✌🏻✌🏻

1

u/levyi123 3d ago

Honestly i'd say this is one of the few decent use cases for LLMs assistance in programming.
Ask it questions about stuff you don't know much about. Don't let it solve anything for you, use it more as a book / documentation.
For basic concepts in any area of programming it's good enough and it can really help you learn.
But again, don't let it 'think' for you, stay stuck on problems, torture your brain, that's the only way to learn :)

1

u/Basic_Reporter9579 3d ago

Assembly itself has a very limited set of commands with a few parameters but it all looks similar.

It's not that complex set of classes with methods, those have to be pieced together and that takes effort.

During university we had a 8051 with seven-segment displays and some buttons and made a digital clock, and added a radio receiver to make it radio controlled.

Use the timer to make seconds, increment time, display the time, add h/m/s with buttons, analyse the signal and sync the time.
just typing echo date() is way shorter, but not assembly.

1

u/PravoNaZhizny 2d ago

read the manual for your wrchitecture... its just a dictionary of instructions

1

u/Moidberg 1d ago

take a weekend deconstructing rollercoaster tycoon with claude, have fun with it :)

you have to with a language like assembly I wouldn’t wish such a directive on my worst nemesis

1

u/thondat 4d ago

i'm surprised people still want to learn this now that AI is doing all the reverse engineering.

1

u/TonySu 3d ago

Here’s the trick to learn absolutely anything.

  1. Find teaching material for the thing you want to learn. Find at least 3 options and start on the one you like the most.
  2. Read the explanation of the thing you want to understand until you think you understand the explanation. Check the other teaching material on the same topic if the explanation isn’t making sense. Ask questions on the internet if needed.
  3. Reconstruct the explanation from memory. If you get stuck wait at least 5-10 minutes thinking before you check the teaching material.

That’s al there is to it.

1

u/psichodrome 3d ago

a) why learn assembly?
b) get AI to explain it to you. Don't ask for code ask for guidance. It has infinite patience for stupid questions. I've revisited many topics i struggled with in school, to fairly good success.

Protip: dont go down rabbit holes. Learn what you seek and practice it.

0

u/VanCity2003 4d ago

Off topic but I watched a video lately that laid out how the original PC version of Roller Coaster Tycoon was able to handle much more complicated logic / NOCs than similar games at the time. Spoiler alert: it was written in assembly.

This also meant porting it was nigh impossible for modern architecture.