NASM x64: Floating Point Part Incorrectly Printed as 0.000000

I’m a newbie to assembly and I tried to write an assembly program using NASM on Windows (x64) to extract and print the fractional part of a floating-point number. However, my program prints Floating Point Part: 0.000000 instead of the expected Floating Point Part: 0.500000.

Here is my current code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>section .data
fmt db "Floating Point Part: %f", 10, 0
float_value dq 10.5
result dq 0.0
section .text
global main
extern printf
main:
; Function prologue
push rbp
mov rbp, rsp
sub rsp, 32 ; Allocate shadow space
; Load floating-point value into the FPU
fld qword [rel float_value]
; Call floatVal function
call floatVal
; Store result from the FPU to memory
fstp qword [rel result]
; Print the result using printf
lea rcx, [rel fmt] ; First argument: format string
lea rdx, [rel result] ; Second argument: address of result
movsd xmm0, qword [rdx] ; Move the double to xmm0 for printf
call printf
; Function epilogue
add rsp, 32 ; Deallocate shadow space
mov eax, 0
pop rbp
ret
floatVal:
; Function prologue
push rbp
mov rbp, rsp
sub rsp, 32 ; Allocate shadow space
; Get the integer part
fld st0 ; Copy the value to the top of the stack
frndint ; Round to integer
fsub ; Subtract integer part from original value (st0 - st1)
; Function epilogue
add rsp, 32 ; Deallocate shadow space
pop rbp
ret
</code>
<code>section .data fmt db "Floating Point Part: %f", 10, 0 float_value dq 10.5 result dq 0.0 section .text global main extern printf main: ; Function prologue push rbp mov rbp, rsp sub rsp, 32 ; Allocate shadow space ; Load floating-point value into the FPU fld qword [rel float_value] ; Call floatVal function call floatVal ; Store result from the FPU to memory fstp qword [rel result] ; Print the result using printf lea rcx, [rel fmt] ; First argument: format string lea rdx, [rel result] ; Second argument: address of result movsd xmm0, qword [rdx] ; Move the double to xmm0 for printf call printf ; Function epilogue add rsp, 32 ; Deallocate shadow space mov eax, 0 pop rbp ret floatVal: ; Function prologue push rbp mov rbp, rsp sub rsp, 32 ; Allocate shadow space ; Get the integer part fld st0 ; Copy the value to the top of the stack frndint ; Round to integer fsub ; Subtract integer part from original value (st0 - st1) ; Function epilogue add rsp, 32 ; Deallocate shadow space pop rbp ret </code>
section .data
    fmt db "Floating Point Part: %f", 10, 0
    float_value dq 10.5
    result dq 0.0

section .text
global main
extern printf

main:
    ; Function prologue
    push rbp
    mov rbp, rsp
    sub rsp, 32              ; Allocate shadow space

    ; Load floating-point value into the FPU
    fld qword [rel float_value]

    ; Call floatVal function
    call floatVal

    ; Store result from the FPU to memory
    fstp qword [rel result]

    ; Print the result using printf
    lea rcx, [rel fmt]       ; First argument: format string
    lea rdx, [rel result]    ; Second argument: address of result
    movsd xmm0, qword [rdx]  ; Move the double to xmm0 for printf
    call printf

    ; Function epilogue
    add rsp, 32              ; Deallocate shadow space
    mov eax, 0
    pop rbp
    ret

floatVal:
    ; Function prologue
    push rbp
    mov rbp, rsp
    sub rsp, 32              ; Allocate shadow space

    ; Get the integer part
    fld st0                  ; Copy the value to the top of the stack
    frndint                  ; Round to integer
    fsub                     ; Subtract integer part from original value (st0 - st1)

    ; Function epilogue
    add rsp, 32              ; Deallocate shadow space
    pop rbp
    ret

Compilation and Execution Steps:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>nasm -f win64 flo.asm -o flo.o
gcc -m64 -o flo flo.o -lkernel32 -lmsvcrt
.flo.exe
</code>
<code>nasm -f win64 flo.asm -o flo.o gcc -m64 -o flo flo.o -lkernel32 -lmsvcrt .flo.exe </code>
nasm -f win64 flo.asm -o flo.o
gcc -m64 -o flo flo.o -lkernel32 -lmsvcrt
.flo.exe

Expected Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Floating Point Part: 0.500000
</code>
<code>Floating Point Part: 0.500000 </code>
Floating Point Part: 0.500000

Actual Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Floating Point Part: 0.000000
</code>
<code>Floating Point Part: 0.000000 </code>
Floating Point Part: 0.000000

Environment:

  • Windows 10 x64
  • NASM
  • Mingw-w64

What I’ve Tried:

  • Ensured that the floating-point operations are correctly handled.
  • Checked that the stack is properly aligned and shadow space is allocated.
  • Attempted to print the entire floating-point value directly to isolate the issue.
  • Used RIP-relative addressing to ensure correct address handling.

The floating-point operations seem to be executed, but the result is not what I expect. It appears as though the result might be overwritten or not correctly passed to printf.

What exactly do I need to know

  • Why does the program print 0.000000 instead of the expected fractional part?
  • Are there any mistakes in the floating-point handling or function calling conventions?
  • How can I correctly print the fractional part of the floating-point number?

I’m very new to low-level programming and any help or suggestions would be greatly appreciated.

PS:
I found the same problems published before in StackOverflow and I tried to update my code referring to them. They aren’t working for me and my apologies for re-posting

  • Floating point in nasm
  • Floating Point always returns 0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật