Jump, Loop and Call Instructions

After you have understood the tutorial on Introduction to assembly language which includes simple instruction sets like input/output operations,  now it’s time to learn how to create loops, function calls and jumps while writing a code in assembly language.

Let us first discuss an important concept that relates RAM & ROM. You can skip this section if you wish but it is an important to understand registers which helps in building up understanding of architecture of microcontrollers.

How a program which is burned in ROM gets executed in RAM?

The program you write is burned in ROM and this program is executed in RAM. The data burned is in the form of logics or binary digits (0 & 1) also called an Opcode or machine code. The program written in assembly language is converted to opcode by assembler.

Each line of the assembly code is assigned a unique opcode by the assembler as shown below. These opcodes are then stored in ROM one after another. There is a register called Program Counter (PC) which always points to the current opcode being executed. When the power is switched on it sets itself to zero and it keeps on incrementing itself as opcodes are executed one after another. This information is used by RAM to extract correct opcode from ROM which is then executed. This process goes on line by line till the whole program is executed.

 

How a program burned in ROM gets executed in RAM?

 

Let’s take an example:-

PC                                           Mnemonic, Operand          Opcode (Machine code)              

0000                                             ORG       0H                                                                          

0000                                             MOV      R0, #0                      7800

0002                                             MOV      A, #55H                                                                7455

0004                                             JZ           NEXT                        6003

0006                                             INC         R0                            08

0007             AGAIN:                     INC         A                              04

0008                                             INC         A                              04

0009             NEXT:                       ADD       A, #77H                    2477

000B                                             JNC         OVER                        5005

000D                                            CLR         A                               E4

000E                                             MOV       R0, A                         F8

000F                                             MOV       R1, A                         F9

0010                                             MOV       R2, A                         FA

0011                                             MOV       R3, A                         FB

0012            OVER:                       ADD,       R3                             2B

0013                                             JNC         AGAIN                       50F2

0015             HERE:                       SJMP        HERE                          80FE

0017                                             END

We can clearly see that the PC increases with execution of program. The PC starts with zero when ‘ORG 00H’ line is executed and then in subsequent lines PC keeps on incrementing  as machine codes are executed one after another.

Program Counter is a 2 byte or 16 bit register.  Therefore we cannot have internal ROM of more than the number this register can hold (i.e. not exceeding the FFFF hex value).

 

 

LOOP AND JUMP INSTRUCTIONS

Let us start with a simple example that will help you to learn how to create loops in assembly. In the following code the instruction DJNZ is used to reduce the counter and is repeated till the counter becomes zero.

Eg-1:

                    ORG     0H

        MOV    A, #0                  ; clear A

        MOV    R1, #10              ; load counter R1 =10

AGAIN:         ADD     A, # 05              ; add five to register A

        DJNZ    R1, AGAIN         ; repeat until R1=0 (10 times)

        MOV    R3, A                  ; save A in R3

        END

 

In this code R1 acts as a counter. The counter value is initialized i.e. 10 HEX is loaded to R1. In each iteration, the instruction DJNZ decrements R1 by one until it becomes zero. This loop adds 5 HEX to A every time it runs. After ten iterations R1 becomes zero and the instructions below it are executed.

Note: - Some Jump statements can only be performed on some special register A (or bit CY) as mentioned in the table below.

 

 

Nested loops:

                            ORG 0H

                MOV    A, #55H                  ; A= 55 hex

                MOV    R1, #100                ; the outer counter R1 =100

NEXT:                   MOV    R2, # 20                 ; the inner counter

AGAIN:                 CPL      A, # 05                  ; add five to register A

    DJNZ    R2, AGAIN             ; repeat until R1=0 (100 times)

    DJNZ    R1, NEXT               ; repeat till 20 times (outer loop)

                            END

 

SJMP refers to short jump and LJMP refers to long jump. All the conditional jumps are short jumps.

SJMP: This instruction is of two bytes in which first one is opcode & second is the address. The relative address of the instruction called should be in between -127 to 127 bytes from the current program counter (PC).

LJMP: This instruction is of three bytes in which the first is the opcode and the second & third are for address. The relative address of the instruction can be anywhere on the ROM.

So it is clear from the above examples that we can use different jump instructions with a condition or counter called conditional loop. And when we create loop inside an existing loop it is called nested loop.

 

 

CALL INSTRUCTIONS:

Example:

 

LCALL (long call)

ORG      0H

BACK :              MOV     A, #55H                                  ; load A= 55 hex value

                        MOV     P1, A                                       ; issue value of register A to port1

                        LCALL  DELAY                                      ; to call DELAY function created below

                        MOV A, #0AAH                                    ;load AAH hex value to A

                        MOV P1,A                                            ;issue value of register A to port 1

LCALL   DELAY                                     ; to call DELAY function as created below

SJMP     BACK                                        ; keep doing this

; ________ this is the delay subroutine

 

DELAY:

                            MOV         R5, #0FFH                       ; R5= 255 hex, the counter

AGAIN:                 DJNZ     R5, AGAIN                           ; stay here until R5 becomes zero

                RET                                                         ; return to caller

                END

In this code we keep on toggling the value of the register of port 1 with two different hex values and a DELAY subroutine is used to control how fast the value is changing. Here in DELAY subroutine the program is kept busy by running an idle loop and counting 256 counts.  After the DELAY subroutine is executed once the value of port 1 is toggled and this process goes on infinitely.

By using DELAY we can create PWM (Pulse Width Modulation) to control motors or LED blinking for further details view our tutorial on Input/ output instructions in Assembly Language[coming soon].

We can also use ACALL i.e. absolute call for calling a subroutine that is within 2K byte of PC.

 

 

 

 

 

 

 

 

  

Tags:

Comments

Member since:
9 February 2017
Last activity:
17 weeks 3 days

Basketball Jerseys Outlet

beat headphones

Nike Air Max TN

oakley frogskins glasses

pandora bracelet uk

prada bags

louis vuitton outlet

pandora bracelet

Basketball Jerseys Outlet

oakley glasses

kate spade bags

ray ban wayfarer

chrome store uk

canada goose coats

michael kors outlet online

Hockey Jerseys Outlet

prada uk

pandora necklaces

nike air max 95

shop mlb

major league baseball

nfl store near me

pandora uk

Shopping Online

louis vuitton outlet

michael kors uk

michael kors canada

pandora charms uk

Mens Sunglasses

Tiffany

louis vuitton factory outlet

burberry canada

nba mock draft

burberry outlet

authentic prada outlet online

prada outlet

Dreamland Jewelry - Official

Prada Shoes - Official

Prada bags

nike running

kate spade handbags

nike outlet

michael kors tote

Nike Air Jordan

north face outlet

Clothing

ugg boots uk

Burberry Shirt - Official

coach handbags

Mizuno Running Shoes

oakley frogskins sunglasses

chrome store

Oakley Sunglasses

prada outlet online

michael kors bags outlet

myhermes

pandora bracelet charms

coach outlet us

michael kors outlet online

louis vuitton handbags

prada bags outlet

burberry

michael kors outlet online

myhermes parcel shop

Nike Zoom

mlb store

chrome store

Gift

pandora bracelet

ray ban sunglasses

moncler outlet

canada goose sale

ugg uk

nfl schedule

pandora uk

mlb jerseys

mlb shop

ray ban sunglasses

michael kors outlet

nike air huarache

balenciaga outlet

shoes online sale

michael kors factory outlet

official

coach australia

prada bags

snow boots

polo outlet

salomon hiking shoes

Ray Ban Outlet

Burberry Australia

kate spade outlet online

michael kors online

coach outlet

prada bag

Oakley Australia

chrome hearts earrings

KD 8 shoes

Tiffany and Co.

Pandora Bracelet - Official

Hugo Boss Online

oakley australia

kate spade

nflshop com

Sports

Football Jerseys Outlet

prada outlet

adidas australia

mens sunglasses

ugg boots

Table

coach tennis shoes

rayban prescription

payless shoe source

kate spade handbags

ralph lauren australia

shoes sale

pandora bracelets charms

TCO

michael kors bags

nba score

michael kors us

coach crossbody

moncler outlet

yeezy boost 350 white

michael kors bags

balenciaga bag

Online Marketing

chrome store

nfl jerseys

kate spade

nike outlet

coach online

michael kors

Nike Free Run

Jewelry

Wedding Rings- Official

pandora bracelet

Zales Jewelry

prada men

beats studio

prada outlet prices

moncler men jackets

Oakley Australia

prada outlet

chrome store canada

louis vuitton outlet

prada wallet men

longchamp outlet

payless shoe

Salomon Speedcross Shoes

sunglasses

coach factory outlet

kate spade

Products Made in China

payless shoe store

hermes parcel

coach carter

pandora.com

pandora rings uk

Jewelry Armoire - Official

north face outlet sale

Flatware

pandora charms sale

pandora deutschland

Jewelry

coach factory outlet

uggs outlet

pandora sale

oakley holbrook glasses

mcm factory outlet

Coach Australia

Prada Shoes - Official

prada sunglasses

ray-ban aviator

pandora uk

Wine

pandora uk

yeezy boost 350

Gift Card

prada outlet online

prada saffiano

ray ban aviator

chrome hearts outlet

michael kors wallet

piumini moncler replica

Prada Sunglasses - Official

rayban eyeglasses

prada outlet california

Cheap Ray Ban

moncler jacket

pandora charms sale

coach outlet usa

pandoracharms

hermes belt

online shoes

ray-ban glasses

Ray Ban Australia

dre beats

pandora earrings

Coach Bags

nike shox

Nike Air Jordan

moncler clothing

ray ban sunglasses

pandora necklace charms

air jordan shoes

nike huarache shoes

nike shoes

pandora rings

Nike Free

kate spade black friday

louis vuitton outlet online

ray-ban outlet

Nike Air Max

coach outlet online

payless shoe store near me

ua shoes

louis vuitton outlet online

prada bags prices

Dinnerware

Prada Sunglasses - Official

ray-ban aviator

pandora earrings

prada online

Diamonds

pandora rings

Mobile Phones

louis vuitton outlet

pandora sale

balenciaga us

kate spade newyork

prada handbags

michael kors outlet

louis vuitton us

tracking ups

pandora rings

ray ban prescription sunglasses

celine factory outlet

michael kors bags

louis vuitton

homepage keywords

chrome hearts

ray ban wayfarer sunglasses

michael kors

kate spade bags

tiffany and co

coach outlet

authentic shoes

Nike Air Jordan

oakley frogskins

michael kors handbags

coach outlet

moncler jacket

michael kors outlet online

salomon sneakers

michael kors outlet

ugg boots sale

michael kors handbags

coach outlet

michael kors online

Sunglasses for men

prada outlet bags

moncler jacket

top sunglasses

adidas shoes australia

ray ban uk

nike air

burberry outlet

prada sale

coach bags

prada handbags

MIZUNO Shop US

coach outlet

balenciaga bags

kate spade online

louboutin shoes

coach

pandora jewelry

prada bags on sale

nfl fan shop

michael kors uk online

prada outlet price

ray ban aviator sunglasses

Nike Free Run

louis vuitton

kate spade canada

ups com tracking

michael kors australia official

ralph lauren polo

Wedding Registry

michael kors outlet

Nike Hyperdunk 2015

louis vuitton outlet

mlb.com

nike id

mk outlet

kate spade black friday

MIZUNO Shop On Line

brand sunglasses

prada outlet

prada outlet nj

Accessories

kate spade italian

shoes online

Engagement Rings - Official

prada clearance sale

michael kors

Nike Dunk SB Low

Oakley Sunglasses

coach outlet online

Oakley glasses

ray ban polarized sunglasses

prada bags

pandora rings

Mizuno Wave

sunglasses hut

pandora rings

ugg uk

michael kors outlet

nfl shop com

chanel australia

ups tracking number

christian louboutin shoes

Nike Free

polo online

michael kors outlet

prada bags

kate spade handbags

kate spade bags

2015 Nike Dunk

pandora bracelet

kate spade

abercrombie canada

moncler down jackets

ugg slippers uk

louis vuitton outlet online

Home

jordan shoes

ray ban online

michael kors shoes

discount nfl jerseys

louis vuitton outlet online

coach outlet

rayban prescription glasses

gel kayano

Coach Outlet Store

nike air max 2013

Watches

Sterling Silver Flatware

shoes online

Minisinbox

coach australia

ups shipping

nike shoes

official rayban

nfl store nyc

nba finals

sunglasses sm

north face outlet online

Nike Air Jordan

salomon mens shoes

kate spade outlet

burberry outlet online

nike air max

coach shoes

rayban sunglasses

kate spade bags outlet

yeezy boost 350

sunglass hut

Air Jordan

coach outlet online sale

pandora australia

Jimmy Choo Australia

pandora charm bracelet

Kay Jewelers - Official

michael kors outlet online

nba store

michael kors outlet clearance

coach purses

coach factory outlet online

prada outlet

Nike Air Jordan

ray-ban wayfarer

chanel bag

Prada Outlet

kate spade bags

my hermes tracking

Promise Rings - Official

Abercrombie Fitch

hermes handbags

moncler us

mcm outlet

prada outlet online

coach outlet

Jared Jewelry - Official

ray-ban sunglasses

Michael Kors Outlet

nike huarache

kate spade outlet

mlb jerseys

michael kors outlet

brand sunglasses

spades uk

tiffany co

snow boots uk

beats by dr dre

dr dre beats

pandora charms

pandora charms 2016

Wave Prophecy 2 Shoes

pandora charms cheap sale

snow boots

ray ban new wayfarer

nfl shop coupon code

ray ban polarized

boots

wayfarer sunglasses

CHanel Factory Outlet

tiffany and co

official prada site

nfl store

pandora rings

Nike Air Max

tiffany australia

louis vuitton factory outlet

coach watches

beats by dre headphones

polo outlet online

Air Jordan

pandora charms uk

prada outlet

kate spade outlet

pandora jewelry charms

Football Jerseys Outlet

Diamond Rings - Official

ugg slippers

pandora necklace

Mizuno Shop Japan

nike free shoes

Chanel bags Outlet

Shoes

ray ban sunglasses

classic

kate spade purses

michael kors outlet

chrome hearts

pandora jewelry

Mizuno Shop

moncler down Jackets

pandora rings uk

prada purses

yeezy boost 350 black

chrome hearts jewelry rings

Nike Air Max Lunar

prada outlet

ray ban prescription glasses

Jewelry Armoire - Official

louis vuitton australia

prada outlet online

burberry purse

payless boots

huaraches shoes

nike air max

beats headphones

celine outlet online

coach shoes

burberry outlet canada

coach outlet online

kate spade outlet

burberry outlet online

nbastore

Gift Certificate

Jordan Flight 45 Femme

burberry online

kate spade outlet online

michael kors outlet

Coach outlet online

coach usa online

official prada site

canada goose jacket

Birkin Bag Hermes

Longchamp Outlet

China Wholesale

michael kors outlet

Hyperdunk 2016

prada outlet bags

beats audio

payless sandals

pandora rings

payless shoes near me

canada goose outlet

nike shox

Nike Air Jordan

coach bags

pandora jewelry

coach australia

chrome hearts

canada goose mens

prada outlet texas

chrome hearts jewelry

coach usa

nike australia

coach sunglasses

adidas yeezy boost 350

coach outlet online

louis vuitton factory outlet

rayban glasses

sunglasses store

pandora charms sale clearance

pandora jewelry

michael kors bags

2017 Nike Air Max

pandora jewelry

coach wallets

Longchamp Sale

michael kors shoes

Air Jordan

rayban sunglasses

canada goose jacket

prada us

coach outlet online

nfl pro shop

Mizuno Shoes

ray ban

nike store

the beat

coach outlet store

ray-ban eyeglasses

michael kors factory outlet

ray ban new wayfarer sunglasses

pandora online

Cheap coach purses

ray ban sunglasses

ray ban sunglasses

nike com

michael kors outlet

nike shox

adidas shoes

Vase

michael kors bags

nike free

ups ground tracking

nfl apparel

Dreamland Jewelry - Official

michael kors outlet online

official chrome hearts

michael kors outlet

canada goose kensington parka

louis vuitton factory outlet

Hugo Boss Store

nfl shop

Nike Air Jordan

prada bags 2016

chrome hearts jewelry rings

michael kors wallet

pandora charms

payless shoes online

chrome hearts outlet

prada outlet store near me

official michael kors

rayban wayfarer

Nike Air Jordan

adidas australia

michael kors

sunglasses online

nba jerseys

pandora bracelet charms

celine outlet

beats by dre studio

pandora australia

pandora charms

pandora bracelet

michael kors outlet

kate spade online outlet

michael kors purses

kate spade italy

coach outlet

pandora charms

cheap mlb jerseys

coach outlet online

prada bags outlet

Air Max

louis vuitton purses

nba shop

huaraches

ugg

pandora charms sale clearance

michael kors outlet

coach outlet

chrome store

prada bags

kate spade handbags

chrome store

coach shoes for women

ray ban wayfarer

pandora bracelet charms

Drinkware

nba schedule

hermes uk

ups tracking

pandora bracelet

tiffany australia

prada outlet online

Nike Zoom

pandora australia

yeezy boost 350 shoes

payless coupons

Air Max

Tiffany And Co

pandora rings uk

rayban sunglasses

michael kors uk online

Wave Prophecy 2 Shoes

kate spade uk

coach factory outlet

nba scores

chrome hearts

coach factory

michael kors australia

adidas

michael kors outlet online

prada sale

Home Accessories

chrome hearts jewelry for men

prada online

michael kors bags outlet

pandora jewelry

coach outlet online

moncler outlet

pandora charm bracelet

michael kors tote

pandora app

Salomon Running Shoes

michael kors watches

pandora charms

shop mlb

Nike Air Max

pandora bracelet

louis vuitton online sale

kate spade wallet

burberry australia

prada official site

mlb clothing

Jimmy Choo Shoes

ray ban aviator

softball jerseys

pandora sale

ugg

prada shoes for men

Michael Kors

pandora bracelet

sunglasses

ray ban outlet

coach outlet

ray ban eyeglasses

Salomon Shoes

coach outlet online

moncler jackets

Air jordan Femme 2015

prada outlet bags

ups freight tracking

Canada Goose Outlet

coach outlet online

adidas yeezy boost

Gift Registry

Oakley Sunglasses

Hermes Outlet

Hugo Boss Outlet

michael kors watches

prada outlet online

mk outlet online

coach handbags

salomon shoes

michael kors bags outlet

Abercrombie Nederland

asics gel kayano

prada handbags

kate spade handbags

Phones

pandora earrings

burberry scarf

pandora uk

michael kors handbags

louis vuitton australia

pandora jewelry sale

kate spade

Nike Free

michael kors

coach outlet online

mlb store

payless shoes coupons

christian louboutin uk

louis vuitton 80% off

mcm outlet online

hermes parcel shop

official kate spade website

nike air max

louis vuitton bags

Coach Handbags

Air Jordan

payless shoes

prada outlet online sale

pandora official site

coach handbags outlet

Coach Sunglasses Outlet

prada outlet

rayban outlet

burberry australia

oakley holbrook sunglasses

Jared Jewelry - Official

nike air max

nba standings

coach handbags sale

coach outlet

beats by dre

mlb news

official michael kors

import sunglasses

yeezy boost 350 Pink

michael kors

louis vuitton australia

fox fur

Air Max

ray ban glasses

ray ban clubmaster sunglasses

michael kors online

cheap ray ban sunglasses

pandora rings

sunglasses outlet

dr beats

drum beats

Ray Ban sunglasses

chrome hearts jewelry

asics gel

pandora charms

oakley sunglasses

kate spade outlet online

nbashop

my herm茅s

pandora necklace

michael kors outlet

pandora charms

nike air max 90

michael kors outlet

Wedding

pandora charms sale

Home

nike free run shoes

Jordan Aero Mania Homme

ray ban wayfarer

dr dre headphones

adidas shoes

coach outlet online

coach factory

pandora bracelets

coach factory outlet

Kate Spade - Online Sale

coach outlet

kate spade outlet store

prada outlet sale

BURBERRYburberry australia

pandora outlet

huarache nike

nike jordan shoes

Brighton Jewelry - Official

mlb apparel

north face sale

burberry scarf

pandora bracelet

pandora charms

michael kors

coach totes

Nike Jordans

Sunglasses outlet sale

pandora jewelry

burberry online

Air Max

burberry scarfs

kate spade black friday

prada factory outlet

ray ban sunglasses

longchamp outlet online

pandora jewelry

michael kors purses

prada handbags

prada online

Longchamp tote

oakley glasses

yeezy boost

Phones case

Ray Ban Outlet store

nike outlet

chrome hearts us

michael kors uk

kate spade

rayban aviator

michael kors outlet bags

Baseball Jerseys Outlet

Mizuno Shop Japan

pandora rings

Coach Outlet

chrome hearts jewelry

prada online

michael kors outlet online

salomon boots

baseball jerseys

prada bags

ugg australia

poly sunglasses

burberry outlet

coach factory outlet

Nike Hyperdunk Shoes

michael kors outlet

chrome hearts

rayban prescription sunglasses

pandora uk

Company

prada outlet woodbury

ray-Ban sunglasses

chanel

ray ban clubmaster

Nike KD 8

coach factory outlet

beat maker

coach factory outlet

louis vuitton bags

chrome hearts earrings

michael kors outlet online

ray ban sunglasses

Tiffany Co

pandora bracelets

cheap moncler jacket

michael kors factory outlet

mlb shop

oakley holbrook.

coach outlet

chrome hearts canda

andora charms cheap sale

rap beats

Coach Outlet Online

tiffany co

prada outlet online

Baseball Jerseys Outelt

michael kors outlet online

Electronics

nba playoffs

nike shoes

pandora charms

Nike Air Max

burberry us

pandora charms 2016

prada outlet online

kate spade outlet

Nike Air Max

prada glasses

nba store nyc

michael kors outlet

kate spade UK

Abercrombie Outlet

Watches

Chanel Outlet

louis vuitton

Nike Jordan Shoes

prada sale

China Manufacturers

kate spade wallet outlet

asics kayano

beats dr dre

Bridal Registry

Michael Kors Canada

hermes bag

moncler coats

moncler sale

canada goose sale online

pandora charms

louis vuitton sale

michael kors outlet

Premier Jewelry. - Official

coach sunglasses for women

kate spade outlet

Nike Air Max

chrome hearts uk sale

prada bags

pandora rings

official coach outlet site

pandora rings

burberry outlet

Prada Sunglasses - Official

mlb com shop

Kevin Durant Shoes

Hermes Belt

kate spade bags

rayban aviator

kate spade outlet

Brighton Jewelry - Official

asics shoes australia

michael kors bags

China Suppliers

Premier Jewelry - Official

Nike Free Run

ugg boots

me adc5.20