C float in NASM x86 assembly -
in university project have use binary representation of float number in x86 assembly arithmetic operations. using fpu forbidden try read float number , return dword whatever try "-nan". advices?
edit: use gcc , 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 checked still 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; }
if declare return type func float
result returned in fpu (st0). returning value in eax
have declare integer type. printf
have fake 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 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
Comments
Post a Comment