简体   繁体   English

如何在汇编中编写if-else?

[英]How to write if-else in assembly?

How do you write the if else statement below in assembly languange?下面的if else语句用汇编语言怎么写?

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)不完整的组装:(MC-微控制器)

CMP Input, WaterLevel
MOV word[MC], 1

MOV word[MC], 2

If we want to do something in C like:如果我们想在 C 中做一些事情,比如:

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

it would look in Assembly like this:它在Assembly中看起来像这样:

 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.对于前三个命令:大多数汇编语言都有条件分支命令来测试零或符号位的值,并根据该位是否设置进行跳转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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