The types and functions of left-shift and right-hand commands:
left shift instruction: the left shift command moves the bit bits of the operand to the left n bits, and the vacated bits are filled with 0.
The left-shift directive contains SAL and SHL, and the two instructions are the same, and the vacated bits are filled with 0.
where to move Sal's instruction usage:
. section. Text.global _start_start:movb $0b11111111,%al #8字节 salb $,%al movw $0b11111111,%ax #16字节 sal W $ $,%ax movl $0b11111111,%eax #32字节 sall $,%eax movq $0b11111111,%rax #64字节 Salq $ $,%rax
where the left-side of the SHL instruction is used:
. section. Text.global _start_start:movb $0b11111111,%al #1个字节 shlb $,%al movw $0b11111111,%ax #2个字节 SHLW $ $,%ax movl $0b11111111,%eax #4个字节 shll $,%eax movq $0b11111111,%rax #8个字节 SHLQ $,%rax
The right shift instruction functions: the right shift instruction shifts the bit bit of the operand to the right n bits, the SAR performs an arithmetic shift (fills in the sign bit), and the SHR performs a logical shift (fill in 0). The purpose of the shift operation can be a register or a memory location.
Right-shift command SAR and SHR
where the SAR's instruction usage is shifted right , the SAR right shift is populated with the sign bit, if the sign bit is 1, it fills 1, and if 0 fills 0.
. section. Text.global _start_start:movb $0b01111111,%al #符号位是0 sarb $,%al movb $0b11111111,%al #符号位是1 Sarb $ $,%al movw $0x7fff,%ax sarw $,%ax movw $0xffff,%ax sarw $ A,%ax movl $0x7fffffff,%eax s ARL $ $,%eax movl $0xffffffff,%eax sarl $,%eax movq $0x7fffffffffffffff,%rax Sarq $ A,%rax movq $0xffff FFFFFFFFFFFF,%rax Sarq,%rax
where right-shift SAR instruction usage, SHR isalways populated 0
. section. Text.global _start_start:movb $0b01111111,%al #符号位是0 SHRB $,%al movb $0b11111111,%al #符号位是1 shr B $,%al MOVW $0x7fff,%ax SHRW $,%ax movw $0xffff,%ax SHRW $,%ax movl $0x7fffffff,%eax Shrl $, %eax movl $0xffffffff,%eax shrl $,%eax movq $0x7fffffffffffffff,%rax SHRQ $ A,%rax movq $0xffffffffffff FFFF,%rax Shrq,%rax
The same type of operator in the C language:
Practice:
The exercises were taken from the 5th chapter of the C and the instructions and expressions
3. Write your function
unsigned int reverse_bits (unsigned int value);
The return value of this function is the value that transforms the bits mode of value from left to right. For example, on a 32-bit machine, 25 This value contains the following individual bits:
00000000000000000000000000011001
10011000000000000000000000000000
Assembly Code:
. section. Text.global reverse_bits.type reverse_bits, @functionreverse_bits: Xorl%eax,%eax movl $32,%ecxrever Se_bits_start:cmpl $,%ecx je reverse_bits_end shll $,%eax movl%edi,%esi Andl $,%esi orl%esi, %eax SHRL $ $,%edi decl%ecx jmp Reverse_bits_startreverse_bits_end:ret
C Test Code
#include <stdio.h> #include <assert.h>extern int reverse_bits (unsigned int ui); Int main () { unsigned u; u = reverse_bits (0x7fffffff); assert (U == 0xfffffffe); printf ("u (%x) \ n", u); u = reverse_bits (0X00FFFFFF); assert (U&NBSP;==&NBSP;0XFFFFFF00); printf ("U (%x) \ n", u); u = reverse_bits (0XFFFF00FF); assert (u == &NBSP;0XFF00FFFF); printf ("U (%x) \ n", u); u = reverse_bits (0XFFFFFF00); assert (U&NBSP;==&NBSP;0X00FFFFFF); printf ("u (%x) \ n", u); u = reverse_bits (0XFF00FFFF); assert (U&NBSP;==&NBSP;0XFFFF00FF); printf ("U (%x) \ n", u); return 0;}
4. Write a set of functions that implement a bit array. The prototype of the function should be as follows:
void Set_bit (char bit_array[], unsigned bit_number), void Clear_bit (char bit_array[], unsigned bit_number); void Assign_ Bit (char bit_array[], unsigned bit_number); int test_bit (char bit_array[], unsigned bit_number);
The 1th parameter of each function is an array of characters that is used to actually store all the bits. The 2nd parameter identifies the bits that need to be accessed. The caller of the function must ensure that this value is not too large to exceed the bounds of the array, the 1th function sets the specified bit to 1, and the 2nd function zeros the specified bits. If value is 0, the 3rd function sets the specified bit to 0, otherwise it is set to 1. As for the last function, if the bit specified in the argument is not 0, the function returns True or FALSE.
Assembly Code:
Assembly Summary: Left SHIFT, right shift instruction