© Jo Van Hoey 2019
J. Van HoeyBeginning x64 Assembly Programminghttps://doi.org/10.1007/978-1-4842-5076-1_17

17. Bit Manipulations

Jo Van Hoey1 
(1)
Hamme, Belgium
 

You already know that you can set or clear bits using bit operations such as and, xor, or, and not. But there are other ways to modify individual bits: bts for setting bits to 1, btr for resetting bits to 0, and bt for testing if a bit is set to 1.

Other Ways to Modify Bits

Listing 17-1 shows the example code.
; bits3.asm
extern printb
extern printf
section .data
      msg1  db   "No bits are set:",10,0
      msg2  db   10,"Set bit #4, that is the 5th bit:",10,0
      msg3  db   10,"Set bit #7, that is the 8th bit:",10,0
      msg4  db   10,"Set bit #8, that is the 9th bit:",10,0
      msg5  db   10,"Set bit #61, that is the 62nd bit:",10,0
      msg6  db   10,"Clear bit #8, that is the 9th bit:",10,0
      msg7  db   10,"Test bit #61, and display rdi",10,0
      bitflags   dq      0
section .bss
section .text
      global main
main:
push  rbp
mov   rbp,rsp
      ;print title
      mov   rdi, msg1
      xor   rax,rax
      call  printf
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
;set bit 4 (=5th bit)
      ;print title
      mov   rdi, msg2
      xor   rax,rax
      call  printf
      bts   qword [bitflags],4    ; set bit 4
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
;set bit 7 (=8th bit)
      ;print title
      mov   rdi, msg3
      xor   rax,rax
      call  printf
      bts   qword [bitflags],7    ; set bit 7
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
;set bit 8 (=9th bit)
      ;print title
      mov   rdi, msg4
      xor   rax,rax
      call  printf
      bts   qword [bitflags],8    ; set bit 8
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
;set bit 61 (=62nd bit)
      ;print title
      mov   rdi, msg5
      xor   rax,rax
      call  printf
      bts   qword [bitflags],61   ; set bit 61
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
;clear bit 8 (=9th bit)
      ;print title
      mov   rdi, msg6
      xor   rax, rax
      call  printf
      btr   qword [bitflags],8    ; bit reset 8
      ;print bitflags
      mov   rdi, [bitflags]
      call  printb
; test bit 61 (will set carry flag CF if 1)
      ;print title
      mov   rdi, msg7
      xor   rax, rax
      call  printf
      xor   rdi,rdi
      mov   rax,61              ; bit 61 to be tested
      xor   rdi,rdi             ; make sure all bits are 0
      bt    [bitflags],rax      ; bit test
      setc  dil                 ; set dil (=low rdi) to 1 if CF is set
      call  printb              ; display rdi
leave
ret
Listing 17-1

bits3.asm

We again use the printb.c program here; make sure to adapt your makefile or SASM build settings accordingly.

The variable bitflags is the object of study here; we will be manipulating bits in this variable.

The bitflags Variable

Remember that the bit count (the index) starts at 0. This means that in a byte, which has 8 bits, the first bit is at position 0, and the last bit is at position 7. Setting bits to 1 with the instruction bts and resetting bits to 0 with btr is simple: just specify the index of the bit to be changed as the second operand.

Testing a bit is a bit more complicated. Put the index of the bit to be tested in rax and use the instruction bt. If the bit is 1, the carry flag, CF, will be set to 1; otherwise, CF will be 0. Based on the value of the flag, you can direct your program to execute certain instructions or not. In this case, we use a special instruction setc, a conditional set. In this case, the instruction sets dil to 1 if the carry flag is 1. dil is the lower part of rdi; be careful to set rdi to 0 before using setc to set dil. It might well be that the higher bits of rdx are set during the execution of a previous instruction.

The setc instruction is an example of setCC. setCC sets a byte in the operand if the condition in CC is met, where CC is a flag, such as CF (abbreviated as c), ZF CF (abbreviated as z), SF CF (abbreviated as s), and so on. Take a look in the Intel manuals for more details.

Figure 17-1 shows the output of the program.
../images/483996_1_En_17_Chapter/483996_1_En_17_Fig1_HTML.jpg
Figure 17-1

bits3.asm output

Summary

In this chapter, you learned about the following:
  • Setting bits, resetting bits, and examining bits, with btr, bts, and bt

  • The setCC instruction

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.142.232