Jump to content

Making a Programming Language From Scratch/Comparing Two Values

From Wikibooks, open books for an open world

Comparisons

[edit | edit source]

Comparisons in programming are the logical comparison between two data types of the same type to determine which is larger, or which is smaller or if both are equal. Based on the result thus derived which is a Boolean value ( 1 or 0 ) certain statements are executed or not.

Whole number comparisons

[edit | edit source]

Whole numbers include chars and ints of all types. The basic format for all comparisons is the same.

CMP [ operand1 ] [ operand2 ] 

The conditional jumps based on this result are :

JL or jnge for lesser than
Jle or jng for lesser than equal to 
JG or Jnle for greater than
Jge or Jnl for greater than equal to
Je for equal to
Jne for not equal to

Floatational comparisons

[edit | edit source]

The syntax for floatational comparisons are more complex.

Fld [ operand1 ]
Fld [ operand2 ]
Fcompp
Fstsw ax
Sahf

Comparisons based on this are:

Jc for lesser than
Jce for lesser than equal to
Je for equal to
Jne for not equal to
Ja for greater than
Jae for greater than equal to


Clipboard

To do:
Add sample conversions


Logical appendations

[edit | edit source]

Logical appendations are when two or more conditions are logically connected such that the result of one influences the result of the other.

For example: If(a>b&&b>c){ This condition depends on both the logical relations of a,b and c. If any one of the double conditions are false the resultant output is false.

The counterpart the || operator is used for or relations, i.e if any one of the double conditions are true then the resultant is also true.

Note that these logical operators can be used for more than two conditions.