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
7

2 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:
5

Not knowing the particular assembly language you are using, I'll write this out in pseudocode:

compare input to waterlevel
if less, jump to A
if equal, jump to B
jump to C
A:
send 1 to microcontroller
turn motor on
jump to C
B:
send 0 to microcontroller
turn motor off
C:
...

For the first three commands: most assembly languages have conditional branch commands to test the value of the zero or sign bit and jump or not according to whether the bit is set.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like