【问题标题】:Intel Assembly program using function使用函数的 Intel 汇编程序
【发布时间】:2018-01-24 09:25:35
【问题描述】:

我是汇编新手,一直坚持使用这个程序,其中使用了一个简单的加法函数,该函数接受两个输入并给出总和。 我现在得到的输出总是0。 我认为这与数据类型的处理方式有关。 任何帮助表示赞赏。

注意:程序在没有函数的情况下也能正常工作

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib



.data

msg1 db "Please enter first number ",0
msg2 db "Pleae enter second number ",0
input1 db 10 DUP(0)
input2 db 10 DUP(0)
sum dword 0
sums db 10 DUP(0)
msg3 db "The sum of your numbers is :",0
temp1 dword     0
temp2 dword     0



fsum PROTO :dword, :dword, :dword

.code


start:

    invoke StdOut , addr msg1
    invoke StdIn , addr input1,10
    invoke StdOut , addr msg2
    invoke StdIn , addr input2,10


    ;Strip CRLF
    invoke StripLF, addr input1
    invoke StripLF, addr input2


    ;string to int

    invoke atodw, addr input1
    mov temp1,eax
    invoke atodw , addr input2
    mov temp2,eax

    ;Function CALL
    invoke fsum, addr temp1,addr temp2,addr sum




    ;int to string
    invoke dwtoa,sum, addr sums

    ;Printing OUTPUT
    invoke StdOut, addr msg3
    invoke StdOut, addr sums

    invoke ExitProcess, 0

    ;Function Definition

    fsum PROC x:DWORD , y:DWORD , z:DWORD

    mov eax,x
    add eax,y
    mov z,eax

    ret
    fsum endp

end start

【问题讨论】:

    标签: assembly x86 masm32


    【解决方案1】:

    通过地址传递前两个参数是没有意义的。当您可以只返回结果时,使用输出参数也没有任何实际意义。

    所以更合理的方法是:

    invoke fsum, temp1, temp2
    ; The sum is now in eax
    
    ...
    
    fsum PROC x:DWORD , y:DWORD
        mov eax,x
        add eax,y
        ret 
    fsum endp
    

    如果您绝对坚持使用输出参数,您仍然需要进行一些小的更改:

    ; First two arguments are passed by value; last one by address
    invoke fsum, temp1,temp2,addr sum
    
    ...
    
    fsum PROC x:DWORD , y:DWORD , z:DWORD
        mov eax,x
        add eax,y
        mov edx,z       ; Get the address to write to
        mov [edx],eax   ; ..and write to it.
        ret
    fsum endp
    

    【讨论】:

    • 谢谢@Michael,澄清一下,你能告诉我有地址和无地址传递参数的区别吗?例如:-invoke fsum,addr temp1 并调用 fsum,temp1
    • 地址传递对于仅用作输入的标量类型没有任何意义。如果您的参数是结构/数组或输出参数,则按地址传递,否则按值传递。
    • 为了能够获取通过地址传递的参数的值,您首先必须将地址加载到寄存器中,然后从该地址加载值。因此,与按值传递相比,您将引入不必要的加载操作。
    • 谢谢,有道理。
    猜你喜欢
    • 2013-08-04
    • 2017-12-26
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多