Live data from Hacker News

I believe 6502 instruction set is a good first assembly language

nemanjatrifunovic.substack.com

261–270 of 297 posts

Re: I believe 6502 instruction set is a good first assembly language

#261
post #121

I cannot agree that the simplicity of the 6502 wins over the 68000. The 68000 has more registers and wider data types: but the registers are all uniformly the same. It's really just two registers A and D, copied a bunch of times in to D0 to D7, and A0 to A7. Whatever you can do with D0 can be done with D3 and so on. Some A's are dedicated, like for a stack pointer. Simplicity of structure has to be balanced with simp…

One of the downsides having learned to code close to the metal on old CPUs is that you understood how that system worked and it‘s easy to assume assembler and the resulting machine code are the truth, but on modern CPUs that‘s a lie. A modern beefy out of order CPU will do insane optimization dynamically at runtime and the instructions you carefully scheduled to avoid register spills are nothing but a suggestion to t…

Not sure why you were downvoted. What you say is true. You could count the cycles of the instructions to figure out exactly how long a loop would take at a given clock frequency. The entities manipulated by the program literally existed. If the ISA said there are 8 registers, you could find 8 areas of the silicon die corresponding to those. The ISA features on a modern processor are a fiction. Especially if it something riddled with 50 years of backwards compatibility, like Intel.

Re: I believe 6502 instruction set is a good first assembly language

#262

Earlier quoted context omitted.

> even though they are physically connected to internal ones They're not physically connected, other than through the bus. I mean, zero page is part of DRAM.

you're right, I meant $0000 and $0001 which _are_ registers. The rest 254, yeah. Zeropage is a must on C64 at least. I don't know much about other platforms.

So 0 and 1 are connected to the 6510 I/O data direction and I/O data register, but there is still RAM there too. Seems the 6510 won't issue write signals to the bus for addresses 0 and 1, but the VIC can see them and it's readable through sprite collisions.

Re: I believe 6502 instruction set is a good first assembly language

#263

Earlier quoted context omitted.

> Because it lacks many features of normal ISAs Do you have some examples of this?

The most important are the lack of integer overflow detection and indexed addressing. Integer overflow detection is required for any arithmetic operation unless it is possible to prove at compile time that overflow is impossible (which is possible mostly for operations with some counters or indices, whose values are confined inside known ranges), while indexed addressing is needed in all loops that access arrays, i.e…

> indexed addressing

Ok, this time I have a proper benchmark.

The XuanTie C910 core supports a custom extension with indexed loads [0] and I got my access to a MILK-V Pioneer server with that CPU working again.

As a quick benchmark I used a self compile of the chibicc C compiler: https://godbolt.org/z/4MMxsEarE

Measuring with the equivalent of rv64gcb and rv64gcb_xtheadmemidx, the xtheadmemidx variant ended up about 0.3% faster on the same core.

[0] https://github.com/XUANTIE-RV/thead-extension-spec/blob/mast...

Re: I believe 6502 instruction set is a good first assembly language

#264

Earlier quoted context omitted.

Great flamebait ;) what makes riscv suitable for teaching is that the ISA is trivial to map to hardware. This is because it is so regular. Students with zero experience can design simple riscv cpus in their first asm course. More experienced students can design cpus with pipelining, branch prediction, ooo execution, etc. Old ISAs from the 1980s are way more difficult.

Why is designing a simple CPU a relevant goal for an asm course? It's a goal for a hardware course.

Universities usually do both, and it makes some academical sense for it to be the same.

Re: I believe 6502 instruction set is a good first assembly language

#265
post #250

Earlier quoted context omitted.

You can do LDA ($0f),y This is using a 16 byte address in zp as a ptr + y offset For LD[$0f] #65 It's more instructions but you LDA #65 STA ($0f),Y (assume Y = 0)

Sure, but the point is that you can't treat a zero page address like a register. There's a LDA, LDX, LDY, but no way to load directly to a zero page address. You have to load a real register, then store it in the ZP address. Zero page isn't a bunch of registers because register operations by and large don't work on them directly.

I'm not trying to convince you that zero page are hardware registers just that they can be thought of as register-like. If you were coding on a 68000 you might do some memory operation indirect via an address register like (a0)++ but on 6502 you do it via a zp pointer with the ++ handled by incrementing Y. You can have many of these pointers. They fufill what you might use registers for on another CPU. So no, they are not registers but they are register-like in the way that can be used as pointers, holding constants, temporaries and have the advantage of a cycle faster than regular memory access. I notice that some people who programmed the Z80 struggle with this way of looking at the 6502 which can be as efficient as the Z80 if coded properly.

From you POV this is probably a specious argument since there is a very definitive view of what a register is in a CPU and that because of the Church-Turing a 6502 can pretend to be anything including a 68000 or RISC-V - but all I will say is that it's still a worthwhile abstraction to hold when you're designing code for the 6502.

Going off track, a further abstraction that I find amusing is to consider the zero page a register file and 6502 assembly microcode. For sure you could then come up with your own ISA for the 6502 that would be nicer to program and more directly amenable to compilers. Steve Wozniak's Sweet16 was fun virtual 16 bit CPU that ran on the 6502.

Re: I believe 6502 instruction set is a good first assembly language

#266

I do not know. I learned it when I was 12, but it was quite challenging for me. Pros: It is very ortagonal, has no memory-to-memory instructions Zero page is like a having 256 generic RISC registers (a bit faster than accessing to other memory areas). Very easy to understand how the assembly is micro-coded Cons: It is impossible to create a "generic" memory pointer with an index >255 without auto-modifying code. It i…

>Steve Wozniac created a small VM just to manage 16 bit Integers (see the his Byte magazine articles).

It was called SWEET16: https://en.m.wikipedia.org/wiki/SWEET16

A BYTE Magazine article by Wozniak: http://amigan.1emu.net/kolsen/programming/sweet16.html

Porting Sweet 16 by Carsten Strotmann (with source code) http://www.6502.org/source/interpreters/sweet16.htm

Re: I believe 6502 instruction set is a good first assembly language

#267
The 6502 was a great CPU for its time and price point. I wrote many programs in its assembly language. However, if you're going to work on modern systems, there are too many differences for the 6502 to be a good first assembly language (unless the 6502 is your focus).

The 6502 was designed to be easily implemented with relatively few transistors. For that it was amazing. There is a reason it was popular! But its CPU registers are only 8-bit, its call stack is 256 bytes, and for real work you need to use the zero page (zpage) well. None of these are likely to relevant to a modern software developer using assembly. Its design encourages the use of global locations, again, an approach that don't make sense on modern systems.

I say this as someone who admires what the 6502 was able to achieve in its time, and I even have a long-running page dedicated to the 6502: https://dwheeler.com/6502/

If you want retro and easy, the 68000 has it beat in terms of simplicity of development. The 68K is quite regular & a little more like modern systems (sort of).

However, I think starting retro is often a disservice. Most the time the reason to use assembly is performance. If you're running code on chips more than a dollar or so, getting performance out of modern architectures is MUCH different than retro computers. For example, today a lot depends on memory cache latency, yet is typically not a serious concern on most retro computers. So learners will learn to focus on the wrong problems.

If you want a modern simple architecture, may I suggest RISC V? That's under a royalty-free open source license, and real computers use it today. It's pretty simple (your basic load-store architecture) and you can pick only the modules you care about. Full disclosure: I work for the Linux Foundation, but I'd say that anyway.

Plausible alternatives are ARM and x86. If you subset them, they're okay.

The reality is that assembly languages reflect the underlying system, and that will change over time.

Re: I believe 6502 instruction set is a good first assembly language

#268

The topic of the 6502 ISA simplicity is a pet peeve of mine, because to me it's clear that anybody thinking that such simplicity is a good thing, never progressed past a hello world. Programming anything of moderate complexity on the 6502 is hard. 8 bits are way too restrictive (e.g. screen addressing on the Commodore 64). Multiplications/divisions need to be hand-rolled. And even 16 bit sums/subtractions are simple…

> Multiplications/divisions need to be hand-rolled. In the context of someone's first contact with assembly language, that's a good thing! Translating various multiplication and division algorithms to assembly is a great way to learn it.

I learned to implement my own multiplication and division as part of learning bignum with a high level language (in my case Pascal). By the time I learned assembly language I simply focused on how to translate high level language to assembler; basically be a manual compiler and compare my output against a real compiler.

It seems unnecessary to learn any algorithm including the multiplication algorithm in assembly language.

Re: I believe 6502 instruction set is a good first assembly language

#269

Earlier quoted context omitted.

I think one of the values 6502 provides is that it has so many retro platforms. Apple ][, NES, Atari 2600, BBC, C64, they all use 6502. If you want to do something cool retro stuffs that a lot of people today still enjoy, that's the best bet. But again Z80 looks like a very good option too because it has GB/GBC plus ZX Spectrum. The same goes to 68K -- Mac 68K, Sega Genesis and Neogeo are popular hits. When I was wri…

If you want to do something cool retro stuffs that a lot of people today still enjoy, that's the best bet. This shouldn't be discounted, since it has great pedagogical value in itself. On the other hand, personal opinion here, is that not many things are transferable to modern ISAs. Even back in the 90's, we were shown to work on MIPS instead. 68k is very nice to read and write, almost feels like higher language, but…

Yeah maybe jumping directly into x86-64 is not a bad idea. e.g. for Windows just have to understand the calling conventions and syscalls and such, to push some windows.

I need to look into my SDL2/C++ game's assembly code and see how it is done.

Re: I believe 6502 instruction set is a good first assembly language

#270
post #123

Earlier quoted context omitted.

The era in which there was nothing but assembly language was very short-lived. By the time the 1950s were over, we had higher level languages. Like before The Beatles recorded their first album.

> The era in which there was nothing but assembly language was very short-lived. Hell, I'm not even sure the era existed. Grace Hopper was creating the first few high level languages for UNIVAC I. A-0 was complete in May 1952. A-2 (the first which saw extensive use) was created in August 1953. As far as I can tell, UNIVAC I never had an assembler. If you weren't using A-0, programmers were expected to just type in ra…

I've done some more research into this:

Part of the problem is that "Assembly language era" is ill-defined. Personally, I don't think it counts as Assembly language unless you are using a symbolic assembler, because that's what modern programmers think about when you say "assembly".

There is a reasonably common interpretation includes the whole machine code era as "Assembly language", as you are writing it out and then hand-assembling it. Which means the UNIVAC's C-10 machine code counts as "Assembly language", even if they weren't using that terminology. With this interpretation, the "assembly language era" lasted a few years. but I think this inspiration is very misleading to any programmer exposed to a proper assembler.

Anyway, even with my stricter definition, there was an assembly-only era, but it only seems to have existed inside IBM's research labs. They had their first symbolic assemblers running on the "test assembly" by October 1950.

There is very little information about this "test assembly" computer on the internet, doesn't even have a wikipedia page (same with the "Tape Processing Machine" or TPM that followed). But "IBM's early computers" by Charles J Bashe documents it. This computer had not one but two symbolic assemblers running by autumn 1950, which does actually seem to beat most high level languages.

Long before the first 701 was even installed (in April 1953, at IBM's HQ), programmers had already gotten sick of assembly programming, which is why speedcoding was created.

Though, this wasn't just internal IBM programmers. Customers who bought the 701 were given documentation about both the computer and the assembler as early as 1951. These customers had hired programmers, who had started writing assembly code, months before the 701 assembler was even debugged and running on the first prototype 701, and years before they received their computers. So maybe there was also an "Assembly language only era" in the offices of these early 701 customers. But it's kind of an edge case if they didn't have a computer to run the assembler on, or test their programs.

I assume these early programmers were occasionally visiting the prototype 701 to assemble and test their code.

Post reply on HN