Language learning Notes (iii)

Source: Internet
Author: User
Tags arithmetic bitwise

Eight, logical operation instruction

Numbers are stored in the computer as binary, with each bit number being 0 or 1, when two binary digits are logically bitwise &, logical bitwise |, logical XOR, or ^ operation,

Can use assembly language to provide logic operation instruction And,or, xor,not and other instructions.

and directives:

C Language & operation, 0110&1101 results to 0100

The C language code is

if (Flag & Maskit)   count+ +;

Assembly language advanced directives can be implemented:

mov eax, flag.if eax & Maskit Inc Count.endif

The following code can be used without the use of advanced directives:

if01:     mov eax, Flag            and eax, Flag           JZ endif01 Then01:   Inc Count endif01:  NOP

As with the CMP directive, the response bit of the EFlags register is set after the and operation is executed.

Similarly xor,not, or and other instructions will also be set eflags corresponding bit.

JZ instruction was introduced before, when the corresponding bit of the EFlags register is set to 0, the instruction will jump logic to the specified position.

Same zero? You can also determine if the corresponding bit of the EFlags register is set to 0, zero is true, and the reverse is false

mov eax, Flag  and eax, Maskit.if! ZERO? Inc Count.endif

or directive:

The or directive is the same as the and instruction usage

; flag = Flag | Maskit mov eax, Flag or eax, Maskit mov flag, EAX

XOR directive:

; flag = flag ^ Maskit; mov eax, Flag XOR eax, Maskit mov flag, EAX

The two parameter type limits for three directives are the same

and Mem, IMM

and Mem, Reg

and Reg, Reg

and Reg, IMM

and Reg, mem

Or, the XOR parameter type is the same as above and no three commands can directly manipulate two memory variables.

Nine , the logical shift instruction

SHL Reg, CL moves the data in Reg to the left by the number of digits in the value size of the CL register.

SHL Reg, IMM moves the data in Reg to the left by the number of digits of the IMM immediate count size.

Example:

mov 3 SHL Reg, CL; can also take immediate count SHL 3

The code above is to move the value of Reg to the left three bits, why use CL register? Because in the old

On 8086/8088 processors, the only immediate number that can be used in the operand is 1, if you call SHL reg,3

will be problematic, so the usual practice is MOV cl, 3 first move 3 to the CL register, and then call

SHL Reg, CL can complete the Reg value to move 3 bits to the left.

The parameters of SHL go out of Reg, also can be mem memory

SHL Mem, cl

SHL Mem, IMM

The same shr is moved to the right, with the same parameters as SHL.

If 10101010 calls SHL to the left, then the digits of all bits move left,

The No. 0 position bit digit number fills 0, the 7th bit digit 1 moves to the left into the CF carry Mark.

The number becomes 01010100.

Similarly, the SHR logical right Shift is also the 7th bit after the move to supplement 0, and the No. 0 bit digit is placed in the CF tag.

Write a piece of code to test the number of bits per bit digit 1.

mov 0 mov 8 mov temp, Al.repeat mov Ah, Al  and 00000001b . if! ZERO? Inc Count.endif SHR 1 . Untilcxz mov al, temp

Because the and instruction causes the first parameter value to be modified, the AL value is

Copy to AH. Finally, all the bits are processed and the temp data is copied back to al.

The test command avoids the problem of and modifying the value of the first parameter, as well as when the test command is used

The corresponding bits of the EFlags register are also set.

mov 0 mov 8 mov temp, Al.repeat Test 00000001b . if! ZERO? Inc Count.endif SHR 1 . Untilcxz mov al, temp

Ten , the logical shift instruction

Arithmetic left shift

SAL Reg, CL

Sal Reg, IMM

Sal, Mem, cl

SAL Reg, CL

Parameters are the same as SHL. The mobile effect, like SHL, is to move the low bit to the high bit, the highest bit

Move to the CF tag and fill 0 in the empty space.

Move:

If the sign bit is 0, then the left shift is just like SHL, no problem.

If the symbol bit is also the seventh bit 1, such as 1111 0000, move left one so the data is

1110 0000, the value of the left-hand operation is multiplied by 2, and negative numbers are stored in complementary form on the computer.

The complement of the whole number is the original code, the complement of the negative number is the integer original code to the end of the counter plus 1, 1111 0000-1 for

1110 1111, the bitwise inverse is 0001 0000, the value is decimal 16, so 1111 0000 is

-16,-16 multiplied by 2, that is, the left shift operation, is-32, then-32 in the binary how to express it?

32 is represented as 0010 0000 with 8-bit binary, negative number is positive the original code is reversed at the end of +1,0010 0000

Is 1101 1111, the end +1 is 1110 0000, 1110 0000 is exactly 1111 0000, and the left one gets the number.

Consider the question that if the number 10101010 left shifts in the figure, it will cause the number to change to 0101 0100, so

What happens when the highest bit becomes 0?

This is called a left-shift overflow, and the 8-bit binary can represent a binary number of 0111 11111~1111 1111.

0111 1111 means decimal 127

1111 1111 means decimal-1

1000 0000 is a negative complement, the number 1 inversion to get 1000 0000 This value is 128 original code

So 1000 0000 means-128,

Then the decimal number represented by the 8-bit binary can be 127~-128

10101010 indicates a negative number of-86, over-64.

Any more than 64 will cause the left shift overflow, the negative complement goes out the sign bit, the highest bit is 0, at this time the logical left movement causes the data overflow.

Arithmetic Right Shift

The arithmetic right shift holds the sign bit unchanged, the remaining bits move to the right, the bits that are vacated are 0, the rightmost bit displacement is removed and the CF tag is entered.

Calculate Product = num * 8; can be completed by shift instruction, faster

; Product = NUM1 * 8; mov eax, Num1 Sal 3 mov product, eax

Multiply by 8, not Sal eax, 8, this is multiplied by 256 and should be moved three bits.

Answer = AMOUNT/4;

; answer = AMOUNT/4; mov eax, Amount SHR 2 mov answer, EAX

11 . Cyclic SHIFT Instruction

Loop shift places the data that is removed at the end into a vacant bit at the other end.

Cyclic left shift

ROL Reg, CL

ROL Reg, IMM

Rol Mem, cl

Rol Mem, IMM

Loop Right Shift

ROR Reg, CL

ROR Reg, IMM

ROR Mem, cl

Ror Mem, IMM

Loop Right Shift

Cyclic left shift

The number of bits in a 8-bit binary number of bits 1 is detected before the program can be completed by looping right-shift,

Since the loop shifts 8 bits right, the result of the shift is the same as the initial value.

mov 0 mov 8 . Repeat Test 0000 0001b . if! ZERO? Inc Count.endif Ror 1 . Untilcxz

11 . Stack Operation

Push command

Push Reg

Push MEM

Push IMM

The push instruction is the stack instruction, the parameter can be register Reg, memory mem, the immediate number IMM.

Pop instructions

Pop mem

Pop reg

Pop out of the stack instruction, parameters can only be memory and register.

Combines a bit number with a bit value of 1 and a stack operation instruction

Push eax Push Al mov 0 mov 8 . Repeat Test 0000 0001b . if! ZERO? Inc Count.endif shr al,1. Untilcxzpop  alpop eax

12. Xchg Exchange Instruction

The XCHG exchange instruction is used to exchange two address space data, and the XCHG parameter can be

XCHG Reg, Reg

XCHG Reg, Mem

Xchg Mem, Reg

It is also not possible to operate two memory.

Achieve two number exchange

temp == = temp;

Use the MOV instruction to achieve:

mov edx, Num1 mov eax, num2 mov NUM1, eax mov num2, edx

With push, the pop instruction is implemented as

Push NUM1 Push num2 Pop    num1pop   num2

Use the XCHG directive to implement the

mov eax, Num1 Xchg eax, num2 mov  NUM1, EAX

Three methods of efficiency compared to the MOV command the fastest, followed by XCHG instructions, and finally the push pop instructions.

This article describes here, the next chapter about macros and procedures.

My public number:

Language learning Notes (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.