Home > Coding > Sneaky Assembler Problem

Sneaky Assembler Problem

One of our team had a problem with the way some assembler code was assembling. Basically an instruction was not assembling into the correct binary code.

As an Example, take the following Load Address instruction. The general form is:

LA   R1,D2(X2,B2)

Where R1 is the target register to receive the result, X2 in the source address index register, B2 is the source address Base register and D2 is a displacement that is added to the sum of the addresses in X2 and B2.

So if you coded say:

LA  R2,9(R3,R4)

It would assemble into the following hex code:

41234009

If the index and base registers are both zero the instruction becomes:

LA  R2,9(0,0)

Or you can write it as

LA R2,9

Basically, this says put the number 9 into register 2. The hex code for it would resolve to:

41200009

However in their code, the same instruction (LA R2,9) was generating the following binary code:

41205009

It was as though they had coded:

LA  R2,9(0,R5)

A quick search up the listing showed that earlier on in the code the following line occured:

USING  NAME,R5

This should really not cause a problem since all it does (or so we thought) was to tell the assembler to use the value equated to the symbol R5 (as you might expect, the equated value is 5)  as a base register whenever it encounters a field within the DSECT named NAME.

However, for reasons I will not go into here, we often do not map storage layouts using DSECTS but instead map them using equated values that define the offsets of the fields within the area. In this case NAME was at offset zero and so had been defined as:

NAME    EQU X'0000'

On it’s own this is not a problem but when used in conjunction with the USING statement it has the effect (intentional or not, I am not sure) of assigning the value of the R5 equate (5 in this case) to IT’S equated value of zero, thus in effect telling the assembler to use the value 5 anywhere it would normally use a zero as the base register.

The motto of the story is to always review the generated assembler code as well as the source. It’s not always what you expect!

Advertisements
Categories: Coding
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: