Assembly Language Summary 03--contains multiple segments of the program

Source: Internet
Author: User

When we first wrote the assembler source program, there was only one code snippet, and now there is a question of where to use if the program needs to store the data in a different space. In summary the second mentioned that 0:200~0:2ff is relatively safe, but this space capacity is only 256 bytes, if we need more than 256 bytes of space to do?

In an operating system environment, the space that is legitimately obtained through the operating system is secure, because the operating system does not allow the space used by a program to conflict with other programs and the system's own space. With the operating system permitting, the program can take any amount of space.

There are two ways to get the required space, one is to assign the program when the program is loaded, and then to apply the program to the system during the execution.

We are only discussing the first method here.

If we want a program to make the required space when it is loaded, it must be explained in the source program. We get the memory space by defining segments in the source program.

Put data, code, stacks into different segments.

(i) Use of data in code snippets

The following 8 data are programmed and the results exist in the AX register:

0123h, 0456h, 0789h, 0abch, 0defh, 0fedh, 0cbah, 0987h

I wrote the following code:

Assume Cs:code

Code segment

DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h

MOV bx,0

MOV ax,0

MOV cx,8

S:add AX,CS:[BX]

Add bx,2

Loop s

MOV ax,4c00h

int 21h

Code ends

End

The test is as follows:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/92/09/wKiom1j6vQmAXOVIAAAz_PWA9ew696.png-wh_500x0-wm_ 3-wmp_4-s_2753185431.png "title=" 0.png "alt=" Wkiom1j6vqmaxoviaaaz_pwa9ew696.png-wh_50 "/>

The above assembly code is described as follows:

  1. The meaning of DW is to define the font data,DW is "define word", here using DW to define 8 font data, they occupy the size of memory space is 16 bytes.

  2. Where exactly are these 8 data? When the program is running, CS stores the segment address of the code snippet, so you can get their segment address from CS, what is the offset address? Since the data defined with DW is at the beginning of the code snippet, the offset address is 0, and the 8 data is at offset 0, 2, 4, 6, 8, A, C, and E of the code snippet. When the program runs, their addresses are cs:0, Cs:2, Cs:4, Cs:6, Cs:8, Cs:a, Cs:c, and Cs:e.

  3. With BX storage plus 2 increments the offset address, with the loop to accumulate. Before the loop starts, set (BX) =0,cs:bx points to the cell where the first data resides. In each loop (BX) = (BX) +2,cs:bx points to the cell where the next data resides.

  4. Through, we know ds=0b3a, in front I have mentioned for everyone, in the first 256 bytes of sa:0 for the PSP area , that is, sa+10h:0 Place to start to store code, the PSP area is DOS and programs to communicate, Specifically, you can learn how DOS works. So the value of CS is cs=0b4a.

  5. Why didn't you see the instructions in the program? In fact, the U command from 0b4a:0000 to see is also the contents of the program, but not the source program in the assembly instructions corresponding to the machine code, but the source program, we can use the D command to more clearly see the program in the first 16 bytes of content, as follows:

    650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/92/09/wKioL1j6wcaDkRAjAAAYuhKEMeU299.png-wh_500x0-wm_ 3-wmp_4-s_1783096967.png "title=" 1.png "alt=" Wkiol1j6wcadkrajaaayuhkemeu299.png-wh_50 "/>

    The machine instructions to be executed in the program can be viewed from 0b4a:0010, as follows:

    650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/92/09/wKiom1j6wiDBO9DfAAAUbIlEGi8808.png-wh_500x0-wm_ 3-wmp_4-s_3384365219.png "title=" 2.png "alt=" Wkiom1j6widbo9dfaaaubilegi8808.png-wh_50 "/>

    Is it the same as I wrote earlier?

Think about it, isn't it perfect? I personally think this is not the result I want. Why is it? Because the entrance to the program is not the instruction we want to execute. How can it be run directly in the system? We can indicate the entry of the program in the source program, and I will change the code as follows:

Assume Cs:code

Code segment

DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h

Start:mov bx,0

MOV ax,0

MOV cx,8

S:add AX,CS:[BX]

Add bx,2

Loop s

MOV ax,4c00h

int 21h

Code ends

End Start

I added the start label to the original base, notifying the compiler where the entry is. How does the compilation know which instruction is the first one to execute? The answer is the end start, which indicates the entry point of the program, is converted to an entry address, stored in the description of the executable file, and when loaded, the CS:IP is set so that the CPU starts executing at the address we want.

(ii) Use of stacks in code snippets

The following is a code to illustrate

Assume CS:CODESG

DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h

DW 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Start:mov Ax,cs

MOV Ss,ax

MOV sp,30h

MOV bx,0

MOV cx,8

S:push CS:[BX]

Add bx,2

Loop s

S0:pop CS:[BX]

Add bx,2

Loop S0

MOV ax,4c00h

int 21h

CODESG ends

End Start

(c) Put data, code, stacks into different segments

Further optimization of the above code

Assume Cs:code,ds:data,ss:stack

Data segment

DW 0123h,0456h,0789h,0abch,0defh,0cbah,0987h

Data ends

Stack segment

DW 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Stack ends

Code segment

Start:mov Ax,stack

MOV Ss,ax

MOV sp,20h

MOV Ax,data

MOV Ds,ax

MOV cx,8

S:push [BX]

Add bx,2

Loop s

MOV bx,0

MOV cx,8

S0:pop [BX]

Add bx,2

Loop S0

MOV ax,4c00h

int 21h

Code ends

End Start

Summary complete!

This article is from the "where No Play" blog, please be sure to keep this source http://liaofan.blog.51cto.com/12295212/1918409

Assembly Language Summary 03--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.