LOAD
Loads a value into a the given register
Loads from memory into a register
LD(PC-Relative)
Loads a value into a register, relative to when this command is run
0010 DDD PPPPPPPPP
- 0010: Load Command
- DDD: Destination register(3bits, R0-R7)
- PPPPPPPPP: Source address, relative to this command + 1
Example
Load EXACTLY what is in register R3(say R0 = 0000 0000 0000 0001), into the relative source address(Number of instructions in your code away from when the LD command is executed + 1.
0011 0000 0000 0000 ; line 1: starting address of 0x3000 to start the program
0010 011 0 0000 0010 ; line 2: LD, R3 <- offset by 2 lines(0 0000 0010)
0001 010 010 1 00001 ; line 3: ADD, R2 <- R2 + 1, Random instruction
1111 0000 0010 0101 ; line 4: halt end program, HALT
; Variables
0000 0000 0000 1111 ; line 5: Random value to store into R3
Let's inspect the LD instruction in detail now
0010 011 0 0000 0010
0010
: This is the opcode, if you want to use the LD instruction it will ALWAYS be this for the first 4
011
: This is the destination register, we are STORING a value into this register.
- In this case,
011
(binary) = 3(decimal) so we are storing the value in Register 3(R3).
0 0000 0010
: This is the source address.
This part is 9-bits wide and can also be written as
000000010
But I added spaces for organization.This number is used as an offset to your current location + 1 to get to the address who's value is to be stored into our given register above.
- The + 1 is because the program counter increments by 1 after the instruction but before the next(Not 100% sure on the reason but just know that its offset + 1).
The calculation for
0 0000 0010
(binary) = 2(decimal) so our offset will be 2 + 1 = 3Our
LD
instruction was on line 2, therefore our source address, who's value we will be moving into R3, will be line 2 + 3 lines = line 50000 0000 0000 1111
.