Page 1 of 1
Machine code challenge
Posted: Wed Nov 22, 2017 11:59 am
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
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 12:09 pm
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 1:18 pm
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 1:59 pm
by R-Tape
Nice.
That last INC HL could be INC L, saving you 2 errrr T-states
Re: Machine code challenge
Posted: Wed Nov 22, 2017 3:25 pm
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 3:43 pm
by Spud
I tried to be different, came up with two approaches, both more bytes than R-Tape's original and gave up.
Re: Machine code challenge
Posted: Wed Nov 22, 2017 4:15 pm
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...
Re: Machine code challenge
Posted: Wed Nov 22, 2017 6:17 pm
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 6:26 pm
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.
Re: Machine code challenge
Posted: Wed Nov 22, 2017 6:57 pm
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
Re: Machine code challenge
Posted: Wed Nov 22, 2017 7:06 pm
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!
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.
Re: Machine code challenge
Posted: Wed Nov 22, 2017 7:07 pm
by krt17
and ofc jp p ret equ ret m jr
Re: Machine code challenge
Posted: Wed Nov 22, 2017 11:13 pm
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!