2

I am having two issues with my Assembly code. According to the guidelines, I must do all of this using floating point operation. With that said, it seems like I am not getting the correct answer from this, and I don't know what is wrong. The function is as follows: y=3x^3+2.7x^2-74x+6.3. I have to give X, and it is suppose to go into this function and output Y.

The code is also suppose to end when I type N, but it keeps giving me a floating point error.

EDIT: I figured out my problem with the function, however whenever I type N it doesn't jump down and end my code.

input       REAL8 ?         ; user input
result      REAL8 ?         ; result of calculation
three       REAL8 3.0       ; constant
twoSeven    REAL8 2.7       ; constant
seventyFour REAL8 74.0      ; constant
sixThree    REAL8 6.3       ; constant


prompt  BYTE "Enter an Integer or n to quit",0dh,0ah,0
again   BYTE "Would you like to go again? Y/N?", 0dh, 0ah, 0
no      BYTE "N",0dh,0ah,0
rprompt BYTE "The result of the calculation is ",0

.code
main PROC
result_loop:
finit                   ; initialize the floating point stack
mov edx,OFFSET prompt   ; address of message
call WriteString        ; write prompt
call ReadFloat          ; read the input value
fst input               ; save input of user


fmul three             ; multiplies by three
fadd twoSeven          ; Adds by 2.7
fmul input             ; multiplies by the input
fsub seventyFour       ; subtracts 74
fmul input             ; multiplies by input 
fadd sixThree          ; adds by three

fst result              ; puts value in area
mov edx,OFFSET rprompt  ; address of message
call WriteString        ; write prompt
call WriteFloat         ; writes the result
call CrLf               ; prints new line 

mov edx, OFFSET again
call WriteString
Call ReadString

cmp edx, 'n'            ; compares the input to n
je end_if               ; jumps if its equal to n
jmp result_loop         ; jumps back to the top

end_if:                 ; end statment 
call WaitMsg            ; 
exit                    ;
main ENDP
END main
Unleaver
  • 59
  • 6
  • `FCOMI input, 'n'` makes no sense whatsoever. You probably want to read a string, compare that to `"n"`, and if it isn't equal, convert it to float. – Jester Oct 19 '17 at 01:25
  • @jester I made a minor tweak to the code, adding input2, and trying to setup the compare between n and the input, however I am still coming up short. – Unleaver Oct 19 '17 at 01:55
  • `'n'` is not a number, you realize that, right? You can not read it as a float. – Jester Oct 19 '17 at 02:00
  • Are you really supposed to check for an `'n'` character with FP compares? If so, I guess you need the ASCII code of `'n'` as a floating-point constant. Also, you don't seem to `fstp` anywhere, so you'll eventually overflow the FP stack. I assume ReadFloat pushes the return value onto the FP stack. Use a debugger to see registers as you single-step your code. – Peter Cordes Oct 19 '17 at 02:45
  • 1
    With that change, are you sure `ReadString` returns a *string* by *value* in a 4-byte register fixed-width? (And are you sure it's `edx`? In most calling conventions, return values go in `eax`.) – Peter Cordes Oct 19 '17 at 07:31

1 Answers1

1
 Call ReadString
 cmp edx, 'n'            ; compares the input to n
 je end_if               ; jumps if its equal to n
 jmp result_loop         ; jumps back to the top
end_if:                 ; end statment 

ReadString does not work the way you think it works.

You need to pass it in EDX a pointer to a buffer that can store the input. You also need to tell in ECX how many characters you can allow this buffer to contain.

When ReadString returns, you'll get in EAX the number of characters that were effectively inputted.

So define a buffer and setup the parameters.

Then your code becomes:

 mov  edx, offset InBuffer
 mov  ecx, 1
 Call ReadString
 test eax, eax
 jz   result_loop     ; Jump back if no input given!
 mov  al, [edx]       ; Get the only character
 or   al, 32          ; Make LCase to accept 'n' as well as 'N'
 cmp  al, 'n'         ; compares the input to n
 jne  result_loop     ; jumps back if its not equal to n
end_if:               ; end statment 
Fifoernik
  • 9,779
  • 1
  • 21
  • 27