0

I am writing a toggle bit which toggles the nth bit of a number function in x86-64 bit: But it is not working, especially for when the nth bit is 0 or 3. How can i fix this? Please help.

toggle_bit:
           push rdi
           push rsi
           shl rsi, 1
           xor rid, rsi
           mov rax, rdi
           pop rdi
           pop rsi 
           ret
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
dp123
  • 21
  • 5
  • If this is 64-bit SYs V ABI you don't need to preserve RDI or RSI so no need to save and restore them. – Michael Petch Nov 10 '17 at 17:30
  • Is this a function taking 2 parameters where the bit number to toggle is in the second argument and the value in the first? What OS? – Michael Petch Nov 10 '17 at 17:58
  • Something like `mov ecx, esi` `mov esi, 1` `shl rsi, cl` `xor rdi, rsi` `mov rax, rdi` `ret` might be close to what you might be looking for. the _CL_ register is the only register that the bit shift functions take as an operand where you can shift a value a variable number of places. As well if you have a destination operand that is a 32-bit register, the CPU automatically zero extends it through the entire 64-bit register. – Michael Petch Nov 10 '17 at 17:58
  • @MichaelPetch you are the best!!!! it works now!! thank you so so much!! :D – dp123 Nov 10 '17 at 18:26

0 Answers0