Help with attributes and logical and (z80 assembler)

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Hi,

I wrote a little routine to put random attributes around the border of the screen. Later I'm going to rotate the attributes.

Image

I would like the random number to be between 0 and 63 to avoid the flashing and bright attributes. I thought adding an

Code: Select all

and 63
would be the answer, but I appear to end up with cells without an attribute set. Could anyone help me out please?

Many thanks

Code: Select all

org $6000

main

    call clsscr
    

    call text

    ld hl,22528         ;start of attribute memory
    ld de,32            ;used to jump up and down rows on left and right
    ld b,31             ;counter for columns




top                     ;fill the top row

    push hl
    call random
    and 63
    pop hl
    ld (hl),a
    inc hl
    djnz top

    ld b,23

right                   ;fill the right hand side
    push hl
    call random
    and 63
    pop hl
    ld (hl),a
    add hl,de
    djnz right

    ld b,31

bottom                  ;fill the bottom
    push hl
    call random
    and 63
    pop hl
    ld (hl),a
    dec hl
    djnz bottom


    ld b,23

left                   ;fill the left
    push hl
    call random
    and 63
    pop hl
    ld (hl),a
    sbc hl,de
    djnz left

waitkey                 ;From JC Book - Thanks!
    ld hl,23560
    ld (hl),0
wait
    ld a,(hl)
    cp 0
    jr z,wait
    ret


random                  ; From JC Book - Thanks!
    ld hl,(seed)        ; Pointer        
    ld a,h
    and 31              ; keep it within first 8k of ROM.
    ld h,a        
    ld a,(hl)           ; Get "random" number from location.
    inc hl              ; Increment pointer.
    ld (seed),hl
    ret 
seed defw 0

clsscr                  ;Thanks to seven-fff
    xor a
    ld hl,22528
    ld de,22528 +1
    ld bc,768-1
    ld (hl),a
    ldir
    ret

text                    ;Thanks to seven-fff for the idea & JC for the code
    ld a,2              ; upper screen        
    call 5633           ; open channel        
    ld de,string        ; address of string        
    ld bc,eostr-string  ; length of string to print        
    call 8252           ; print our string        
    ret
string defb 22,1,0,32,22,7,6,17,0,19,1,16,2,83,16,6,80,16,5,69,16,3,67,16,7,84,16,4,82,16,2,85,16,6,77,16,0,32
       defb 16,5,67,16,3,79,16,7,77,16,4,80,16,2,85,16,6,84,16,5,73,16,3,78,16,7,71
eostr  equ $ 

END $6000
User avatar
bob_fossil
Manic Miner
Posts: 654
Joined: Mon Nov 13, 2017 6:09 pm

Re: Help with attributes and logical and (z80 assembler)

Post by bob_fossil »

Code: Select all

and 63 
will mask off the accumulator to give you the lower 6 bits but this will still let through values like 0 - 7 (PAPER 0 : INK 1 - 7) and 56 - 63 (PAPER 7 : INK 1 - 7). If you want solid colours for the attributes you may want to generate a random number between 0 - 7 and then multiply it by 8 for a PAPER attribute which will give you a solid colour attribute. Apologies if I'm misunderstanding what you're trying to achieve.
Last edited by bob_fossil on Sat Jul 04, 2020 5:44 pm, edited 1 time in total.
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Thanks [mention]bob_fossil[/mention]

You made me have a thought. I foolishly forgot that the grey is paper white without the bright. Apologies.

I will look at the option you suggest, which would certainly be an improvement. So if I create a random number between 0 and 7 with and 7, then do a bit rotate 3 times would that work?

Code: Select all

    call random
    and 7
    sla a
    sla a
    sla a
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: Help with attributes and logical and (z80 assembler)

Post by R-Tape »

You could replace the "random" subroutine with a just "LD A,R"

It's not random, but you get a pretty colour cycle, and bit 7 is never set, so need for ANDs or CPs.
User avatar
utz
Microbot
Posts: 114
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Help with attributes and logical and (z80 assembler)

Post by utz »

PeterJ wrote: Sat Jul 04, 2020 5:42 pm Thanks @bob_fossil

You made me have a thought. I foolishly forgot that the grey is paper white without the bright. Apologies.

I will look at the option you suggest, which would certainly be an improvement. So if I create a random number between 0 and 7 with and 7, then do a bit rotate 3 times would that work?

Code: Select all

    call random
    and 7
    sla a
    sla a
    sla a
Instead of doing that, you can also just AND with 56 ;)
User avatar
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Help with attributes and logical and (z80 assembler)

Post by MatGubbins »

For your text string:

Code: Select all

string defb 22,7,6, 17,0, 19,1, 16,2,"S", 16,6,"P", 16,5,"E", 16,3,"C", 16,7,"T", 16,4,"R", 16,2,"U", 16,6,"M", 16,0," "
       defb 16,5,"C", 16,3,"O", 16,7,"M", 16,4,"P", 16,2,"U", 16,6,"T", 16,5,"I", 16,3,"N", 16,7,"G"
       
Placing quotes around the letters make it easier for you to read. There was also a stray 22,1,0,32 ; at 1,0,"space" at the start.

Great start into the world of machine code Peter!
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Thank you [mention]utz[/mention]. I will need to look at that one written down in binary so I understand!
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

MatGubbins wrote: Sat Jul 04, 2020 6:09 pm Placing quotes around the letters make it easier for you to read. There was also a stray 22,1,0,32 ; at 1,0,"space" at the start.

Great start into the world of machine code Peter!
Thanks [mention]MatGubbins[/mention]. It's only taken me 35 years and 100s of false starts!

Good idea about improving readability. The printing of a space is to clear the B from Bytes which appears after loading the code.
User avatar
Morkin
Bugaboo
Posts: 3251
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Help with attributes and logical and (z80 assembler)

Post by Morkin »

Always found playing with display attributes quite relaxing, it makes my brain hurt less than display pixels... 8-)

One small suggestion, you might want to move "SPECTRUM COMPUTING" one character to the right... ;)
My Speccy site: thirdharmoniser.com
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Agreed [mention]Morkin[/mention]. The attribute memory is nice an easy.

Moved one column!
User avatar
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Help with attributes and logical and (z80 assembler)

Post by MatGubbins »

PeterJ wrote: Sat Jul 04, 2020 6:16 pm
MatGubbins wrote: Sat Jul 04, 2020 6:09 pm Placing quotes around the letters make it easier for you to read. There was also a stray 22,1,0,32 ; at 1,0,"space" at the start.

Great start into the world of machine code Peter!
Thanks @MatGubbins. It's only taken me 35 years and 100s of false starts!

Good idea about improving readability. The printing of a space is to clear the B from Bytes which appears after loading the code.
Ah! I see, so you need the CLS rom routine...or be clever and write a faster one! Hang on, the code is there in your program, just need to change a few numbers and you're there.
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Thank you for pointing me in the right direction [mention]MatGubbins[/mention], but not giving me the answer. That is a great way to learn. I was clearing the attribute memory, rather than the pixel memory!

Code: Select all

clsscr              
    xor a
    ld hl,16384
    ld de,16384 +1
    ld bc,6144-1
    ld (hl),a
    ldir
    ret
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Help with attributes and logical and (z80 assembler)

Post by PeterJ »

Many thanks to you all for the suggestions. My finished code (for this evening) is:

Code: Select all

org $6000

main
    call clsscr
    call border
    call text
    ld hl,22528         ;start of attribute memory
    ld de,32            ;used to jump up and down rows on left and right
    ld b,31             ;counter for columns

top                     ;fill the top row
    push hl
    call random
    and 56
    pop hl
    ld (hl),a
    inc hl
    djnz top
    ld b,23

right                   ;fill the right hand side
    push hl
    call random
    and 56
    pop hl
    ld (hl),a
    add hl,de
    djnz right
    ld b,31

bottom                  ;fill the bottom
    push hl
    call random
    and 56
    pop hl
    ld (hl),a
    dec hl
    djnz bottom
    ld b,23

left                   ;fill the left
    push hl
    call random
    and 56
    pop hl
    ld (hl),a
    sbc hl,de
    djnz left

waitkey                 ;From JC Book - Thanks!
    ld hl,23560
    ld (hl),0
wait
    ld a,(hl)
    cp 0
    jr z,wait
    ret

random                  ;From JC Book - Thanks!
    ld hl,(seed)        ;Pointer        
    ld a,h
    and 31              ;keep it within first 8k of ROM.
    ld h,a        
    ld a,(hl)           ;Get "random" number from location.
    inc hl              ;Increment pointer.
    ld (seed),hl
    ret 
seed defw 0

clsscr                  ;Thanks to seven-fff
    xor a
    ld hl,16384
    ld de,16384 +1
    ld bc,6144-1
    ld (hl),a
    ldir
    ret

text                    ;Thanks to seven-fff for the idea
    ld a,2              ;upper screen        
    call 5633           ;open channel        
    ld de,string        ;address of string        
    ld bc,eostr-string  ;length of string to print        
    call 8252           ;print our string        
    ret
string defb 22,7,7,17,0,19,1,16,2,"S",16,6,"P",16,5,"E",16,3,"C",16,7,"T",16,4,"R",16,2,"U",16,6,"M",16,0," "
       defb 16,5,"C",16,3,"O",16,7,"M",16,4,"P",16,2,"U",16,6,"T",16,5,"I",16,3,"N",16,7,"G"
eostr  equ $ 

border
    ld a,0
    out ($FE),a
    ret

END $6000
User avatar
utz
Microbot
Posts: 114
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Help with attributes and logical and (z80 assembler)

Post by utz »

PeterJ wrote: Sat Jul 04, 2020 6:14 pm Thank you @utz. I will need to look at that one written down in binary so I understand!
Hehe, I was thinking whether I should give an explanation, but I figured you could work it out on your own. And ideed you immediately had the right idea: Looking at the number in binary will give you the answer as to why this works.

That's one reason why I prefer using hexadecimal numbers: They're easier to translate into binary. And now for some practise:
https://flippybitandtheattackofthehexad ... ase16.com/

Best of luck with your assembly adventures!
User avatar
MatGubbins
Dynamite Dan
Posts: 1238
Joined: Mon Nov 13, 2017 11:45 am
Location: Kent, UK

Re: Help with attributes and logical and (z80 assembler)

Post by MatGubbins »

Brilliant. You feel like a king for the day!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Help with attributes and logical and (z80 assembler)

Post by Ast A. Moore »

utz wrote: Sat Jul 04, 2020 7:48 pm And now for some practise:
https://flippybitandtheattackofthehexad ... ase16.com/
Goodness me! After five minutes of utter frustration I realized you could flip the bits by hitting the number keys! (Yeah, I didn’t scroll down for the instructions.) :lol:
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.
azesmbog
Manic Miner
Posts: 306
Joined: Sat May 16, 2020 8:43 am

Re: Help with attributes and logical and (z80 assembler)

Post by azesmbog »

PeterJ wrote: Sat Jul 04, 2020 5:22 pm Hi,
I wrote a little routine to put random attributes around the border of the screen.
He also wrote a program with colored squares along the border of the screen. only for some reason they were outside the border :)
https://youtu.be/mfzlofEvy3U?t=22
User avatar
Lethargeek
Manic Miner
Posts: 735
Joined: Wed Dec 11, 2019 6:47 am

Re: Help with attributes and logical and (z80 assembler)

Post by Lethargeek »

PeterJ wrote: Sat Jul 04, 2020 7:21 pm Thank you for pointing me in the right direction @MatGubbins, but not giving me the answer. That is a great way to learn. I was clearing the attribute memory, rather than the pixel memory!

Code: Select all

clsscr              
    xor a
    ld hl,16384
    ld de,16384 +1
    ld bc,6144-1
    ld (hl),a
    ldir
    ret

Code: Select all

clsscr              
    ld hl,16384
    ld de,16384 +1
    ld bc,6144-1
    ld (hl),l
    ldir
    ret
XoRRoX
Manic Miner
Posts: 231
Joined: Wed Jul 11, 2018 6:34 am

Re: Help with attributes and logical and (z80 assembler)

Post by XoRRoX »

Lethargeek wrote: Sun Jul 05, 2020 10:20 am

Code: Select all

clsscr              
    ld hl,16384
    ld de,16384 +1
    ld bc,6144-1
    ld (hl),l
    ldir
    ret
Smart. I realised that this will make more sense when numbers are used in hex.
User avatar
Guesser
Manic Miner
Posts: 639
Joined: Wed Nov 15, 2017 2:35 pm
Contact:

Re: Help with attributes and logical and (z80 assembler)

Post by Guesser »

utz wrote: Sat Jul 04, 2020 7:48 pm That's one reason why I prefer using hexadecimal numbers: They're easier to translate into binary. And now for some practise:
https://flippybitandtheattackofthehexad ... ase16.com/
That's jolly addictive. I got my high score up to 39 before the arm cramp stopped me playing more.
Post Reply