Is there a way to set all registers to 0? (without explicitly assigning them val

Started by starsiege, March 30, 2009, 05:36:22 PM

Previous topic - Next topic

starsiege

Hi guys
             is there a way to set registers to 0? without having to manually assign them to 0


i usually end up doing

mov eax, 0
mov ebx,0
mov ecx,0
mov edx, 0


is there a better way? maybe a single line of code that sets all registers to 0?

thanks :)

qWord

Quote from: starsiege on March 30, 2009, 05:36:22 PM
mov eax, 0
mov ebx,0
mov ecx,0
mov edx, 0

is there a better way? maybe a single line of code that sets all registers to 0?
use xor instead of mov reg,imm.

here is an macro doing the job in one line:

ZeroRegs macro
xor eax,eax
xor edx,edx
xor ecx,ecx
...
endm


FPU in a trice: SmplMath
It's that simple!

jj2007

Macros apart, that is not possible in one line. The minimum is four lines:

include \masm32\include\masm32rt.inc

.data?
Null8 dd 8 dup (?)

.code

start:
mov Null8[12], esp ; save stack pointer in a global variable
mov esp, offset Null8 ; put stack pointer in front of 8 nullwords
popad
mov esp, Null8[12] ; restore stack pointer

getkey
exit ; short form of invoke ExitProcess, 0

end start

starsiege

thanks for the answers guys. I think i will use a macro for this, as you suggested.  :)

dedndave


jj2007

Quote from: dedndave on March 30, 2009, 07:24:50 PM
i use xor - then mov

XOR AX,AX
MOV CX,AX
MOV DX,AX


As a macro:

   clr eax, ebx, ecx, edx, esi, edi, ebp
... translates to:

33C0                        xor eax, eax
8BD8                        mov ebx, eax
8BC8                        mov ecx, eax


clr MACRO arg1:REQ, args:VARARG ; usage clr eax, MyVar1, MyVar2, ...
  Local IsReg
  IsReg = (opattr(arg1)) AND 127
  if IsReg eq 48
xor arg1, arg1 ; this register trashed
  else
and arg1, 0 ; 7 bytes
  endif
  for arg, <args>
if IsReg eq 48
mov arg, arg1 ; mov MyVar1, eax
else
and arg, 0
endif
  endm
ENDM



It even works for ordinary variables, but it's one byte longer than the four-liner above ;-)

Just for fun, here the shortest at 12 bytes:

mov eax, offset Null8 ; save stack pointer in a global uninitialised variable
mov [eax+32], esp
mov esp, eax
popad
pop esp ; restore stack pointer

Jimg

I don't know about intel, but on amd, this sets those four to zero-

    mov al,99
    cpuid

takes 4 bytes

drizz

Guys i think we have a winner here:
fldz
.xmm
prefetchnta qword ptr es:[esp-8]
push cs
verw word ptr es:[esp]
pop ecx
fistp qword ptr es:[esp-8]
pause
lock cmpxchg8b qword ptr es:[esp-8]
cmovne ecx,eax
cmovnz ebx,ecx

It's pretty fast because it uses all the features of your cpu
Its only drawback is that it only works on NT systems - sorry but you can't have it all.


   for i,<eax,ebx,ecx,edx,esi,edi,ebp>
      xor i,i
   endm
   
   i=0
   while i lt 8
      %pxor @CatStr(<mm>,%(i)),@CatStr(<mm>,%(i))
      i=i+1
   endm

The truth cannot be learned ... it can only be recognized.

jj2007

Quote from: Jimg on March 30, 2009, 08:37:03 PM
I don't know about intel, but on amd, this sets those four to zero-

ecx, edx and ebx are zeroed on Intel Celeron M. I just wonder how fast cpuid is :wink
:U

herge

 Hi jj2007:

Be careful this only happens if CPUID receives an invalid arguement!
If it's a valid argument it will Not return Zeros.

0:000> g @@masm(`jjbitup.asm:292+`)
eax=00000000 ebx=7ffde000 ecx=00000000 edx=7c90e4f4 esi=00403008 edi=00abf6f2
eip=004013ee esp=0012ffc0 ebp=0012fff0 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
jjbitup!main+0xca:
004013ee 0fa2            cpuid

0:000> g @@masm(`jjbitup!jjbitup.asm:293+`)
eax=0000000a ebx=756e6547 ecx=6c65746e edx=49656e69 esi=00403008 edi=00abf6f2
eip=004013f0 esp=0012ffc0 ebp=0012fff0 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246



Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

herge

 Hi There:

Also Note you guys and gals with 64 bit processors.

1. On Intel 64 processors, CPUID clears the high 32 bits
of the RAX/RBX/RCX/RDX registers in all modes.


This was info from: Intel 3-180 Vol. 2A CPUID-CPU Identification.
I think this translate to the high 32 bits of said register are trashed,
over-written with zeros.

Can some one with a 64 bit chip verify this?

Regards herge



// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

donkey

Here's one...

sub esp,32
mov edi,esp
xor eax,eax
mov ecx,8
rep stosd
popad
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

Quote from: donkey on March 31, 2009, 06:27:07 PM
Here's one...

sub esp,32
mov edi,esp
xor eax,eax
mov ecx,8
rep stosd
popad


It looks elegant, Edgar, but surely I would not have expected so ridiculously bloated code from you ::)

This one is 20% shorter:

push 32
pop ecx
sub esp, ecx
mov edi, esp
xor eax, eax
rep stosb
popad

Mark Jones

"Please place all holier-than-thou drivel in the collesseum where it may putrify properly, thank you."
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

dedndave

Shorter - perhaps - but all those memory accesses are munching clock-cycles