Wang Shuang-assembly language-comprehensive study three-using memory space

Source: Internet
Author: User

(i) Research overview

Not only can data be stored in registers, but it can also be stored in memory. This time we're going to study how to store data directly in memory in C language. As well as some of the extension issues of doing so. In addition, in the Appendix study, we also explored the implementation of cyclic and branching structures in C language.

(ii) The research process

1) Use the memory space directly in the C language

The words in the book are quoted here:

For storage space, to use they generally need to give two information: first, indicate the storage space, which is the information, and the second is to indicate how much storage space has type information.

For registers, you need to give the name of the register, and the name of the register contains their type information.

For memory space, you need to give the address (exactly, the first address of the memory space) and the type of spatial storage data.

we know that in C language, use pointer data to represent the address of memory space and the type of spatial storage data. For example, towrite a character ' a ' to a memory space where the offset address is 2000h and one byte is stored, we use the following method:

* (char * ) 0x2000 = ' a ';

The first one ' * ' indicates that a memory space is to be accessed;

"0x2000 "is a numeric value (0x represents hexadecimal), "(char * ) "Inside of the" * ' indicated this

numeric value represents a memory space address, "char "indicates that this address is stored in Char The memory space address of the type data.

of course, you can also use the method to give a segment address and offset address to access memory space, for example, we want to address : 0 , storing a byte of memory space to write the character ' a ', as follows:

* (char FAR * ) 0x20000000= ' a ';

" Far "indicates that the address of the memory space is a segment address and an offset address," 0x20000000 "in the" 0x2000 "Given the segment address," 0000 "gives an offset address.

Thus, we know the basic method of direct access to memory space in C language.

2) write a C language program that accesses memory directly

We write a program um1.c as follows:

The compile link is complete and the debug load is deserialized as follows:

3) Write a program that uses a C language to display a green character "a" in the middle of the screen

We write the source program as follows:

Compile the link to run with the following effect:

4) About global variables and local variables

We analyze the assembly code for all functions in the following program:

After compiling the link, debug loads, disassembled as follows.

Here, we are very intuitive to see. The global variables in the program are placed in the data section of the program, while the local variables are placed in their stack segments. This also illustrates the role of the push Bp;mov BP sp at the beginning of each function. We put the local variables in the stack, so we must find the variable smoothly when we use it. The use of the SP, if the program has a stack of operations, the stack top pointer changes, we can not find our stored variables. So using the BP register, the SP's value is given to BP at the beginning of the program, and then the value of the SP is increased, leaving the location of the local variable. At this point, we can easily use BP to find local variables, but also can be easily used in the function of the stack. When the function returns, the BP value is assigned to the SP, and the local variable within the function disappears. This is why the value of a local variable in C is only valid within the function.

5) Where is the return value of the C function stored?

We studied where the variables are stored, so where are the function return values of the C language stored? We write the following procedure.

Compile link after disassembly analysis:

We see the assembly instruction for function f () at 020A. The first five sentences, according to the previous content, we are easy to understand. But the difference is that one more mov ax,[01aa]. So what is the function of this statement? Will it be related to our function return value? is the function return value out of ax?

We debug run the program: G 021d, the results are as follows

As can be seen, the return value of the function is indeed outgoing by register AX.

6) Comprehensive analysis

We write a program like this:

First we want to understand the two statements of # define and malloc.

#define的作用, is a macro definition, simply speaking here is the ((char *) * (int FAR *) 0x02000000) This statement is replaced with buffer, and later when the use of this statement, the direct use of buffer can be.

(char *) * (int FAR *) 0x02000000) The surface is a char-type pointer that points to the 0200:0000 address.

The role of malloc (x) is to apply X-length memory.

Buffer= (char *) malloc (20) is a 20-byte memory unit that assigns the first address to the memory unit pointed to in buffer.

Understand this, we can understand the content of this C program

We compile the link, disassembled as follows:

We step into the implementation, found

It should be returned by a malloc application that is stored in ax. Should be our first address to apply for memory.

We continue to carry out

We found this address to be the offset address of the application memory returned by malloc.

(iii) Appendix Study

Note: The Appendix study has nothing to do with the use of memory cells, mainly to study three kinds of structures in C language. The sequential structure we can easily understand, the cyclic structure and the selection of branching structures are as follows:

1. Implementation of loop structure for statement in C language

What is the loop statement in C and how is the For statement implemented? We write the following procedure

We try to reduce the complexity of the problem so that it highlights the focus of our research. There is no other statement inside the loop, just an empty statement. is to make it easier for us to focus on the implementation of the For loop.

We compile the connection, after loading the disassembly view, as follows:

We can see that the for loop is using the SI register, first storing the SI into the stack, then emptying the SI, and then comparing the SI with our original values by constantly adding the value of SI.

So, if you add a statement to for, where does this statement add?

We analyze this statement in JMP 0203, which should be transferred to the loop. So, should be compared first, after executing the loop inside the statement? Or do you execute the intra-loop statement before comparing it?

We go on to analyze that the value of SI is starting from 0, and the last is compared with 5, which is less than the jump. If you compare and execute, the number of executions is 4 times, which is incorrect. So, it should be performed first, after comparison.

We verify that:

We are looking at JMP, this time direct jmp to 020B, really is each cycle first executed, after comparison. But at the same time, we find that when we enter the For loop, he jumps to the comparison statement. Why is that?

What happens if we write a C statement like this: for (i=0;i<1;i++)? If you do not compare first, the inside loop statement must be executed once. This is the reason why you must compare the first step into the for loop.

2. Implementation of branch structure if in C language

We write a program like this:

After compiling the connection, the load is viewed as follows:

We can intuitively see that the implementation of the selection branch of the program is still done using comparison statements such as CMP,JNZ.

So, referring to this study, we'll look back at our assembler program. We can use the assembly language more systematically, to realize the various options of the program, the loop structure.

(iv) Personal sentiment

What is the relationship between memory units, registers, and variables? This study gives a partial answer. The connotation of local variables, the connotation of global variables, the implementation of return values. The implementation of the compiled knowledge after the study of C language should have a more in-depth understanding and a more structured programming concept.

Wang Shuang-assembly language-comprehensive study three-using memory space

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.