0% found this document useful (0 votes)
77 views

Practical No.11:: Become Familiar With Looping in Assembly Programming

The document discusses assembly language programming concepts like loop instructions, nested loops, the OFFSET and PTR operators. It provides examples of using loops to calculate Fibonacci numbers and print sequences, and exercises for students to write assembly code to generate Fibonacci sequences, print nested loops, and print letters of the alphabet using loops. The objective is for students to become familiar with looping constructs in assembly programming.

Uploaded by

Vishal Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Practical No.11:: Become Familiar With Looping in Assembly Programming

The document discusses assembly language programming concepts like loop instructions, nested loops, the OFFSET and PTR operators. It provides examples of using loops to calculate Fibonacci numbers and print sequences, and exercises for students to write assembly code to generate Fibonacci sequences, print nested loops, and print letters of the alphabet using loops. The objective is for students to become familiar with looping constructs in assembly programming.

Uploaded by

Vishal Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of: Subject of:

Computer Systems Engineering Computer Architecture and Assembly


Programming
Mehran University of Engineering Year 2nd Semester 3rd
&Technology
Batch 19CS Duration 03
Hours
Jamshoro

Practical no.11

Objective: Become familiar with looping in assembly programming


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.
Assembly L

 Otherwise, if ECX equals zero, no jump takes place and control passes to the instruction
following the loop.

Nested Loop:
If you need to code a loop within a loop, you must save the outer loop counter's ECX value.
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:

TE ? wVal WORD ? dVal DWORD ? dVal2 DWORD


; Assume bVal?is at 00404000h

SI = 00404000h
SI = 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
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
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(?)
Assembly Language Lab # 5
Loop Instruction

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

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
1

3.Rewrite 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 PTR and OFFSET Operators.

You might also like