Indirect Addressing Advanced Exercise from PLCDEV.com

MOeZ

Lifetime Supporting Member
Join Date
Nov 2009
Location
singapore
Posts
101
Dear All,

When i read sample exercise with code and explanation, i think i understand about Indirect Addressing partially. but when i look at exercise #2 below, i realize i am totally don't have idea how to start? Could anybody give me an idea to solve this Question 2? Thanks!🔨


P.S admit this is my HW.

Exercise #2 (Advanced)

  1. Create a DB with an array of 10 real numbers. Populate the array with random values.
  2. Create a function that will return the max number in the array and its position. Use the indirect addressing method of your choice.
 
pmmdavid,

You are letting the entire assignment overwhelm you.
There are two excersises, so treat them individually.

develop the 10 part data array. get it working and understand how it works.
excercise #2 will be much more simple and easier.

regards,
james
 
FUNCTION FC 1 : INT

VAR_INPUT
rRandom_Data : ARRAY [1 .. 10 ] OF Real ;
END_VAR

Then :unsure::unsure: hue.
 
That is not pseudo code, it is a function definition with an input.
Here's my interpretation of psuedo code for your problem:

for i:=1 to 10 do
rRandom_Data:=RND(i);
end_for

Just how random do the random numbers have to be ?
 
Last edited:
Having re-read the assignment, there is no mention of creating a function to populate the DB, so you could create the DB in the editor and fill in the random values manually....
 
Indirect Addressing Assignment

I have create DB area with manually fill real value.
Q1.
DATA_BLOCK "Prefill values"
TITLE = Shared DB containing random values


STRUCT
Value : ARRAY [1 .. 10 ] OF REAL ;
END_STRUCT ;

BEGIN
Value[1] := 0.000000e+000;
Value[2] := 0.100000e+000;
Value[3] := 0.200000e+000;
Value[4] := 0.300000e+000;
Value[5] := 0.400000e+000;
Value[6] := 0.500000e+000;
Value[7] := 0.600000e+000;
Value[8] := 0.700000e+000;
Value[9] := 0.800000e+000;
Value[10] := 1.000000e+000;
END_DATA_BLOCK

Q2. Create a function that will return the max number in the array and its position. Use the indirect addressing method of your choice.

for this Q, how do i get the position of DB index? any hint :geek: please :geek:
 
Start with the psuedo code:

Code:
//Initialise max value and position as first value in array
//max:=Value[1];
//pos:=1;
//Loop looking for max value, saving position when found
//For i:=2 to 10 do
//  if Value[i] > max then
//     Begin
//       max:= Value[i];
//       pos:= i;
//     end;
//  end_if;
//end_for;
 
Next, break down lines of pseudo code to lines of STL:

e.g.

Code:
//Initialise max value and position as first value in array
//max:=Value[1];
//pos:=1;
      L     "PreFill Values".Values[1]
      T     #max
      L     1
      T     #pos
 
Next, implement the for loop leaving out the processing inside the loop:

Code:
//For i:=2 to 10 do
// ..
//end_for;

      L     2
FL:   T     #ii
      L     10
      >I    
      JC    endf

//..

      L     #ii
      +     1
      JU    FL
endf: NOP   0
 
Now introduce a temp variable x for assigning Values for later processing and noting that:

1) the array is indexed from 1 to 10 but it starts at DBD0 so to compute the correct address you must subtract 1 from the index
2) Indirect addressing starts at the bit level so to compute a byte offset multiply by 8, a word/int offset multiply by 16, and a dword/real offset multiply by 32. For brevity the multiplcation is implemented using SLD 3/4/5

Code:
//For i:=2 to 10 do
// x:=Values[i];
// ..
//end_for;

      L     2
FL:   T     #ii
      L     10
      >I    
      JC    endf

//Let x:=Values[i]  for convenience
      OPN   "PreFill Values"            //open value db
      L     #ii                         //fetch array index 
      +     -1                          //correct for zero based addressing 
      SLD   5                           //shift 3 for bytes,4 for words,5 dwords/reals
      LAR1  P#DBX 0.0                   //point to base of array
      +AR1                              //add offset calculated from index
      L     D [AR1,P#0.0]
      T     #x                          //x=Values[i]



      L     #ii
      +     1
      JU    FL
endf: NOP   0
 
Now add in the comparison processing, leaving out the processing between the Begin and End

Code:
//For i:=2 to 10 do
// x:=Values[i];
//  if Values[i] > max then
//     Begin
//..
//     end;
//  end_if;
//end_for;

      L     2
FL:   T     #ii
      L     10
      >I    
      JC    endf

//Let x:=Values[i]  for convenience
      OPN   "PreFill Values"            //open value db
      L     #ii                         //fetch array index 
      +     -1                          //correct for zero based addressing 
      SLD   5                           //shift 3 for bytes,4 for words,5 dwords/reals
      LAR1  P#DBX 0.0                   //point to base of array
      +AR1                              //add offset calculated from index
      L     D [AR1,P#0.0]
      T     #x                          //x=Values[i]
//..
      L     #max                        //if x > max
      >R    
      JCN   enif
//Begin
//..
//end

enif: NOP   0

      L     #ii
      +     1
      JU    FL
endf: NOP   0
 
Finally, add in the Begin..End processing.

Code:
//For i:=2 to 10 do
// x:=Values[i];
//  if Values[i] > max then
//     Begin
//       max:= Value[i];
//       pos:= i;
//     end;
//  end_if;
//end_for;

      L     2
FL:   T     #ii
      L     10
      >I    
      JC    endf

//Let x:=Values[i]  for convenience
      OPN   "PreFill Values"            //open value db
      L     #ii                         //fetch array index 
      +     -1                          //correct for zero based addressing 
      SLD   5                           //shift 3 for bytes,4 for words,5 dwords/reals
      LAR1  P#DBX 0.0                   //point to base of array
      +AR1                              //add offset calculated from index
      L     D [AR1,P#0.0]
      T     #x                          //x=Values[i]
//..
      L     #max                        //if x > max
      >R    
      JCN   enif
//Begin
      L     #x
      T     #max
      L     #ii
      T     #pos
//end
enif: NOP   0

      L     #ii
      +     1
      JU    FL
endf: NOP   0
 
Many Thanks LD!

Finally, add in the Begin..End processing.

Many Thanks LD :geek:. Now i know more about what u meant pseudo code. But give me a time to understand your code and will test it todays until i get idea.

Thanks again!
 

Similar Topics

Howdy folks, I am an Allen Bradley guy currently living in an Emerson world. Working with Rx3i on PacSystems Machine Edition v. 9.6? i think...
Replies
3
Views
608
Hello, I'm very new to programming with absolutely zero schooling in this field and pretty hands off training in my new role, it's been fun...
Replies
4
Views
662
Hello Friends, I am trying to index M Bits, however GX Works2 is not allowing it with following message. https://ibb.co/zPcqj6M...
Replies
3
Views
1,365
Hi All, which the best way to do the indirect addressing in an optimize DB? Ccurrently this is my partial code inside an FB...
Replies
7
Views
2,266
Hey everyone, Just used the PLC5/Logix migration utility to convert a program, and while addressing the PCEs, I noticed a lot of errors for "XIC...
Replies
12
Views
1,943
Back
Top Bottom