The definitive "teach yourself machine code" text?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Einar Saukas
Bugaboo
Posts: 3143
Joined: Wed Nov 15, 2017 2:48 pm

Re: The definitive "teach yourself machine code" text?

Post by Einar Saukas »

TMD2003 wrote: Sat Oct 03, 2020 11:32 am 10 FOR n=22528 TO 22879: POKE n,15: POKE n+352,48: NEXT n
[...]
This could be done in 20 bytes, maybe even ten, but who needs this many?"
I know it was a rethoric question but since you mentioned my name...

I believe the minimum implementation is 17 bytes :)
BenHanson
Drutt
Posts: 9
Joined: Sat Oct 03, 2020 2:22 pm

Re: The definitive "teach yourself machine code" text?

Post by BenHanson »

For anyone that is interested you can find my Z80 assembler here: https://www.codeproject.com/Articles/52 ... -Assembler

Be sure to report any issues, this is all very new!

Thanks
User avatar
Morkin
Bugaboo
Posts: 3276
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: The definitive "teach yourself machine code" text?

Post by Morkin »

Einar Saukas wrote: Sat Oct 03, 2020 7:41 pm I believe the minimum implementation is 17 bytes :)
* (Gets popcorn and waits for the optimization to begin) :lol:
My Speccy site: thirdharmoniser.com
User avatar
TMD2003
Rick Dangerous
Posts: 2043
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: The definitive "teach yourself machine code" text?

Post by TMD2003 »

Is this also the right time to suggest a Machine Code sub-forum for threads like this one, the one cmal's just made, and others? It might well be useful.
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

Einar Saukas wrote: Sat Oct 03, 2020 7:41 pm I believe the minimum implementation is 17 bytes :)
It’s late and I’m tired, but I can’t for the life of me to get it below twenty:

Code: Select all

	ld bc,352
	ld de,$5800+352	
	ld hl,$5800
	ld a,48		

loop	ld (hl),15
	ld (de),a
	inc de
	cpi
	jp pe,loop	
:cry:
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Einar Saukas
Bugaboo
Posts: 3143
Joined: Wed Nov 15, 2017 2:48 pm

Re: The definitive "teach yourself machine code" text?

Post by Einar Saukas »

There's a simpler solution in 20 bytes:

Code: Select all

ld hl, $5800
ld de, $5801
ld bc, 352
ld (hl), 15
ldir
ld bc, 351
ld (hl), 48
ldir
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

Okay, down to 19 bytes:

Code: Select all

	ld hl,$5800
	ld a,15
	call fill
	ld a,48

fill	ld bc,352
loop	ld (hl),a
	cpi
	ret po
	jr loop
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
TMD2003
Rick Dangerous
Posts: 2043
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: The definitive "teach yourself machine code" text?

Post by TMD2003 »

Two to go! Only two more to go!

Right now, I am hacking my way through the New Generation Software tutor. I've already improved the code for lesson 6, although I'm not supposed to know about INC and DEC yet. (ITV's new Geordie presentation team while Ant's drying out!) So far it's a bit like trying to write Python programs with functions I'm not supposed to know about, but I thought "what the hell, I'll look them up - how hard can it be?" And I wrote some iterative maths programs to calculate constants using Taylor series, just to see how easy it was.

I suppose that's something I could aim at with machine code - seeing as the Z80 has to work out functions like SIN and EXP using simple arithmetic until the result fills up all the available decimal places correctly, I'll see if I can (eventually) translate the BASIC programs for the Euler-Mascheroni constant and the (very slowly converging) Gregory-Leibniz formula for pi. It should also be relatively straightforward to calculate factorials, although the gamma function might be a bit more tricky...
Last edited by TMD2003 on Sun Oct 04, 2020 4:05 pm, edited 1 time in total.
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

TMD2003 wrote: Sun Oct 04, 2020 3:49 pm seeing as the Z80 has to work out functions like SIN and EXP using simple arithmetic until the result fills up all the available decimal places correctly, I'll see if I can (eventually) translate the BASIC programs for the Euler-Mascheroni constant and the (very slowly converging) Gregory-Leibniz formula for pi.
On smaller, underpowered systems like the Spectrum, coding is always a compromise between optimizing for speed or size. Sometimes, both can be achieved simultaneously, but only if the code is not very optimized to begin with. I am much more skilled at optimizing for speed, rather than size, so take my suggestion with a grain of salt, but for the most part, the trick with many complex math calculations is to resort to look-up tables of pre-calculated values.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Re: The definitive "teach yourself machine code" text?

Post by Dr beep »

Everyone did it in his own way.

I would suggest a simple program that uses RST 16 to display a character and slowly expand that code into a moving character and finally a controlled moving character. This will give you ideas how to show progress and how input can be used in games.

First job is to get familiar with the input and output on a ZX Spectrum

Special effects like bitwise movement will come later.
User avatar
PeterJ
Site Admin
Posts: 6878
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: The definitive "teach yourself machine code" text?

Post by PeterJ »

Dr beep wrote: Sun Oct 04, 2020 5:05 pm

I would suggest a simple program that uses RST 16 to display a character and slowly expand that code into a moving character and finally a controlled moving character. This will give you ideas how to show progress and how input can be used in games.

First job is to get familiar with the input and output on a ZX Spectrum
Agreed!
BenHanson
Drutt
Posts: 9
Joined: Sat Oct 03, 2020 2:22 pm

Re: The definitive "teach yourself machine code" text?

Post by BenHanson »

Also have a look at http://www.primrosebank.net/computers/z ... blyThe.pdf

You can reuse some of the ROM routines.
User avatar
TMD2003
Rick Dangerous
Posts: 2043
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: The definitive "teach yourself machine code" text?

Post by TMD2003 »

After finishing the first of the four sides of the New Generation tutorial tapes, plus what I've picked up off this thread, would this be a reasonable summary of the registers?

Use A for doing any kind of calculations.
Use BC if you want the result to be returned to BASIC (i.e. via PRINT USR ..... or LET variable = USR .....).
Use DE if you'll need to keep on swapping with HL (because so far EX DE,HL is the only DE-specific instruction I've come across).
Use HL to store addresses that need to be easily referenced and retrieved.

As for the "BC loop" that occurred earlier in this thread (i.e. DEC BC | LD A,B | OR C | DJ NZ ...), is there any reason why that wouldn't work with DE instead of BC? I suppose I could modify that successful routine and try it, and see what happens...
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
BenHanson
Drutt
Posts: 9
Joined: Sat Oct 03, 2020 2:22 pm

Re: The definitive "teach yourself machine code" text?

Post by BenHanson »

DJNZ is decrement B and Jump relative Not Zero

Yes you can:

DEC DE
LD A, D
OR E
JR NZ, ...
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

TMD2003 wrote: Sun Oct 04, 2020 9:22 pm Use A for doing any kind of calculations.
Mostly, yes.
TMD2003 wrote: Sun Oct 04, 2020 9:22 pmUse BC if you want the result to be returned to BASIC (i.e. via PRINT USR ..... or LET variable = USR .....).
Well, this is a very specific case that has to do with the Spectrum’s operating system. BC (sometimes referred to as the Byte Counter) is used in block copy/compare/input/output operations and the B register itself is used by the DJNZ instruction.
TMD2003 wrote: Sun Oct 04, 2020 9:22 pmUse DE if you'll need to keep on swapping with HL (because so far EX DE,HL is the only DE-specific instruction I've come across).
DE (sometimes referred to as the DEstination) is also used in block copy operations and points to the destination address of a byte (or block of bytes) to be copied to.
TMD2003 wrote: Sun Oct 04, 2020 9:22 pmUse HL to store addresses that need to be easily referenced and retrieved.
HL is the most versatile of the register pairs. It is somewhat similar to the A register, as it is the place where the results of arithmetic operations will be stored (e.g. ADD HL,DE, SBC HL,BC). It is also the only register pair that can point to a byte in memory to be loaded directly with a value (e.g. LD (HL),n) or used in conjunction with the stack pointer SP.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Einar Saukas
Bugaboo
Posts: 3143
Joined: Wed Nov 15, 2017 2:48 pm

Re: The definitive "teach yourself machine code" text?

Post by Einar Saukas »

OK, here's the solution in 17 bytes:

Code: Select all

org $fefe
    ld hl, 22528
    ld de, 22528+352
    ld bc, $610F
    ld a, 48
    call x1
x1:
    call $0cf0
I hope my math is correct, I don't have an emulator to test it right now...
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

Einar Saukas wrote: Tue Oct 06, 2020 7:20 pm OK, here's the solution in 17 bytes:
Cheater! :lol:

A great trick, though. (Well, two tricks.)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
cmal
Manic Miner
Posts: 630
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: The definitive "teach yourself machine code" text?

Post by cmal »

A clever solution indeed! :o I guess no one said you couldn't use ROM routines. I tested it and it works.
User avatar
Morkin
Bugaboo
Posts: 3276
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: The definitive "teach yourself machine code" text?

Post by Morkin »

...Am still trying to get my head around that - what's the significance in the ORG $FEFE...? It doesn't seem to work elsewhere... :o
My Speccy site: thirdharmoniser.com
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

Morkin wrote: Tue Oct 06, 2020 11:44 pm ...Am still trying to get my head around that - what's the significance in the ORG $FEFE...? It doesn't seem to work elsewhere... :o
It strategically places the code so that the POP BC in the ROM’s subroutine loads the correct values into the B and C registers, namely—the low and high bytes of the memory address immediately after the CALL $0cf0 instruction.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
cmal
Manic Miner
Posts: 630
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: The definitive "teach yourself machine code" text?

Post by cmal »

Yup, Einar's routine is extremely unrelocatable.
User avatar
Morkin
Bugaboo
Posts: 3276
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: The definitive "teach yourself machine code" text?

Post by Morkin »

Ah, OK, I see. So not easily relocatable but still clever.. :lol:
My Speccy site: thirdharmoniser.com
User avatar
TMD2003
Rick Dangerous
Posts: 2043
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: The definitive "teach yourself machine code" text?

Post by TMD2003 »

Sound the trumpets long and loud in a PLAY command or a series of BEEPs, I have written my first ever machine code routine with no outside help from anyone! And if I can do it, anyone can, at least anyone who's covered the first side of New Generation's Machine Code Tutor.

The routine switches the bright channel on or off in a 4x4 square on rows 11-14, with the column position adjustable as required. All I have to do is POKE a couple of values into two two-byte addresses and the machine code does the rest. There are no loops, as I'm using all of HL, BC, DE and A to hold and manipulate the values - and all I need is some LDs, ADDs and INCs (and RET...) - no DJNZ or any loop instructions, no PUSH and POP to and from the stack, I don't know how to do that yet, only that it exists (I'll get there soon enough, mind). But it works, it does what I needed it to do, and there was plenty of space to hold it amongst some undefined characters in a set. As long as I POKE the right values (64 for on, 192 for off - work out why...) and then LET u=USR 64600, it's instant. No waiting for two nested FOR n/m=0 TO 4 loops in BASIC now!

And to see the result - it's coming soon to a Crap Games Competition near you! That is, as soon as I've got it all finished and [mention]PROSM[/mention] reviews it and puts it up for download. That might take a while, so maybe, just maybe, I'll show a quick preview of this and my previous submission on the BASIC Dumping Ground...
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: The definitive "teach yourself machine code" text?

Post by Ast A. Moore »

TMD2003 wrote: Thu Oct 08, 2020 10:51 pm so maybe, just maybe, I'll show a quick preview of this . . .
Please do!
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
AndyC
Dynamite Dan
Posts: 1408
Joined: Mon Nov 13, 2017 5:12 am

Re: The definitive "teach yourself machine code" text?

Post by AndyC »

Nice going. You taken your first steps into a larger world... ;)
Post Reply