Click here to Skip to main content
12,507,343 members (69,749 online)
Click here to Skip to main content

Stats

49.8K views
281 downloads
12 bookmarked
Posted

Convert Float to Int using Assembly Language Programming

, 15 Jun 2005 CPOL
An article on Converting Float value to an integer value trimming off the decimal part
ftoi
ftoi.dsp
ftoi.dsw
ftoi.opt
ftoi.plg
test.opt
test.plg
#include <stdio.h>
int ftoi(float flt)
{
	int i;
	_asm
	{
		mov  eax,flt; //loaded mem to acc
		rcl  eax,1;   //left shift acc to remove the sign
		mov  ebx,eax; //save the acc
		mov	 edx,4278190080; //clear reg edx;
		and  eax,edx; //and acc to retrieve the exponent
		shr  eax,24;
		sub  eax,7fh; //subtract 7fh(127) to get the actual power 
		mov  edx,eax; //save acc val power
		mov  eax,ebx; //retrieve from ebx
		rcl  eax,8;	 //trim the left 8 bits that contain the power
		mov  ebx,eax; //store
        mov  ecx, 1fh; //subtract 17 h
		sub  ecx,edx; 
        mov  edx,00000000h;
        cmp  ecx,0;
		je   loop2;
		shr  eax,1;
		or   eax,80000000h;		
loop1:	
		shr  eax,1; //shift (total bits - power bits);
		sub  ecx,1;
		add  edx,1;
		cmp  ecx,0;
		ja   loop1;
loop2:  
		mov  i, eax;		
	
//check sign +/-		
sign:
		mov  eax,flt;
		and  eax,80000000h;
		cmp  eax,80000000h;
		je	 putsign;
	}

	return i;

putsign:
	return -i;
}


void main()
{
	float f = 0;
	while (true)
	{
		printf("Enter the Float value\n");
		scanf("%f", &f);
		printf("float %f\n", f);
		printf("int %d\n",ftoi(f)); 
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Jayadev MV
Web Developer
India India
Hi.. I have been working on C/C++ for more than 2 years. My areas of interest are Network programming and System Level programming.
I work with C/C++ over Windows and Solaris.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 15 Jun 2005
Article Copyright 2005 by Jayadev MV
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid