Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

x86-64 Toggle a bit

Anyone have any suggestions for toggling (EDIT (clarifying): switch from a 1 to a 0 or vice versa) a given bit within a number in 64 bit x86 assembly?

Something like:
Code
toggle:
mov rax, 0x0 # zero rax
mov rax, rdi  # move first argument (the number whose bit will be toggled) to rax
mov rbx, 0x0 # zero rbx
mov rbx, rsi   # move second argument (the bit number within the first argument to be toggled) to rbx

# toggle the bit?

ret # return the value after having the specified bit toggled (which is sitting in rax)

I'm not quite sure how to manipulate the number. I figured I could do a loop and jump when I am sitting on the specified bit, but I'm thinking there is an easier way to do this that I'm unaware of.

Any and all suggestions are greatly appreciated!
Keep calm, chive on.
Easiest way to toggle a bit is using to bitwise XOR the value with the nth bit. Formula would be like (x ^ (1 << y)) where x is your value, y is the bit number, ^ is bitwise xor and << is bitwise lshift.
As @Gorzoid mentioned, XOR'ing would be the simplest solution. I'm not quite sure why you're nullifying the registers first before moving data to them, or why you are moving the second arg from rsi to rbx, though. I'd just do the following:
Code
toggle:
mov rax, rdi
xor rax, rsi
ret
PHP internals developer


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Assembly] by SIM-3149568May 19, 2020 03:36 AM 8 352 May 19, 2020 03:36 AM
Last Post: Jean-Jacques Rousseau
[Assembly] by RCEJan 12, 2020 08:58 AM 0 717 Jan 12, 2020 08:58 AM
Last Post: RCE
[Assembly] by MordiciaMay 2, 2019 11:36 AM 0 307 May 2, 2019 11:36 AM
Last Post: Mordicia