Machine code challenge

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Machine code challenge

Post by R-Tape »

I've been talking to Peter about assembly routines, one of which was to draw a blue attribute box around the limit of the screen. Just wondering if the code wizards here can scare the hell out of him by showing the minimum number of bytes it takes :mrgreen:

My first example, can definitely be improved upon.

29 bytes.

Code: Select all

org 32768
	ld hl,22528
	ld a,10
	ld de,31
	ld b,33
lp0:	ld (hl),a
	inc l
	djnz lp0
	ld b,22
lp1:	add hl,de
	ld (hl),a
	dec hl
	ld (hl),a
	inc hl
	inc hl
	djnz lp1
lp2:	ld (hl),a
	inc l
	jr nz,lp2
	ret
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Machine code challenge

Post by Ralf »

A way to save one byte :)

Use C as attribute instead of A

Code: Select all

org 32768
	ld hl,22528
	ld de,31
	ld bc,33*256+10  <- use C as attribute instead of A
lp0:	ld (hl),c
	inc l
	djnz lp0
	ld b,22
lp1:	add hl,de
	ld (hl),c
	dec hl
	ld (hl),c
	inc hl
	inc hl
	djnz lp1
lp2:	ld (hl),c
	inc l
	jr nz,lp2
	ret
User avatar
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Machine code challenge

Post by MatGubbins »

Hopefully this is correct.....

Code: Select all

Box     LD  HL,22528              ; 3
        LD  C,10 ; colour         ; 2
        LD  DE,31                 ; 3
        LD  B,E                   ; 1
        CALL loopalongB           ; 3
        LD B,22                   ; 2
loopdown
        LD (HL),C                 ; 1
        INC HL                    ; 1
        LD (HL),C                 ; 1
        ADD HL,DE                 ; 1
        DJNZ loopdown             ; 2
        LD B,33                   ; 2
loopalongB
        LD (HL),C                 ; 1
        INC HL                    ; 1
        DJNZ loopalongB           ; 2
        RET                       ; 1    ;27
        
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: Machine code challenge

Post by R-Tape »

Nice.

That last INC HL could be INC L, saving you 2 errrr T-states :|
Hikaru
Microbot
Posts: 100
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Machine code challenge

Post by Hikaru »

24 bytes. Still plenty of ways to go, I believe.

Code: Select all

	ld hl,#5800
	ld de,#4917
	cp a
.lp2
	ld (hl),d
	inc hl
	ld b,30
.lp1
	jr nz,$+3
	ld (hl),d
	inc hl
	djnz .lp1
	ld (hl),d
	inc hl
	dec e
	jp p,.lp2
	ret
Inactive account
User avatar
Spud
Manic Miner
Posts: 372
Joined: Sun Nov 12, 2017 8:50 pm
Contact:

Re: Machine code challenge

Post by Spud »

I tried to be different, came up with two approaches, both more bytes than R-Tape's original and gave up.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: Machine code challenge

Post by R-Tape »

I tried filling the screen then putting a solid white box inside, nope.

I can see Hikaru's makes a blue box, but I still need to work out how it's doing it...
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Machine code challenge

Post by Ast A. Moore »

R-Tape wrote: Wed Nov 22, 2017 4:15 pm I can see Hikaru's makes a blue box, but I still need to work out how it's doing it...
I commented his code. Hope I didn’t make it sound more complicated than it actually is:

Code: Select all

	ld hl,$5800	;begining of attr area
	ld de,$4917	;attr into D, 23 (no. of rows-1) into E
	cp a		;set the Zero flag
lp2
	ld (hl),d	;place attr
	inc hl		;next cell
	ld b,30		;column counter
lp1
	jr nz,$+3	;skip next instruction if Z is 0
	ld (hl),d	;place attr
	inc hl		;next cell
	djnz lp1	;until we reach next row
	ld (hl),d	;place attr
	inc hl		;next cell
	dec e		;we're on the next row (affects the Z flag):
			;while E≥0, Z will be reset and LD (HL),D in lp1
			;will be skipped
	jp p,lp2	;loop while positive (so as to use another flag)
	ret
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
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Machine code challenge

Post by MatGubbins »

Hikaru's code looks alien to me too. I Changed it to...

Code: Select all

        ld hl,#5800    
        ld de,#4917    
        cp a               
lp2
        ld (hl),d
        inc hl
        ld b,30
lp1
        jr nz,skip
        ld (hl),d
skip    inc hl
        djnz lp1
        ld (hl),d
        inc hl
        dec e
        jp p,lp2
        ret
and placed a few HALTs before the LD (HL),D command
Do one line at a time, run and watch to see how it does it.
Clever, very clever indeed.
krt17
Drutt
Posts: 7
Joined: Wed Nov 22, 2017 6:54 pm

Re: Machine code challenge

Post by krt17 »

from Russia with love
23

Code: Select all

frame
	ld	hl, 0x5aff
	xor	a
	ld	a, 0x17
.l2
	ld	bc, 0x1e49
	ld	(hl), c
	dec	hl
.l1
	jr	nz, $+3
	ld	(hl), c
	dec	hl
	djnz	.l1
	ld	(hl), c
	dec	hl
	dec	a
	ret	m
	jr	.l2
Hikaru
Microbot
Posts: 100
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Machine code challenge

Post by Hikaru »

Err, you might be looking too much into it. There's basically two loops, the outer one makes the edges, the inner one fills the horizontal lines if the Z flag is set. Before entering the overall loop, we set the Z flag using CP A so the first line is filled horizontally. Next time this happens is at the last line, where Z is set by DEC E when E reaches 0. In between these two it's just putting the edges, and the inner loop does nothing apart from INC HL x 30.

Ast A. Moore wrote: Wed Nov 22, 2017 6:17 pm
R-Tape wrote: Wed Nov 22, 2017 4:15 pm I can see Hikaru's makes a blue box, but I still need to work out how it's doing it...
I commented his code. Hope I didn’t make it sound more complicated than it actually is
Scratch that, now I have no idea what's going on in there myself. Well done! :lol:

krt17 wrote: Wed Nov 22, 2017 6:57 pm from Russia with love
23
Not bad. I knew something was off about that JP P: RET construction. :)
Inactive account
krt17
Drutt
Posts: 7
Joined: Wed Nov 22, 2017 6:54 pm

Re: Machine code challenge

Post by krt17 »

and ofc jp p ret equ ret m jr
User avatar
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Machine code challenge

Post by MatGubbins »

I had some trouble getting the asm from krt17 to work properly with EmuZWin, it was due to the 0x before the hex, so they were stripped out and replaced with dec for those of us without a working hex head. The colour variable was moved to the start to allow for different borders to be set by the user. It is a magnificent bit of code, that works the screen backwards (bung some HALTs in there to see it in action) and in less bytes too.

Code: Select all

Frame     LD      C,10 ; colour
        ld      hl,23295

        xor     a
        ld      a,23
L2
        ld      b,30
        ld      (hl), c
        dec     hl
L1
        jr      nz,skip
        ld      (hl), c
skip    dec     hl
        djnz    L1

        ld      (hl), c
        dec     hl
        dec     a
        ret     m
        jr      L2
        
Asking for snippets of code from forum members shows us that we all write things in a different way, upper/lower case, hex/dec mixtures
The result is that we learn from each other, new ways of improving the code and hopefully squeezing an extra graphic or something into the end game.
Dave, you have 6 extra bytes so far... use them wisely!
Post Reply