%include "asm_io.inc"
segment .data
outmsg1 db "Input Integer: ", 0
outmsg2 db "After Operation": ", 0
segment .bss
input1 resd 1
input2 resd 1
segment .text
global asm_main
asm_main:
enter 0,0
pusha
mov eax, outmsg1
call print_string
call read_int
call print_nl
dump_regs 1
rol eax, 8
rol ax, 8
ror eax,8
mov ebx, 0
mov ebx, eax
dump_regs 2
popa
mov eax, 0
leave
ret
given the above assembly program which swaps the highest value Byte with the lowest value byte of a given integer. im trying to figure out how to make it swap the highest value BIT with the lowest value BIT.
im somewhat stuck, maybe you can help me out
How about this? The input is in ecx or any other register you like.
// initial state: ECX = A...B
ror ecx // ECX = BA..., CF = B
bt ecx, 30 // ECX = BA..., CF = A
rcl ecx, 2 // ECX = ...AB, CF = A
ror ecx // ECX = B...A
As Peter Cordes indicated, if you are optimizing for performance you might want to change the code like this:
ror ecx
bt ecx, 30
adc ecx, ecx
adc ecx, ecx
ror ecx
This is because adc r,r performs better than rcl r,imm or rcl r on modern microarchitectures.
You only have to toggle the both bits, if they differ. You don't need to do anything, if the bits both are set or both are cleared:
%include "asm_io.inc"
segment .text
global asm_main
asm_main:
enter 0,0
pusha
; Test values, comment it as needed
; mov eax, 0x00123400 ; Bit0 and Bit31 are cleared
mov eax, 0x00123401 ; Bit0 is set, Bit 31 is cleared
; mov eax, 0x80123400 ; Bit0 is cleared, Bit31 is set
; mov eax, 0x80123401 ; Bit0 and Bit31 are set
dump_regs 1
bt eax, 0 ; Copy the least significant bit into CF
setc cl ; Copy CF into register CL
bt eax, 31 ; Copy the most significant bit into CF
setc ch ; Copy CF into register CH
cmp cl, ch ; Compare the bits
je skip ; No operation, if they don't differ
btc eax, 0 ; Toggle the least significant bit
btc eax, 31 ; Toggle the most significant bit
skip:
dump_regs 2
popa
mov eax, 0
leave
ret
Another idea is to use TEST and to operate according to the flags - advantage: you don't need an additional register:
%include "asm_io.inc"
segment .text
global asm_main
asm_main:
enter 0,0
pusha
; Test values, comment it as needed
; mov eax, 0x00123400 ; ZF PF
mov eax, 0x00123401 ; - -
; mov eax, 0x80123400 ; SF PF
; mov eax, 0x80123401 ; SF
test eax, 0x80000001
dump_regs 1
jns j1
jnp skip
j1:
jz skip
doit: ; Touch the bits if (SF & PF) or (!SF & !PF)
btc eax, 0 ; Toggle the least significant bit
btc eax, 31 ; Toggle the most significant bit
skip:
dump_regs 2
popa
mov eax, 0
leave
ret
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With