I recently posted a content which was related to bit manipulation in C programming and now I decided to show you how to do same things in ARM assembly languages. In advance, I would like to mention that I am learning ARM assembly language currently and I am tring to share my learning process on my blog. On my blog, I try to share with you my knowledge and explain some topics which I have learnt. In this post I would like to show you only how to do the bit manipulation. However, if you want to broaden your knowledge in the topic of ARM, I recommend you to obtain documents provided by ARM and read details about each instruction.
SETTING A BIT
; SETting a bit MOV R0,#variable MOV R1,#1 ORR R0,R1,LSL #position_of_bit
RESETTING A BIT
; RESETting a bit MOV R0,#variable MOV R1,#1 BIC R0,R1, LSL #position_of_bit
TOGGLING A BIT
; TOGGLing a bit MOV R0,#variable MOV R1,#1 EOR R0,R1, LSL #position_of_bit
CHECKING A BIT
; CHECKing a bit MOV R0,#variable MOV R1,#1 AND R2,R0,R1, LSL #position_of_bit
R0 contains the original value of #variable and we do not want to impact this value while checking a bit. So, we use R2 register to store the result.
While writting this content, one question came to my mind – cannot I use shift operators like I was doing in C? Then, I decided to give it a try. And… The answer is YES! So lets examine it 🙂
;Setting a bit MOV R0,#variable ORR R0,#(1<<position_of_bit) ;Resetting a bit MOV R1,#variable AND R1,#~(1<<position_of_bit) ;Toggling a bit MOV R2,#variable EOR R2,#(1<<position_of_bit) ;Checking a bit MOV R3,#variable AND R4,R3,#(1<<position_of_bit)
While checking a bit, you should not forget to save a result in another register in order not to affect the original value of variable.
I realised that learning assembly language caused that I am more keen C programming. However, I would like to keep learning assembly to have better understanding of hardware and computer architecture. Additionaly, I believe that in the nearest future I will find some reasons to use assembly by mixing it with C.