Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assembly Language stack issue

    I'm trying to use stacks in this fibonacci program and I'm not doing it right and looking for suggestions because if i comment out the push/pop statements it still writes to the string correctly but if i push a different register it changes the output....or am i already doing it correctly?...

    .data
    count DWORD 12 ; Number of loops to perform
    val1 DWORD ?
    string byte (" "),0 ; use for the space between number

    .code
    main PROC
    mov ecx, count ; loop counter
    mov eax, 0
    mov val1, 1


    ADDLOOP: ; First time loop:
    mov ebx, val1
    mov val1, eax ; save current eax for our next addition
    add eax, ebx
    push eax
    call DisplaySum
    pop eax
    loop ADDLOOP ; If not done, perform loop again (ecx is automatically decremented)
    exit
    main ENDP


    DisplaySum PROC
    call WriteInt ; Display number
    mov edx, OFFSET string ; add space between the numbers
    call WriteString
    ret
    DisplaySum ENDP

    END main

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It has been a little while since I've done x86 assembly but why are you pushing/popping EAX around that call to DisplaySum? The call instruction will automatically push all the registers onto the stack and the ret pops them off. Why would you want to push any different registers? (You don't need to.)

    This "WriteInt" procedure which you didn't include, does it print whatever the value of EAX is?
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    call does NOT push all the regs on the stack, and ret does NOT pop all the regs. I know call pushes the IP, and I also think it pushes the CS. (And ret would pop the same ones). If ret is given an argument, it'll pop the corresponding count from the stack.
    Trinithis

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Trinithis View Post
    call does NOT push all the regs on the stack, and ret does NOT pop all the regs. I know call pushes the IP, and I also think it pushes the CS. (And ret would pop the same ones). If ret is given an argument, it'll pop the corresponding count from the stack.
    I stand corrected nevertheless, unless WriteInt and WriteString don't properly push/pop the registers they use, what I said about pushing/popping EAX still stands.
    OracleGuy

  • #5
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My issue is that when I view the values of the ESP register it is decrementing it by 4 like it should for DWORDs and all the numbers are being pushed onto the stack correctly because I get the fibonacci numbers as output...the trouble is that the pop call should be popping them off in reverse order which it is not doing. The reason for the first DisplaySum call was to get the first 0 to show up for the fibonacci, otherwise it started with the number one, but nonetheless it still isn't using the pop correctly.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    What is the output of the program that you are getting? I don't see anything wrong with that code.

    The reason 0 isn't being outputted is that you need to call DisplaySum once before you start your loop is all.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •