Assembly Language for Beginners

(Jeff_L) #1

2.4 AND


From [Brian W. Kernighan, Dennis M. Ritchie,The C Programming Language, 2ed, (1988)]:


Exercise 3-4. In a two’s complement number representation, our version ofitoadoes not
handle the largest negative number, that is, the value ofnequal to−(2wordsize−^1 ). Explain
why not. Modify it to print that value correctly, regardless of the machine on which it runs.

Theansweris: thefunctioncannotprocesslargestnegativenumber(INT_MINor0x80000000or-2147483648)
correctly.


How to change sign? Invert all bits and add 1. If to invert all bits in INT_MIN value (0x80000000), this
is 0x7fffffff. Add 1 and this is 0x80000000 again. So changing sign has no effect. This is an important
artifact of two’s complement system.


Further reading:



  • blexim – Basic Integer Overflows^19

  • Yannick Moy, Nikolaj Bjørner, and David Sielaff – Modular Bug-finding for Integer Overflows in the
    Large: Sound, Efficient, Bit-precise Static Analysis^20


2.4 AND


2.4.1 Checking if a value is on 2 nboundary


If you need to check if your value is divisible by 2 nnumber (like 1024, 4096, etc.) without remainder, you
can use a%operator in C/C++, but there is a simpler way. 4096 is 0x1000, so it always has 4 ∗3 = 12lower
bits cleared.


What you need is just:


if (value&0xFFF)
{
printf ("value is not divisible by 0x1000 (or 4096)\n");
printf ("by the way, remainder is %d\n", value&0xFFF);
}
else
printf ("value is divisible by 0x1000 (or 4096)\n");


In other words, this code checks if there are any bit set among lower 12 bits. As a side effect, lower 12
bits is always a remainder from division a value by 4096 (because division by 2 nis merely a right shift,
and shifted (and dropped) bits are bits of remainder).


Same story if you want to check if the number is odd or even:


if (value&1)
// odd
else
// even


This is merely the same as if to divide by 2 and get 1-bit remainder.


2.4.2 KOI-8R Cyrillic encoding


It was a time when 8-bitASCIItable wasn’t supported by some Internet services, including email. Some
supported, some others—not.


It was also a time, when non-Latin writing systems used second half of 8-bit ASCII table to accommodate
non-Latincharacters. TherewereseveralpopularCyrillicencodings, butKOI-8R(devisedbyAndrey“ache”
Chernov) is somewhat unique in comparison with others.


(^19) http://phrack.org/issues/60/10.html
(^20) https://yurichev.com/mirrors/SMT/z3prefix.pdf

Free download pdf