Implementation of stacks and memory operations in the Assembly

Source: Internet
Author: User

First, the learning process

To use a storage space in a computer, you must know two information: (1) where is the storage space? (2) How large the storage space is. For registers, it is only possible to give the name of the register, since each register is unique on the computer and the register size is specified. For memory space, the address and data type are given, and the data type represents the size of a storage space.

Used to learnClanguage, and did not study it carefully, the originalCThe language can also be used to write data directly to an inner cell in this syntax:*(char *)0x2000=' A ';The offset address is2000hmemory space to writeChartype of dataa. *(Char FAR *)0x20000000=' A ';to the segment address is2000h, the offset address is0000memory space to writeChartype of dataa. Comparing the above two methods can be found, the first one gives only the offset address, then where is the segment address, is the statement where the function of the segment address?

When we write a C program, we assign a value to the memory unit, and it is common to declare the variable to assign a value to the memory space, similar to assigning the offset address to the register in the assembly, and then assigning the data to the memory space represented by the Register, where the direct assignment Similar to the assembly in mov ds:[si],200h statement.

Write the program um1.c Compile the connection:

Use the U command to view the relevant code in debug :

Here I found that the compiler will compile the segment address to ES, offset address to BX, the results are stored in

ES:[BX].

After executing the first statement, the contents of the memory unit are:

After executing the second statement, the contents of the memory unit are:

After executing the third statement, the contents of the memory unit are:

After executing the fourth statement, the contents of the memory unit are:

After executing the fifth statement, the contents of the memory unit are:

After executing the sixth statement, the contents of the memory unit are:

Can be seen in the C language directly using the memory space operation and the memory unit address to register the memory space represented by the Register to operate is the same, and C language in the memory space of the address directly with the register add minus (such as * (char FAR *) (0X20001000+_BX) = * (char *) _ax;), and in statement * (char *) (_BX+_BX) = ' a '; the value of the register BX after execution has changed, that is, the latter statement * (char FAR *) (0X20001000+_BX) =* (char *) _ax; is to assign the values in the 0x2000 to 0x20003000 instead of 0x20002000.

To display a green character A in the middle of the screen, assign a to b800:[12*160+40*2] and assign 2 to b800:[12*160+40*2+1], but the book requires a C statement. A character A in the C language is a char type, a byte, an int type of two bytes, then we can convert 2 and character a into an int type data and then assign to b800:[12*160+40*2].

The result of the execution is correct.

Then look at the next program:

Compilation found there are many warnings, there is no need to tube.

After compiling the connection with the debug U command to see, found that the address of the A1 01A6,A2 for the 01a8,a3 for 01AA,B1, B2, B3 is not directly given, but by Bp-6, Bp-4, Bp-2, and at the beginning of the program to the SP's value gave BP,SP self-reduced by 6, Because the SP holds the stack top offset address, it is known that the program is actually B1, B2, B3 in turn into the stack. But at the end of the program has MOV bp,sp, the stack top pointer is restored, then B1, B2, B3 memory space is not in the stack, at any time can be overwritten by the data of other programs. It is known that the global variables are stored in the specified memory space, while local variables are stored in the stack and released at the end of the function life cycle. Because BP is used to record the SP's original position so that the SP is restored at the end of the function, the BP is protected so there will be push Bp;mov bp,sp.

Then look at the next program:

The watcher can find that the return value of the function f () is placed in ax. So what if the program has more than one return value to store? And if you are passing arguments to f () from the main function, is it also stored in AX? If the returned parameter is not of type int, but char or long int, what should be stored? Through the network access data, found that if it is 1 bytes of data, with Al storage, if it is 4 bytes of data, high 16 bits with DX pass, low 16 bits with ax pass.

Then look at the next program:

#define is a macro definition and is replaced with Buffer in the program ((char *) * (int FAR *) 0x02000000)

mallocis a memory space that opens up to five bytes.

I personally understand: here is a pointer to the opening of the memory space of the five bytes assigned to buffer, that is:0000~200 :The first address of the memory space that is opened up is 0001 . Buffer[10] is a counter where the a~h is saved to Buffer[0] to buffer[7 ] in the memory space.

Ii. problems to be solved

(1) C language directly using the memory space to operate and first the memory unit address to register and the register represents the memory space to operate the same, and C language can be the memory space address directly with the register add minus

(2) why * (char FAR *) (0X20001000+_BX) =* (char *) _ax; is to assign 0x2000 value to 0x20003000 instead of 0x20002000.?

A: In the statement * (char *) (_BX+_BX) = ' a '; the value of the register BX after execution has changed, for 0x2000.

(3) where does the C language store the global variables? Where are local variables stored? Each function starts with "push BP;mov bp,sp." What does it mean? "

A: Global variables are stored in the specified memory space, while local variables are stored in the stack and released at the end of the function life cycle. Because you want to use BP to record the SP's original position so that the SP is restored at the end of the function, you have to protect the BP so there will be

Push Bp;mov BP,SP.

(4) where does the C language place the return value of the function?

A: If it is 1 bytes of data, with Al storage, if it is 2 bytes of data, with Al storage, if it is 4 bytes of data, high 16 bits with DX pass, low 16 bits with ax pass.

Iii. Unresolved issues

(1)*(char *)0x2000=' A ' gives only the offset address, so where is the segment address? is the segment address of the function where the statement resides?

(2) function has multiple return values in the assembly how should it be stored?

(3) If the parameter is passed to F () from the main function, is it also stored in AX?

(4) How did the last program be implemented in the assembly?

Iv. Learning Impressions

In the research experiment, we look at the problem of C language from a compilation point of view, figuring out how some things in C are implemented in the compilation. But this involves some knowledge of C language, my practice is to check the information and consult others, but the answer is from the C language angle rather than the assembly angle, this does not give us to solve the problem to bring help. Sometimes a thing can look different from a different point of view, a problem that may not be understood from one point of view, and it might be easier to understand from another point of view. But if it takes a lot of time to figure out, but the current study does not greatly help the problem, it is possible to temporarily put aside to complete more important learning progress.

Implementation of stacks and memory operations in the Assembly

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.