Thread: Calling Assembly Language programs from C++ program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Calling Assembly Language programs from C++ program

    Okay, for a lab assignment, I need to construct a C++ program that will call an Assembly Language procedure. The provided code is not what I am going to turn in. It is just a dummy program which is to help me get a better understanding on how to do it. Unfortunately, I cannot get the C++ program to call the assembly language program. It assembles and builds fine, but it just does not call the ASM program. I was wondering if anyone here could help me out?

    Here is the header file (called Solution.h):
    Code:
    //
    //
    
    extern "C"
    {
    	int AsmTest(float* a, float* b, float* c);
    }
    Here is the ASM code (called AsmTest):
    Code:
    TITLE 
    
    ; Author: 
    ; Class: 
    ; Date: 
    ; 
    
    .586
    .model flat,C
    
    AsmTest PROTO,
    	aVal:PTR REAL8, bVal:PTR REAL8, cVal:PTR REAL8
    
    .data
    
    	value REAL8 3.5
    
    	answer INT 1
    
    .code
    
    AsmTest PROC,
    	aVal:PTR REAL8, bVal:PTR REAL8, cVal:PTR REAL8
    
    	FINIT
    
    	FLD aVal
    
    	FMUL bVal
    
    	FSTP aVal
    
    	FLD value
    
    	FSTP cVal
    
    	int 3
    
    	mov EAX, answer
    
    	ret
    
    AsmTest ENDP
    
    ;------------------------------------------------------------------------------
    
    END
    Here is the main procedure:
    Code:
    //
    //
    
    #include <iostream>
    #include "Solution.h"
    
    using namespace std;
    
    int main()
    {
    	float a;
    
    	float b;
    
    	float c;
    
    	int x;
    
    	cout << "Enter the first decimal number (if it does not have a decimal number," << '\n'
    		 << "then put a .0 at the end of the number): ";
    
    	cin >> a;
    
    	cout << "Enter the second decimal number (if it does not have a decimal number," << '\n'
    		 << "then put a .0 at the end of the number): ";
    
    	cin >> b;
    
    	cout << "Enter the third decimal number (if it does not have a decimal number," << '\n'
    		 << "then put a .0 at the end of the number): ";
    
    	cin >> c;
    
    	float* a2 = &a;
    
    	float* b2 = &b;
    
    	float* c2 = &c;
    
    	x = AsmTest(a2, b2, c2);
    
    	cout << a << '\n' << b << '\n' << c << '\n' << x << endl;
    
    	system("Pause");
    
    	return 0;
    
    }
    Just FYI, I made these using Microsoft Visual Studio 2010, and I placed the ASM file in the Source Files of the Visual Studio Solution Explorer.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Oh, and the "int 3" that appears in the ASM code is just so that the debugger would activate since there was a problem with the return value (but that is useless if I can't get the C++ code to call the ASM code).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So are you getting any error messages out of this?

    Like say unresolved symbols, or bad object file format?

    One thing to try is to put a leading underscore on the name of the asm function (if you have unresolved symbols)
    Like
    _AsmTest PROC,

    Also, you can put the VS debugger into asm mode, so when you single-step, it is by instruction (and not line).
    Follow the function call in detail to see what happens.

    Write the same function in C++, to compare function prolog/epilog sequences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    I am not getting any error messages from this. As I said, it compiles, builds, and executes fine. It just does not call the AsmTest function.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you're just going to have to single-step it at the asm level, and see where it really is going.

    Are your ASM FPU instructions for floats or doubles?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    They should be for floats.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alt+8 brings up diassembly view, if I am not mistaken. I find it hard to believe that it doesn't call the function. More like you don't notice when it calls the function.
    The best advice here is to use your debugger and see what's going wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly language.
    By JOZZY& Wakko in forum Tech Board
    Replies: 0
    Last Post: 12-18-2009, 05:58 AM
  2. Assembly language in C
    By lruc in forum C Programming
    Replies: 5
    Last Post: 08-27-2008, 07:29 AM
  3. Calling C++ from assembly?
    By Chris361 in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2003, 06:17 AM
  4. calling programs from a c++ program
    By md4u in forum C++ Programming
    Replies: 0
    Last Post: 05-04-2003, 07:39 AM
  5. Calling DOS batch Program or other programs?
    By xzer0cool in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2002, 05:35 PM