100% found this document useful (1 vote)
207 views6 pages

Assembly Language Loop Instructions

The document discusses loop instructions in assembly language. It provides examples of using loop instructions to repeat blocks of code a specific number of times by decrementing the ECX register on each iteration. It also discusses nested loops, and how to save the outer loop counter value to use the ECX register for the inner loop. The lab work includes exercises to calculate the Fibonacci sequence using a loop, and to print numbers in sequences multiple times using nested loops. The homework assignments are to print letters of the alphabet using a loop, and modify the Fibonacci sequence exercise to use only additions and without PTR or OFFSET operators.

Uploaded by

Ali Muhammad
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
100% found this document useful (1 vote)
207 views6 pages

Assembly Language Loop Instructions

The document discusses loop instructions in assembly language. It provides examples of using loop instructions to repeat blocks of code a specific number of times by decrementing the ECX register on each iteration. It also discusses nested loops, and how to save the outer loop counter value to use the ECX register for the inner loop. The lab work includes exercises to calculate the Fibonacci sequence using a loop, and to print numbers in sequences multiple times using nested loops. The homework assignments are to print letters of the alphabet using a loop, and modify the Fibonacci sequence exercise to use only additions and without PTR or OFFSET operators.

Uploaded by

Ali Muhammad
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

Faculty of Engineering

Computer Engineering Department


Islamic University of Gaza

Assembly Language Lab # 5


Loop Instruction

Eng. [Link]
Assembly Language Fundamentals

Objective:
To know more about Assembly language, such as how to repeat a block of statements using
Loop Instructions.

 Loop Instruction
The Loop instruction provides a simple way to repeat a block of statements a specific number of
times. ECX is automatically used as a counter and is decremented each time the loop repeats.

Syntax:
Loop target

The execution of the Loop instruction involves two steps:


1. First, it subtracts 1 from ECX.
2. Next, it compares ECX to zero.
 If ECX is not equal to zero; a jump is taken to the label identified by destination.
 Otherwise, if ECX equals zero, no jump takes place and control passes to the instruction
following the loop.

Nested Loop:
Loop Instruction

If you need to code a loop within a loop, you must save the outer loop counter's ECX value.

.data
count dd ?
.code
mov ecx,value ; set outer loop count
L1:
mov count,ecx ; save outer loop count
Assembly Language Lab # 5

mov ecx,New_value ; set inner loop count


L2:
(inner loop code)
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1 ; repeat the outer loop

1
OFFSET Operator

The OFFSET operator returns the offset of a data label. The offset represents the distance, in
bytes, of the label from the beginning of the data segment. To illustrate, the following figure
shows a variable named myByte inside the data segment.

For example:
.DATA
bVal BYTE ? ; Assume bVal is at 00404000h
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.CODE
mov esi, OFFSET bVal ; ESI = 00404000h
mov esi, OFFSET wVal ; ESI = 00404001h
mov esi, OFFSET dVal ; ESI = 00404003h
mov esi, OFFSET dVal2 ; ESI = 00404007h

PTR Operator
You can use the PTR operator to override the declared size of an operand. This is only necessary

Loop Instruction | 2/20/2013


when you’re trying to access the variable using a size attribute that’s different from the one used to
declare the variable.

Suppose, for example, that you would like to move the lower 16 bits of a doubleword variable
named myDouble into AX. The assembler will not permit the following move because the operand
sizes do not match. But the WORD PTR operator makes it possible to move the low-order word
(5678h) to AX:
.data
myDouble DWORD 12345678h
.code
mov ax,myDouble ; error
mov ax,WORD PTR myDouble ;ax=5678h
Assembly Language Lab # 5

mov ax,WORD PTR [myDouble+2] ; 1234h

2
Lab work:
Excercise1:
Write a program that uses a loop to calculate the first seven values in the Fibonacci number
sequence { 1,1,2,3,5,8,13 } where The Rule is Fn = Fn-1 + Fn-2. The Fibonacci sequence is referenced
in the memory by the byte memory array called Fibonacci save the remaining five elements in the
same array.
Fabonacci db 1h,1h,5 dup(?)
Loop Instruction
Assembly Language Lab # 5

3
Excercise2:
Write an assembly code that prints the numbers from 1 to 5, 5 times on the screen. Each sequence
of numbers from 1 to 5 is separated by new line.
Ex:
12345
12345
12345
12345
12345

Loop Instruction | 2/20/2013


Assembly Language Lab # 5

4
Homework:
Write an assembly language program using the Loop instruction to print all letters as
follows :
A
B
.
.
.
Y
Z

2. Write an assembly code that prints the following on the console using nested loop.
1234567
123456
12345
1234
123
12
Loop Instruction

[Link] Excercise1 (Fibonacci number sequence) with just 2 add instructions and without
PTR Operator.

4. Rewrite Excercise1 (Fibonacci number sequence) with just 2 add instructions and without
Assembly Language Lab # 5

PTR and OFFSET Operators.

Common questions

Powered by AI

The OFFSET operator in Assembly Language provides the offset of a data label, representing the distance of the label from the beginning of the data segment in bytes. This is useful for managing data positions because it helps calculate the exact memory location of variables relative to the segment start, facilitating memory access and pointer arithmetic without hardcoding memory addresses .

To structure an Assembly program to print "1 2 3 4 5, five times" using nested loops, an outer loop is initialized to run five times with an inner loop, printing numbers 1 to 5 sequentially. The outer loop count decrements each time an iteration completes, and a newline is added after each inner execution to separate the sequences. This nested structure simplifies the control flow of repeating sequences, minimizes code repetition, and leverages loop instructions for concise iteration management .

In nested loops, saving and restoring the ECX register value is crucial because both the outer and inner loops use ECX as their counter. Before entering an inner loop, the current ECX value of the outer loop (the loop count) is saved into a variable. After the inner loop is executed, the saved outer ECX value is restored before continuing with the outer loop. This prevents the outer loop count from being altered unexpectedly by the inner loop operations, ensuring logical flow and accurate loop execution .

The PTR operator is necessary in Assembly Language when there is a need to override the declared size of an operand. It solves the problem of size mismatch issues between instruction operands and the data. For example, if you want to access a part of a variable that is not naturally aligned with the variable's declared size, the PTR operator allows you to specify a different size, enabling proper data access and manipulation, such as moving lower bits of a DWORD into a 16-bit register .

Using OFFSET in Assembly Language allows direct access to the memory address of a variable, making it easier to perform pointer arithmetic or access memory-relative data without hardcoding addresses. Not using OFFSET means calculations or explicit knowledge of segment addresses is necessary for accessing values, increasing the complexity and potential for errors in addressing logic. OFFSET improves readability and maintainability by abstracting address calculations .

Rewriting the Fibonacci calculation with two add instructions challenges include maintaining the sequence integrity without intermediate storage or complex logic to track the last two sums accurately. This can be addressed by efficiently managing registers to hold temporary values and cleverly structuring loop control to minimize redundancy. This requires a deep understanding of register manipulation and effective use of available instructions to consolidate operations .

The loop instruction in Assembly Language is used to repeat a block of statements a specific number of times. It utilizes the ECX register as a counter. In each iteration, the loop instruction subtracts 1 from ECX. If ECX is not zero, the loop takes a jump to the target label, continuing the loop. When ECX reaches zero, the loop exits, and execution continues with the next instruction following the loop .

In Assembly Language, the Fibonacci sequence can be calculated using loop instructions by sequentially adding two preceding numbers to derive the next number in the sequence. The ECX register can serve as a counter for iterations, while an array in memory holds the sequence values. In-memory referencing through a byte memory array allows efficient storage and retrieval of sequence elements. The initial values are stored in the array, and subsequent elements are calculated within a loop and saved, utilizing the ECX to manage iteration count .

Storing and iterating over the Fibonacci series in memory demonstrates critical Assembly Language concepts by requiring efficient data handling and loop utilization. The sequence is stored in a memory array, each value is calculated and stored sequentially. Control over loop iteration and memory access through registers showcases precise data manipulation, highlighting memory management and the utility of loops for repetitive data-dependent tasks, exemplifying core assembly operations .

The LOOP instruction simplifies code by automatically handling the decrement of ECX and conditional continuation of loops, reducing error potential and improving readability. However, it relies on ECX for counting, limiting flexibility with other counters. Manual loops using conditional jumps offer greater flexibility, such as handling multiple conditions and using other registers as counters. The drawback is increased code complexity and potential for errors, requiring meticulous tracking of conditions and counters .

Faculty of Engineering 
Computer Engineering Department 
 Islamic University of Gaza 
 
 
      
 
             Eng. Ala
1 
Assembly Language Lab # 5                  Loop Instruction  
 
Assembly Language Fundamentals 
 
Objective: 
To know
2 
Assembly Language Lab # 5                  Loop Instruction |  2/20/2013 
OFFSET Operator 
  
The OFFSET operator return
3 
Assembly Language Lab # 5                  Loop Instruction  
 
Lab work:  
Excercise1:  
Write a program that uses a
4 
Assembly Language Lab # 5                  Loop Instruction |  2/20/2013 
Excercise2:  
Write an assembly code that prin
5 
Assembly Language Lab # 5                  Loop Instruction  
 
 
 
 
Homework:  
Write an assembly language program u

You might also like