简体   繁体   中英

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM