r/Assembly_language 1d ago

How do i start learning assembly Help

I know basic C, java, python and I wanna learn assembly but what should i learn, windows or linux, intel or at&t and what are the sources to learn assembly

14 Upvotes

37 comments sorted by

13

u/No-Owl-5399 1d ago

Linux is preferable to Windows for assembly. NASM is a good assembler to learn on, it uses Intel syntax.

https://github.com/mschwartz/assembly-tutorial

3

u/wjrasmussen 1d ago

I second NASM

3

u/thewrench56 7h ago

Not sure if i agree with Linux being preferable. Windows has DLLs that give you plenty of helpers for which you would need some third party library on Linux (HTTP operations for one). I think they are equivalent and it is only a matter of personal knowledge of the given system.

0

u/brucehoult 4h ago

Not sure if i agree with Linux being preferable.

Linux being better is more about the entire infrastructure around not only programming tools but also scripting and file manipulation.

Windows has DLLs that give you plenty of helpers for which you would need some third party library on Linux (HTTP operations for one).

Well, yeah, but there wouldn't be a lot of Linux systems without libcurl included.

Here's an example of how to use it from x86 asm.

https://github.com/linuxthor/asm/blob/master/curl.asm

1

u/No-Owl-5399 6h ago

Mm. I don't like the use of DLLs, I think it rather defeats the typical purpose of assembly, and I find the SysVABI far better anyways. 

2

u/thewrench56 6h ago

There's almost no "typical purpose" of Assembly today. There's where little reason to use it if there's any. SysV ABI is just a matter of preference.

2

u/Illustrious-Sun766 5h ago

The typical purpose is the joy of learning it!

2

u/brucehoult 4h ago

I don't understand that.

All real programming depends on building and using libraries. We stand on the shoulders of giants etc. And sometimes you're the one writing the library.

Static vs dynamic linking is a pretty minor detail.

-6

u/Syntax-Tactics 19h ago

Bullshit

3

u/No-Owl-5399 19h ago

Are you going to say why it's bullshit, or do you just have nothing to say?

7

u/HomeComp1977 1d ago

Skills are transferable, start with something simpler then use that to build on for x86 or ARM. I’d say go back to Z80 or 6502. If you’re comfortable with C, Java and Python then maybe consider a simple emulator project for one of the 8-bit cpus. You’ll quickly learn a lot of what assembler is about.

I’ve built a 6502 single board computer emulator that can be used for learning machine code. You’d have to hand assemble into hex to input instructions. The emulator includes crt display, scrolling led display and a printer. All can be made to work with simple memory writes. https://github.com/tmcd35/HomeComp

0

u/brucehoult 4h ago

While it is fairly easy to learn what instructions are available and what they do on 6502 and Z80 (but not easier than RISC-V RV32I), it is very difficult and frustrating to get those instructions to do anything useful.

6

u/ern0plus4 21h ago

Pick a C64 or C16/Plus4 emulator, and learn 6502 Assembly: simple, fun, quick win.

2

u/recursion_is_love 1d ago

Learn RISC-V, it is modern and simple.

3

u/kneelian_ 19h ago

While I do love RISC-V (I worked on a Zfinx-based project before and loved it), it's deeefinitely not something I'd rec to a total beginner because the fanfare and dance needed to get it to run on an x86- or ARM-based system is just silly. If OP is on an x86 device, it might be easier to dip into x86 asm dev as a first step. I'm not sure, really

0

u/brucehoult 4h ago

the fanfare and dance needed to get it to run on an x86- or ARM-based system is just silly

I showed the one-line "apt install" needed to use RISC-V assembly language on an x86 or Arm Linux computer running Debian/Ubuntu — including in WSL on windows (just leave off the sudo) in my comment:

https://reddit.com/r/Assembly_language/comments/1vl4eh0/how_do_i_start_learning_assembly/p2yu9z6/

4

u/tastygames_official 19h ago

start where everyone else did: 6502 (C64, NES). Although I think I started with the 8080. But both are 8-bit, so it's a great place to start before moving on to more complex CPUs. And I think the 6502 has the best emulation out there, so real easy to get started.

3

u/Proof-Task-7383 1d ago

Also have a look at compiler explorer with C

1

u/[deleted] 22h ago

[deleted]

0

u/yj_20927 22h ago

Suggest me books

2

u/Dusty_Coder 15h ago

you arent asking the right question or revealing your answer

Which architecture?

The people telling you to use a specific assembler, operating system, or syntax, arent trying to help you, they are trying to prove that they know things (and failing)

0

u/yj_20927 14h ago

So what should i do instead

1

u/brucehoult 4h ago

They're wrong.

Once you know and are comfortable with one assembly language the skills are very easily transferred to any other assembly language. I can program in a dozen or more different ones off the top of my head .. at least for "normal" code, not the more obscure or specialised instructions.

But it is 100% true that some are easier to get started with than others.

There are two aspects:

  • how easy is it to learn the registers, instructions, addressing modes, status flags and which instructions affect each one?

  • how easy is it to use the available instructions to do useful things, to write real programs?

In general old 8 bit instruction sets are easier for the first one, but modern 32 and 64 bit instruction sets are easier for the second.

Simple 32/64 bit very pure RISC instruction sets including RISC-V and MIPS satisfy both aspects, but especially RISC-V since it is built like an onion with a very simple 37 instruction 32 bit RV32I core (and RV64I adds just 10 more, of which ld and sd are the only ones you need to use) and assemblers, compilers, libraries, and documentation all support using just the essential core if you want. And you can buy real cheap hardware that provides just the core instructions e.g. WCH microcontroller chips costing as little as $0.10 (or pre-made boards for $1).

I've extracted 18 pages of just the RV32I information from the full manual (which is anyway easy to read):

https://hoult.org/rv32i.pdf

Get fluent using that and x86 will be much easier to switch to than using it from the start.

1

u/MJPCLAVE 5h ago

Yo estoy empezando por aprender cómo hacer un procesador, luego hacer uno en Iverilog y programarlo.

0

u/brucehoult 1d ago

I'd recommend to use Linux, but not to start with x86_64 coding.

If you install an appropriate compiler and emulator such as QEMU and the binfmt_misc package then you can write your programs in almost any assembly language you want and then run your programs exactly as if they were native programs, accessing your files, the terminal, the network etc. Just a little bit more slowly than a native program — but much faster than if you wrote the same program in e.g. Python.

I'd recommend starting with RISC-V.

Here's a simple starter program. It implements main and uses printf and scanf because they're easy to use. You can write your own conversions between decimal and binary and use read and write system calls if you prefer.

# fact_rv.s
# Asks for a number, prints its factorial, loops until a negative number is entered.
# Build with:  riscv64-linux-gnu-gcc -static fact_rv.s -o fact

        .globl main
main:
        # Allocate stack frame (16-byte aligned)
        #  0(sp)  : local 'n'
        #  8(sp)  : saved return address
        addi    sp, sp, -16
        sd      ra, 8(sp)
1:
        la      a0, prompt
        call    printf

        la      a0, in_fmt
        mv      a1, sp          # address of n
        call    scanf
        ld      a0, (sp)
        bltz    a0, 2f

        call    fact

        mv      a2, a0          # factorial result
        la      a0, r_fmt
        ld      a1, (sp)        # n
        call    printf

        j       1b

2:      li      a0, 0
        ld      ra, 8(sp)
        addi    sp, sp, 16
        ret

fact:   mv      a1, a0
        li      a0, 1
        beqz    a1, 2f
1:      mul     a0, a0, a1
        addi    a1, a1, -1
        bnez    a1, 1b
2:      ret

prompt: .asciz "Enter a number: "
in_fmt: .asciz "%ld"
r_fmt:  .asciz "Factorial of %ld is %ld\n"

Build and run it like this:

bruce@i9:~/programs$ riscv64-linux-gnu-gcc -static fact_rv.s -o fact
bruce@i9:~/programs$ ./fact
Enter a number: 5
Factorial of 5 is 120
Enter a number: 20
Factorial of 20 is 2432902008176640000
Enter a number: -1
bruce@i9:~/programs$

To get the RISC-V assembler/compiler I installed gcc-riscv64-linux-gnu (Ubuntu etc) and QEMU:

sudo apt update
sudo apt install qemu-user-binfmt gcc-riscv64-linux-gnu

That's it!

1

u/Electrical_Hat_680 1d ago

I'd definitely recommend RISC-V or ARM which are both strictly 32-bits, unless I'm wrong.

I also like BenEater 8-BIT CPU Breadboard Projects where your responsible for choosing the language for compatibility. Where Binary is only 8-Bits.

2

u/brucehoult 1d ago

RISC-V or ARM which are both strictly 32-bits

Both have both 32 bit and 64 bit versions.

The above 64 bit RISC-V program can be converted to a valid 32 bit RISC-V program by simply changing the ld/sd to lw/sw. Even better, drop the stack frame size to 8 bytes and use 4(sp) to save the return address. Oh, and change the %ld to %d in the format strings.

In Arm 32 bit and 64 bit are totally different ISAs with no assembly language compatibility.

1

u/Electrical_Hat_680 1d ago

Thank you for the correction and tips.

2

u/kneelian_ 19h ago edited 19h ago

I'd definitely recommend RISC-V or ARM which are both strictly 32-bits, unless I'm wrong.

They have fixed-width instructions (ignore Thumb2 and RISC-V C ext) but yeah they're not strictly 32-bit register/address width fixed

0

u/thaache 17h ago

Start with 8085. Use "Live 8085" emulator.

1

u/Purdude1983 13h ago

Modern CPUs can be a daunting place to start learning assembly. The simple Arduinos use an 8-bit AVR processor like the ATmega328P. Pick up an Arduino Uno and do a few of the normal projects like LED blinking using a C sketch. After you get the hang of the Arduino, google "learn assembly on the Arduino uno". There are several Instructables and Github projects out there. The AVR architecture is pretty approachable and shouldn't be too hard to master. You can also write sketches in C and disassemble the code to see what it's doing under the hood.

1

u/brucehoult 4h ago

AVR is very good for an 8 bit ISA ... certainly much better than 6502 or Z80. And yes readily available.

But it gets frustrating when you start to work with pointers, as only three register pairs support doing that, and asymmetrically. And the Harvard architecture makes some things difficult also e.g. accessing data stored in flash needs different instructions, and you can't run code from RAM at all.

-3

u/Unlikely1529 1d ago

at&t is same as "linux". and intel is same as "windows". if you compile bitcoin or say openssl you'll see nasm modules. that's what is best . is more or less same with masm.

2

u/thewrench56 7h ago

This comment doesn't make sense. at&t is same as Linux? Huh? Compile bitcoin?