c code (the target part is the loop which copies some characters from one array to another array.
the code was compiled with -O0
the code is commented only for the first iteration
| ; CODE XREF from sym.testArr @ 0x140001592
.--> 0x14000156d mov eax, dword [var_8h] ; eax = i = 0
:| 0x140001570 movsxd rdx, eax ; mov eax to rdx, and sign extend it... rdx = 0
:| 0x140001573 mov rax, qword [var_10h] ; rax = addr of arr
:| 0x140001577 add rax, rdx
:| 0x14000157a movzx edx, byte [rax] ; edx = a, and zero extend
:| 0x14000157d mov eax, dword [var_4h] ; eax = 6
:| 0x140001580 cdqe
:| 0x140001582 mov byte [rbp + rax - 0xf], dl ; *(rbp + rax - 0xf) = dl = 'a'
:| 0x140001586 sub dword [var_4h], 1 ; num--
:| 0x14000158a add dword [var_8h], 1 ; i++
:| ; CODE XREF from sym.testArr @ 0x14000156b
:`-> 0x14000158e cmp dword [var_8h], 6 ; i <=6
`==< 0x140001592 jle 0x14000156d
so, I had a question regarding the first two instructions
why does the compiler set the eax register to 0, then copies the value to rdx, and then another valu copied to it?
can't the value at var_8h be copied to rdx directly? or will it have some side effects when it goes that way?
Do you have the original code? A very cool website is godbolt.org
No, there's no side effect. It could have been written movsxd rdx, dword [var_8h] It's pretty normal to see useless steps at -O0 optimization level.
thanks.. hadn't seen this initially (i was in some session at the same time)
how did you produced this?
the c code: https://paste.centos.org/view/42027307 assembly code for testArr(): https://paste.centos.org/view/d38ee7b6
disassembled with radare2
Обсуждают сегодня