Hi. I am trying to use assembly in C++, and need a few pointers(pun intended) lol

I want to use C++ structs as operands, but can't figure out how.

my code is like this so far:

TEST.CPP

extern "C" WORD _add(WORD num1, WORD num2);
int _tmain(int argc, _TCHAR* argv[])
{
	WORD number = 5;
	WORD number2 = 5;

	number = _add(number, number2);
	cout << number << endl;
	
	getch();
    return 1;
}

_ADD.ASM

.586
.MODEL FLAT, C
.STACK
.DATA
.CODE

_add PROC num1:WORD, num2:WORD

mov ax, num1
mov bx, num2
add ax, bx
ret

_add ENDP 
END

I cant figure out how to use pointers to memory as operands =(

this is what I was trying, could someone point out what I am doing wrong please?

TEST.CPP

extern "C" WORD _add(WORD *num1, WORD *num2);
int _tmain(int argc, _TCHAR* argv[])
{
	WORD *number;
	WORD *number2;
	*number = 5;
	*number2 = 5;
	number = _add(number, number2); //pass the address in
	cout << number << endl;
	
	getch();
    return 1;
}

_ADD.ASM

.586
.MODEL FLAT, C
.STACK
.DATA
.CODE

_add PROC num1:DWORD, num2:DWORD ;pointers are 4 bytes or ulong

mov ax, [num1]  ;dereference to get the data pointed to by num1
mov bx, [num2]  ;and num2
add ax, bx
ret

_add ENDP 
END

I know I have a lot of errors..but I cant find a decent clearly written article on this for some reason..

Thanks again =)

It's generally easier to use the inline assembler, unless you're in VC++(GCC's is far superior).

heh...I tried that..
it was giving me all kinds of errors where there is nothing wrong...
Even this rediculously simple code failed:

__asm
{
	mov eax, 256
	mov ebx, 3
	shr eax, ebx  <--error "C2415 Improper operand type"
}

I was like W.T.?
...

Thanks! but wow...I knew assembly was finicky, but I not to this degree.. I guess I better keep that reference you gave me in the bookmarks.

this code worked fine:

__asm
{
	mov eax, 256
	mov cl, 3
	shr eax, cl
}

I guess Ill start digging through Intel's manual, but I would still like some help on how to use information from a struct as an operand

http://www.codeproject.com/KB/cpp/gccasm.aspx
In VC++'s assembler you wrap variables with [], __asm { add [name.variable], value } With pointers __asm { mov type ptr [name], value } Learn to use Google, read manuals and assembly output(in compiler options).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.