[OT] Re: choice of operation (-- or =-1)



luserXtrog <mijoryx@xxxxxxxxx> writes:

On Apr 5, 5:59 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
Cross wrote:
Hello

When the value of a variable is zero,is decrement preferable or assigment?
Generalizing the question, how do I know which operation takes how much time to
complete on an exact scale or may be a relative scale?

It will depend on your implementation.

(x = 0, --x) and (x = 0, x = -1) should both be pretty quick.

A long long time ago,
one of my instructors told me of a system where it was faster
to bitwise xor a register with itself
(resulting with a value of zero in that register),
than it was to assign a value of zero to that register,
but I have never seen a system like that.


Actually, I think this is likely to be true with CISC archs
(ie. x86). If the instruction refers solely to registers, it
will likely be shorter than a MOV with a literal. But it is
seriously trivial. It *might* save a byte. One measly little
byte. Maybe. On a RISC or VLIW arch, there may be no difference
(size/speed-wise).

Indeed, this is a common idiom on x86. On the 386 family in 32-bit
mode, zeroing a register in the "obvious" way with "mov reg, 0" requires
a six-byte instruction: one byte for the opcode, one byte which
specifies the destination register, and four bytes for the 32-bit
immediate operand. But "xor reg, reg" or "sub reg, reg" are only two
bytes. So you save *four* measly little bytes :) This is significant in
situations where code space is at a premium, or in general to fit more
code in cache, and compilers will generally do it.

This is common to the point that seeing "mov reg, 0" in an assembly
language listing is generally a tipoff that it was written by a novice
programmer, or a poor compiler. Programmers have stylistic disputes
over whether "xor reg, reg" or "sub reg, reg" is preferable, though
"xor" seems to be more popular (as far as I know, they are equivalent
performance-wise). The trick has been around since the 16-bit 8086
days, where the savings was only two bytes.

(This is also convenient for people writing shellcode to exploit buffer
overflows: the instruction "mov reg, 0" contains four zero bytes, which
can be a problem if overflowing a null-terminated string. "xor reg, reg"
doesn't have this problem.)

In fact, if moving 1 into a register, one has the option to do

xor reg, reg
inc reg

which is three (nonzero!) bytes, instead of six for "mov reg, 1".
However, it would normally take more cycles (4 instead of 2 on i386).
-1 can be achieved by using "dec reg" or "not reg" (one's complement).

Some architectures, including, I believe, SPARC, have the clever feature
of a virtual "register zero" whose value is always zero. Thus one can
zero out a register by moving r0 into it, using a fast and small
register-to-register move instruction.

(This also brings to mind the x86 programmer's pastime of finding "long
no-ops": instructions which occupy several bytes and do nothing, but
execute faster than the corresponding sequence of one-byte "nop"
instructions. These could be useful for inserting padding, if a
particular instruction (perhaps a jump target) should be aligned on some
power-of-two boundary. Indeed, the "nop" opcode itself logically
belongs to the instruction "xchg eax, eax" which swaps the eax register
with itself.)

Sorry for the digression.
.



Relevant Pages

  • Re: void *malloc(size_t num_bytes);
    ... executing the VAX's normal subroutine-call instruction, ... (The startup code was thus at address 2. ... so the two zero bytes at location zero ... forgotten what the register setup was supposed to be, ...
    (comp.lang.c)
  • Re: cl.exe (x86 for amd64) bugs
    ... extend if the lower 32 bits are modified by an instruction. ... of some register and when your process is resumed, ... an interesting mix of promotions, non-promotions, default operand size, zero ... I just looked at the AMD web site and see some updates and new info on the ...
    (microsoft.public.development.device.drivers)
  • Re: choice of operation (-- or =-1)
    ... to bitwise xor a register with itself ... (resulting with a value of zero in that register), ... than it was to assign a value of zero to that register, ... had the advantage of being a two-byte instruction rather than ...
    (comp.lang.c)
  • Re: How does this make you feel?
    ... >> XOR instruction that applies to a 1M range of VM; ... > register gives a memory address, and the second gives a byte-count (up ... in a CPU that has one execution pathway. ... instruction set and the specifics of its addressing modes; ...
    (comp.arch)
  • Re: Designing my own architecture to be simulated in software - need help with the ISA
    ... > memory address range is limited to 16 bits. ... > I'm reserving the HO byte of the word for the instruction type, ... The register operands are half-bytes in length, ... > sub - store the difference of two registers in a register ...
    (comp.arch)