C float in NASM x86 assembly

Qba05550

In my university project i have to use binary representation of float number in x86 assembly for arithmetic operations. Using FPU is forbidden so i try to read float number and return it as DWORD but whatever i try to do i get "-nan". Any advices?

Edit: I use gcc and it's 32 bit code

Declaration in C (i can't change that)

extern "C" float func(float num);

*.asm file

section .text
global  func

func:
;prolog
    push    ebp
    mov ebp, esp

; zapamiętanie rejestrów zachowywanych
    push ebx
    push esi
    push edi

    mov eax, DWORD [ebp+8]
    ;mov eax, 0xffffffff i checked that but i still get the same result

; odtworzenie rejestrów, które były zapamiętane
    pop edi
    pop esi
    pop ebx

;epilog 
    pop ebp
    ret

Example result (for 256)

01000011100000000000000000000000
11111111110000000000000000000000
num1: 256.000000
num2: -nan

Edit:

C code without checking bits part

#include <stdio.h>

extern "C" float func(float num);

int main()
{
    float num1;
    float num2;

    scanf("%f", &num1);
    num2=func(num1);

    printf("num1: %f\nnum2: %f\n", num1, num2);
    return 0;
}
rkhb

If you declare the return type func as float the result will be returned in the FPU (ST0). For returning a value in EAX you have to declare it as an integer type. For printf you have to fake a float. Example:

caller.c:

#include <stdio.h>
#include <stdint.h>
extern float asmfunc1(float);
extern uint32_t asmfunc2(float);

int main (void)
{
    printf ("asmfunc1: %f\n", asmfunc1(456.78));

    uint32_t ifl = asmfunc2(123.45);
    float* pfl = (float*) &ifl;             // Faking a float
    printf ("asmfunc2: %f\n", *pfl);

    return 0;
}

callee.asm:

section .text
global asmfunc1, asmfunc2

asmfunc1:
    fld dword [esp+4]
    ret

asmfunc2:
    push ebp
    mov ebp, esp
    mov eax, [ebp+8]
    leave
    ret

Build & run:

nasm -felf callee.asm
gcc -m32 callee.o caller.c
./a.out

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling NASM float in x86 assembly from C

From Dev

Input incorrect Assembly x86 NASM

From Dev

Assembly x86 NASM - Avoid read return key

From Dev

x86 Assembly (NASM): Floating Point Exception, not dividing by 0?

From Dev

Read 16 bits from memory in Assembly x86 NASM

From Dev

calling gets after malloc (assembly NASM x86)

From Dev

Assembly x86 NASM - Avoid read return key

From Dev

Read 16 bits from memory in Assembly x86 NASM

From Dev

Indexed float array in NASM x86 16-bit

From Dev

Assembly x86 - Calling C functions

From Dev

Assembly x86 - Calling C functions

From Dev

Does the stack automatically get popped when leaving a function in x86 NASM assembly language?

From Dev

unable to read from file when user provides filename (x86 assembly program using nasm)

From Dev

Cursor won't move upon BIOS interrupt (x86 assembly/NASM)

From Dev

Incorrect output when computing square root with Nasm x86 Assembly

From Dev

x86 Nasm assembly - push'ing db vars on stack - how is the size known?

From Dev

Convert x86 Assembly Jump Table to C

From Dev

writing c code based on assembly 32 bit (x86)

From Dev

Converting a snippit of x86 Assembly Code into C

From Dev

Factorial Assembly x86

From Dev

For loop in x86 assembly

From Dev

intro to x86 assembly

From Dev

Assembly code x86

From Dev

Sqrt in Assembly x86

From Dev

Modulus in Assembly x86

From Dev

using atof function in x86 NASM

From Dev

Segmentation faults in x86 NASM program

From Dev

NASM x86, unexpected result of FIST

From Dev

malloc and free x86 NASM + compiling