Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

64 bit assembly, when to use smaller size registers

I understand in x86_64 assembly there is for example the (64 bit) rax register, but it can also be accessed as a 32 bit register, eax, 16 bit, ax, and 8 bit, al. In what situation would I not just use the full 64 bits, and why, what advantage would there be?

As an example, with this simple hello world program:

section .data
msg: db "Hello World!", 0x0a, 0x00
len: equ $-msg

section .text
global start

start:
mov rax, 0x2000004      ; System call write = 4
mov rdi, 1              ; Write to standard out = 1
mov rsi, msg            ; The address of hello_world string
mov rdx, len            ; The size to write
syscall                 ; Invoke the kernel
mov rax, 0x2000001      ; System call number for exit = 1
mov rdi, 0              ; Exit success = 0
syscall                 ; Invoke the kernel

rdi and rdx, at least, only need 8 bits and not 64, right? But if I change them to dil and dl, respectively (their lower 8-bit equivalents), the program assembles and links but doesn't output anything.

However, it still works if I use eax, edi and edx, so should I use those rather than the full 64-bits? Why or why not?

like image 633
mk12 Avatar asked Jul 05 '11 02:07

mk12


People also ask

What is the size of register in 64-bit machine?

A 64-bit register can theoretically reference 18,446,744,073,709,551,616 bytes, or 17,179,869,184 GB (16 exabytes) of memory. This is several million times more than an average workstation would need to access.

Are there 64-bit registers?

x64 extends x86's 8 general-purpose registers to be 64-bit, and adds 8 new 64-bit registers. The 64-bit registers have names beginning with "r", so for example the 64-bit extension of eax is called rax. The new registers are named r8 through r15.

How many registers does a 64-bit CPU have?

As well, 64-bit x86 includes SSE2, so each 64-bit x86 CPU has at least 8 registers (named XMM0–XMM7) that are 128 bits wide, but only accessible through SSE instructions.

Why do 64-bit registers start with R?

R just stands for "register". The AMD64 ISA extension added 8 additional general-purpose registers, named R8 through R15 . The 64-bit extended versions of the original 8 registers had an R prefix added to them for symmetry. E stands for "extended" or "enhanced".


2 Answers

You are asking several questions here.

If you just load the low 8 bits of a register, the rest of the register will keep its previous value. That can explain why your system call got the wrong parameters.

One reason for using 32 bits when that is all you need is that many instructions using EAX or EBX are one byte shorter than those using RAX or RBX. It might also mean that constants loaded into the register are shorter.

The instruction set has evolved over a long time and has quite a few quirks!

like image 113
Bo Persson Avatar answered Oct 10 '22 03:10

Bo Persson


First and foremost would be when loading a smaller (e.g. 8-bit) value from memory (reading a char, working on a data structure, deserialising a network packet, etc.) into a register.

MOV AL, [0x1234]

versus

MOV RAX, [0x1234]
SHR RAX, 56
# assuming there are actually 8 accessible bytes at 0x1234,
# and they're the right endianness; otherwise you'd need
# AND RAX, 0xFF or similar...

Or, of course, writing said value back to memory.


(Edit, like 6 years later):

Since this keeps coming up:

MOV AL, [0x1234]
  • only reads a single byte of memory at 0x1234 (the inverse would only overwrite a single byte of memory)
  • keeps whatever was in the other 56 bits of RAX
    • This creates a dependency between the past and future values of RAX, so the CPU can't optimise the instruction using register renaming.

By contrast:

MOV RAX, [0x1234]
  • reads 8 bytes of memory starting at 0x1234 (the inverse would overwrite 8 bytes of memory)
  • overwrites all of RAX
  • assumes the bytes in memory have the same endianness as the CPU (often not true in network packets, hence my SHR instruction years ago)

Also important to note:

MOV EAX, [0x1234]
  • reads 4 bytes of memory starting at 0x1234 (the inverse would overwrite 4 bytes of memory)
  • overwrites all of RAX, but the high bits will all be zero
    • see: Why do most x64 instructions zero the upper part of a 32 bit register

Then, as mentioned in the comments, there is:

MOVZX EAX, byte [0x1234]
  • only reads a single byte of memory at 0x1234
  • extends the value to fill all of EAX (and thus RAX) with zeroes (eliminating the dependency and allowing register renaming optimisations).

In all of these cases, if you want to write from the 'A' register into memory you'd have to pick your width:

MOV [0x1234], AL   ; write a byte (8 bits)
MOV [0x1234], AX   ; write a word (16 bits)
MOV [0x1234], EAX  ; write a dword (32 bits)
MOV [0x1234], RAX  ; write a qword (64 bits)
like image 45
Matty K Avatar answered Oct 10 '22 04:10

Matty K