叶子函数中的推入和弹出操作

时间:2019-04-06 21:10:18

标签: c linux assembly x86 nasm

以下是我编写的x86汇编语言子例程,旨在从C程序调用:

;================================================================
; IsBetweenAandB - tests a character if it is between 'a' and 'b'
; arguments:
;   ch = character to be tested
;   a = start character
;   b = end character
; return value:
;   1 = yes
;   0 = no
;----------------------------------------------------------------
IsBetweenAandB:
    push    ebp
    mov ebp, esp    
    add esp, 12 ;<------------------------------------------(1)
                        ; [ebp]     = old ebp stack frame
                        ; [ebp + 4] = return address
    mov eax, [ebp + 8]  ; [ebp+8] = ch
    push ebx ;<------------------------------------------(2)
    mov ebx, [ebp + 12] ; 'a' = start
    push edx ;<------------------------------------------(2)
    mov edx, [ebp + 16] ; 'e' = end 

    cmp eax, ebx    ; compare 'ch' with 'a'
    jae next_test_ibab  ; if(ch>='a')
    jb  set_zero_ibab   ; if(ch<'a')

next_test_ibab:
    cmp eax, edx    ; compare 'ch' with 'e'
    jbe set_one_ibab        ; if(ch<='e')
    ja  set_zero_ibab   ; if(ch>'e')

set_one_ibab:
    mov eax, 1  
    jmp returns_ibab

set_zero_ibab:
    mov eax, 0

returns_ibab:
    pop edx ;<------------------------------------------(2)
    pop ebx ;<------------------------------------------(2)

    mov     esp, ebp
    pop     ebp
    ret

我在这里有两个问题:

  1. 因为我在这里没有使用任何局部变量,所以我需要写 add esp, 12 吗?
  2. (a)这些推动和弹出按钮实际上是否真的必要(它们旨在恢复 ebx edx 的旧值寄存器)?
    (b)他们会对esp造成任何干扰吗?

0 个答案:

没有答案