本文介绍了混合程序(.asm + .cpp):修改小型数学程序的代码以包含浮点数输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Intel x86,TASM和BorlandC编译器,以及使用的TLINK。)

main1.cpp中程序需要 int 输入(直到你输入一个小于-999999的数字),把它放入一个数组 x [] ,将输入的数量放入数组的第0个元素,将数组的指针发送到 f1.asm ,将数字相加,并将结果返回给 main1.cpp



如何修改它以便将浮点数字作为输入?



我的具体问题是:
$ b


  1. 输入的位置在当我将 int 转换为 float 时,f1.asm 发生了变化,我无法找到它在$ .asm 中的浮点数上的数学运算。



  2. $ b

    (我无法真正理解在其他地方找到的 .asm float)的解释。 p>

    预先感谢您。

    main1 .cpp:

      #include  
    #include< stdlib.h>
    #include< math.h>

    externCint f1(int * x);

    int main()
    {

    int x [100],i; (i = 1; x [i-1]> = - 999999; i ++)
    {
    cout< x [<<我 ] =;
    cin>> x [i]; //输入元素,当它们> = -999999
    }

    x [0] = i-1; //第0个数组元素获得输入元素的数量

    cout <<<<<
    返回0;

    f1.asm:

      .model SMALL,C 

    .data

    .code

    PUBLIC f1
    f1 PROC

    push BP
    mov BP,SP; SP包含来自c ++函数的输入

    mov ax,[bp + 4];得到数组的地址
    mov bp,ax; BP现在指向数组的第0个元素
    ; (这是要添加的c ++输入的数量)

    mov di,0
    mov ax,0

    mov cx,[bp] ;输入数量存储在cx
    dec cx

    add bp,2;将bp指向下一个数字 - 第一个c ++输入

    loop1:
    mov bx,[bp]
    add ax,bx;将输入添加到增长的堆

    add bp,2;移动偏移量指向下一个输入
    inc di;增加加法计数器

    cmp di,cx;如果你加起来所有的c ++输入,退出循环
    jne loop1

    pop BP
    ret

    f1 ENDP

    .stack
    db 100(?)

    END


    解决方案

    一个例子,作为除了罗斯里奇的意见。


    $ b

    main.cpp:

      #include< iostream.h> 

    externC{
    int f1(int *);
    float f2(float *);
    }

    int main()
    {
    int x1 [100] = {5,3,4,5,6};
    float x2 [100] = {5,3.0,4.0,5.0,6.5};

    cout<< x1 =<< f1(x1)<< ENDL;
    cout<< x2的和=< f2(x2)< ENDL;

    返回0;

    f1.asm:

      .model SMALL,C 

    LOCALS @@
    PUBLIC f1,f2

    .code
    f1 PROC

    push BP
    mov BP,SP; SP包含来自c ++函数的输入

    mov ax,[bp + 4];得到数组的地址
    mov bp,ax; BP现在指向数组的第0个元素
    ; (这是要添加的c ++输入的数量)

    mov di,0
    mov ax,0

    mov cx,[bp] ;输入数量存储在cx
    dec cx
    add bp,2;将bp指向下一个数字 - 第一个c ++输入

    @@ loop1:
    mov bx,[bp]
    add ax,bx;将输入添加到增长堆
    add bp,2;移动偏移量指向下一个输入
    inc di;增加加法计数器
    cmp di,cx;如果将所有的c ++输入相加,退出循环
    jne @@ loop1

    pop BP
    ret

    f1 ENDP

    f2 PROC
    push bp
    mov bp,sp
    sub sp,2;用于本地临时变量的空间

    mov bx,[bp + 4];获取数组的地址为BX
    ; (否则使用BP)

    fld dword ptr ss:[bx];加载第一个浮点数
    fistp word ptr [bp-2];并将其存储为int
    mov cx,[bp-2];数组的长度
    dec cx

    mov di,0
    fldz;在ST0中加载null
    add bx,4;移动bx指向下一个浮点数

    @@ loop1:
    fadd dword ptr ss:[bx]; ST0 = ST0 + [BX]
    add bx,4;移动偏移量指向下一个输入
    inc di;增加加法计数器
    cmp di,cx;如果将所有的c ++输入相加,退出循环
    jne @@ loop1

    mov sp,bp
    pop bp
    ret;在ST0中的返回值
    f2 ENDP

    END

    构建&运行:

     路径< BCC的路径> \ BIN;< TASM的路径> \ BIN 
    BCC.EXE main.cpp f1.asm
    main.exe


    (Intel x86. TASM and BorlandC compilers, and TLINK used.)

    In main1.cpp the program takes int input (until you input a number smaller than -999999), puts it into an array x[], puts the number of inputs into array's 0th element, sends array's pointer to f1.asm, adds the numbers, and returns the result to main1.cpp so it can be displayed.

    How to modify it so it will include floating-point numbers as input?

    My specific problems:

    1. The location of the input's offset in f1.asm changes when I turn int into float, and I can't find it ;

    2. Math operations on floats in .asm.

    (I couldn't really comprehend explanation on .asm floats I found elsewhere.)

    Thank you in advance.

    main1.cpp:

    #include <iostream.h>
    #include <stdlib.h>
    #include <math.h>
    
    extern "C" int f1( int* x );
    
    int main()
    {   
    
        int x[100], i ;
    
        for( i = 1 ; x[i-1]>=-999999 ; i++ )
        {
             cout << "x[" << i << "] = " ;
             cin  >> x[i] ;                // Input elements while they're >= -999999
        }
    
        x[0] = i-1 ; // 0th array element gets the number of inputed elements
    
        cout<<"\nSum of inputs = " << f1(x) ;
    
        return 0;
    }
    

    f1.asm:

    .model SMALL, C
    
    .data
    
    .code
    
    PUBLIC f1
    f1 PROC
    
        push    BP
        mov BP,SP       ; SP contains input from the c++ function
    
        mov ax,[bp+4]       ; get the address of the array
        mov bp, ax          ; BP now points to the array's 0th element
                    ; (which is the the number of the to-be-added c++ inputs)
    
        mov di, 0
        mov ax, 0
    
        mov cx, [bp]    ; number of unputs gets stored in cx
        dec cx
    
        add bp, 2       ; Move bp to point at the next number -- the first c++ input
    
    loop1:
        mov bx, [bp]      
        add ax, bx      ; add the input to the growing pile
    
        add bp, 2       ; move the offset to point to the next input
        inc di      ; increase the Additions Counter
    
        cmp di, cx      ; if you add up all of the c++ inputs, exit loop
        jne loop1
    
        pop BP
        ret
    
    f1 ENDP
    
    .stack
    db 100(?)
    
    END
    
    解决方案

    An example as addition to the comments of Ross Ridge.

    main.cpp:

    #include <iostream.h>
    
    extern "C" {
        int f1( int* );
        float f2( float* );
    }
    
    int main()
    {
        int x1[100]   = {5,3,4,5,6};
        float x2[100] = {5,3.0,4.0,5.0,6.5};
    
        cout << "Sum of x1 = " << f1(x1) << endl;
        cout << "Sum of x2 = " << f2(x2) << endl;
    
        return 0;
    }
    

    f1.asm:

    .model SMALL, C
    
    LOCALS @@
    PUBLIC f1, f2
    
    .code
    f1 PROC
    
        push BP
        mov BP,SP           ; SP contains input from the c++ function
    
        mov ax,[bp+4]       ; get the address of the array
        mov bp, ax          ; BP now points to the array's 0th element
                            ; (which is the the number of the to-be-added c++ inputs)
    
        mov di, 0
        mov ax, 0
    
        mov cx, [bp]        ; number of unputs gets stored in cx
        dec cx
        add bp, 2           ; Move bp to point at the next number -- the first c++ input
    
        @@loop1:
        mov bx, [bp]
        add ax, bx          ; add the input to the growing pile
        add bp, 2           ; move the offset to point to the next input
        inc di              ; increase the Additions Counter
        cmp di, cx          ; if you add up all of the c++ inputs, exit loop
        jne @@loop1
    
        pop BP
        ret
    
    f1 ENDP
    
    f2 PROC
        push bp
        mov bp, sp
        sub sp, 2                   ; Space for a local temporary variable
    
        mov bx,[bp+4]               ; Get the address of the array into BX
                                    ; (BP is used otherwise)
    
        fld dword ptr ss:[bx]       ; Load the first float
        fistp word ptr [bp-2]       ;     and store it as int
        mov cx, [bp-2]              ; Length of array
        dec cx
    
        mov di, 0
        fldz                        ; Load null into ST0
        add bx, 4                   ; Move bx to point to the next float
    
        @@loop1:
        fadd dword ptr ss:[bx]      ; ST0 = ST0 + [BX]
        add bx, 4                   ; move the offset to point to the next input
        inc di                      ; increase the Additions Counter
        cmp di, cx                  ; if you add up all of the c++ inputs, exit loop
        jne @@loop1
    
        mov sp, bp
        pop bp
        ret                         ; Return value in ST0
    f2 ENDP
    
    END
    

    Build & run:

    PATH <Path to BCC>\BIN;<Path to TASM>\BIN
    BCC.EXE main.cpp f1.asm
    main.exe
    

    这篇关于混合程序(.asm + .cpp):修改小型数学程序的代码以包含浮点数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:37