Do compilers usually use registers for their "intended" purpose?

I am studying assembly, and I read that the four main x86 general purpose registers (eax, ebx, ecx, and edx) had intended or intended purpose. For example, eax is a battery register, ecx is used as a counter for cycles, etc. Do most compilers try to use registers for their intended purpose or ignore what is “supposed” for registers and simply assign values ​​to the next available register?

Also, when looking at the x64 registers, I noticed that eight more general registers were added, leaving the total number of gp registers to twelve if you ignore rbp, rsp, rsi and rdi (since they don't matter, common goals) and sixteen if you turn them on. In ordinary user programs (i.e., in browsers, word processors, etc., but not in cryptographic programs requiring a large number of registers), how many of these registers are usually used at any given time? Is it common for a program like, say, Firefox to use all 12/16 regular registers at once, or use only a subset, since they don't have enough variables to fill them all? I myself will consider this, parsing the binaries to see what is common, but I would appreciate a response from someone more knowledgeable,than me.

Also, do compilers usually use registers with semi-gp (rsi, rdi, rsp and rbp) for general use if they are not currently used for their non-general application? I was curious because I saw that these registers are listed as a “common goal”, but even I can think of cases from the top of my head where these registers cannot be used for general storage (for example, you do not want to store variables in rbp and rsp, then push the values ​​onto the stack!). So compilers try to use these registers when they can? Is there any difference between compiling x86 and x64, since x64 processors have more registers available, so there is no need to insert variables into any accessible register?

+4
2

GP .
, , .

, rsi, rdi, rbp, rsp - call, ret, push .
, ( , ), .

, .

[1]:

void maxArray(int* x, int* y, int*z, short* w) {
    for (int i = 0; i < 65536; i++)
    {
        int a = y[i]*z[i];
        int b = z[i]*z[i];
        int c = y[i]*x[i]-w[i];
        int d = w[i]+x[i]-y[i];
        int e = y[i+1]*w[i+2];
        int f = w[i]*w[i];

        x[i] = a*a-b+d; 
        y[i] = b-c*d/f+e;
        z[i] = (e+f)*2-4*a*d;
        w[i] = a*b-c*d+e*f;
    }
}

GCC

maxArray(int*, int*, int*, short*):
        push    r13
        push    r12
        xor     r8d, r8d
        push    rbp
        push    rbx
        mov     r12, rdx
.L2:
        mov     edx, DWORD PTR [rsi+r8*2]    
        mov     ebp, DWORD PTR [r12+r8*2]
        movsx   r11d, WORD PTR [rcx+r8]
        mov     eax, DWORD PTR [rdi+r8*2]
        movsx   ebx, WORD PTR [rcx+4+r8]
        mov     r9d, edx
        mov     r13d, edx
        imul    r9d, ebp
        imul    r13d, eax
        lea     r10d, [rax+r11]
        imul    ebx, DWORD PTR [rsi+4+r8*2]
        mov     eax, r9d
        sub     r10d, edx
        imul    ebp, ebp
        sub     r13d, r11d
        imul    eax, r9d
        imul    r11d, r11d
        sub     eax, ebp
        add     eax, r10d
        mov     DWORD PTR [rdi+r8*2], eax
        mov     eax, r13d
        imul    eax, r10d
        cdq
        idiv    r11d
        mov     edx, ebp
        sub     edx, eax
        mov     eax, edx
        lea     edx, [0+r9*4]
        add     eax, ebx
        mov     DWORD PTR [rsi+r8*2], eax
        lea     eax, [rbx+r11]
        imul    r9d, ebp
        imul    r11d, ebx
        add     eax, eax
        imul    edx, r10d
        add     r9d, r11d
        imul    r10d, r13d
        sub     eax, edx
        sub     r9d, r10d
        mov     DWORD PTR [r12+r8*2], eax
        mov     WORD PTR [rcx+r8], r9w
        add     r8, 2
        cmp     r8, 131072
        jne     .L2
        pop     rbx
        pop     rbp
        pop     r12
        pop     r13
        ret

, GP ( ), rbp, rsi rdi.
.

rsi rdi ( ) , .
integer/pointer.

int sum(int a, int b, int c, int d)
{
    return a+b+c+d;
}

sum(int, int, int, int):
        lea     eax, [rdi+rsi]
        add     eax, edx
        add     eax, ecx
        ret
+4

( 16- 8086) , x86. BX, BP, SI DI , CISC, .

, LOOP CX, , . , , , DEC JNE. , .

80386 32- , . , , , , .

, , , -. , ESP/RSP .

+2

Source: https://habr.com/ru/post/1674228/


All Articles