0 Members and 1 Guest are viewing this topic.
AND mask
Possibly one of the best uses of XOR is in combination with AND, to act as a bitwise multiplexer.For example, this uses the bits of C to select bits from A and B: if the bit in C is 1, that bit is selected from A, and if the bit in C is 0, that bit is selected from B.xor band cxor b
Quote from: calc84maniac on December 24, 2011, 12:35:24 amPossibly one of the best uses of XOR is in combination with AND, to act as a bitwise multiplexer.For example, this uses the bits of C to select bits from A and B: if the bit in C is 1, that bit is selected from A, and if the bit in C is 0, that bit is selected from B.xor band cxor bWhat could you/would you want to use this for?
;ix=masked byte to draw;ix+1=byte mask;hl=location in gbuf to draw to ld a,(ix+1) ;load mask into a and (hl) ;and the mask onto the byte in gbuf or (ix) ;draw the byte ld (hl),a ;update gbuf
ld a,(ix) xor (hl) and (ix+1) xor (hl) ld (hl),a
drawPlayerLoop: push bcdrawPlayerRow ld d,(ix+32) ;sprite mask ld e,(ix+33) ld a,$ff ;a 1 means the gbuf bit will be preserved dec b \ inc b \ jr z,skipMaskClip ;if b=0, no need to rotate scf \ rr d \ rr e \ rra \ djnz $-6 ;rotate mask b bitsskipMaskClip: and (hl) ;a = mask to the furthest right ld (hl),a ;store masked gbuf dec hl ;next byte... ld a,(hl) and e ld (hl),a dec hl ;last byte ld a,(hl) and d ld (hl),a pop bc ;recall b=x offset, how many bits to rotate inc hl ;return hl to starting gbuf position inc hl push bc ;store bc again ld d,(ix) ;sprite ld e,(ix+1) xor a ;empty a to receive flowover from shifting cp b jp z,skipSpriteClip srl d \ rr e \ rra \ djnz $-5 ;rotate spriteskipSpriteClip: or (hl) ;load sprite into masked gbuf ld (hl),a ;update gbuf dec hl ;next byte, etc. ld a,(hl) or e ld (hl),a dec hl ld a,(hl) or d ld (hl),a inc ix ;next sprite row inc ix ld de,14 ;jump to next row in gbuf, +2 (draw rightmost byte first) add hl,de pop bc dec c jp nz, drawPlayerLoop
;Inputs:; DE is the line to jump to; (965Bh) points to the start of the program; (965Fh) points to the end of the program;Notes:; The two addresses above are set accordingly while a BASIC program is running push de ld de,(965Bh) ;This is the start of the currently running program ld hl,(965Fh) ;This is the end of the currently running program or a sbc hl,de ;HL is now the size of the program ex de,hl ld b,d ld c,e pop de dec hl;HL points to the start of the program;DE is the line to jump to;BC are the size of the program;A is the newline tokenSearchLoop: dec de ld a,d \ or e jr z,StoLine ld a,3Fh cpir ret nz ;this means BC hit 0. In other words, the line does not exist.;the byte is 3Fh, but we need to make sure that it is not part of a two-byte token dec hl \ dec hl ld a,(hl) inc hl \ inc hl cp 7Eh \ jr z,SearchLoop+5 cp $AA \ jr z,SearchLoop+5 cp $BB \ jr z,SearchLoop+5 cp $EF \ jr z,SearchLoop+5 cp 5Fh \ jr z,SearchLoop sub 5Ch \ jr c,SearchLoop sub 7 \ jr c,SearchLoop+5 jr SearchLoopStoLine: dec hl ld (965Dh),hl ;this loads the address to the BASIC program counter ret
ld bc,9 ld hl,OP1 xor a cpir jr nz,$+3 ;This means the string ended at maximal length ccf ld a,9 \ sbc a,c ld c,a
push bc xor a cpir pop hl jr nz,$+3 ;This means the string ended at maximal length ccf sbc hl,bc ;the c flag was reset by 'xor a', so hl is the length ret