JMP and LOOP Instructions


JMP Instruction
The instruction causes an unconditional transfer, the offset of destination (from the start of the code segment) is moved into the instruction pointer, causing execution to continue at the new location.

LOOP Instruction
The instruction repeats a block of statements a specific number of times. The loop destination must be within –128 to +127 bytes of the current location counter. is automatically used as a counter and is decremented each time the loop repeats. The execution of the instruction involves two steps:
  1. It subtracts 1 from .
  2. It compares to zero. If , a jump is taken to the label identified by destination. Otherwise, if , no jump takes place and control passes to the instruction following the loop.
Compare and Jump Loop Double Loops
 INCLUDE Irvine32.inc
 .data
  ; 10 × 5 = 
  ;  10+10+10+10+10
  X = 10
  Y =  5
  dummy  WORD  ?
 .code
 main PROC
     mov   eax, 0
     mov   ecx, Y
 L1: cmp   ecx, 0
     je    L2
     add   eax, X
     dec   ecx
     jmp   L1
 L2: call  WriteInt
     exit
 main ENDP
 END main
 INCLUDE Irvine32.inc
  ; 10 × 5 =
  ;  10+10+10+10+10
  X = 10
  Y =  5
 .data
  dummy  WORD  ?
 .code
 main PROC
     mov   eax, 0
     mov   ecx, Y
 L1: add   eax, X
     loop  L1
     call  WriteInt
     exit
 main ENDP
 END main

 INCLUDE Irvine32.inc
  ; 10 × 3 × 3 =
  ;  (10+10+10) + (10+
  ;   10+10) + (10+10+10)
  X = 10
  Y =  3
  Z =  3
 .data
  save  DWORD  ?
 .code
 main PROC
     mov   eax, 0
     mov   ecx, Z
 L1: mov   save, ecx
     mov   ecx, Y
 L2: add   eax, X
     loop  L2
     mov   ecx, save
     loop  L1
     call  WriteInt
     exit
 main ENDP
 END main

Output Output Output

 

 

 

 

 

 






      “I’m not arguing, I’m just explaining why I’m right.”    
      ― Unknown