Search code examples
visual-studioassemblystackmasmirvine32

How to properly use PUSH and POP


I have a code that has 3 procedures, one to get an input from the user, one to display a multiplication result, and lastly one for an error message. I am trying to implement the PUSH and POP operations and get my code in to the stack. It will seem long but it makes sense to me, here it is...

.data
    line  BYTE "The answer is ",0
    line2 BYTE "Enter a number",0
    kline3 BYTE Wrong.",0
    int SWORD ?

.code

Val PROC
    call ReadInt
    mov int,edx    
    cmp int, 10000
    jl 1
    jmp end 
    L1: cmp intVal1, -10000
    jg 2        
    call error
    jmp end 
    2: ret
Val ENDP

main PROC
    call Val
    call Val
    imul val, val
    exit
main ENDP
END main

All this simply does it call to get 2 inputs twice and then call to display the the multiplied result. My question is how do you implement push and pop in to here to have it all make sense?

I would assume that you need to push in the GetValue procedure to put in input in to the stack and then pop it after each call in the main procedure and maybe do the same with the display procedure?

I am just struggling to figure it out so any help would be great!

PS. This code is an asm file in visual studio 2010


Solution

  • Your first call to GetValue stores its result in intVal. But then your second call to GetValue also stores its result in intVal, so the first result is forever lost.

    Your MultiplyAndDisplay function expects one of the operands in intVal, and the other operand in eax. So, what you need to do is push [intVal] after the first call to GetValue, and pop eax after the second call to GetValue.

    Note that the square brackets in push [intVal] are in some notation that actually makes sense, but if I remember correctly the microsoft assembler does not support that notation which actually makes sense, so you might have to code push intVal instead, or push dword ptr intVal, or something nonsensical like that to get it to work.