The assembly language uses memory space and the assembly language memory

Source: Internet
Author: User

The assembly language uses memory space and the assembly language memory

Memory space used by Assembly Language

In the previous experiment, we know that using registers in C language requires that the Register names be given, which can be one-to-one in the function execution process.
Next, we will study how to use the memory space in C language:
We already know how to write a byte in the image offset address 2000h. We use the following method:

 
 
  1. *(char *)0x2000=’a’;
  2. For example, to write a byte at, use the following method:
  3. *(char far *)0x20000000=’a’;


Experiment with the above two knowledge points:

1. Compile a program um1.c


Compile and generate the. exe file, and load it with debug to analyze the main function code.


We can find the compilation language corresponding to the c code.

Perform the following one-step tracking: Set cs to 0d6c ip to 01fa


We can find the assigned value in the corresponding place.
The following commands are similar to those above.

2. Compile a program and use a C language to display a green character 'a' in the center of the screen'


In the figure, 0b8000000 indicates the middle address of 12 rows after the first address of the monitor.
02 indicates that the property is green, and 61 indicates the character 'a '.

3. Analyze the following Assembly Code


Compile and link and use debug to load and view the corresponding assembly code

From the image code above, we can easily know that the C language places global variables in the Data Segment of ds: [01A6, the data storage address defined first is lower than the data defined later. The preceding a1 offset address is smaller than a2, and a2 is smaller than a3.
From the last two commands, we can see that the C language stores local variables in the stack segment and follows the sequence of Defined variables from top to bottom of the stack by b1, b2, b3, which variable is defined first, and which variable is placed in the lower address of the stack, that is, closer to the top of the stack.
As for the push bp mov bp and sp commands, we have already been involved in the previous learning to protect the values required by these return programs, this is because the value of bp may be damaged when stack is used later.

4. Analyze the assembly code of the following program and think about related issues


Compile and link the corresponding exe file with debug

First, we can see that the call 020a command is called, and find the 020a address to check the corresponding program. It is not difficult to find that the ax register is used in the code, so we can easily guess that the C language stores the return value in AX.

5. The following describes the usage of the malloc function.


We can understand the usage of the malloc function in C language.
Malloc (size );
Malloc requests the system to allocate a specified size of memory space.
Now let's analyze the program,

The first line indicates that when malloc applies for a space from the address 0200:0000 when applying for a space from the memory, this space enables our programmers to use a safe space.
Row 4: Apply for 20 char-type space using malloc, that is, 20 bytes
The fifth line indicates adding 10 to the base address of the buffer, that is, starting from the tenth address applied for, the value is assigned to zero.


The entire while LOOP below is to assign 'a'-'H' values to buffer [0] to buffer [7'
Buffer [10] acts as a counter. First, this counter is 0, and the address Buffer is assigned (61 h + 0), and then the counter is incremented by 1, then, assign (61 h + 1) to the address Buffer + 1, and then cyclically, so that Buffer [0] to Buffer [7] Stores 'A' B 'c'… in sequence '... 'H '.

@yujin753  2015-02-28 22:43  1228 words  Read 0 ×Loading Article image. Please wait...

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.