Machine-level representation of the C program-4

Source: Internet
Author: User

/*************************************** * *****/Text: machine-level representation of C program Author: Arden Chao Date: 2006-10-17 Email: arden1019@gmail.com version: 1.0.0 /************************************** * ***** // 2006-10-25 falls into the addressing mode ///////// //////// the addressing mode of the computer is very complicated (I think ). I am also in this situation. I will not write it today.

//// //-Process Control //////////////////

North cannot be found. I don't know how to proceed. Let's get familiar with the most basic process control today.

Int fun_int (int x, int y) {If (x> Y) x = x-y; else x = Y-X;

}

// Command: gcc-s asm03.c. file "asm03.c ". text. globl _ fun_int. def _ fun_int ;. SCL 2 ;. type 32 ;. endef_fun_int: pushl % EBP movl % ESP, % EBP movl 8 (% EBP), % eax CMPL 12 (% EBP), % eax jle L2 movl 12 (% EBP ), % eax subl % eax, 8 (% EBP) JMP l3l2: movl 8 (% EBP), % eax movl 12 (% EBP), % edX subl % eax, % edX movl % edX, % eax movl % eax, 8 (% EBP) L3: popl % ebp ret cmpl 12 (% EBP), % eax; this statement compares X and Y to jle L2. If y <X, it jumps to L2, which is exactly the same as the preceding two statements. The sentence completes the IF function. Speaking of this kind of program flow jump, we can divide it into two types, one is unconditional transfer, and the other is conditional jump. If is a conditional jump, and goto is unconditional. The following code shows the differences: // filename: asm04.c/* If and goto */INT fun_goto (int A) {if (a> 1) {A ++; goto end;} A --; end: return a;} // command: gcc-s asm04.c. file "asm04.c ". text. globl _ fun_goto. def _ fun_goto ;. SCL 2 ;. type 32 ;. endef_fun_goto: pushl % EBP movl % ESP, % ebp cmpl $1, 8 (% EBP) jle L2 incl 8 (% EBP) JMP l3l2: Decl 8 (% EBP) L3: movl 8 (% EBP), % eax popl % EBP ret. In fact, looking back at the two programs above, if can be expressed as follows :( From csapp) if (test-expr) then-statementelse else-statement ==> T = test-expr; If (t) goto true; else-statement goto done; true: then-statementdone: The following is a loop: in fact, the loop is basically a goto added to the IF statement. You don't need to elaborate on this. We have a special understanding of. For (init-expr; test-expr; Update-expr) body-statement; can be expressed using the following while: init-expr; while (test-expr) {body-statement; Update-expr;} is expanded to goto: init-expr; t = test-expr; If (! T) goto done; loop: Body-statement; Update-expr; t = test-expr; If (t) goto loop; done: by the way, let's look at a method to improve the efficiency of the for loop. see the following code: // filename: asm05.c/* Two for function */void fun_for1 (char * s) {int I; for (I = 0; I <strlen (s ); I ++) {s [I] = 0 ;}} void fun_for2 (char * s) {int I, j; j = strlen (s); for (I = 0; I <j; I ++) {s [I] = 0 ;}// command: gcc-s asm05.c. file "asm05.c ". text. globl _ fun_for1. def _ fun_for1 ;. SCL 2 ;. type 32 ;. en Def_fun_for1: pushl % EBP movl % ESP, % EBP subl $8, % ESP movl $0,-4 (% EBP) L2: movl 8 (% EBP ), % eax movl % eax, (% ESP) Call _ strlen CMPL % eax,-4 (% EBP) Jae L1 movl 8 (% EBP ), % eax addl-4 (% EBP), % eax movb $0, (% eax) Leal-4 (% EBP), % eax incl (% eax) JMP l2l1: leave ret. globl _ fun_for2. def _ fun_for2 ;. SCL 2 ;. type 32 ;. endef_fun_for2: pushl % EBP movl % ESP, % EBP subl $24, % ESP movl 8 (% EBP), % eax movl % eax ,( % ESP) Call _ strlen movl % eax,-8 (% EBP) movl $0,-4 (% EBP) L6: movl-4 (% EBP ), % eax CMPL-8 (% EBP), % eax jge L5 movl 8 (% EBP), % eax addl-4 (% EBP), % eax movb $0, (% eax) Leal-4 (% EBP), % eax incl (% eax) JMP l6l5: Leave ret. def _ strlen ;. SCL 3 ;. type 32 ;. endef we often use a loop like for (I = 0; I <strlen (s); I ++) in the code to traverse a string. However, in some cases, strlen calls may become a bottleneck for our program to run. Because the implementation of strlen in the standard library actually traverses strings. Code that calls strlen only once will run faster... (You can input a 65535-long string to the above function and print the time to see the result)

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.