GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, December 21, 2010

Assembly CMP and JNZ

This is the second type of use CMP with comparison. JNZ is a command that tell the program to jump is comparison result is not equal. If equal, please don't jump.

start:
    MOV AH,08    ; Function to read char from keyboard
    INT 21h      ; and save it to AL

    CMP AL,65    ; Compare what valu in AL to "A"
    JNZ output   ; if not same (not zero), please jump and 
                 ; skip middle. If zero, please run code normally

middle:
    MOV AH,02    ; Function to output char
    MOV DL,"R"   ; Char to output
    INT 21h
output:
    MOV AH,02    ; Function to output char
    MOV DL,"T"   ; Char to output
    INT 21h
exit:
   MOV AH,4Ch    ; Exit function
   MOV AL,00     ; Return 0
   INT 21h
 
If you type "A", it means that the comparison is same, and the command is JUMP IF NOT EQUAL, so the result is that the code will executed normally:
output> RT
But if you type other than "A", it means that the comparison result is not equal to zero (not same), and the command is JUMP IF NOT EQUAL, so the result is that the program will jump to output and skip middle label:
output> T
Share/Bookmark

2 comments: