14

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
    With conditional branches... Nov 15, 2016 at 4:30
  • Yes rest assured i just started learning this today.. I am a student.. Nov 15, 2016 at 13:08
  • 1
    @Ped7g Coming from an old man with 30 years of coding experience, i think you should have more respect for youngster trying to learn. I am here on stack to learn, eventhough i dont even a grasp of the basics yet. If i had 30 years of experience like you old man i wouldnt be on here would I? Be more respectful. And if it doesnt answer the question, go away. Nobody needs you around. Nov 15, 2016 at 13:39
  • I give zero respect to bad code. Even if it's my own. If you are student, I have respect to you as a person and to your effort to learn. Not to that particular else branch, which is fundamentally wrong. As a student you should rather try to figure out why, or ask for reason, than asking for respect - that will do little good to you, even if I would comply. If you are asking me to have empathy with your view, try also mine. I'm much more worried about such code slipping into real product, than I'm worried about being nice. Being rude does hurt people less than a water flood.
    – Ped7g
    Nov 15, 2016 at 13:49
  • @Ped7g Its my first day of learning assembly language and your worried that such a code will slip into a real product. At least I have now understand the statements with the answers provided to me by other users. All Im saying is, what are you doing here if you arent here to answer my question? What help could i possibly get from your 'concern' of bad codes. Nov 15, 2016 at 13:54

3 Answers 3

21

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
  • 1
    How to write the equal condition (in the question) in assembly? Your example has an else statement while mine uses an else if. Nov 15, 2016 at 4:57
  • je equal means jump to equal, if equal Nov 15, 2016 at 5:00
  • 3
    There's not only one assembly language. Assembly for ARM obviously must be different from Sparc, MIPS or x86. They all have different instructions for comparison. You must specify the architecture. About else if, the latter if is just another block of code in else.
    – phuclv
    Nov 15, 2016 at 5:11
  • It's x86 Assembly Architecture Nov 15, 2016 at 5:12
  • 1
    CMP Input, WaterLevel JE Equal MOV word[MC], 1 JMP Both Equal: MOV word[MC], 0 Both: How would i have tested for the (input<water level) condition if i used this. Nov 15, 2016 at 5:34
9

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.

0

To use if statement in NASM assembly first line should write:

comp eax, ebx

In this line NASM understands that it should compare two registers. Now u should specify how NASM assembly should compare them. Lets compare for example if greater or equal:

main:
    comp eax, ebx
    jge greater_or_equal
greater_or_equal:
    ; your code if greater or equal

Please check the link for other jumps.

https://www.philadelphia.edu.jo/academics/qhamarsheh/uploads/Lecture%2018%20Conditional%20Jumps%20Instructions.pdf

Not the answer you're looking for? Browse other questions tagged or ask your own question.