这种交换 w/o 推送寄存器有多安全?

How safe is this swap w/o pushing registers?

我是 Assembly 的新手,下面的代码应该通过两个不同的函数交换两个整数:首先使用 swap_c 然后使用 swap_asm.

但是,我怀疑我是否需要在汇编代码之前 push(我的意思是保存)寄存器的每个值,然后 pop 它们之后(就在 return 到 main).换句话说,如果我 return 不同的寄存器内容(不是像 ebpesp 这样的关键内容;但是 eax, ebx, ecx & edx) 在 运行 swap_asm 函数之后?是不是把汇编部分的行取消注释比较好?

这段代码对我来说运行正常,我设法将 27 行汇编 C 代码减少到 7 行汇编。

p.s.: 系统是Windows10, VS-2013 Express.

main.c部分

#include <stdio.h>

extern void swap_asm(int *x, int *y);

void swap_c(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(int argc, char *argv[]) {
    int x = 3, y = 5;
    printf("before swap    => x = %d     y = %d\n\n", x, y);

    swap_c(&x, &y);
    printf("after swap_c   => x = %d     y = %d\n\n", x, y);

    swap_asm(&x, &y);
    printf("after swap_asm => x = %d     y = %d\n\n", x, y);

    getchar();
    return (0);
}

assembly.asm部分

 .686
.model flat, c
.stack 100h
.data

.code
swap_asm proc
    ; push eax
    ; push ebx
    ; push ecx
    ; push edx
    mov eax, [esp] + 4  ; get address of "x" stored in stack into eax
    mov ebx, [esp] + 8  ; get address of "y" stored in stack into ebx
    mov ecx, [eax]      ; get value of "x" from address stored in [eax] into ecx
    mov edx, [ebx]      ; get value of "y" from address stored in [ebx] into edx
    mov [eax], edx      ; store value in edx into address stored in [eax]
    mov [ebx], ecx      ; store value in ecx into address stored in [ebx]
    ; pop edx
    ; pop ecx
    ; pop ebx
    ; pop eax
    ret
swap_asm endp
end

通常,这取决于您正在使用的系统的调用约定。 调用约定 指定如何调用函数。一般来说,它会说明将参数放在哪里以及被调用的函数必须保留哪些寄存器。

在 i386 Windows 上使用 cdecl 调用约定(这是您可能使用的约定),您可以自由覆盖 eaxecxedx寄存器。 ebx 寄存器必须保留。虽然您的代码似乎可以工作,但当一个函数开始依赖于 ebx 被保留时,它神秘地失败了,所以最好保存并恢复它。