Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use subroutine calls using msvc inline assembly. This is the code:
C
int main() {
	int a = 4, b = 2, c = 1;
	__asm {
	LOC_FUN:
		mov eax, 6
		mov ebx, 21
		mov ecx, 32
	    ret
		
	MAIN :
		xor eax, eax
		xor ebx, ebx
		xor ecx, ecx
	    call LOC_FUN

		mov a, eax
		mov b, ebx
		mov c, ecx
	};
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", c);

	return 0;
}


What I have tried:

I have tried to comment the LOC_FUN subroutine definition and subroutine call, and that outputs the value of a, b, c to the console. I think I am making a mistake in defining subroutine, or calling it. Please help in letting me know what I am doing wrong.
Posted
Updated 24-Oct-23 12:08pm
v3
Comments
Dave Kreskowiak 21-Oct-23 11:34am    
Just to add what 'Griff said below, use the debugger. That would have shown you what's going on immediately just by running the code and stepping through it one line at a time.

The debugger is there to debug YOU and your understanding of the code. Use it.
OriginalGriff 21-Oct-23 12:07pm    
It's been a looong time since I last wrote any assembler code - this reminds me of how much I miss it!
Dave Kreskowiak 21-Oct-23 12:15pm    
Yeah, same. The last thing I wrote was text and graphics routines way back in the Windows-on-DOS days.
OriginalGriff 21-Oct-23 16:45pm    
Central bootload and Threading OS embedded in an Industrial Inkjet printer for products as they went down production lines. I still see my handiwork in supermarkets when I go shopping! UI in C++, interrupts / drivers / OS in ARM assembler.
Dave Kreskowiak 21-Oct-23 17:15pm    
Cool stuff!

1 solution

First off, you don't need to clear eax, ebx, or ecx - your function always overwrites what is there so it's irrelevant what they contain on entry.

Secondly, your assembly code is within a function body, so the assembler label LOC_FUN just marks a point in the assembler code, not a "new function" start in the C sense.
You need to jump over the function to your actual call - because otherwise the ret is encountered without a call, and the app crashes or exits, providing no printout.

Additionally, Assembler labels aren't case sensitive: so having yours called MAIN may confuse things with the C code main function.

Try this:
C/ASM
#include <stdio.h>
int main()
	{
	int a = 4, b = 2, c = 1;
	__asm 
		{
			jp START
		LOC_FUN:
			mov eax, 6
			mov ebx, 21
			mov ecx, 32
			ret

		START :
			call LOC_FUN
			mov a, eax
			mov b, ebx
			mov c, ecx
		};
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", c);

    return 0;
	}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900