LC3 Subroutine - Custom Trap Routine

922 Views Asked by At

I am working on a question which the goal is to create a subroutine that imitates the trap (PUTS) and it will write a string to console, this strings addres can be assumed to be in r0

this is what I have so far, it works for the first character 's' after that it keeps looping printing >>>>>> ive tried everything any suggestions?

 .orig x3000 
 lea r0, string ;


 br putss




 putss

  ldr r1, r0,#0 
   add r0, r1,#0
  add r4, r0, #-4
  brz theend
  out
  and r1,r1,#0
 add r0,r0,#1 ; keeps fetching next chara


   br putss





  theend
   halt









     string .STRINGZ "salazar"

     .end
1

There are 1 best solutions below

2
Brandon On

Alright

1.

br putss

Subroutines are called with JSR. If you have to branch into the subroutine, then how would control flow go back to the caller of the subroutine once the subroutine is finished?

Another point is that if you need to call TRAPs within your subroutine, then you must save and restore R7 since as part of calling a TRAP it will write its return address in R7 which would clobber the return address to get back to the caller. Perhaps this is why you used BR instead?

2.

add r0, r1,#0

This is bad. This clobbers the address of the string data that was contained in R0 at the time the subroutine was called. I don't see why you are doing this?

3.

and r1,r1,#0

This seems unnecessary, you will overwrite the value in R1 when the LDR R1, ... happens.

4.

add r4, r0, #-4

Seems unnecessary? PUTS finishes when reading a character with value 0 (nul terminator).

5.

theend
halt

Don't do this in a subroutine, the subroutine when finished should simply use ret to return to the caller. You wouldn't want the program to terminate if you called someone else's subroutine.