Linux Platform x86 compilation (10): integer arithmetic

Source: Internet
Author: User
Tags arithmetic mul

"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
This section describes how to use integer arithmetic in the language, including addition, subtraction, multiplication, and division.
    • Addition instructions
The Add command is used to add two integers. The format is as follows:add src, destwhere SRC can be an immediate value, memory address, register. Dest can be a register or in-memory value and cannot use memory addresses as both source and destination. The results are stored in the dest. As with other GNU assembly directives, you need to add B, W, l at the end of the add to specify the length of the operand. If you do not use the entire register, make sure that the 0 fills the target register so that the register is high without content. The add instruction can perform addition operations on all signed integers. When performing integers like overtime, pay attention to the EFlags register, which is set to 1 when an add operation in the unsigned number causes the carry (the result is greater than the maximum allowed). The overflow flag is set to 1 when there is an overflow condition with a signed integer (the result is less than the minimum allowable negative value or greater than the allowable maximum value). The following example shows the addition of an unsigned integer in the case of rounding:
<span style= "Font-family:microsoft Yahei;" > #add1. s.section text.globl _start_start:    NOP    movl $,%ebx    movb $240,%BL movb $     ,%al#    Movb    $%al addb%al,%bl    JC over    MOVL $,%eax    int $0x80over: MOVL $    ,%eax    movl $,%ebx    in T $0x80</span>
If the addition causes rounding, the carry flag is set to 1 and the program returns 0, otherwise the result of the addition is returned. We modify the above code to cause rounding and non-rounding (uncomment lines and comment on one line of code) respectively, make, execute the result as follows:
<span style= "Font-family:microsoft Yahei;" >$ make As-o add1.o add1.s--gstabsld-o add add1.o$./add  $ echo $?0$ vim add1.s//Modify the code so that it does not carry the $ makes clean;makerm- F add1.o Add *~as-o add1.o add1.s--gstabsld-o add add1.o$./add $ echo $?242    //Non-rounding program returns addition result </span>
when you perform an unsigned integer addition, you should always check the carry flag, and if you know the bounds of the input value, you do not have to check the rounding flags. When working with signed integers, you do not need to judge the carry flag, but you need to pay attention to the overflow flag, which is set to 1 when the result overflows positive or negative bounds. The ADC instruction allows you to handle additions greater than the double-word data length.
    • Subtraction directives
subtraction is similar to the addition operation, the subtraction instruction is a sub, and the format is similar to the addition Directive:
Sub src, destThe subtraction instruction also needs to add B, W, l at the end of the sub. Like the Add Directive, the Sub directive modifies the bits of the EFlags register. use the Carry flag to determine the case where the unsigned integer subtraction produces a negative result. The subtraction of signed numbers relies on the overflow flag to determine the data length limits. the SBB directive is similar to the ADC directive, which can be used to help perform a subtraction operation of large unsigned integers.
    • Increment decrement Instruction
the INC directive and the DEC directive can increment and decrement unsigned integer values separately. These two instructions do not affect the carry flag. The instruction format is as follows:Dec destInc destThe dest can be a 8-bit, 16-bit, 32-bit register, and in-memory values.
    • Multiplication instructions
The mul instruction is used to multiply two unsigned integers in the following format:mul srcSRC can be 8, 16, 32-bit registers or memory values. When using the GNU assembler, the command mnemonic end is prefixed with the correct length character. The target operand of the directive is implied. Depending on the value length of the meta operand, the multiplication operation must be placed in an AL, Ax, eax register using another operand. Because multiplication can yield very large values, the MUL instruction target location must be twice times the length of the unary operand. The following table is required for unsigned integer multiplication:
source operand length (bit) target operand target location
8 al ax
16 ax dx:ax
32 EAX Edx:eax
The Imul command allows you to multiply a signed integer. The IMUL directive has three command formats:Imul src # This instruction format is the same as the use of Mul Imul src,dest #允许指定eax之外的目标操作数Imul Multipler, SRC, dest #multipler是立即值
    • Division Instruction
unsigned Division instruction: Div Divisorsigned Division instruction: Idiv Divisordivisor is the value that the implied dividend is to be divided by.
    • Shift Instructions
when writing high-level languages in peacetime, it is known that multiplication and division are very processor-intensive, and sometimes displacement is used instead of the multiplication method. As in assembly language, it is also possible to use shifts, which provide multiplication and division based on the powers of 2. To shift the binary number to the left 1 is multiplied by 2, the left 2 is multiplied by 4, and the shift operation is much faster than the binary multiplication operation.
shift left Command: Sal (arithmetic left) and SHL (logical left shift), these two instructions perform the same operation. There are 3 types of formats:sal Dest #左移一位, equivalent to multiplying by 2Sal%cl, dest #左移cl寄存器中的数Sal Shifter, dest #左移shifter值指定的数The left Shift command performs a left-shift instruction on unsigned and signed integers, 0 of the space, and bits beyond the length of the data are first stored in the carry flag and discarded during the next shift operation. Shift Right Command: SHR and SARThe shr shifts the unsigned integer, and the empty space generated by the right shift is filled with 0. The SAR command determines the number of padding for the position generated by the shift based on the integer sign bit, the negative vacancy fills 1, and the positive vacancy is 0. the right shift out of the data element is first moved to the carry flag, and then moved out, here and left to move the same.
    • Logic Operations Directives
Boolean logic operations: Add, not, or, XORand , or, the XOR instruction format:and SRC, destThe not directive uses a single operand. The most efficient way to clear a register is to use the XOR instruction to XOR the register and the user itself.
bit test instruction: Test src, destThe most common use of the test directive is to check for flags in the EFlags register.

Linux Platform x86 compilation (10): integer arithmetic

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.