Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Some floating point issues

my first time messing with floating point numbers in assembly
the following code works fine with the initialized values so
i'm relatively certain my floating point operations are correct

the problem is when i try to grab user input, i get some weird results
as i'm new to assembly i'm having a difficult time figuring out why.

hoping someone can let me know what my problem is in the following code

[code]
; Author: Dustin K. Scott
;Class: SCSC 210
;Source name :hwk7.asm
;Executables name :hwk7
;
;Build using following command:
;nasm -f elf hwk7.asm
;gcc hwk7.o -o hwk7

%define ARRAY_SIZE 5

[SECTION .text]

extern scanf
extern printf
global main

%define total 16
%define product 24
%define difference 32

[SECTION .bss]
struc floatStruc
array resq ARRAY_SIZE
endstruc


[SECTION .data]

authorName db 'Dustin K. Scott',10,0 ;defines a C style string or array of chars for the autor's name
; Dustin K. Scott

numberPrompt1 db 'Please enter a number: ',0

numberPrompt2 db 'Please enter a second number: ',0

inputFormat db "%f",0

totalIs db 'The total is: %g',10,0

productIs db 'The product is: %g',10,0

differenceIs db 'difference is: %g',10,0

goodbye db 'Thank you for using the program, good-bye!',10,0

myStruc:
istruc floatStruc
at array, dq 1.5
at array+8, dq 2.5
iend

echoFormat db "%e",10,0
main:
push ebp
mov ebp, esp ; building stack for program
push eax
push ebx
push ecx
push edx
push esi
push edi
;;;Begginning of code

;1. Display author's name
push authorName
call printf
add esp, 4

;2. prompt user to enter a number and grab input
push dword numberPrompt1
call printf
add esp, 4


lea eax, [myStruc+array]
push eax
push inputFormat
call scanf
add esp, 8
;echo
push dword [myStruc+array+4]
push dword [myStruc+array]
push dword echoFormat
call printf
add esp, 12

;3. prompt user to enter a second number a grab input
;push dword numberPrompt2
;call printf
;add esp, 4

;lea eax, [myStruc+array+8]
;push dword eax
;push inputFormat
;call scanf
;add esp, 8

fld qword [myStruc + array]
fld qword [myStruc + array + 8]

;4. print total of the two numbers to user
faddp st1
fstp qword [myStruc+array+total]

push dword [myStruc+array+total+4]
push dword [myStruc+array+total]
push dword totalIs
call printf
add esp, 12

;5. print product of the two numbers to user
ffree st0
fld qword [myStruc+array]
fld qword [myStruc+array+8]
fmulp st1
fstp qword [myStruc+array+product]

push dword [myStruc+array+product+4]
push dword [myStruc+array+product]
push dword productIs
call printf
add esp, 12


;6. print difference of thw two numbers to user
ffree st0
fld qword [myStruc+array]
fld qword [myStruc+array+8]

fsubp st1
fstp qword [myStruc+array+difference]

push dword [myStruc+array+difference+4]
push dword [myStruc+array+difference]
push dword differenceIs
call printf
add esp, 12

;7. print goodbye message and end program
push dword goodbye
call printf
add esp, 4

;;;End of code
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
mov esp, ebp ;destroying stack
pop ebp

ret ;Return control to linux
[/code]

Comments

  • fangxifangxi Posts: 13Member
    i solved it after talking with a fellow programmer in my class
    the problem was that my inputFormat should be [code] inputFormat db "%lf",0[/code] to specify a long float as input(thats %LF, bad font made me believe it was %oneF), i have no idea why %f gives such weird error i'd have to do some research to find out why, but this is in case anyone else has this problem and needs an answer
Sign In or Register to comment.