Jump to content
IGNORED

set nth bit


Danjovic

Recommended Posts

This function will set a bit within an array of elements, from msbit to lsbit.

 

Examples:
; array = 00 00 00 00 00 00 00 ...
;


;     a = 0
; after run...
; array = 80 00 00 00 00 00 00 ...    
;
;     again, now with a = 1
; after run...
; array = c0 00 00 00 00 00 00 ...    
;
;     again, now with a = 8
; after run...
; array = c0 80 00 00 00 00 00 ...    
;
;     again, now with a = 7
; after run...
; array = c1 80 00 00 00 00 00 ...    
;  

 

Here's the code:

define _a $d7 ; address of first element of array
lda #8  ; nth bit of array elements

setnthbit:
      ldx #0
a1:   sec        ; divide by 8, x = quocient, a = reminder-8
      sbc #8
      bcc a2
      inx
      jmp a1
a2:   clc     ; rotate the mask 'a' times   
      adc #9  ; 8 + 1 due to compensate first dey
      tay     ; y is the counter used to rotate the mask
      lda #$80 ; mask = 1000.0000
a3:   dey     ; mask rotate loop
      beq a4
      lsr    
      jmp a3
a4:   ora _a,x  ; set the nth bit
      sta _a,x   
      rts
      


 

Edited by Danjovic
corrected instruction (bcc a2 instead of bmi a2)
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...