> For the complete documentation index, see [llms.txt](https://hexisanoob.gitbook.io/hexisanoob/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hexisanoob.gitbook.io/hexisanoob/linux-64-bit-assembly/moving-data.md).

# Moving Data

Before going ahead, here are three rules:

a) In 64-bit mode, operands generate a 64-bit result in the destination GP register\
b) In 32-bit mode, operands generate a 32-bit result, zero-extended to a 64-bit result in the destination  general purpose register\
c) In 8-bit and 16-bit mode, operands generate an 8 or 16 bit result. The upper 56 or 48 bits of the GPR are untouched\
d) If the result of an 8 or 16-bit operation is intended for 64-bit address calculation, explicitly sign-extend the register to the full 64-bits.

"Zero extended"??? We'll  talk about this after introducing a few operands that are used to move data in assembly.

### MOV instruction

MOV is the most common instruction in assembly. It allows data moving in the following formats:

1. Between registers
2. Memory to registers and Registers to Memory
3. Immediate data  to registers
4. Immediate data to memory

### LEA

LEA=> Load Effective Address.

It loads pointer values in registers

Eg: LEA RAX, \[var1]

Where var1 is the label given to any data type (talked in data type article here: <https://hexisanoob.gitbook.io/hexisanoob/application-security/linux-64-bit-assembly/data-types>)

Please note that, these two instructions essentially mean the same thing:

mov rax, sample

lea rax,\[sample]

### XCHG

Swaps values in between:

1. Register and Register: XCHG RAX, RBX
2. Register and Memory: XCHG RAX, \<memory address>

Demo:

Here is an assembly program for us to dissect into.

```
global _start

section .text
_start:

        ; mov immediate data to register 
        mov rax, 0xaaaaaaaabbbbbbbb
        mov eax, 0xaaaaaaaa
        mov rax, 0xaaaaaaaabbbbbbbb
        mov al, 0x11
        mov rax, 0xaaaaaaaabbbbbbbb
        mov ah, 0xcc
        mov rax, 0xaaaaaaaabbbbbbbb
        mov ax, 0xdddd

         
        ; mov register to register 

        mov rbp, rax
        mov r10, rbp

        mov r11d, r10d
        mov r12w, r11w
        mov r13b, r12b


        ; mov from memory into register 

        mov rsi, [sample2]
        mov r14d, [sample]
        mov r15w, [sample]
        mov dil, [sample]


        ; mov from register into memory 

        mov rax, [sample2]
        mov byte [sample], al
        mov word [sample], ax
        mov dword [sample], eax
        mov qword [sample], rax


        ; lea demo

        lea rax, [sample]
        lea rbx, [rax] 


        ; xchg demo 
        mov rax, 0x1234567890abcdef
        mov rbx, 0x9999999999999999

        xchg rax, rbx
 

        ; exit the program gracefully  

        mov rax, 0x3c
        mov rdi, 0
        syscall


section .data

sample: db 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22
sample2: dq 0x1122334455667788
sample3: times 8 db 0x00
```

Note at the end we are exiting by giving  rax a value 0x3c which is hex equivalent of "60" which is the syscall number of  exit()

**gdb -q ./MovingData -tui**

Instruction 1: Rule A applies

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FoNrlWqfeHAcsuacFFw9V%2Fimage.png?alt=media&amp;token=64e21d55-3a6d-45e4-b24a-1939de0830f8" alt=""><figcaption></figcaption></figure>

Instruction 2: Note how a 32 bit value output zeros out the upper 32 bits of the register. 3rd instruction resets RAX.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FuFRWH6ZjK7YiV30aBtyB%2Fimage.png?alt=media&amp;token=5208fefe-117a-473d-92d2-5130465ea084" alt=""><figcaption></figcaption></figure>

Instruction 4:  Rule C applies and the remaining 30 bits are unaffected

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FvbHgKSngFpUiMuee3vvR%2Fimage.png?alt=media&amp;token=7b0aa034-6146-4a9c-954c-df72239542ac" alt=""><figcaption></figcaption></figure>

Instruction  9: Moves rax into rbp

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FZM9z0pjvuKecUi8vnlrG%2Fimage.png?alt=media&amp;token=2d4cc3a6-0523-4584-864e-9941ae2930bf" alt=""><figcaption></figcaption></figure>

Instruction 14: This instruction assigns value of sample2 variable in RSI.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2Fkx5kQxyX0Ar7vL7VgJHs%2Fimage.png?alt=media&amp;token=8c05c280-8950-4d48-b5be-6cceb333c09f" alt=""><figcaption></figcaption></figure>

On stepi we'll see the change

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FZJ4gj3l1GgmRZE0DpnR0%2Fimage.png?alt=media&amp;token=44feae13-a589-4489-b731-aedb90a87c04" alt=""><figcaption></figcaption></figure>

Instruction 19: Changes sample variables 1 byte with that of al.&#x20;

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FEnjfyAbfzKoQazaqy7fG%2Fimage.png?alt=media&amp;token=b50ce15e-84be-4715-b04a-6412b3a6fe3a" alt=""><figcaption></figcaption></figure>

Notice al has 88 right now and sample starts with 0xaa. This gets overwritten

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FBEjJYOCD0kpHzfvqFp6G%2Fimage.png?alt=media&amp;token=d56ece10-7d60-4494-a7c2-cc198f0a98ac" alt=""><figcaption></figcaption></figure>

Instruction 23: LEA would load 0x402000 intro RAX. Note that this is the memory address of sample variable.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FvDBICMpciLaxzy6NNCwn%2Fimage.png?alt=media&amp;token=c63d6569-2be3-4e1c-96a8-424954b65eaa" alt=""><figcaption></figcaption></figure>

Upon stepi or si, we see RAX being overwritten with the address of sample

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FsdoltJO6ypwrB4pEE6Xg%2Fimage.png?alt=media&amp;token=8b16e85e-8598-4b21-bcb2-e562cba61978" alt=""><figcaption></figcaption></figure>

Instruction 24: This instruction (lea rbx, \[rax]) essentially loads the value in RAX into RBX

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2Fzez7PjgGRzVsDa3P1bkp%2Fimage.png?alt=media&amp;token=18453b27-cb4e-4c19-946c-21faac1943f8" alt=""><figcaption></figcaption></figure>

Instruction 27: This instruction would exchange RAX and RBX. Notice how rax and rbx have been overwritten first by 64 bit absolute values

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FWCqdQl8sMzI2ovbnJPFu%2Fimage.png?alt=media&amp;token=92fee9c7-3e32-423d-952c-9204b1d6ef95" alt=""><figcaption></figcaption></figure>

One more stepi

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FurI3H93eAcUVWwt4Ry2n%2Fimage.png?alt=media&amp;token=83012ae6-08cc-42e2-b28f-7bd7454a8974" alt=""><figcaption></figcaption></figure>

Finally, we exit the program using 0x3c (hex value for 60->syscall number for exit()) with rdi as 0 for error code.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FencCLwiWgDZWKzsKyklx%2Fimage.png?alt=media&amp;token=5ae93445-e9df-4629-b187-8322bce51f3d" alt=""><figcaption></figcaption></figure>
