2

I am comparing some input received from the user with some numbers, and the same jump is taken regardless of the compare.

User input is defined as so:

.data?
    userInput REAL4 ?

And when I compare it I use:

cmp userInput, 18200
jge _tb0    ;This jump is always taken

However I have tested with userInput being less than, equal, and great than 18200 but it still jumps to the same label.

I have just started learning assembly from this tutorial, which has been great so far, but doesn't have a lot of information and I am having trouble finding a good, detailed tutorial online so that I can learn what I am doing wrong.

UPDATE:

After reading a bit on using the FPU to do floating point math, I came up with the following code for variables:

.data?
    userInput real4 ?

.const
    tb0_max real4 18200.00

and the following code for comparing:

fld userInput
fcom tb0_max
jge _tb0

However I still get the same result as before, I jump to _tb0 regardless of the value given as user input

UPDATE 2:

After reading this on comparing with the FPU, I came across the following code which should, according to the article, allow me to move the compare states from the FPU to the CPU so that I can use a regular CPU compare instruction:

fld userInput
fcom tb0_max
fstsw ax
fwait
sahf
jge _tb0

However I am still having the same issue, the compare always result as the userInput being less than the constant 18200.00 regardless of userInput

Shadow
  • 3,926
  • 5
  • 20
  • 41
  • You might want to see this [SO answer](http://stackoverflow.com/a/7057771/3857942) about floating point compare . – Michael Petch Nov 17 '15 at 16:21
  • Look at the links at http://stackoverflow.com/tags/x86/info for good reference material. If you think the tutorial you found is worth adding to the tag wiki, go for it. It's pretty light on good links for beginners. – Peter Cordes Nov 17 '15 at 23:58

2 Answers2

2

cmp itself works correctly. But you aren't using it correctly. REAL4 is a floating point type. cmp works with integer operands only. Either use integers or learn how to compare floating point values.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • I looked around on the internet and learnt a bit on comparing floating point values, but I still get the same result. Can you look at my update? – Shadow Nov 20 '15 at 09:23
  • @Shadow Look closely at what flags `fcom` affects and what flags `jge` inspects. Are those the same flags, in the same register? Also look at `fcomi` in case your CPU supports it. – Alexey Frunze Nov 20 '15 at 10:07
  • I read up on moving the FPU states to the CPU so that I can do a regular compare, but it still has the same result. Could you take a look at my second update? I included a link to the article I am using. – Shadow Nov 27 '15 at 03:12
  • @Shadow jge jumps if (SF XOR OF) = 0. Why are you using it and ignoring what the article says about the comparison result appearing in ZF/PF/CF? – Alexey Frunze Nov 28 '15 at 06:22
1

Since you want to compare your single precission float (userInput) with an integer (18200) you could just temporarily store it as an integer and then perform the comparison:

.data?
userInput   REAL4 ?
tempInteger dd ?

...

fld   userInput
fistp tempInteger
cmp   tempInteger, 18200
jge   _tb0
Fifoernik
  • 9,779
  • 1
  • 21
  • 27