0% found this document useful (0 votes)
159 views12 pages

17 Microprocessor Systems Lecture No 17 JMP and LOOP Instructions PDF

The document discusses JMP and LOOP instructions in microprocessor-based systems. It explains that JMP unconditionally jumps to a label, while LOOP decrements ECX and jumps to a destination label if ECX is not equal to 0, creating a counting loop. An example uses LOOP to calculate the sum of integers in an array. Nested loops require saving the outer loop counter, as demonstrated in sample code provided.

Uploaded by

Muhammad Zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views12 pages

17 Microprocessor Systems Lecture No 17 JMP and LOOP Instructions PDF

The document discusses JMP and LOOP instructions in microprocessor-based systems. It explains that JMP unconditionally jumps to a label, while LOOP decrements ECX and jumps to a destination label if ECX is not equal to 0, creating a counting loop. An example uses LOOP to calculate the sum of integers in an array. Nested loops require saving the outer loop counter, as demonstrated in sample code provided.

Uploaded by

Muhammad Zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Microprocessor Based

Systems
Lecture No 17 JMP and LOOP
Instructions

By Nasir Mahmood
This Lecture
JMP and LOOP Instructions

  JMP Instruction
  LOOP Instruction
  LOOP Example
  Summing an Integer Array
  Copying a String

  Book Reading “Assembly Language for x86 Processors” 6th Edition


By Kip R. Irvine
  Section 4.5
JMP Instruction
•  JMP is an unconditional jump to a label that is usually within
the same procedure.
•  Syntax: JMP destination
•  Logic: EIP ← destination
•  Example:
top:
.
.
jmp top

A jump outside the current procedure must be to a special type of label called
a global label

3
LOOP Instruction
•  The LOOP instruction creates a counting loop
•  Syntax: LOOP destination
•  Logic:
•  ECX ← ECX – 1
•  if ECX != 0, jump to destination
•  Implementation:
•  The assembler calculates the distance, in bytes, between
the offset of the following instruction and the offset of the
target label. It is called the relative offset.
•  The relative offset is added to EIP.

4
LOOP Example
The following loop calculates the sum of the integers
5 + 4 + 3 +2 + 1:
offset machine code source code
00000000 66 B8 0000 mov ax,0
00000004 B9 00000005 mov ecx,5

00000009 66 03 C1 L1: add ax,cx


0000000C E2 FB loop L1
0000000E

When LOOP is assembled, the current location = 0000000E (offset of the next
instruction). –5 (FBh) is added to the the current location, causing a jump
to location 00000009:
00000009 ← 0000000E + FB

5
Your turn . . .
If the relative offset is encoded in a single signed byte,
(a) what is the largest possible backward jump?
(b) what is the largest possible forward jump?

(a)  -128
(b)  +127

6
Your turn . . .
mov ax,6
mov ecx,4
What will be the final value of AX? L1:
inc ax
10 loop L1

mov ecx,0
How many times will the loop X2:
execute? inc ax
4,294,967,296= loop X2
2^32

7
Nested Loop
If you need to code a loop within a loop, you must save the
outer loop counter's ECX value. In the following example,
the outer loop executes 100 times, and the inner loop 20
times.
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2: .
.
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1 ; repeat the outer loop

8
Summing an Integer Array
The following code calculates the sum of an array of 16-bit
integers.
.data
intarray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[edi] ; add an integer
add edi,TYPE intarray ; point to next integer
loop L1 ; repeat until ECX = 0

9
Your turn . . .

What changes would you make to the


program on the previous slide if you
were summing a doubleword array?

10
Copying a String
The following code copies a string from source to target:

.data
source BYTE "This is the source string",0 good use of
target BYTE SIZEOF source DUP(0) SIZEOF

.code
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
L1:
mov al,source[esi] ; get char from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1 ; repeat for entire string

11
THE END

You might also like