调用函数不起作用

时间:2022-08-23 13:17:12

Given the following code :

给出以下代码:

.globl  main
    .type main, @function
input:  .string  "%d"
main:

        pushl   %ebp            # save the old frame pointer
        movl    %esp,%ebp       # create the new frame pointer

        movl    $0,%eax
        addl    $-4 ,%esp       # moving down the stack
        pushl   %esp            # push the address of esp to the stack in order to store the number given by the user
        pushl   $input  # push to the stack the format of the input
        call    scanf           # call scanf to get a number from the user
        addl    $8,%esp         # clear the stack
        movl    (%esp),%eax     # get the selection from the user


        subl    $50,%eax
        jmp     *.switching(,%eax,4)


.section .rodata
    .align 4

.switching:

    .long .L1
    .long .L2
    .long .L3
    .long .L4

.text

.L1:
    call    case1
    jmp     .quitTheProgram
.L2:
    call    case2
    jmp     .quitTheProgram
.L3:
    call    case
    jmp     .quitTheProgram
.L4:
    call    case4
    jmp     .quitTheProgram



case1:

            pushl   %ebp            # save the old frame pointer
            movl    %esp,%ebp       # create the new frame pointer
#
# code of case1
# 
            movl    %ebp,%esp       # restore the old ebp
            popl    %ebp            # restore the old stack pointer and release all used memory
            ret                     # return to caller function (OS)

The user presses numbers between 50-54. The problem is after pressing (for example) 50 I jump to case1 , but not to the code itself , but straight to the ret line , and then the code stops and exit case1 (as for the rest of the cases) .

用户按下50-54之间的数字。问题是在按下(例如)50后我跳转到case1,但不是代码本身,而是直接到ret行,然后代码停止并退出case1(就其他情况而言)。

What might be the problem ?

可能是什么问题?

Regards,Ron

1 个解决方案

#1


0  

The problem is after pressing (for example) 50 I jump to case1 , but not to the code itself , but straight to the ret line

问题是在按下(例如)50后我跳转到case1,但不是代码本身,而是直接到ret线

I just built your code on Linux, stepped through it in GDB, and did not observe that behavior.

我刚刚在Linux上构建了你的代码,在GDB中逐步完成了它,并没有观察到这种行为。

It is somewhat likely that you are mis-interpreting what you actually observed.

您有可能错误地解释了您实际观察到的内容。

#1


0  

The problem is after pressing (for example) 50 I jump to case1 , but not to the code itself , but straight to the ret line

问题是在按下(例如)50后我跳转到case1,但不是代码本身,而是直接到ret线

I just built your code on Linux, stepped through it in GDB, and did not observe that behavior.

我刚刚在Linux上构建了你的代码,在GDB中逐步完成了它,并没有观察到这种行为。

It is somewhat likely that you are mis-interpreting what you actually observed.

您有可能错误地解释了您实际观察到的内容。