2

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

2 Answers2

2

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.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • so i understand the whole push intVal part but can you elaborate on the pop eax and what exactly that does? And this is all that i have to do to concern this code with stacks? you dont actually put push or pop's in the procedures them selves? –  Apr 06 '17 at 22:28
  • it pops the previously pushed value into the given operand, in this case, eax. The way you pick your words, "to concern this code with stacks", tells me that you do not yet understand how the stack works, what your code needs to do with it, and even why your code needs to do anything with it at all. Unfortunately, stackoverflow has a very well defined purpose and direction, and that is for solving very specific problems, not for education over broad subjects such as stack use. So, I am afraid that you will need to study. – Mike Nakis Apr 07 '17 at 06:59
1

Because your question is tagged MASM, this is a MASM answer:
Your code can be restructured in a way that uses the MASM directive PROC with parameters and the INVOKE directive for parameter passing:

MultiplyAndDisplay PROC val1: SDWORD, val2: SDWORD
    mov  eax, val1
    imul eax, val2                          ; signed multiply of val1 by val2 
    mov  edx, OFFSET prompt
    call WriteString                        ; Writes the prompt in edx
    call WriteDec                           ; Writes the value in eax
    ret
MultiplyAndDisplay ENDP

main PROC
    call GetValue
    push [intVal]                           ; PUSH firstParam to the stack
    call GetValue
    pop eax                                 ; POP previous param/intVal to EAX
    invoke MultiplyAndDisplay, eax, intVal  ; MultiplyAndDisplay, firstParam(EAX), secondParam(intVal)
    exit
main ENDP
zx485
  • 28,498
  • 28
  • 50
  • 59