Byte saving PRINT AT in machinecode

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Dr beep
Manic Miner
Posts: 381
Joined: Mon Oct 01, 2018 8:53 pm

Byte saving PRINT AT in machinecode

Post by Dr beep »

In most of my games with a PRINT AT I use this

Code: Select all

; PRINT AT B,C;"A"
	ld bc,#030d
	ld a,22
	rst 16
	ld a,b
	rst 16
	ld a,c
	rst 16
	ld a,65
	rst 16
	ret
While coding a VVC3-2022 star on the ZX81 I wondered if a trick I used there would work on the ZX Spectrum too. I found this routine in the ROM to set PRINT AT with BC registers

Code: Select all

; PRINT AT B,C;"A"
	ld bc,#0a01 
	push bc
	call #a9b
	pop bc
	ld a,65
	rst 16
	ret
This routine will not only save 2 bytes but is also a bit quicker.
Can be usefull for shortcoding contest
User avatar
Kweepa
Manic Miner
Posts: 311
Joined: Sat Feb 03, 2018 6:14 pm
Location: Albuquerque, New Mexico

Re: Byte saving PRINT AT in machinecode

Post by Kweepa »

Very nice.
Congrats on winning the contest (by a big margin), by the way!
User avatar
MatGubbins
Dynamite Dan
Posts: 1239
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Byte saving PRINT AT in machinecode

Post by MatGubbins »

Dr beep wrote: Tue Dec 27, 2022 6:59 pm

While coding a VVC3-2022 star on the ZX81 I wondered if a trick I used there would work on the ZX Spectrum too. I found this routine in the ROM to set PRINT AT with BC registers

Code: Select all

; PRINT AT B,C;"A"
	ld bc,#0a01 
	push bc
	call #a9b
	pop bc
	ld a,65
	rst 16
	ret
This routine will not only save 2 bytes but is also a bit quicker.
Can be useful for shortcoding contest

Very nice,
It also works without the surrounding PUSH BC/POP BC instructions. Is there a safety reason for using them within your code?
User avatar
MatGubbins
Dynamite Dan
Posts: 1239
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Byte saving PRINT AT in machinecode

Post by MatGubbins »

This is a nice print routine from Agent X II, from where you have to type in a password to allow the loading of parts 2 and 3. Uses the ROM print routine.

Code: Select all


Org Any
        LD   A,2       ; Upper 21 lines    
        CALL 05633     ; CHAN_OPEN


        CALL PrintRoutine 
  ; directly aftwards place the text to print to the screen
  ; you can use the normal control codes
  ; AT X,Y   22,x,y (0-21,0-31)
  ; INK      16,n   (0-7)
  ; PAPER    17,n  (0-7)
  ; FLASH    18,n  (1 or 0)
  ; BRIGHT   19,n  (1 or 0)
  ; INVERSE  20,n  (1 or 0)
  ; OVER     21,n   (1 or 0)


    
DEFB    22,8,3 ; AT x,y
DEFB    16,1   ; ink blue
DEFB    "PLEASE ENTER THE PASSWORD."
DEFB    31 ; data end marker
; continue with your game code here....
RET


PrintRoutine 
        POP  HL        
        LD   A,(HL)
        INC  HL
        PUSH HL
        CP   31 ; data end marker
        RET  Z
        RST  2  ; print it
        JP   PrintRoutine 

A little slower, but it could save a few more bytes if you have a lot to print.
Post Reply