Recap:
mov
- Transfer data from one register to another
movia
- Setting a register to a known, specific memory address
- Used for initialization or to set up pointers
ld/ldw
- Used to read data from memory and place it into a register
ldw r1, 0(r2)
: Loads a word from the memory address contained in r2
into register r1
- Moving data from memory to a register
st/stw
- Used to write data from a register to a memory location
stw r1, 0(r2)
: Stores the word from register r1
to the memory address contained in r2
- Moving data from a CPU register to memory
Accessing Half-words and Bytes
- So far all data coming from/to memory has been 32 bits in size
- When there is a lot to store, it really matters if we can store a given number in fewer bits
- Recall
ldw r9, (r10)
- Loads a 32-bit word from memory into
r9
(r10)
indicates that the source of data is the memory address contained in register r10
- We can also load 16-bits (half-word) using
ldh
or a byte (8-bits) using ldb
ldb r10, (r12)
- Assuming
r12
contains the address of the byte we want from memory
- This instruction loads that byte from memory into
r10
- Into the low-order bits
r10
(7-0)
- The upper bits
r10
(31-8) are set to 0
stb r8, (r9)
- Stores (puts into memory) bits
r8
(7-0, the LSB of r8
) into the byte at the address given by r9
- In assembly language programs, you’ll need to reserve space in memory for bytes and half-words
- For reserving a byte:
smallthing: .byte 50
- Reserves 1 byte in memory (1 address)
- Sets
smallthing
equal to that address
50
must fit into 8 bits
- Similarly, half words:
onehalfthing: .hword 0x660
Some rules:
- The address of words in memory must be divisible by 4 (aligned)
- The address of half words in memory must be divisible by 2
- All addresses can be byte addresses