Reputation: 3888
Using the LC2K ISA, can someone explain to me what this program is doing?
lw 0 1 five load reg1 with 5 (symbolic address)
lw 1 2 3 load reg2 with -1 (numeric address)
start add 1 2 1 decrement reg1
beq 0 1 2 goto end of program when reg1==0
beq 0 0 start go back to the beginning of the loop
noop
done halt end of program
five .fill 5
neg1 .fill -1
stAddr .fill start will contain the address of start (2)
What is the difference between a symbolic and numeric address? For example, why does line 2 load reg2 with -1? Where does the -1 come from?
What does the "start" mean on line 3, and why does that line decrement reg1 when it's using add? Also, how are lines 4-5 and 7-9 working?
If someone can explain the program concisely I'd greatly appreciate it.
Thank you.
Upvotes: 0
Views: 9811
Reputation: 16612
This looks like one of these made-up instruction sets used for some academic purpose, but it seems obvious enough:
The difference between 'symbolic' and 'numeric' is that the symbolic case uses a symbol (a name) to refer to something, whereas the numeric case uses a number. Symbols are replaced by their numeric values at assembly time, so there is no difference in the final code. The symbol five
is pointing at some data, and so the address of that data will be substituted when the code is assembled.
(I am making the assumption that reg0 is a shortcut for the number 0, or else that register always contains 0 - this is true of some real architectures and seems to be mirrored here)
It seems that the second line takes the contents of the register loaded in the first line (which contains 5, as that is the data stored at the location indicated by the symbol five
), adds 3, and then loads the data at this newly calculated address. 3 + 5 = 8, and if we assume that each line takes up one location in memory, numbering from 0, then the data at location 8 is the line indicated as neg1
, which contains -1.
start
on line 3 is another symbol, which will allow the programmer to refer to this part of the code from somewhere else. In this case it's obviously a loop.
The line decrements the register as it's adding -1
. (it seems to refer to registers 1, 2, and 1 again - so presumably reg1 is both input and output, and reg2, which contains -1, is the other input).
The rest of the code simply loops (the beq instruction appears to allow for comparison - it is checking if two registers are equal, and if they are, it branches). The first branch is checking if reg1 == 0, and uses a numeric branch target of 2, which presumably just jumps forward 2 instructions. The second branch is checking if 0 == 0, which is always true, and then jumping to the symbol start
, which again will be turned into a numeric value when assembled (maybe -2, to go back two instructions to where start
actually is).
The last three lines are just declaring some data which the program uses (actually the last line seems superfluous, but I don't know this fictional architecture).
Upvotes: 3