Ich finde endlich einen anderen Weg, um das Problem zu lösen. Ihre Antwort war sehr gut, aber der Algorithmus, mit dem Sie mich gestern verbunden haben, ist schwer zu implementieren, wenn Sie nur wenige Minuten Zeit haben, das Problem zu lösen. Ich habe nicht angegeben, aber diese Hausaufgaben waren in der Tat ein Prüfungsproblem mit begrenzter Zeit.
Ich denke, dass der Algorithmus von der Stanford University ist es mehr optimiert, aber ein wenig schwierig zu implementieren, oder liege ich falsch ?!
So, hier ist mein Ideea:
MOV EAX,2862962005 ; Register where the odd bits must be mirrored
; Initialised with an random Number as example
; Result should be putted again in EAX
ODD_MIRRORED:
PUSH EBX ;put the values of the other registers on to the Stack
PUSH ECX ;probably this registers hold valuable datas inside
PUSH EDX
MOV EDX,0 ;EDX is counter, form 0 to 31
MOV EBX,0 ;EBX holds the inverted odd bits during the process
;For Auxiliary use only
iteration:
ROR EAX,1 ;EVEN VALUE? JUMP: EAX rotated to the right
ROL EBX,1 ; EBX rotated to the left (for mirroring)
INC EDX ; counter incremented
MOV ECX,0x00000001 ;ODD VALUE? USE mask and extract only one odd value
;from EAX at a given time
AND ECX,EAX
OR EBX,ECX ;put the value in EBX
ROR EAX,1 ;EAX rotated to the right
ROL EBX,1 ;EBX rotated to the left (for mirroring)
INC EDX ;counter incremented again
CMP EDX,31
JGE end ;if counter is greater or equal 31 jump at the end of the program
JMP iteration
end:
;now EAX was rotated to the initial position
AND EAX,0x55555555 ;we want only just to get away from old odd values with
;the mask 0x55555555 (0101), which is deleting the odd values
OR EAX,EBX ;finally we transfer the mirrored odd bits into EAX from the
;auxiliary variable EBX
POP EDX ;restore EBX,ECX,EDX
POP ECX
POP EBX
RET
ich diese Lösung in NASM/8086 Assembler mit verschiedenen Zahlen überprüft und es funktioniert! : P
Ist dies nur für 8-Bit-Werte, oder benötigen Sie es auch für 16, 32, etc? –
@PaulR Ich fürchte, er braucht es für die Hausaufgaben :-( – hirschhornsalz
Ja, es ist in der Regel nicht nur für 8-Bit-Register! – AsamRegnat