Small Turtle 0 Basic assembly Language learning Note the sixth chapter contains multiple segments of the program

Source: Internet
Author: User

In the previous chapters, our program is just one code snippet, this chapter we begin to learn how to write a program that contains multiple segments. 1, using the data in the code snippet first consider such a problem, calculate the sum of the following 8 data, the results are stored in the AX register: 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h in the previous lesson, we all add up to a     Some of the data in the memory cells, not the data itself, but now we have to accumulate is the data given the value. The code is as follows:
1AssumeCS:CODESG2 CODESG Segment3 DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h4      movBx05      movAx06      movCx87 S:  AddAxCS:[BX]8      AddBx29 Loop STen      movax,4c00h One      int21h A CODESG ends -End
View Code

The meaning of "DW" in the first line of the program is to define the font data, that is, define word.     Here, we use DW to define 8 font data (separated by commas), which occupy 16 bytes of memory space. Compile the code, after the connection, debug with debug, we can see that in the address 0b4fh:0000 this address unit, the storage of our defined font data: In the 0b4fh:0010 beginning of the address unit, stored our instructions: But we view 0B 4f:0000 This address unit, it does not store our instructions (supposedly it will be stored in our instructions, but our instructions are stored in the 0b4fh:0010 start of the address unit): The reason is that the CPU reads the instructions corresponding to the machine code, from the 0b4f:0 000 The starting geological unit, stored in the data 0123h, 0456h and other pre-defined font data to the machine code corresponding to the instructions, which caused the CPU misreading. To solve this problem, we should assign a portal to the program instead of just starting with the instruction that defines the font data, so the program should look like this:
1AssumeCS:CODESG2 CODESG Segment3 DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h4 Start:movBx05         movAx06         movCx87  8 S:     AddAxCS:[BX]9         AddBx2Ten Loop S One   A    movax,4c00h -    int21h - CODESG ends theEnd Start
View Code

We recompile the connection to this program, and then using Debug debugging, using the R command to view the register, we can find that the CS:IP default point to the first instruction of the program, because we define a program in the program's entry, but the program is based on the final end to find the entry of the program, This should be a special reminder.     Debug Debug: 2, use the stack in the code snippet first of all, we still consider a problem, using the stack to store program-defined data in reverse order, the program's code should be how to write? When we see the reverse storage of such words, we should first think of the stack, should be implemented in reverse order, it will definitely use the stack "LIFO" this feature (LIFO). So the code for the program is as follows:
1AssumeCS:CODESG2 CODESG Segment3 DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h4Dw0,0,0,0,0,0,0,0 ;here the DW defines 8 font data, after the program is loaded, will get 8 words of memory space, storing the 8 data, we in the later program, the space as a stack to use. 5  6 Start:movAx,cs7         movSs,ax8         movSp + ;here set the stack top ss:sp point to cs:329         movBx0Ten         movCx8 One S:     Push CS:[BX] A         AddBx2 -Loop s;here is the 8-font data in the code snippet 0~16 unit, in turn, into the stack -   the         movBx0 -         movCx8 -  s0:  Pop CS:[BX] -         AddBx2 +Loop S0;this is where the stack of 8 font data is stacked into the code snippet 0~16 cell -   +         movax,4c00h A         int21h at   - CODESG ends -End Start
View Code

After compiling the connection, we use debug to see how the program works: we use the T and D commands to see what happens in the memory unit after executing the instruction: The D command is used to view the memory:

3, the data, code, stack into different groud in the previous program, we put the data, code, stack in the same paragraph, so that the program seems very confusing. If the program in the data, code, the stack is not a problem, but the program becomes larger, the program will become very difficult to see. In 8086CPU, if the space requirement of data, code and stack is greater than 64KB, the three cannot be stored in the same segment and need to be stored separately.     So how do you define multiple segments? We define multiple segments using the same method defined in the code snippet, defining the required data in these segments, or by defining the data to get the stack space. Now let's consider the same problem, storing the data in the stack in reverse order in a register, as shown in the following code:
1AssumeCS:Codeds:DataSS:Stack2  3 Data Segment4 DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h5 Data Ends6  7 Stack Segment8Dw0,0,0,0,0,0,0,09 Stack endsTen   One Code Segment A      Start:movAx,stack -              movSs,ax -              movSp - ;set the SS to point to the stack, set SS:SP point to STACK:16,CPU after executing these instructions, the stack segment is used as a stack space.  the              movAx,data -              movDs,ax;to DS:BX access to the data segment, DS will point to ax -              movBx0 -              movCx8 +         S:  Push[BX] -              AddBx2 + Loop S A   at              movBx0 -              movCx8 -       s0:   Pop[BX] -               AddBx2 - Loop S0 - code ends inEnd Start
View CodeIn the above code, we define the data segment, stack stack, code snippet, and then point to each of these segments with Ds,ss,cs.     In doing so, it seems that the whole program is very clear. How the CPU handles the content of the segment we define is executed as an instruction, as a data access, or as a stack space, and is determined entirely by the setup of Cs:ip, Ss:sp, DS, and so on, by the specific assembly instructions and assembly instructions in the program.

Small Turtle 0 Basic assembly Language learning Note the sixth chapter contains multiple segments of the program

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.