x86 assembly - masm32: Issues with pushing variable to stack

女生的网名这么多〃 提交于 2019-12-11 05:25:43

问题


I'm trying to make a program that gets two input numbers, multiplies them (storing the result in a variable), divides them (storing the result in another variable) and prints the result.

The issue I'm having is that the first line of code push num1 returns invalid instruction operands:

.data
        num1 db "Enter a number:"
        num2 db "Enter another number:"
.data?
        buffer1 dd 100 dup(?) ; this is where I store input for num1
        buffer2 dd 100 dup(?) ; " " num2
.code
start:
        push num1 ; here is where it returns the error
        call StdOut ;I want to print num1 but it doesn't get that far.
                    ; later on in my code it does multiplication and division.
        push buffer1 ; I push buffer1
        call StdIn  ; so that I can use it for StdIn
                    ; I repeat this for num2
        ; I then use those 2 numbers for multiplication and division. 

Why is it causing this error?


回答1:


start:
    push    offset num1
    call    Stdout    
    ; or 
    lea     eax, num1
    call    StdOut

    ;this:
    push    num1 
    ; is pushing the letter 'E' I believe.
    ; here is where it returns the error
    call    StdOut 

    ; this is wrong also:
    push    buffer1 ; I push buffer1 <<<  no, you are trying to push the val of buffer1
    call    StdIn  ; so that I can use it for StdIn

    ; you need to pass an address of the label (variable)
    ; so either
    lea     eax, buffer1
    push    eax
    call    StdIn

    ; or
    push    offset buffer1
    call    StdIn



回答2:


The error message is very clear, the operand is invalid. You can't do this:

push num1

The opcode "push" is valid, but in x86 instruction set, you can only push certain registers, not a byte sequence (string). Your num1 is a byte sequence.

For example:

push ax

is a valid instruction and valid operands.

Sample of valid registers that you can push: AH, AL, BH, BL, CH, CL, DH, DL, AX, BX, CX, DX, etc.



来源:https://stackoverflow.com/questions/10678551/x86-assembly-masm32-issues-with-pushing-variable-to-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!