ESE101: Immediately!
Our existing ADD, SUB, and MOVE instructions move numbers to and from registers and memory. Let’s add a new source for these instructions called an immediate value.
An immediate value is a number that is hardcoded inside the instruction itself; the immediate value doesn’t come from a register or memory location
Let's show some examples to help explain it better.
Our existing instructions look like this:
ADD R0, R1 ; adds the value in R0 to R1 SUB R3, R1 ; subtracts the value in R3 from the value in R1 MOVE R1, [8] ; copies the value in R1 to memory location 8 MOVE [10], R3 ; copies the value in memory location 10 to R3
These instructions should look familiar. The source value comes from either a register or memory location, and the result goes into either a register or memory location.
And here are examples of instructions using our newly-introduced immediate values:
ADD #1, R2 ; adds 1 to the value in R2 SUB #12, R0 ; subtracts 12 from R0 MOVE #7, [13] ; copies the value 7 into memory location 13 MOVE #7, R1 ; copies the value 7 into register R1
The numbers 1, 12, and 7 are called immediate values, and are sometimes written with a pound sign (“#”) in front of the number. These immediate values don’t come from a register or memory location, instead they are encoded in their instruction. They’re called “immediate” values because they’re available immediately during instruction execution - no separate register or memory location is needed to use the number.
Immediate values can be used as a source argument in the ADD, SUB, and MOVE instructions. You can’t use an immediate value as a destination because an immediate value is a constant - it’s just a number.
Here are the definitions for our three instructions, now including immediate values:
ADD
ADD src, regDst src can be a register or immediate value. The ADD instruction adds the value in “src” to the value in register “regDst” and stores the result in register “regDst” Summary: regDst = src + regDst Example 1: ADD R1, R2 performs this operation: R2 = R1 + R2 Example 2: ADD #7, R0 performs this operation: R0 = R0 + 7
SUB
SUB src, regDst src can be a register or immediate value. The SUB instruction subtracts the value in “src” from the value in register “regDst” and stores the result in register “regDst”. Summary: regDst = regDst - src Example 1: SUB R3, R0 performs this operation: R0 = R0 - R3 Example 2: SUB #9, R1 performs this operation: R1 = R1 - 9
MOVE
MOVE src, dst src can be a register, memory location, or immediate value. dst can be a register or memory location. The MOVE instruction copies the data from its “src” argument into its “dst” argument. Summary: dst = src Example 1: MOVE R0, R2 performs this operation: R2 = R0 Example 2: MOVE R0, [8] performs this operation: [8] = R0 Example 3: MOVE [5], R1 performs this operation: R1 = [5] Example 4: MOVE #42, R3 performs this operation: R3 = 42 Example 5: MOVE #37, [12] performs this operation: [12] = 37
Adding immediate values as source arguments may seem trivial, but it makes doing routine math easier and the code more understandable.
Next time we’ll use immediate values when we introduce conditional instructions. See you then!
This post is part of a series. Check out the complete Embedded Software Engineering 101 series here.