Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying EAX to RAX higher bits?

I am wondering if there is any sequence of instructions without use of any other register to copy the lower 32 bits of RAX to its higher 32 bits. Of course, I want EAX intact as well.

Preferably without using any stack memory, either.

like image 857
masec Avatar asked Feb 02 '18 17:02

masec


People also ask

Is EAX same as Rax?

rax is the 64-bit, "long" size register. It was added in 2003 during the transition to 64-bit processors. eax is the 32-bit, "int" size register. It was added in 1985 during the transition to 32-bit processors with the 80386 CPU.

How many bits is the RAX register?

The 64-bit registers have names beginning with "r", so for example the 64-bit extension of eax is called rax.

What is the value of register EAX?

I know when add integers in assembly, it stores result in the destination. and when we multiply it stores in eax. so if I call the function multiply( 3 , 8 ), the value of eax register after that line should be 120.

What is MOV RAX?

In AT&T syntax, the instruction: mov (%rax), %eax # AT&T syntax. or, equivalently in Intel syntax: mov eax, DWORD PTR [rax] ; Intel syntax. dereferences the memory address stored in rax , reads a 32-bit value from that memory address, and stores it in the eax register.


1 Answers

My try... got headache from the music compo going on here at the demo party (or more likely from the travel here), so I gave up on the imul rax,rax,imm32 used to copy 31 bits and trying to save the 1 bit in sign and then patch the intermediate result, as it turned out to have several problems I didn't foresee.

So I instead opted to chicken-out, and copy only 2x 16 bit words, and then reshuffle in a way how Jester did (I was on a verge of posting idea of xchg al,ah just when he posted answer, I swear).

    ; rax =                           00001234 (bytes, not hexa digits)
    ror     rax, 16                 ; 34000012
    imul    rax, rax, 0x00010001    ; 34001212
    shr     rax, 16                 ; 00340012
    imul    rax, rax, 0x00010001    ; 34341212
    ror     rax, 24                 ; 21234341
    xchg    al, ah                  ; 21234314
    ror     rax, 8                  ; 42123431
    xchg    al, ah                  ; 42123413
    rol     rax, 16                 ; 12341342
    xchg    al, ah                  ; 12341324
    ror     rax, 8                  ; 41234132
    xchg    al, ah                  ; 41234123
    rol     rax, 8                  ; 12341234

A bit shorter (instruction count) variant (with a funny twist of using rol ...,8 instructions only since 5th one):

    ; eax =                           00001234 (bytes, not hexa digits)
    ror     rax, 8                  ; 40000123
    imul    rax, rax, 0x01000001    ; 40123123
    rol     rax, 16                 ; 12312340
    mov     al, ah                  ; 12312344
    rol     rax, 8                  ; 23123441
    rol     ax, 8                   ; 23123414
    rol     rax, 8                  ; 31234142
    rol     ax, 8                   ; 31234124
    rol     rax, 8                  ; 12341243
    rol     ax, 8                   ; 12341234
like image 89
Ped7g Avatar answered Oct 12 '22 00:10

Ped7g