【问题标题】:Multiply 2 Values in Assembly Language 8086?将汇编语言 8086 中的 2 个值相乘?
【发布时间】:2016-10-17 16:06:28
【问题描述】:

我将从控制台窗口输入的两个值相乘。我正在使用 32 位寄存器 eax、ebx,但它没有将值相乘。该程序正在运行,但它没有成倍增加。任何人都可以检测到问题吗?这段代码有什么问题?我正在使用汇编语言中的 KIP.R.IRVINE 链接库。

代码如下:

  INCLUDE Irvine32.inc

  .data

   inputValue1st BYTE "Input the 1st integer = ",0
   inputValue2nd BYTE "Input the 2nd integer = ",0
   outputSumMsg BYTE "The sum of the two integers is = ",0

   num1 DD ?
   num2 DD ?
   sum  DD ?


  .code

  main PROC

   ; here we are calling our Procedures

  call InputValues
  call multiplyValue
  call outputValue
  call Crlf

  exit
  main ENDP



InputValues PROC

;----------- For 1st Value--------
; input message

call Crlf
mov edx,OFFSET inputValue1st
call WriteString


call ReadInt    ; read integer
mov num1, eax   ; store the value


 ;-----------For 2nd Value----------

 ; output the prompt message
 mov edx,OFFSET inputValue2nd
 call WriteString


  call ReadInt  ; read integer
  mov num2, ebx     ; store the value

  ret
  InputValues ENDP



   ;---------multiply----------------

   multiplyValue PROC
   ; compute the sum

    mov eax, num1   ; moves num1 to eax
    mov ebx, num2   ; moves num2 to ebx

    mul ebx  ; num1 * num2 = 6 * 2
    mov sum, eax  ; the val is stored in ebx


    ret
    multiplyValue ENDP


    ;--------For Sum Output Result----------

    outputValue PROC


    ; output result
    mov edx, OFFSET outputSumMsg
    call WriteString


    mov eax, sum
    call WriteInt ; prints the value in eax
    ret
    outputValue ENDP


    End main

还有一个问题:我需要在其中使用进位标志吗?如果是这样,那么它的代码会是什么样子?

【问题讨论】:

  • 评论; num1 * num2 = 6 * 2 在 multiply PROC 中就是一个例子
  • 您期望第二个ReadInt 调用返回ebx 中的值。我的猜测是返回值实际上位于eax
  • Ye Alexey Frunze... 我正在 Visual Studio 2010 中编译/调试,并使用 KIP.R.IRVINE 链接库进行汇编语言.. 程序正在运行,但它不是乘以值.. .
  • Michael...ReadInt 在 eax 中取值...但是如果我在 eax (ReadInt) 中取 2 个值,那么它会覆盖第一个中的第二个值。如果我返回第二个值在 eax 中取 2 个值 ..如果我在 eax 和第二个 ebx 中取第一个值,那么它也不会相乘..例如,如果我乘以 5 * 2 它返回我 +2146787328 ..所以我如何将我读过的值相乘关于乘法..但它不起作用...

标签: assembly x86 masm irvine32


【解决方案1】:
;mov eax, num1   ; moves num1 to eax
;mov ebx, num2  ; moves num2 to ebx

如果您要将值实际加载到寄存器中而不是评论这是您应该做的事情,可能会有所帮助,

【讨论】:

  • 如果我注释掉这些指令 mov eax, num1 ,我在发布问题时错误地完成了它;将 num1 移动到 eax mov ebx, num2 然后它也不起作用
  • 如果我删除了评论,那么它也无法正常工作,很抱歉给您带来不便......我写错了......
【解决方案2】:

当您输入第二个值时出现错误 ReadInt 总是将值放入 eax 但您说:

call ReadInt  ; read integer
mov num2, ebx     ; store the value

一定是

call ReadInt  ; read integer
mov num2, eax     ; store the value

【讨论】:

    【解决方案3】:

    迟到总比没有好:)

    TITLE MultiplyTwoNumbers (multiply.asm)
    
    INCLUDE Irvine32.inc
    
    .data
    
    inputValue1st BYTE "Input the 1st integer = ",0
    inputValue2nd BYTE "Input the 2nd integer = ",0
    outputSumMsg BYTE "The sum of the two integers is = ",0
    
    num1 DD ?
    num2 DD ?
    sum  DD ?
    
    
    .code
    
    main PROC
    
    ; here we are calling our Procedures
    
    call InputValues
    call multiplyValue
    call outputValue
    call Crlf
    
    exit
    main ENDP
    
    
    
    InputValues PROC
    
    ;----------- For 1st Value--------
    ; input message
    
    mov edx,OFFSET inputValue1st
    call WriteString
    
    
    call ReadInt    ; read integer
    mov num1, eax   ; store the value
    
    
     ;-----------For 2nd Value----------
    
     ; output the prompt message
     mov edx,OFFSET inputValue2nd
     call WriteString
    
    
      call ReadInt  ; read integer
      mov num2, eax     ; store the value
    
      mov eax,0
    
      ret
      InputValues ENDP
    
    
    
       ;---------multiply----------------
    
       multiplyValue PROC
       ; compute the sum
    
        mov eax, num1   ; moves num1 to eax
        imul eax, num2   ; multiplies content in eax with num2 
        mov sum, eax  ; the val is stored in eax
    
    
        ret
        multiplyValue ENDP
    
    
        ;--------For Sum Output Result----------
    
        outputValue PROC
    
    
        ; output result
        mov edx, OFFSET outputSumMsg
        call WriteString
    
    
        mov eax, sum
        call WriteInt ; prints the value in eax
        ret
        outputValue ENDP
    
    
        End main
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多