Learning machine code

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Hedge1970
Manic Miner
Posts: 388
Joined: Mon Feb 18, 2019 2:41 pm

Re: Learning machine code

Post by Hedge1970 »

^^^^ regarding the opcodes I completely relate to that, not only different codes but the way that you enter decimal, binary and hex differs across the assemblers. I - currently for learning - like the clean approach of writing on paper the hex codes and then the opcodes along with my additional human notes.
AndyC
Dynamite Dan
Posts: 1409
Joined: Mon Nov 13, 2017 5:12 am

Re: Learning machine code

Post by AndyC »

Yeah, 8-bit processor documentation specified opcodes so they should be the same (aside from undocumented instructions and some variations like ADD 6 vs ADD A,6) but didn't really mandate any particular directives, label formats or numbering systems etc. And that inevitably means that every tool out there used a slightly different syntax, which is certainly a bit of a headache when you're starting out.

Modern CPU documentation is a lot more prescriptive in this regards, making assembly sources more portable (not that many people write assembly any more)
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

Hedge1970 wrote: Wed Dec 27, 2023 12:20 pm ^^^^ regarding the opcodes I completely relate to that, not only different codes but the way that you enter decimal, binary and hex differs across the assemblers. I - currently for learning - like the clean approach of writing on paper the hex codes and then the opcodes along with my additional human notes.
It's really not very useful working out the opcodes by hand unless you are writing code that generates other code (advanced stuff that though). It makes jumps way too complicated for a start.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

AndyC wrote: Wed Dec 27, 2023 11:25 am It was a deeply painful experience. A microdrive or disk drive was pretty much essential, as was learning to reason about code without running it as much as possible.
Hah! I learned on a AIM-65 without a tape deck or disk drive about 36 years ago. So everything had to be typed in each time. It did have a printer, so keeping a copy of the program was not a problem. Except now the thermal paper is old, it's now almost blank :twisted:

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

Oh, one other pitfall I discovered when I started playing again with Z80 machine code many years ago. I was using a cross-assembler on a PC (generic Z80, not Spectrum specific). This did have some slightly annoying 'features', but generally worked very well for the short programs that I was working on at the time.

But one of the programs I wrote did not work as intended. It was only when I examined it in a disassembler that I discovered that one instruction was missing, The cross-assembler was not recognising a legal and official Z80 opcode, but rather than produce an error, it just moved to the next instruction.

So, keep in mind that it is possible for there to be problems caused by the tools you use.

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

ParadigmShifter wrote: Wed Dec 27, 2023 11:31 am The main problem with developing on a real machine is the lack of a debugger and Z80 not having a debug break instruction so breakpoints/single stepping is not really viable. (Although I think there may be a debugger that replaces opcodes with a call to the debugger maybe so can use that for breakpoints).
It is possible for real hardware using a Z80 to have "break points". It's just that it's a lot harder to do without either additional hardware, or having system that replaces opcodes with jump instructions (which is much easier if it's possible to use a restart instruction, which of course you can't do on most ZX Spectrum models).

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
Hedge1970
Manic Miner
Posts: 388
Joined: Mon Feb 18, 2019 2:41 pm

Re: Learning machine code

Post by Hedge1970 »

ParadigmShifter wrote: Wed Dec 27, 2023 12:28 pm It's really not very useful working out the opcodes by hand unless you are writing code that generates other code (advanced stuff that though). It makes jumps way too complicated for a start.
Not sure I understand or perhaps I am using the wrong terminology for me the entered code is C9 and the opcode is RET so as a human at a glance I can see I’ve used a Return. When I finally start using Notepad and Pasmo again I’ll just enter the opcode. For now though I am working purely in hex.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

Yeah they should have used an RST for a debug break. They should also have had the default interrupt call a function that's defined in the SYSVARS. Never mind.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

Hedge1970 wrote: Wed Dec 27, 2023 12:52 pm Not sure I understand or perhaps I am using the wrong terminology for me the entered code is C9 and the opcode is RET so as a human at a glance I can see I’ve used a Return. When I finally start using Notepad and Pasmo again I’ll just enter the opcode. For now though I am working purely in hex.
You should definitely just use an editor and an assembler. Knowing C9 is useful I suppose although not all C9's correspond to RET of course (and there's also condition RET e.g. RET Z).

You should also use a debugger which will disassemble the code for you so you can check it (although Spin sometimes starts disassembling from the wrong address so I just put 4 NOP instructions at the beginning when developing to get around that).

Doing everything by hand means you either have to use relative jumps all the time or you have to make space for other functions/addresses you jump to (and you can't do a relative CALL has to be a 16 bit address). People who did that were a bit different ;) I know Ant Attack, Skool Daze and maybe Stonkers were written like that. Madness ;)
EdToo
Manic Miner
Posts: 228
Joined: Thu Nov 03, 2022 4:23 pm

Re: Learning machine code

Post by EdToo »

What I am getting from this is I probably didn't have much hope of actually learning machine code back in the 80's with only a spectrum to learn and develop on. This started out as an exercise to see how possible it would have been. Obviously I'm now using an emulator which is vastly easier than saving to real tape, etc.

I'm going to continue a little with the book by Toni as she seems very clear and readable currently. Certainly more than the Melbourne book for absolutely beginners which is trying to convince me the Spectrum has 8, 8 fingered hands, 2, 16 toed feet and pairs of very stiff gloves.

I'll no doubt soon progress to selecting and settling on a more modern way of developing using the pc. Everyone's input has been súper helpful and interesting to compare the different approaches.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

Back in the 1980s, if you did only have a ZX Spectrum and a tape deck, The Complete Machine Code Tutor did help with the very basics of machine code.

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

The Melbourne book "Spectrum Machine Language For The Absolute Beginner" is just trying to get into the readers head how "simple" a CPU is in terms of what numbers it can handle.

I have this book, but honestly, as I already knew quite a lot about binary, octal and hexadecimal, 8 bit bytes, 4 bit nybles and 16 bit addresses, I skipped a lot of the first part :lol:

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
MustardTiger
Microbot
Posts: 122
Joined: Tue May 02, 2023 8:05 pm

Re: Learning machine code

Post by MustardTiger »

ParadigmShifter wrote: Wed Dec 27, 2023 11:31 am The main problem with developing on a real machine is the lack of a debugger and Z80 not having a debug break instruction so breakpoints/single stepping is not really viable. (Although I think there may be a debugger that replaces opcodes with a call to the debugger maybe so can use that for breakpoints).

I use sjasmplus as my assembler and Spin for emulator. The debugger in Spin is pretty good although I've not worked out how to get symbols imported (so I just refer to the symbol file sjasm produces if I need to inspect variables). The memory window isn't very good in spin though :(
Have you tried DeZog debugger? https://github.com/maziac/DeZog It installs as a Visual Studio Code plugin so you can set breakpoints in your source and step through the source while debugging. You can setup Visual Studio Code to call sjasm too so you never have to leave the Visual Studio environment. The DeZog emulation is a bit slow, so I still use ZXSpin for running my code, but for debugging DeZog is perfect.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

Nope not tried that.

Can you use watch/memory windows as well?

I'll probably stick to Spin though if the emulation isn't great.

I don't like how it's a pain to resize the memory window and you can't pick non-power of 2 numbers for the width either :( Especially when each row of my collision map is 11 bytes long lol.
User avatar
MustardTiger
Microbot
Posts: 122
Joined: Tue May 02, 2023 8:05 pm

Re: Learning machine code

Post by MustardTiger »

ParadigmShifter wrote: Wed Dec 27, 2023 2:54 pm Nope not tried that.

Can you use watch/memory windows as well?

I'll probably stick to Spin though if the emulation isn't great.

I don't like how it's a pain to resize the memory window and you can't pick non-power of 2 numbers for the width either :( Especially when each row of my collision map is 11 bytes long lol.
Yeah, it has watches and memory windows. The memory windows highlight address when registers are pointing at them and when things change. Here's an image of something I'm debugging at the moment.
Image

The internal emulation isn't really slow, maybe 80-90% of fullspeed.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

Might give it a go.

Code: Select all

call DrawTilePixelY
ret
can be replaced with

Code: Select all

jp DrawTilePixelY
for a slight speedup... you can always replace a call/ret pair with jp EDIT: Unless you do stuff like push arguments onto the stack of course.

EDIT: I will often comment out the call/ret instead of removing it in case I need to add something after the call later on.
User avatar
lexi
Dizzy
Posts: 63
Joined: Mon Dec 04, 2023 10:55 pm
Contact:

Re: Learning machine code

Post by lexi »

EdToo wrote: Tue Dec 26, 2023 4:21 pm How on earth did people learn machine code back in the 80's? I remember my school used to have a regular day when someone would come from outside with a selection of books that we could buy. I bought Supercharge your Spectrum on one of these occasions, with the intention, to learn machine code.

My childhood memories were that after trying to follow it I decided machine code was not for mortals such as me.
I wandered down to my local library and I picked up a lot of books at that made me feel that way. The one that was most influential for me was the 1983 Usborne Introduction to Machine Code for Beginners (link provided by publisher) - it had cool robots, good pacing and did a great job at making assembler seem accessible and fun. I'd recommend it to anyone wanting to get started today, especially if they like cool robots.
berarma
Microbot
Posts: 106
Joined: Thu Mar 09, 2023 10:55 am

Re: Learning machine code

Post by berarma »

EdToo wrote: Tue Dec 26, 2023 4:21 pm How on earth did people learn machine code back in the 80's? I remember my school used to have a regular day when someone would come from outside with a selection of books that we could buy. I bought Supercharge your Spectrum on one of these occasions, with the intention, to learn machine code.

I guess anyone serious about it would go to the Z80 datasheet. The source of all the books.

With that information you could then disassemble the ROM and other software and see how it worked. That was probably the fastest path to master professional programming in those days.

Edited by PJ
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

This is probably a better reference since it gets straight to the point and includes the undocumented instructions

https://map.grauw.nl/resources/z80instr.php

EDIT: Seems to be missing SL1 (aka SLL, bit misleading that though) which is a logical shift left that inserts 1 into the LSB.

Although you need something like this to if you want to see the effects on the flags

http://z80-heaven.wikidot.com/instructions-set
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Learning machine code

Post by 1024MAK »

www.z80.info has masses of technical information on the Z80, including details on all the known undocumented instructions.

On the Zilog site, they have the official documentation (link)

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
berarma
Microbot
Posts: 106
Joined: Thu Mar 09, 2023 10:55 am

Re: Learning machine code

Post by berarma »

ParadigmShifter wrote: Fri Dec 29, 2023 6:01 am This is probably a better reference since it gets straight to the point and includes the undocumented instructions

https://map.grauw.nl/resources/z80instr.php

EDIT: Seems to be missing SL1 (aka SLL, bit misleading that though) which is a logical shift left that inserts 1 into the LSB.

Although you need something like this to if you want to see the effects on the flags

http://z80-heaven.wikidot.com/instructions-set
The question was "how they learned in the 80s" so I posted the resource that was availablenin the 80s. Not as a website but on paper.

Of course today there are a lot more and better resources.
User avatar
lexi
Dizzy
Posts: 63
Joined: Mon Dec 04, 2023 10:55 pm
Contact:

Re: Learning machine code

Post by lexi »

berarma wrote: Fri Dec 29, 2023 5:55 pm Of course today there are a lot more and better resources.
I disagree because it's too easy to get overwhelmed - i'd still start someone off with the cute and friendly machine coding robots and then move on from there :)

Image
AndyC
Dynamite Dan
Posts: 1409
Joined: Mon Nov 13, 2017 5:12 am

Re: Learning machine code

Post by AndyC »

lexi wrote: Fri Dec 29, 2023 6:14 pm I disagree because it's too easy to get overwhelmed - i'd still start someone off with the cute and friendly machine coding robots and then move on from there :)

Image
Agreed. A pure reference manual would be unbelievably dry, and you definitely don't need to worry about things like undocumented instructions when you're starting out.
EdToo
Manic Miner
Posts: 228
Joined: Thu Nov 03, 2022 4:23 pm

Re: Learning machine code

Post by EdToo »

I've just been trying The Complete Machine Code Tutor a 1984 Spectrum program, I believe this would have been helpful back in the day. It has the benefit of teaching concepts in small bites and then visually stepping through an example showing the result of each instruction on the registers and memory spaces. I didn't totally grasp the difference between ADD and ADC from Toni's manual, so it helped seeing it work step by step.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: Learning machine code

Post by ParadigmShifter »

16 bit ADD doesn't set S, Z, or V flags! Been burned by that many times lol, need to use ADC for that. So you might need to clear the carry as well (any logical operation will do that though so either OR A or AND A clears the carry).
Post Reply