Home > OS >  What is IF ELSE and ENDIF Directives in Assembly language? I am trying to make a program in which so
What is IF ELSE and ENDIF Directives in Assembly language? I am trying to make a program in which so

Time:03-07

Dosseg
.model small
.stack 100h
.data

X db 89
z db ?

msg1 db "heloo$"


.code
main proc

mov ax,@data
mov ds,ax

mov z,offset x
;X=89
Y=-3
IF (z LT 0) OR (z GT 79)
lea dx,msg1
mov ah,9
int 21h
ENDIF

IF (Y LT 0)
lea dx,msg1
mov ah,9
int 21h
ENDIF 
   
mov ah,4ch
int 21h

MAIN ENDP
END MAIN

CodePudding user response:

As shown in the question, if else endif are assembly time conditionals that either include or exclude lines of assembly code. In Microsoft Masm (6.11 or later), .if .else .endif are run time conditionals. Link to Masm dot directives:

https://docs.microsoft.com/en-us/cpp/assembler/masm/directives-reference?view=msvc-170

CodePudding user response:

In structured programming we have the if-then statement, which has a pattern like this:

if ( condition ) 
    then-part

In assembly language's if-goto-label style (while still in C) the same pattern is like this:

    if ( ! condition ) goto endIf1;
    then-part
endIf1:

In the if-goto-label style, we tell the program when to skip the then-part, as compared with C where we tell the program when to execute the then-part.  Thus, the condition for if-goto-label needs to be negated.

The construct if ( condition ) goto endIf1; is C's version of a conditional branch.  In assembly language that conditional branch is usually done as a compare & branch sequence.  For example:

if ( Y < 0 ) 
    print "hello"

becomes:

    if ( Y >= 0 ) goto endIf1;
    print "hello"
endIf1:

which becomes:

    cmp Y, 0
    jge endIf1
    lea dx, msg1
    mov ah, 9
    int 21h
endIf1:
  • Related