Machine code challenge

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

Machine code challenge

Post by R-Tape » Wed Nov 22, 2017 11:59 am

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
0 x

Ralf
Microbot
Posts: 137
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Machine code challenge

Post by Ralf » Wed Nov 22, 2017 12:09 pm

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
0 x

User avatar
MatGubbins
Dizzy
Posts: 84
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Machine code challenge

Post by MatGubbins » Wed Nov 22, 2017 1:18 pm

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
        
0 x
Bomb Munchies Ver1930 17th Nov 2017 (look for the blue download box ) If you get a time-out message and live in the UK then try after 9pm-3am.
Send me a PM and I can email it to you too. Kent, UK

User avatar
R-Tape
Site Admin
Posts: 276
Joined: Thu Nov 09, 2017 11:46 am

Re: Machine code challenge

Post by R-Tape » Wed Nov 22, 2017 1:59 pm

Nice.

That last INC HL could be INC L, saving you 2 errrr T-states :|
0 x

Hikaru
Dizzy
Posts: 53
Joined: Mon Nov 13, 2017 1:42 pm
Location: Russia
Contact:

Re: Machine code challenge

Post by Hikaru » Wed Nov 22, 2017 3:25 pm

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
3 x

User avatar
Spud
Berk
Posts: 46
Joined: Sun Nov 12, 2017 8:50 pm

Re: Machine code challenge

Post by Spud » Wed Nov 22, 2017 3:43 pm

I tried to be different, came up with two approaches, both more bytes than R-Tape's original and gave up.
0 x

User avatar
R-Tape
Site Admin
Posts: 276
Joined: Thu Nov 09, 2017 11:46 am

Re: Machine code challenge

Post by R-Tape » Wed Nov 22, 2017 4:15 pm

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...
0 x

User avatar
Ast A. Moore
Dizzy
Posts: 78
Joined: Mon Nov 13, 2017 3:16 pm

Re: Machine code challenge

Post by Ast A. Moore » 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:

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
1 x
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
Dizzy
Posts: 84
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Machine code challenge

Post by MatGubbins » Wed Nov 22, 2017 6:26 pm

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.
0 x
Bomb Munchies Ver1930 17th Nov 2017 (look for the blue download box ) If you get a time-out message and live in the UK then try after 9pm-3am.
Send me a PM and I can email it to you too. Kent, UK

krt17
Berk
Posts: 7
Joined: Wed Nov 22, 2017 6:54 pm

Re: Machine code challenge

Post by krt17 » Wed Nov 22, 2017 6:57 pm

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
1 x

Post Reply