混合程序(.asm + .cpp):修改小数学程序的代码以包含浮点输入

时间:2015-06-05 10:56:01

标签: c assembly floating-point hybrid

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

main1.cpp中程序接受int输入(直到输入小于-999999的数字),将其放入数组x[],将输入数放入数组的第0个元素,将数组的指针发送到f1.asm,添加数字,并将结果返回main1.cpp,以便显示。

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

我的具体问题:

  1. 当我将f1.asm变为int时,float中输入偏移的位置发生了变化,我找不到它;

  2. .asm中浮动的数学运算。

  3. (我无法理解我在其他地方找到的.asm花车的解释。)

    提前谢谢。

    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
    

1 个答案:

答案 0 :(得分:2)

罗斯里奇评论的一个例子。

<强> 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

构建&amp;运行

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