【问题标题】:Splitting code in to procedures将代码拆分为过程
【发布时间】:2017-08-28 20:11:19
【问题描述】:

我的代码从用户那里得到两个输入并将它们相乘。正如你在下面看到的。

我试图将事情拆分为单独的程序,然后在最后调用它们。

我确实对我所做的事情有一些疑问。在每个 getValue 过程的第一次比较之后,我明白我需要跳转到下一个比较,但是在第二次比较之后,我不知道该跳转到哪里。

在第二个比较之后跳转到下一个过程的开始是否有意义,或者我应该直接跳到返回或结束过程语句?

我还想就是否只使用一个 getValue 过程然后在最后调用它两次而不是调用两个单独的过程来做同样的事情是否有意义。

它说它构建成功,但是当我运行它时,它只是说“按任意按钮继续......”而不是提供代码,所以我不知道这是怎么回事。

INCLUDE Irvine32.inc
.data
    intVal1 SDWORD ? ; User input one
    intVal2 SDWORD ? ; User input two
    prompt  BYTE "The result of multiplying the numbers is ",0
    prompt2 BYTE "Enter a number that will be multiplied",0
    prompt3 BYTE "Error. Number not in range.",0

.code

; Error procedure displays error prompt
error PROC
    mov edx, OFFSET prompt3
    call WriteString
ret
error ENDP

; GetValue1 procedure will get first input from user and display error      and end program when input not in range
GetValue1 PROC
    mov edx, OFFSET prompt2
    call WriteString    ; tells the user to enter a number
    call ReadInt
    mov intVal1,eax     ; save the returned value from eax to intVal1
    cmp intVal1, 32767
    jl Loop1      ; if intVal1 is less than 32767 jump to Loop1, otherwise continue with error procedure
    call error
    jmp end 
    Loop1: cmp intVal1, -32768
    jg Loop2         ; if intVal1 is greater than -32768 jump to Loop2,  otherwise continue with error procedure
    call error
    jmp end 
    ret
GetValue1 ENDP

; GetValue2 procedure will do the same thing as GetValue1
GetValue2 PROC
    Loop2: mov edx, OFFSET prompt2
    call WriteString
    call ReadInt
    mov intVal2,eax
    cmp intVal1, 32767
    jl Loop3      ; if intVal1 is less than 32767 jump to Loop3, otherwise     continue with error procedure
    call error
    jmp end 
    Loop3: cmp intVal1, -32768
    jg Loop4        ; if intVal1 is greater than -32768 jump to Loop4, otherwise continue with error procedure
    call error
    jmp end 
    ret
GetValue2 ENDP

; MultiplyAndDisplay will multiply the inputs and display the result along with a prompt
MultiplyAndDisplay PROC
    Loop4: imul eax, intVal1     ; signed multiply of eax by intVal1                 
    mov edx, OFFSET prompt
    call WriteString      ; Writes the prompt in edx
    call WriteDec         ; Writes the value in eax
    ret
MultiplyAndDisplay ENDP

main PROC
    call GetValue1
    call GetValue2
    call MultiplyAndDisplay
    exit
main ENDP
END main

【问题讨论】:

  • 编辑删除了未向问题添加任何细节的句子。添加了段落分隔符以提高可读性。

标签: visual-studio-2010 assembly masm irvine32 procedures


【解决方案1】:

使用单个过程获取值。让过程返回 eax 中的值。不要使用试图从一个过程跳转到另一个过程的跳转。主要代码可以是:

main PROC
    call GetValue            ;eax == first value
    push eax                 ;save first value
    call GetValue            ;eax == second value
    pop  ebx                 ;ebx == first value
    call MultiplyAndDisplay  ;change this to multiply eax by ebx
    exit
main ENDP

【讨论】:

  • 为什么不简单地推送两个值,并更改 MultiplyAndDisplay 以使用最后推送的 2 个元素?
  • @Tommylee2k - 是的,但我试图严格遵循原始代码。
猜你喜欢
  • 2012-11-07
  • 2021-06-02
  • 2020-01-30
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
相关资源
最近更新 更多