Visual Studio C++ Assembly Hybrid Programming
Experimental requirements
Write a subroutine in assembly language that implements the GCD recursive formula, with no requirements on the form of the inlet and outlet parameters, but with C functions to get inputs, call the assembly recursive subroutine, and display in C the results returned by the subroutine.
Visual Studio 2020 Download
Check the C++ desktop development option when downloading.
Environment Configuration
optionFile->New->Project Language SelectionC++ Selectionempty program 。
Modify the environment configuration tox86。
New in the project respond in singing
Or add these two files to the project.
Right-click on the item and selectGenerate dependencies - > Generate customizationsCheck the boxmasm Options.
right click file, selectcausality。
Exclusion from generation optionclogged。
Item type optionMicrosoft Macro Assembler。
The following error occurs when compiling and running.
scanf‘: This function or variable may be using scanf_s instead
Answers to related questionsLink
This can be done in the.cpp
The header of the file is added to the#define _CRT_SECURE_NO_WARNINGS
。
Code
cpp file
#define _CRT_SECURE_NO_WARNINGS
#include <>
// Declare the external assembly function
extern "C" int GCD(int a, int b); // Declare an external assembly function.
int main() {
int a, b, result.
// Get user input
printf("Please enter two integers to compute their greatest common divisor: "); scanf("%d %d", &a, &b); // Get user input.
scanf("%d %d", &a, &b);
// Call the assembly function
result = GCD(a, b);
// Output the result
printf("The greatest common divisor of the numbers %d and %d is: %d\n", a, b, result);
return 0; }
}
asm file
.model flat, c
.code
public GCD ; Declares a function public and can be called externally.
GCD proc
mov eax, [esp+4] ; get first parameter a (at esp+4)
mov ebx, [esp+8] ; get second argument b (at esp+8)
gcd_loop.
cmp ebx, 0 ; if b == 0, go to end of loop
je gcd_done
xor edx, edx ; clear edx to avoid interference in remainder calculation
div ebx ; eax = eax / ebx, remainder to edx
mov eax, ebx ; a = b
mov ebx, edx ; b = a % b
jmp gcd_loop
gcd_done.
ret ; Return result is stored in eax
GCD endp
endp