ASSEMBLY / CHAPTER 05
Arithmetic, masks, and shifts
MasterCompute with fixed-width bit patterns and make signedness assumptions explicit.
By the end of this chapter
- Trace modular register addition
- Use a mask to retain selected bits
- Distinguish logical from arithmetic right shift
The idea
Enter with hexadecimal notation and register widths understood. Integer register operations use a fixed number of bits. When an addition exceeds that width, the destination keeps the low bits and status flags describe relevant conditions. The same stored pattern can represent different signed and unsigned numbers. Choose an interpretation deliberately rather than expecting the register to remember a type.
Bitwise AND can isolate selected bits, OR can set them, and XOR can toggle them. A logical right shift inserts zeros; an arithmetic right shift copies the sign bit at the top. Their results differ for patterns with the high bit set. This chapter uses small constant shift counts, avoiding the separate rules for hardware masking of large counts. Track each operation at its specified width.
mov eax, 0x3D
and eax, 0x0F
shl eax, 1Final EAX = 26 (0x1A); no console output.
The mask retains the low hexadecimal digit D, which is thirteen. Shifting it left once gives twenty-six.
Try it yourself
Use AND to test whether the low bit of a register is set, without relying on decimal notation.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING