yowhatsup123
yowhatsup123

Reputation: 287

Converting a code a* b * c written in C to LC3

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)

Upvotes: 1

Views: 429

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26656

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.

Upvotes: 1

Related Questions