Linux Compilation example Explanation (ii)--Max

Source: Internet
Author: User

This article explains the use of assembly language to find the maximum value of a set of numbers, mainly related to the knowledge points have data segments, loops and so on.
# Purpose: Find the largest number in the program # # Variable: The purpose of the Register #       %edi-Save the data item index being checked #       %EBX-the largest item currently found #       %EAX-Current Data # # Use the following memory location: #       Data_ Items-Contains data item #                    0 indicates the end of #.section. Datadata_items:.long 3, 198, 1, 4, ten, 255, one, three, 0.section. Text.globl _start_start:        movl    ,%edi                   #将0移入索引寄存器        movl    data_items (,%edi,4),%eax  # Load the first byte of the data item        MOVL    %eax,%ebx                 #由于是第一项,%eax is the maximum value Start_loop: Cmpl $    ,%eax        JE      Loop_exit        incl    %edi        movl    data_items (,%edi,4),%eax        cmpl    %ebx,%eax        jle     Start_loop        movl    %eax,%ebx        jmp     start_looploop_exit:        movl    $,%eax        int     $ 0x80




Code Explanation:
1. The result of the code execution is:


echo $? The result of the output is 255 because in the cardinality array is: 3, 68, 12, 37, 198, 10, 1, 4, 26, 255, 11, 22, 33, 44 in this array, the largest number is 255, if 255 is removed or replaced by another number, the value will be output, for example We change the value to 168, then "echo $?" Should output 198:

The result of the program output is:


2. The structure of the code is analyzed from the beginning: (1) The comment begins with #, which is no longer explained (2)
. Section. Datadata_items:.long 3, 68, 12, 37, 198, 10, 1, 4, 26, 255, 11, 22, 33, 44, 0

The code defines the data to be used by the program, where Date_items is a label that will be replaced by the actual memory address. Long is the type of data, and a long type of data in a 32-bit system occupies 4 bytes (byte).     Long is followed by the actual number you want to use.     The main data types in assembly language:. Byte the number per byte type occupies one storage location, the numeric range 0~255. int each integer number (this type differs from the INT directive) occupies two storage locations with a numeric range of 0~65535. . ASCII this instruction is used to enter a string into memory. Each character occupies one storage location. (3)
_start:        movl $    ,%edi                   #将0移入索引寄存器        movl    data_items (,%edi,4),%eax  #加载数据项的第一个字节        movl    %eax,%ebx                 #由于是第一项,%eax is the maximum value

movl Data_items (,%edi,4),%eax This sentence is the first one to load the data item, because the value in the%edi register is 0, then the actual address is Data_items +%edi * 4--> Data_items + 0;< /c10>
(4)
start_loop: Cmpl $,%eax je loop_exit incl%edi movl data_items (,%edi,4),%eax Cmpl%ebx ,%eax jle start_loop movl%eax,%ebx jmp start_loop 


The segment code is a loop control structure. Where Start_loop is just a label that marks the starting position of the loop instruction. The statement "cmpl    $%eax" Determines whether the value in the register%eax is 0, and if 0 then jumps directly to the loop_exit instruction. If it is not 0, the execution continues down. This is because we use 0 as the end sign for all numbers, and of course we can change to something else. The statement "incl   %edi" means adding 1 to the value in the%edi register so that the next instruction ("movl    data_items (,%edi,4),%eax" ) can get the address of the next number, the statement "cmpl   %EBX,%eax" to compare the size of two numbers, the next statement "jle     Start_loop" Indicates that if the previous comparison result is a value in%eax less than or equal to the value in%EBX, the program jumps to Start_loop, that is, the start of the program loop, if it is not less than or equal to the case, the number in the%EAX is greater than the number in%EBX, the value in%eax needs to be put in% EBX registers, because the maximum value is returned as the status code when the program exits.

Linux Compilation example explanation (ii)--max value

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.