Connection between memory management and C language in assembly language

Source: Internet
Author: User
C memory management has 1) variable management. Except for global variables and static variables (variables defined in any function do not appear in C language and static variables have static keywords), they are managed in the stack, go to the function to create these variables, and exit the function to destroy them. 2) function call, which is also in the stack. 3) memory dynamically allocated using the library function. The function in the C library actually calls the DOS function. The called interface is int 21 h, and the function number is placed in ax. I cannot remember the function number. For more information, see related technical guidelines. For 1) and 2), for example: Fu (Int J) {int I;} if Fu (2) is called, the assembly code is probably push sipush di, generally, function call does not save all registers. Push 2 calls parameters. Call the stack Fu. Call the sub-function mov BP sp. Save the stack pointer. sub sp 2sp is the stack pointer, And the stack is inverted, that is, from high to low. This operation is to leave space for the Fu variable I mov SP BP resume Stack pointer RET 2 This function parameter occupies two bytes, RET 2 is doing this correction. This is basically what it looks like. The assembly language also has memory allocation commands, which allocate the same memory as the global or static variables in the C language. We recommend that you take a look at how C language is compiled into an assembly language. For VC, you can press the CTR + function key or the ATL + function key. You can try it one by one and you will see the C language. If it is TC, you can find the TCC program in its directory and use TCC /? Let's see how to use it. It seems that the target file of TCC-s can be compiled into the int main (void) {long I, j, k; float FA, FB; /* t = gettickcount (); */for (I = 0; I <100000000; I ++) {J = 132; k = J * 10000; J + = K;}/* t = gettickcount ()-T; printf ("% LD \ n", I, T); t = gettickcount (); */For (I = 0; I <100000000; I ++) {fa = 132.321; Fb = fa * 10000.123; FA + = FB;}/* t = gettickcount () -T; printf ("% LD \ n", I, T); */} assembly code generated by Turbo C _ Mainprocnear near indicates that pushbp stores BP in the local process, because BP is sometimes used as the common variable movbp and SP to save sp. As mentioned above, subsp and 20 are used to allocate local variables ;? Debugl 8 movword PTR [bp-18], 0 pay attention to read, local variables are represented in [bp-18], which is why the just-in SP is saved movword PTR [bp-20], 0 jmpshort @ 5 @ 4 :;? Debugl 10 movword PTR [bp-14], 0 movword PTR [bp-16], 132 ;? Debugl 11 movdx, word PTR [bp-14] movax, word PTR [bp-16] xorcx, cxmovbx, 10000 pushcscallnear PTR lxmul @ movword PTR [bp-10], dxmovword PTR [bp-12], ax ;? Debugl 12 movdx, word PTR [bp-10] movax, word PTR [bp-12] addword PTR [bp-16], axadcword PTR [bp-14], DX @ 3: addword PTR [bp-20], 1 adcword PTR [bp-18], 0 @ 5: cmpword PTR [bp-18], 1525jl @ 4jne @ 10 cmpword PTR [bp-20],-7936jb @ 4 @ 10: @ 2 :;? Debugl 21 movword PTR [bp-18], 0 movword PTR [bp-20], 0 jmpshort @ 9 @ 8 :;? Debugl 23 movdx, 17156 movax, 21037 movword PTR [BP-6], dxmovword PTR [bp-8], ax ;? Debugl 24 flddword PTR [bp-8] fmulqword PTR dgroup: s @ fstpdword PTR [BP-4] fwait ;? Debugl 25 flddword PTR [bp-8] flddword PTR [BP-4] faddfstpdword PTR [bp-8] fwait @ 7: addword PTR [bp-20], 1 adcword PTR [bp-18], 0 @ 9: cmpword PTR [bp-18], 1525jl @ 8jne @ 11 cmpword PTR [bp-20],-7936jb @ 8 This example does not show function call, I happen to find

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.