Practical No.11:: Become Familiar With Looping in Assembly Programming
Practical No.11:: Become Familiar With Looping in Assembly Programming
Practical no.11
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
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:
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.