0

Hi I'm trying to write a lc3 assembly program which computes the square of a number and stores it in r0, the integer is given as a parameter and is located in r1, the problem i noticed while debugging is during the first pass it initially adds 2, but the second pass it fails to add another 2 to r0 - My code is below any help is appreciated

           .orig x3FF8
      ld r1,n
    ld r5,n

  square
 add r2,r1,#0

  add r5,r5,#-1
add r0,r2,#0
brzp square
brn theend

  theend


halt
 n .fill #2

 .end

my final code thanks to the user who helped:

    .orig x3FF8
     ld r1,n
    ld r5,n

   square


  add r2, r2,r1

  add r5,r5,#-1

  brp square


  theend


 halt
  n .fill #4

 .end
rahulchawla
  • 170
  • 1
  • 3
  • 20

1 Answers1

0

If I remember LC-3 syntax correctly, add r2,r1,#0 does r2 = r1 + 0, so it was never actually adding to r2, just overwriting it with r1.

You want something like that outside the loop to initialize r2.

But inside the loop, you want add r2, r2, r1 which does r2 = r2 + r1, i.e. r2 += r1.


I don't understand why you have add r0,r2,#0 inside the loop as well. If you want the final result in r0, accumulate it in r0 in the first place. Of if that was supposed to be a sum of sums, then you have the same bug.

Also note that add r5,r5,#-1 needs to be last so condition code flags are set from it for the loop branch, not from add r0, r0, r2 or whatever else you need inside the loop.


Also: brn theend is totally useless: theend is on the next line, and execution continues to the next line on its own. You don't have to jump over whitespace in the source!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1. oh I assumed it would be a sum of sum in r2, thank you this made it so much clearer I was consued with the add , i guess i was overwriting register 2 – rahulchawla Dec 12 '17 at 05:00
  • Question - At the end i am trying the line add r0,r2,#0 to store the sumofsums in r0, however this is giving 7fff, as you said shouldnt it replace r0 with 25, if r2 has 25 – rahulchawla Dec 12 '17 at 05:09
  • @rahulchawla: Did you notice the edit about not clobbering flags with an `add` after the `add r5,r5,#-1` loop counter? (originally spotted by @ Brandon). Moving that instruction outside the loop would fix your problem. Use a debugger to single-step your code and watch registers change (e.g. for a small input like 2 or 3). – Peter Cordes Dec 12 '17 at 05:12
  • The question(assignment) specifies a single subroutine - So thats why I wanted to do everything in the loop@peter and you are saying move the add r5,r5,#-1 outside the loop if im understanding correctly – rahulchawla Dec 12 '17 at 05:16
  • @rahulchawla: a loop isn't a subroutine. And besides, you need to initialize registers before the loop. If you don't want any instructions other than `halt` after the loop, then do what I suggested and accumulate the sum in `r0` in the first place, so you don't need to move it anywhere. Any time you have a move instruction that just copies a register, see if you can eliminate it by generating the value in the right register in the first place. – Peter Cordes Dec 12 '17 at 05:18
  • wouldnt the registers initially be empty..? – rahulchawla Dec 12 '17 at 05:24
  • I tried replacing add r2,r2,#1 with add r0,r0,#1 however there is a line LD R0, xFD7D that is changing the value in the register after my code is processed so at the end r0 is showing 7fff Im really confused at why r0 is manipulating @Peter Cordes – rahulchawla Dec 12 '17 at 05:26
  • @rahulchawla: At the start of an arbitrary function, no, the caller could have left any values there. If the LC-3 machine (or process) startup state is guaranteed to have all registers zeroed, instead of undefined (i.e. possibly filled with random garbage) then yes you can take advantage of their initial zero values. ("Empty" is not a meaningful word. Registers *always* have a value. If it's known to be zero, that's useful.) – Peter Cordes Dec 12 '17 at 05:27
  • @rahulchawla: I've never used LC-3, just read stuff about it online. You didn't show that instruction in your question. What register do you want the result in? Put the result there. End of story. If there's an instruction that loads R0 before you actually `halt`, then remove that instruction if you don't want it. – Peter Cordes Dec 12 '17 at 05:30
  • I am putting the result in register 0, there is no instruction that I wrote that manipulates r0 as u can see however at the end the value isn't correct. @peter cordes – rahulchawla Dec 12 '17 at 05:34