Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C++

Convert Float to Int using Assembly Language Programming

Rate me:
Please Sign up or sign in to vote.
2.93/5 (10 votes)
15 Jun 2005CPOL 68.6K   307   12   8
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:

C++
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;
}

History

  • 15th June, 2005: Initial version

License

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


Written By
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.

Comments and Discussions

 
GeneralProblem with conversion of float to int using C++ Pin
Kishore JP3-Mar-08 1:25
Kishore JP3-Mar-08 1:25 
Hello All,
Please go through the following piece of code which is giving problem.
#include <iostream.h>
void main()
{
int angle = 2330,angul_step=55;//change angul_step to 60
float nAngle = (float)(angle / 10) - 2;
float nAngulationStep = (float)angul_step / 100;
float nFrames = (int)(nAngle/nAngulationStep);
int nPhaseFrames = (int)nFrames + 1;
cout<<"The nFrames is "<<nFrames<<endl;
cout<<"The nPhaseFrames is "<<nPhaseFrames<<endl;
}
The out put is in Debug mode.
The nFrames is 419
The nPhaseFrames is 420
The out put is in Release mode
The nFrames is 420
The nPhaseFrames is 421

In the same way: If you change the angul_step to 60 and run the out put in Debug and Release mode are
Debug mode:
The nFrames is 384
The nPhaseFrames is 385
The out put in Release mode is
The nFrames is 385
The nPhaseFrames is 386

I have replace Float with Double:
The output are different.
Debug mode:
The nFrames is 419
The nPhaseFrames is 420
Release Mode:
The nFrames is 420
The nPhaseFrames is 421

In the same way: If you change the angul_step to 60 and run the out put in Debug and Release mode are
Debug Mode
The nFrames is 385
The nPhaseFrames is 386
Release Mode
The nFrames is 385
The nPhaseFrames is 386

Changing float to double works only for specific numbers. The problem is unknown to me.
Please let me know if any one had this experience and solution.
I need to calculate the value "ROUNDDOWN((Angle-2)/AngulationStep)+1"
where Angle will be ((angle/10) -2) and angulation Step would be angul_step/100.
Thanks much.
Kishore
Questionhi why so difficult ? Pin
justme2215-Jun-05 2:00
justme2215-Jun-05 2:00 
Answer2+2 == sqrt(16) Pin
Kochise15-Jun-05 3:11
Kochise15-Jun-05 3:11 
GeneralRe: 2+2 == sqrt(16) Pin
Rick York15-Jun-05 8:11
mveRick York15-Jun-05 8:11 
AnswerRounding mode Pin
Don Clugston15-Jun-05 14:29
Don Clugston15-Jun-05 14:29 
GeneralRe: Rounding mode Pin
John M. Drescher15-Jun-05 16:16
John M. Drescher15-Jun-05 16:16 
AnswerRe: hi why so difficult ? Pin
John M. Drescher15-Jun-05 16:10
John M. Drescher15-Jun-05 16:10 
AnswerRe: hi why so difficult ? Pin
JeanLuc_17-Sep-09 6:52
JeanLuc_17-Sep-09 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.