I\'m a CS student writing in Intel x86-64 assembly, compiling with nasm
, and running on an Core i7 processor with Ubuntu 12.04 as the guest OS. Does anyone hav
Finally, an answer to this question. Thanks to user: harold who helped answer the question for me. A summary of what I've found:
Set up a memory space in .data and align it on a 64-byte boundary. Then you can use the commands with that memory space. If you want to use the stack, you should be able to do so similarly ensuring that the stack is 64-byte aligned, but this way seems easier to me for this purpose.
eax: edx is used to set the flags of which registers you WANT to save, restore. This combined is 64-bits and is ANDed with an internal control which knows which registers you CAN save/restore (this allows processors that don't have ymm for example to ignore those registers) I find it easiest to just set all bits on and save / restore everything:
segment .data
align 64
regsave times 1024 dq 0
segment .text
mov rdx, 0xFFFFFFFFFFFFFFFF
mov rax, 0xFFFFFFFFFFFFFFFF
xsave [regsave]
vzeroall
mov rdx, 0xFFFFFFFFFFFFFFFF
mov rax, 0xFFFFFFFFFFFFFFFF
xrstor [regsave]
The xsave
/xrstor
/xsaveopt
instructions are used to perform a full save/restore of the extended state in the processor to/from memory. Similar to fxsave
/fxrstor
, it saves/restores fpu state st[0..7]
, xmm[0..7]
, mxcsr
, etc... in addition to supporting ymm[0..15]
and future extensions (zmm[0..31]
). The actual values saved, and the data layout are enumerated via the relevant cpuid
leaves. The use is generally operating system context switching. The programmer reference describes how to use them correctly.
For general userspace register save/restore, the assembler usually has a facility for saving/restoring a set of registers.
For example...
foo PROC USES eax,ebx,ecx
xor ebx, ebx
loop:
mov eax, [esi + ebx*4]
mov [edi + ebx*4], eax
inc ebx
dec ecx
jnz loop
ret
foo ENDP
%macro mpush 1-*
%rep %0
push %1
%rotate 1
%endrep
%endmacro
%macro mpop 1-*
%rep %0
%rotate -1
pop %1
%endrep
%endmacro
foo:
mpush rax,rbx,rcx
xor rbx, rbx
loop:
mov rax, [rsi + rbx*8]
mov [rdi + rbx*8], rax
inc rbx
dec rcx
jnz loop
mpop rax,rbx,rcx
ret
In ia-32, there is a pushad
to save all the general purpose registers, but with amd64 you need to have corresponding push
/pop
pairs for each of the registers you use.