10/11/04

A student writes:

```     Dr. Patt,
If I wanted to place a large number into a register (which isn't in
the memory), the best algorithm I can come up with is to add some
multiple of it several times.   For instance, if I wanted to put
x8000 into r2, I'd have to and r2 with 0 (to set it with zero),
and immediate add 1111 to it 2,184
times.  I'm assuming this isn't the best way to do it```

So, the first comment that comes to mind is: Why isn't it in the memory? If this large number is something the programmer needs the computer to use in carrying out the work of the program, the best answer is to just make it part of the program, as we actually did in class last week with the value x0030 (the ASCII template).

Recall the example we worked out in class on the board last week, after the instruction

`                   1111 0000 0010 0101`

which was a TRAP to have the operating system halt the computer (the trap vector x25 tells the operating system to halt the computer).

Recall that AFTER that instruction, we put in the next two locations in memory the starting address of the file containing the characters that we were going to examine AND the ASCII template to be used during the last phase of the program (i.e., output the count).

Every language makes available to the programmer the ability to include such large values which then get allocated locations in memory during the translation phase. In fact, we are going to see how the LC-3 Assembly Language does it this week in class. (...using .FILL, by the way).

So, simplest way: allocate a location in the program memory, and a simple LD will usually be good enough to get that value into a register.

If you insist on not doing that, and you want to construct the constant while the program is executing, you can do that (painfully) if you insist. You have given an example (x8000). Thank you for at least picking an easy example. That one will take not a whole lot of work. You can do it as follows:

```                        0101 000 000 1 00000      R0 <--- 0
0001 000 000 1 01000      R0 <--- 8
0001 000 000 0 00000      R0 <--- 16
0001 000 000 0 00000          R0 <--- 32```

and on and on until R0 contains 2^15 as an unsigned integer, or -2^15, as a signed integer, if you prefer.

Personally, if I know the large number I need when I write the program, it is much easier to just make it part of the program, and put it somewhere after the TRAP x25.

```     This is another reason why I don't understand short instruction
length.  I mean, might as well make it easy for the programmers
instead of the architect, though I suppose making it a little
easier for the programmers and a lot harder for the architect
wouldn't be a good tradeoff.```

Yes, indeed. A good tradeoff to not make it much harder for the hardware designer in order to make it a little easier for the programmer.

`     Please help me find the answer to this ponderous question.`

"Ponderous"?

Good luck.

Yale Patt

`     << name withheld to protect the man with the ponderous question >>`