How to Check if a Register is Equal or Not to a Specific Value in x86 Assembly Language

x86 Assembly programming language


In this article, we show how to check if a register is equal to or not equal to a specific value in x86 assembly language.

We can do this through conditional statements that function as if statements.

x86 assembly does not have an if statement.

However, it does have an equivalent of an if statement to produce the same type of logic and output, as in a conventional high-level language.

How it works in assembly is through flags.

First, we use the cmp operation.

For example, we have the statement, cmp al, 5

With this statement, we check to see if the al register is equal to 5.

If it is, the zero flag (ZF) turns to 1.

This is the equivalent of an if statement evaluating to true, if (al == 5)

Next, we use the je operation, which stands for jump if equal.

This je operation will run if the zero flag is equal to 1, which occurs when the cmp operation evaluates to true.

Below we will have code that



So let's go through the code now.

So we move the value 5 into the al register.

We then use the cmp operation to check if value in the al register is equal to 5.

Since the value in the al register is equal to 5, the ZF=1 and the JE operation jmps to label 1, where it outputs the character 'E' to the emulator output screen.

If you set al equal to a value other than 5, then the cmp operation would produce a ZF equal to 0, which means the condition is not true. It would not jump to the label1 label and 'N' would be output to the emulator output screen.



So now that you know that how to write code for a conditional statement to check if it is equal to a certain value, now let's write code for a conditional statement to see if it is not equal to a certain value.

We use the same cmp operation.

But instead of using the JE operation, which stands for jump if equal, we use the JNE operation, which stands for jump if not equal.

This is the equal to having an if statement with a not equals operation, such as, if (al != 5)

The following code below will print 'N' if



So you can try running the following code above.

The code, as stated above, will print 'N' to the emulator output screen, since 6 and 5 are not equal.

If you change al to a value of 5, then the program will output 'E' to the emulator screen, signaling that the values are equal.

So this is how we can check if a register is equal or not to a specific value in x86 assembly language.

Related Resources



HTML Comment Box is loading comments...