Is there "If"s in ASM?

Thread Starter

sharkfire

Joined Feb 13, 2008
23
I just want to ask if there is a code for Assembly language, which you can set conditions like IF, which is used in C programming and other high level languages.
 

Mark44

Joined Nov 26, 2007
628
The variant I'm most familiar with is x86 assembly, so I'll answer relative to that. There is no IF op code, per se, but there are a variety of jump instructions (e.g., JMP, JA, JB, JE, JNE, and quite a few others). The simplest is JMP, which is the same as goto in C.

An example would be helpful. Suppose you had the following C code:

if (var == 2)
/* do one thing */
else
/* do some other thing

This would be written in x86 assembly code something like this, assuming that the value of var in the code above has been moved into the AX register:

cmp ax, 2
jne Label1
;; code to do one thing
jmp Label2
Label1:
;; code to do some other thing
Label2:


The first line of the assembly code compares AX with 2 by subtracting 2 from AX. If the result is zero (the two values are equal), the zero flag of the FLAGS register gets set.
The next line jumps if not zero (JNZ) to the location labelled Label1. You might ask, "jumps if what is not zero?" That's where the zero flag (ZF) comes in. The jump occurs if ZF is not set; i.e., ZF is zero. In that case, the instruction at the label is executed. On the other hand, if ZF is set (ZF = 1), the jump doesn't occur, and the next instruction is executed.

I have another jump instruction following this, because I want to have one or the other, but not both, statements execute.

I've seen a bit of Motorola 68000 assembly, and long ago, some 6502 assembly, and neither of these had any instructions that correspond directly with C's if statement. As far as I know, there aren't any kinds of assembly language that have direct counterparts to C's if statement. However, there was one computer produced back in the 80s, I believe--Lilith--that could be programmed natively in Modula-2, the language created by Niklaus Wirth, who also created Pascal. The instruction set for the Lilith consisted exactly of the control structures in Modula-2, which means that that type of computer did have an IF instruction. Or so I remember reading.
Mark
 

n9352527

Joined Oct 14, 2005
1,198
In PIC you have btfss (bit test skip if set), btfsc (bit test skip if clear), decfsz (decrement a register skip if zero). You could apply the bit test to either zero flag (Z), carry flag (C), digit carry flag (DC) or any bit in a register. Coupled with other instructions, such as xor, ior, and, add, sub, etc., you could do pretty much anything you need.
 
Please note any Hi Level Language Instruction has to have an equivalent set of instructions. The conversion from Hi Level to Lo level assembly lang. is done by an online
interpreter ( as in some Basic uC Devices , etc ) or a compiler. The equiv. set may or may not be a single instruc. in asmb. lang. depending on the Complexity of the Hi level insr. & its equiv. for any given uP or uC device. Also the set of asmb. instr. will vary from processor to processor as per its native asmb. lang. set.

For your particular query for IF , the equiv. will have to take into account the range of value you want to include for the IF condition. Do a comparison ( Logical or Arith ) with the Input & your assigned value. The comparison will result in setting of Carry / sign / Overflo or other appropriate flags. Examine which combination suits your condition of IF and either get a logical result of the said condition being satisfied or not . Then proceed accordingly.
 

Dave

Joined Nov 17, 2003
6,969
Analogous to the JUMP instructions mentioned above, some processors use BRANCH as well (e.g. the MIPS processor) which has many of the same features. Typically the branch on the MIPS is a comparative branch:

Rich (BB code):
BEZ $0, LABEL
Is "Branch to LABEL if $0 is equal to zero", which would be analogous to:

Rich (BB code):
if $0 == 0 then
Branch to LABEL​
end
Dave
 

mik3ca

Joined Feb 11, 2007
189
CMP is compare in assembly and is the best equivalent to IF.

So if you use "if ax = 0 then do this", then in assembly, you do:

cmp ax,0
jne skip
do this
skip:

in asssembly, you compare the two values. If they are not equal, you jump to a label which means the "do this" code (in the above example) will be skipped.
 

hgmjr

Joined Jan 28, 2005
9,027
That is a valid question, papabravo. Unfortunately the original post is not clear on whether the question is on the "IF" construct in the sense of condition execution or in the sense of conditional compilation.

Hopefully, the OP will return soon and clarify his question.

hgmjr
 
Top