Moving data to/from memory, endianness
Notes
- Most instructions in intel assembly are in the form
-
[label:] operand opcode, opcode
- Operand is a mnemonic like mov, add, ...
- The operands are normally dest, src
- some are single operand instructions (push, pop)
- Some are no operand instructions (ret)
- Operands can be
- a register
- Jorgensen uses
<reg>
- And also
<regn>, where n ∈ {8, 16, 32, 64}
- An immediate
-
<imm>
- And also
<immn>, where n ∈ {8, 16, 32, 64}
- Memory
- He specifies
<src
- This can be mem, imm or reg
- And
<dest
- But src and dest can not both be mem.
-
mov dest, src
- The src and dest must be the same size.
- The assembler decides which of the mov instructions to execute
- See this page.
- There is some strange behavior
- quadword instructions are fine
- double word instructions zero out the top of the register
- word and byte instructions do not change the value of the register.
- More on memory indexing later but
-
mov dest, label is actually an immediate
- It loads the address into memory
-
mov dest,[label]
-
mov [label], src
- deals with the memory location
-
x/8bx (char*)&storage+1
- Look at play.asm
- Examine memory with
x/1gx &storage
- Step through each of the moves, looking at what is in memory after the move.
- After the last mov
- Examine storage as a
- big integer:
x /1gx &storage
- string:
x /1s (char*)&storage
- As an individual characters:
x /8bx &storage
- Double check rax, as hex.
- What is going on?
- Examine the data file in the code directory, that is even worse!
- Little Endian vs Big Endian
- Assume you want to implment your own number type.
- You might use an array to hold the digits.
- So the number 42601
- Is in a 5 element array.
- But where do you put the 1?
- If you select 0, you are little-endian
- If you select the 4, you are big-endian.
- See
(from Wikipedia)
- Look the article on Endinness on wikipedia.
- Intel is little endian
- The least sugnificant byte goes at address+0
- The next sugnificant byte goes at address+1
- What we see on the file system however, is words as they are stored
- Two characters per word
- First character shows up "last" in the word
- But
od -c changes the order.
- Try
od -cx !