Gooey
Gooey

Reputation: 4778

Jump not working

I'm currently busy making a small (with so far fixed questions) quiz in assembly (AT&T).

I designed a small menu which asks for the a certain input either 1 2 or 3
The problem is my cmpl doesn't do it's job, and i cant figure out why. It just quits, no matter what the input is.

Below is some of my code:

.text

menu: .asciz "Please select an option: 1 - Start the quiz! 2 - Highscores 3 - Quit\n"
input: .asciz "%i"

.global main

main:
    call menushow

menushow:
    push $menu
    call printf
    addl $4,(%esp)

    leal -4(%ebp), %eax 
    pushl %eax
    pushl $input

    call scanf      

    popl %eax
    popl %eax       # the number that has been entered is now in eax

    cmpl $1,%eax        #1 entered? 
    je qone         #show question 1

    cmpl $2,%eax        #2 entered??
    je showHighScores   #show current highscores

    call quit       #something else? (3, 99 w/e) then we quit

Upvotes: 2

Views: 203

Answers (1)

Jens Björnhager
Jens Björnhager

Reputation: 5649

  • You are not allocating space on the stack for the result from the scanf. You need to either push some dword value to the stack before you push the arguments to scanf, or delete the addl $4,(%esp) and use the space previously occupied by the argument to printf. The address of this space would be -12(%ebp) on a Windows system. Instead of using the ebp you get from the operating system, i'd suggest you set it yourself in the start of your program so that you know where it points to.

  • You pop off two values from the stack, but as scanf has two arguments, the value you're after is the third value, so you need to pop once more.

Upvotes: 1

Related Questions