I completely understand using 'AND %00000111' for example to mask out the highest 5 bits, but many examples in the books seem to use it as a means of 'modulo' or something similar (ie. getting a larger number to fit within a smaller number).
Here are some examples from books:
Code: Select all
; Simple pseudo-random number generator.
; Steps a pointer through the ROM (held in seed), returning
; the contents of the byte at that location.
random ld hl,(seed) ; Pointer
ld a,h
and 31 ; keep it within first 8k of ROM. ;;<- THIS LINE
ld h,a
ld a,(hl) ; Get "random" number from location.
inc hl ; Increment pointer.
ld (seed),hl
ret
seed defw 0
Code: Select all
yx2pix: ;don't worry about how this works yet! just arrive with arrive with B=y 0-192, C=x 0-255
ld a,b ;return with DE at corresponding place on the screen
rra
rra
rra
and %00011000
or %01000000
ld d,a
ld a,b
and 7 ;; <--- THIS LINE
or d
ld d,a
ld a,b
rla
rla
and %11100000
ld e,a
ld a,c
rra
rra
rra
and %00011111
or e
ld e,a
ret
So those two parts confuse me still, then on to the next example. This one slightly more simple for me, I think I realise he uses value 7 because each screen character has 8 lines (0-7), so again he wants to take a high number and reduce it to a number equal or less to 7. Thing that confuses me is yet again, is this biased top heavy? doesn't 'AND 7' just mean that anything above 7 will equal 7? (NOTE: I think i do realise that in this yx2pix function he immediately uses 'OR D' which more than likely is where the formula gets the modulo of the division. But that could be completely wrong, I am badly confused lol.
I have seen other examples of 'AND n' being used to keep numbers within 0-n , every single time I have just used them and not worried too much of how they work, but now I am trying to implement a random spawn function of my own, and the result i have so far doesnt seem very random (in fact there is a distinct pattern in how they are spawned)
(for context, I am trying to build a road scene a' la Horace Goes Skiing. So I am trying to spawn cars on left side of screen at random intervals also with random y position offsets)