Fifth - virtual machine opcodes 30 – 39
Table of Contents
1. 30: not
- Stack Effect:
n -- ~n - Description: Performs a bitwise NOT operation on the top value of the data stack.
- Notes: Inverts all bits of the dword at the top of the stack.
Example:
0x0000FFFF not
After execution, stack contains 0xFFFF0000.
2. 31: i
- Stack Effect:
-- r - Description: Pushes the top value from the return stack onto the data stack.
- Notes: Commonly used to access loop counters or subroutine parameters.
Example:
42 push i
After execution, data stack contains 42.
3. 32: cprt@
- Stack Effect:
port -- byte - Description: Reads a byte from the specified hardware I/O port.
- Notes: Uses IN instruction to read from the port. Replaces the port address with the read byte.
Example:
0x60 ; Keyboard port cprt@
4. 33: cprt!
- Stack Effect:
byte port -- - Description: Writes a byte to the specified hardware I/O port.
- Notes: Uses OUT instruction to write to the port. Consumes both the byte and port address.
Example:
0x42 ; Byte to write 0x60 ; Keyboard port cprt!
5. 34: i2
- Stack Effect:
-- r2 - Description: Pushes the second value from the return stack onto the data stack.
- Notes: Useful for accessing deeper items in the return stack without popping.
Example:
1 2 push push i2
After execution, data stack contains 1.
6. 35: i3
- Stack Effect:
-- r3 - Description: Pushes the third value from the return stack onto the data stack.
- Notes: Allows access to the third item in the return stack for deeper subroutine contexts.
Example:
1 2 3 push push push i3
After execution, data stack contains 1.
7. 36: shl
- Stack Effect:
value count -- result - Description: Shifts
valueleft bycountbit positions. Each bit position shifted left multiplies the value by 2. - Bits Shifted Out: Bits that are shifted beyond the most significant bit (bit 31) are discarded.
- Count Masking: Only the low byte of
countis used. On x86 processors, the actual shift amount is further masked to 5 bits (count & 0x1F), meaning the effective shift is always 0-31.
Corner Cases:
count = 0: Value unchanged.count >32=: Due to 5-bit masking, the effective shift iscount mod 32. For example, shift by 32 results in no change, shift by 33 shifts by 1.value = 0: Result is always 0 regardless of count.
Examples:
1 \ Value to shift 2 \ Shift amount (count) shl
Result: 4 (0b0001 << 2 = 0b0100).
FF \ Value: 255 (0x000000FF) 4 \ Shift by 4 shl
Result: FF0 (0x00000FF0 = 4080 decimal). The low 4 bits (F) were shifted up, and the upper bits remain zero. #+begin_example
8. 37: shr
- Stack Effect:
value count -- result - Description: Shifts
valueright bycountbit positions. Each bit position shifted right divides the value by 2 (integer division). - Bits Shifted Out: Bits that are shifted beyond the least significant bit (bit 0) are discarded.
- Zero Fill: This is a logical shift right (unsigned). Zeros are shifted in from the left (MSB side). This differs from an arithmetic shift right which would preserve the sign bit.
- Count Masking: Only the low byte of
countis used. On x86 processors, the actual shift amount is further masked to 5 bits (count & 0x1F), meaning the effective shift is always 0-31.
Corner Cases:
count = 0: Value unchanged.count >32=: Due to 5-bit masking, the effective shift iscount mod 32. For example, shift by 32 results in no change, shift by 33 shifts by 1.value = 0: Result is always 0 regardless of count.- Shifting by 31 always results in 0 (all bits shifted out).
Examples:
8 \ Value to shift 2 \ Shift amount (count) shr
Result: 2 (0b1000 >> 2 = 0b0010).
1 \ Value: only bit 0 set 1 \ Shift by 1 shr
Result: 0. The single bit was shifted out and discarded.
9. 38: or
- Stack Effect:
n1 n2 -- n1|n2 - Description: Performs a bitwise OR operation on the top two values.
- Notes: The top value (n1) is ORed with the second value (n2).
Example:
1 2 or
After execution, stack contains 3 (0b01 | 0b10).
10. 39: xor
- Stack Effect:
n1 n2 -- n1^n2 - Description: Performs a bitwise XOR operation on the top two values.
- Notes: The top value (n1) is XORed with the second value (n2).
Example:
1 2 xor
After execution, stack contains 3 (0b01 ^ 0b10).