Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write if-else in assembly?

How do you write the if else statement below in assembly languange?

C Code:

If ( input < WaterLevel)
{
     MC = 1;
}
else if ( input == WaterLevel)
{
     MC = 0;
}

Pseudocode

If input < Water Level
Send  1 to microcontroller
Turn Motor On


Else if input == Water Level
Send 0 to microcontroller
Turn Motor Off

Incomplete Assembly: (MC- Microcontroller)

CMP Input, WaterLevel
MOV word[MC], 1

MOV word[MC], 2
like image 627
Ern Tasha Avatar asked Nov 15 '16 04:11

Ern Tasha


People also ask

Can we use if else in assembly language?

Consider the two examples presented above, they could be written in assembly language with the following code: // IF.. THEN..ELSE form: mov( i, eax ); test( eax, eax ); // Check for zero.

What is the syntax of if else if?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

What is conditional statement in assembly language?

It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making.

What does JNE mean in assembly?

The jnz (or jne) instruction is a conditional jump that follows a test. It jumps to the specified location if the Zero Flag (ZF) is cleared (0). jnz is commonly used to explicitly test for something not being equal to zero whereas jne is commonly found after a cmp instruction.


1 Answers

If we want to do something in C like:

if (ax < bx)
{
    X = -1;
}
else
{
    X = 1;
}

it would look in Assembly like this:

 cmp    ax, bx      
 jl     Less  
 mov    word [X], 1    
 jmp    Both            
Less: 
 mov    word [X], -1  
Both:
like image 128
Muhammad Faizan Uddin Avatar answered Sep 21 '22 21:09

Muhammad Faizan Uddin