How to run both programs together, 1 is C++ the other is assembly? ASM: .586P .M
ID: 3721812 • Letter: H
Question
How to run both programs together, 1 is C++ the other is assembly?
ASM:
.586P
.MODEL FLAT, stdcall ; Flat Memory Model
PUBLIC PROCEDURE1
_TEXT SEGMENT ; Tells the assembler what type of the following is
.data
primeArray DWORD 3,5,7,11,13,17,19,23,29,31,37,41,43,47
.code
PROCEDURE1 PROC Prime: DWORD
MOV ECX, 0
L1:
MOV eax, [primeArray+ecx*4]
CMP Prime, eax
JE L2
inc ECX
CMP ecx, 14
jl L1
mov eax, 0
RET
L2:
mov eax, 1
RET
PROCEDURE1 ENDP
_TEXT ENDS
END
C++:
#include
//prototype for COPYSTR extern function (in this case in asm file)
extern "C" int _stdcall PROCEDURE1( int);
int main()
{
char dest[100] = {0};
char* source = "Enter a number";
char* source1 = "The number is prime";
char* source2 = "Tne number is not prime";
int test1 = 7;
int prime = PROCEDURE1(test1);
if(prime == 1)
{
printf("%s ",source1);
}
else
{
printf("%s ",source2);
}
}
Explanation / Answer
We can mix the assembly statements within C++ programs using keyword asm. Let's first define what are inline functions.
The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called. Inline Assembly is just a set of assembly instructions written as inline functions.
We can use either of the following formats for basic inline assembly.
Just in case of more than one assembly instruction, use semicolon at the end of each instruction.
Compile it using "-g" option of GNU C compiler "gcc" to keep debugging information with the executable and then using GNU Debugger "gdb" to inspect the contents of CPU registers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.