【问题标题】:x86 assembly - masm32: Issues with pushing variable to stackx86 程序集 - masm32:将变量推送到堆栈的问题
【发布时间】:2012-05-21 01:12:02
【问题描述】:

我正在尝试制作一个程序,它获取两个输入数字,将它们相乘(将结果存储在一个变量中),将它们相除(将结果存储在另一个变量中)并打印结果。

我遇到的问题是第一行代码push num1 返回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. 

为什么会导致这个错误?

【问题讨论】:

    标签: assembly input x86 masm masm32


    【解决方案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】:

      错误信息很清楚,操作数无效。 你不能这样做:

      push num1
      

      操作码“push”是有效的,但是在x86指令集中,你只能push某些寄存器,而不是字节序列(字符串)。你的 num1 是一个字节序列。

      例如:

      push ax
      

      是有效指令和有效操作数。

      您可以推送的有效寄存器示例:AH、AL、BH、BL、CH、CL、DH、DL、AX、BX、CX、DX 等

      【讨论】:

      • 我现在尝试将变量移动到 EAX,现在我使用以下代码得到 invalid instruction operands:mov eax, num1 ; the variable is num1。这是为什么呢?
      • 您可以将一个字节或(d/q)字作为数字(立即操作数)推送,后者自然是一个字节序列。
      猜你喜欢
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2014-08-04
      相关资源
      最近更新 更多