Skip to content

R2 is clobbered by the multiply loop and no documentation says so #7

Description

@dmccoystephenson

Observed

The multiply loop decrements R2 to drive its termination test (line 16 of src/multiplyTwoNumbers.asm):

ADD R2, R2, #-1 ; decrement R2

R2 is the register the header comment names as the second operand: "pre execution we have num1 in R1 and num2 in R2". Nothing in the file, in README.md, or in the GitHub repository description states that R2 does not survive execution.

Impact

A caller following the documented contract has no way to learn that its second operand is consumed. The three header comment lines document what goes in (R1, R2) and what comes out (R0), but are silent on what is destroyed — which is the third thing a calling contract has to say.

The exact post-HALT value of R2 is UNVERIFIED. No LC-3 simulator is installed in the environment this issue was filed from, so no register dump was produced and no value is asserted here. What is verified from the source is that R2 is written by an instruction inside the loop.

Suggested fix

Either of two directions, in ascending cost:

  1. The header comment is extended to state which registers are clobbered, with the actual post-HALT values confirmed by a simulator run rather than asserted from a trace. This is the smaller change and keeps the program at 21 lines.
  2. The loop is rewritten to count down in a scratch register, leaving R2 intact. This is a real behavior change and should be weighed against keeping the program readable line by line, which is the point of the exercise.

Both are downstream of #2: if the program is declared a self-contained 5 x 6 demonstration rather than a callable routine, a clobbered R2 is not a defect at all and only the comment needs to say so.


This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

drafted by Claude on behalf of Daniel Stephenson

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions