simple assembly programming

Started by
8 comments, last by randomDecay 22 years, 5 months ago
I was wondering how I can test if a value is within 100 of another value. How would I go about doing this using assembly? Thanks
Advertisement
Write it in C and look at the assembly code prior to linking.
how do I do that? Thanks.

Most compilers have an option to output an assembly file. Check the docs on your compiler in question. (I think it''s a -S switch w/ Borland, but I''m not sure).

Then open the assembly file in your favorite text editor.

Actually, writing it in C and then doing the conversion yourself might be better. Some compilers are more efficient than others.

JSwing
quote:Original post by randomDecay
I was wondering how I can test if a value is within 100 of another value. How would I go about doing this using assembly? Thanks



I can almost guarantee that writing this routine in assembly will not be faster than writing the same routine in C/C++, so if that is why you are writing the routine please reconsider, from one programmer to another.

relax, it''s kinda a homework assignment
quote:
how do I do that?


In VC Project->Settings->C/C++
change CATAGORY to LISTING FILES
chage LISTING FILE TYPE to ASSEMBLY WITH SOURCE CODE
its pretty easy, here is part of what you have to do:

cmp ax,value + 100
jbe label1

label1:
cmp ax,value - 100
jge label2

label2:
here you know that it is within the range, if you don''t come here then its not withing the range
sorry the jbe should be jle
C version...

BOOL Range_Within_100(int value, int another_value)	{	return (abs(another_value-value) <= 100);	}  


in ASM you'd do something like this...

BOOL Range_Within_100(int value, int another_value)	{	_asm		{		// do subtraction: another_value - value		mov ecx, another_value		sub ecx, value		// absolute value		mov	edx, ecx		sar edx, 15		xor ecx, edx		sub ecx, edx		// assume it's not within range		sub eax, eax		// we were correct		cmp ecx, 100		jg done		// no we weren't change it to 1 (withing range)		inc eax	done:		// return value in EAX		}	} 


Hope this helps....

Edited by - Gladiator on November 14, 2001 2:41:32 AM

This topic is closed to new replies.

Advertisement