Assembly x86_64 number bad printed
by catalanux from LinuxQuestions.org on (#583AA)
Hello everybody:
I made a program to convert any integer double number into a floating point one, so that --in a later program- it will be use as an FP operand into an arithmetic operation with double FP.
f.i.:
int a = 28 ---> conversion to FP double --> 28.00
double b= 12.50
double c= a + b = 40.50
In this point I think I've converted the integer number into a double one, but I gets the next ask:
Quote:
I need help to fix it because i don't know how to do it.
The code of the program is this:
Code:--------------------------------------------------------------
; calculam.asm
; This program will check if a number is an INT or a FP:
; if it's an INT the program will convert it to FP
; if it's a FP the program won't nothing
; Finally the program will diplay the final FP number got.
; -------------------------------------------------------------
; Assembly for GNU/Linux x86_64, NASM compiler & GCC for link
; -------------------------------------------------------------
; 1) Compile: nasm -f elf64 calculam.asm
; 2) Link: gcc -no-pie calculam.o -o calculam
; 3) Run:./calculam
; ------------------------------------------------------------
bits 64 ; Only for x86_64 engines !
extern printf ; invoking the printf command
section .data
a: dq 28 ; We will test 'a'
fmt: db "Result is: %g", 10, 0 ; format of message to print
section .bss
op1: resq 1 ; let's reserve 1 qword (64 bits)
section .text
global main
main:
push rbp ; setting up the stack
.rounding:
movsd xmm1, qword [a] ; move the value of 'a' into xmm1
fld qword [a] ; push xmm1 into st(0) stack
roundsd xmm2, xmm1, 3 ; xmm2 will be the truncated number of xmm1
.comparision:
ucomisd xmm2, xmm1 ; comparision between xmm2 & xmm1
jne .display ; if both are not equal numbers, xmm1 is a FP
fild dword [a] ; but if them are equals means that xmm1 is an integer
; so we'll need to convert 'a' to FP
; and push the st(0) stack with its value.
.display:
fstp qword [op1] ; pop sr(0) stack to 'op1'
mov rdi, fmt ; loading format 'fmt' to display
movsd xmm0, qword [op1] ; loading 'op1' like the argument to display
mov rax, 1 ; tell 1 argument to display only
call printf ; let's go to display
pop rbp ; pop the stack
.exit: mov rax, 0 ; exit (0 = normal)
ret ; return to system


I made a program to convert any integer double number into a floating point one, so that --in a later program- it will be use as an FP operand into an arithmetic operation with double FP.
f.i.:
int a = 28 ---> conversion to FP double --> 28.00
double b= 12.50
double c= a + b = 40.50
In this point I think I've converted the integer number into a double one, but I gets the next ask:
Quote:
Result is: 1.38338e-322 |
The code of the program is this:
Code:--------------------------------------------------------------
; calculam.asm
; This program will check if a number is an INT or a FP:
; if it's an INT the program will convert it to FP
; if it's a FP the program won't nothing
; Finally the program will diplay the final FP number got.
; -------------------------------------------------------------
; Assembly for GNU/Linux x86_64, NASM compiler & GCC for link
; -------------------------------------------------------------
; 1) Compile: nasm -f elf64 calculam.asm
; 2) Link: gcc -no-pie calculam.o -o calculam
; 3) Run:./calculam
; ------------------------------------------------------------
bits 64 ; Only for x86_64 engines !
extern printf ; invoking the printf command
section .data
a: dq 28 ; We will test 'a'
fmt: db "Result is: %g", 10, 0 ; format of message to print
section .bss
op1: resq 1 ; let's reserve 1 qword (64 bits)
section .text
global main
main:
push rbp ; setting up the stack
.rounding:
movsd xmm1, qword [a] ; move the value of 'a' into xmm1
fld qword [a] ; push xmm1 into st(0) stack
roundsd xmm2, xmm1, 3 ; xmm2 will be the truncated number of xmm1
.comparision:
ucomisd xmm2, xmm1 ; comparision between xmm2 & xmm1
jne .display ; if both are not equal numbers, xmm1 is a FP
fild dword [a] ; but if them are equals means that xmm1 is an integer
; so we'll need to convert 'a' to FP
; and push the st(0) stack with its value.
.display:
fstp qword [op1] ; pop sr(0) stack to 'op1'
mov rdi, fmt ; loading format 'fmt' to display
movsd xmm0, qword [op1] ; loading 'op1' like the argument to display
mov rax, 1 ; tell 1 argument to display only
call printf ; let's go to display
pop rbp ; pop the stack
.exit: mov rax, 0 ; exit (0 = normal)
ret ; return to system