1

I don't know if this question is vague or lacks enough information but I was just wondering If I want to convert this line a = a * b * c written in C language to LC-3, how can I do it? Assuming that a, b and c are local variables and that the offset of a is 0, b is -1, c is -2?

I know that I can start off like this:

LDR R0, R5, #0; to load A
LDR R1, R5, #-1; to load B

Is there a limitation to the registers I can use? Can I use R2 to load C?

Edit:

         LDR R0, R5, #0; LOAD A
         LDR R1, R5, #-1; LOAD B
         LDR R2, R5, #-2; LOAD C
         AND R3, R3, #0; Sum = 0
LOOP     ADD R3, R3, R1; Sum = sum + B
         ADD R0, R0, #-1; A = A-1
         STR R0, R5, #0; SAVE TO A (a = a*b)
         BRp LOOP
         ADD R4, R4, R2; Sum1 = sum1 + C
         ADD R2, R2, #-1; C = C-1
         BRp LOOP
         STR R0, R5, #0; SAVE TO A (a = a*c = a*b*c)
yowhatsup123
  • 287
  • 1
  • 11
  • Who would be imposing this limitation, if it existed? – Scott Hunter May 23 '22 at 12:44
  • You tell me. Is there a limitation to the registers you can use? – user253751 May 23 '22 at 12:45
  • I don't think there is, however, I'm not too sure how to work this whole 3 number multiplication out in LC3 – yowhatsup123 May 23 '22 at 13:02
  • 1
    Basically you have a bunch of instructions like lego bricks in front of you, and your job have to figure out how to put them together into a 3-number multiplication like a lego house. Did they give you a sheet telling you all the LC3 instructions? – user253751 May 23 '22 at 13:39
  • 1
    I see the LC3 has no multiply instruction. I guess they told you to use a loop (add A B times)? – user253751 May 23 '22 at 13:43

1 Answers1

1

If you're writing a whole program, which is often the case with LC-3, the only physical limit is the instruction set, so modulo that, you can use the registers as you like.

Your coursework/assignment may impose some environmental requirements, such as local variables and parameters being accessible from a frame pointer, e.g. R5, and having the stack pointer in R6.  If so, then those should probably be left alone, but in a pinch you could save them, and later restore them.

If you're writing just a function that is going to be called, then you'll need to follow the calling convention.  Decode the signature of the function you're implementing according to the parameter passing approach.  If you want to use R7 (e.g. as a scratch register, or if you want to call another function) be aware that on entry to your function, it holds the return address, whose value is needed to return to the caller, but you can save it on the stack or in a global storage for later retrieval.

The calling convention in use should also inform which registers are call clobbered vs. call preserved.  Within a function, call-clobbered registers can be used without fuss, but call-preserved registers require being saving before use, and later restored to original values before returning to the function's caller.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53