12,756,447 members (32,921 online)
alternative version

#### Stats

50.8K views
12 bookmarked
Posted 14 Jun 2005

# Convert Float to Int using Assembly Language Programming

, 15 Jun 2005 CPOL
 Rate this:
An article on Converting Float value to an integer value trimming off the decimal part

## Introduction

This program takes a floating point value as input and converts it into an integer by trimming the decimal point. Please e-mail your suggestion to Jayadev at jayadevmv@rediffmail.com.

## Background

The code uses the tag `_asm `in VC++. Using the assembly code, the floating point value is converted into an integer value.

## Using the Code

The code is very simple and it can be plugged into any other code easily.

The function `ftoi `looks like this:

```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;
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;
}```

## History

• 15th June, 2005: Initial version

## Share

 Web Developer 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

 View All Threads First Prev Next
 hi why so difficult ? justme2215-Jun-05 3:00 justme22 15-Jun-05 3:00
 hi why so difficult ?I'm not 100% sure if it's supported (floating instructions) as inline assembler in vc FLD srcvalue ; Push real onto stackFISTP destvalue ; Convert top of stack to integerthat´s it
 2+2 == sqrt(16) Kochise15-Jun-05 4:11 Kochise 15-Jun-05 4:11
 Re: 2+2 == sqrt(16) Rick York15-Jun-05 9:11 Rick York 15-Jun-05 9:11
 Rounding mode Don Clugston15-Jun-05 15:29 Don Clugston 15-Jun-05 15:29
 Re: Rounding mode John M. Drescher15-Jun-05 17:16 John M. Drescher 15-Jun-05 17:16
 Re: hi why so difficult ? John M. Drescher15-Jun-05 17:10 John M. Drescher 15-Jun-05 17:10
 Re: hi why so difficult ? JeanLuc_17-Sep-09 7:52 JeanLuc_ 17-Sep-09 7:52
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Feb-17 10:41 Refresh 1