我有一个关于编译器的学校项目,以及英特尔x86与ARMv7在汇编代码上的区别,但是我一直在试图理解英特尔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

和esp,-16;逻辑和
sub 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)

关于c - 了解英特尔x86汇编输出以进行模计算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29769459/

10-12 01:55