了解用于模计算的 Intel x86 程序集输出

Understanding Intel x86 assembly output for a modulo calculation

我有一个关于编译器的学校项目,以及它在 Intel x86 和 ARMv7 之间的汇编代码有何不同,但我一直在尝试理解 Intel x86 架构的汇编。

源代码为:

int main()
{
int a=5,b=2;
int result;
result = a % b;
printf("Result of 5 modulo 2 is %i\n", result);
}

汇编输出 (gcc masm=Intel)

main:
/*
Intel32-x86 Arhchitecture
Little endian

ebp register -- base pointer
esp register -- stack pointer
*/
push    ebp ; ebp register put on stack
mov     ebp, esp ; Move data from ebp to esp
and     esp, -16 ; Logical AND ??
sub     esp, 32 ; Subtraction ??
mov     DWORD PTR [esp+20], 5 
;5 as 32 bits
;00000101-00000000-00000000-00000000
mov     DWORD PTR [esp+24], 2 
;2 as 32 bits
;00000010-00000000-00000000-00000000
mov     eax, DWORD PTR [esp+20]
mov     edx, eax
sar     edx, 31
;Shift Arithmetically right - edx med 31. 
;00000101-00000000-00000000-00000000 BEFORE
;00000000-00000000-00000000-00000000 AFTER
idiv    DWORD PTR [esp+24]
;Signed divide - IDIV r/m32 - EDX:EAX register
;Dividing EDX:EAX on value of esp+24, and save the remainder in edx.
;EDX:EAX 00000000-00000000-00000000-00000000-00000101-00000000-00000000-00000000
mov     DWORD PTR [esp+28], edx
mov     eax, OFFSET FLAT:.LC0
mov     edx, DWORD PTR [esp+28]
mov     DWORD PTR [esp+4], edx
mov     DWORD PTR [esp], eax
call    printf
leave
ret

尤其是 -16 ;逻辑与
子 esp, 32 ;减法

这两条指令的目的是什么?

目的在评论中提到:

        and     esp,-16    ;round esp down to 16 byte boundary
        sub     esp,32     ;allocate 32 bytes of space for local variables

如果你没有听懂这部分关于延长红利的符号:

        mov     eax, DWORD PTR [esp+20] ; eax = dividend
        mov     edx, eax   ; edx = dividend
        sar     edx, 31    ; edx = 0 or -1 (the sign extension)