Wednesday, May 22, 2013

ret

     mov  cx, 0
s:   cmp  cx, 000A
     je   end
     inc  cx
     mov  ax, [si]
     mov  [di], ax
     push s
     ret
end:
What and why does this code do?

Tuesday, May 21, 2013

Weak refs

Look at this Perl code:
use Scalar::Util qw( weaken );

my ( $weak_a, $weak_b, $weak_c );

{
        my $a = {
                name => 'a',
        };

        my $b = {
                a    => $a,
                name => 'b',
        };

        my $c = {
                a    => $a,
                b    => $b,
                name => 'c',
        };

        $a->{b} = $b;

        $weak_a = $a;
        $weak_b = $b;
        $weak_c = $c;

        weaken( $weak_a );
        weaken( $weak_b );
        weaken( $weak_c );
}

print $weak_a->{name};
print $weak_b->{name};
print $weak_c->{name};

What and why will be printed?

Monday, May 20, 2013

INC_AND_READ

Imagine you have a shared object containing integer value that supports three atomic operations:
  • READ (read value)
  • INC_AND_READ (increment and read value)
  • DEC (decrement value)
The init value is 0.

How do you implement mutex and semaphore using this object?

Sunday, May 19, 2013

Lost minus

def quiz
    a = Proc.new do
        return 1
    end
    
    b = Proc.new do
        return -1
    end
    
    c = Proc.new do |x, y|
        return x.call * y.call
    end
    
    return c.call(a,b)
end

puts quiz
This Ruby script prints "1\n". Why?

Saturday, May 18, 2013

if 0;

There are a number of Perl scripts starting with:
#!/usr/local/bin/perl 
eval 'exec perl $0 ${1+"$@"}' if 0;
Why?

Friday, May 17, 2013

string constant

What's a point of using string constants in this Ruby code?
ANIMAL3 = 'rat'
ANIMAL6 = 'rabbit'
def animal3to6(animal)
  length = animal.length
  if length > 6
    return ANIMAL6
  elsif length < 3
    return ANIMAL3
  else
    return animal
  end
end

Thursday, May 16, 2013

self-call

Malware programs often use code like this:
   call a
a: pop ax
Why?