Assembly language Learning chapter sixth-programs with multiple segments

Source: Internet
Author: User

This blog series reference from << assembly Language >> third edition, author: Wang Shuang


There is only one code snippet in the previously described program. So what if we need to store the code in separate memory spaces? We know that we can't use any memory space, because our memory address space may store very important content. In fact, this is just too much to think about, once we load the program into memory, the operating system allocated for our program to run the memory space is safe, and will never overlap with the memory space of other programs.

Often the program gets memory in two ways: one is the memory space that the operating system has allocated when the program is loaded into memory, and the other is that we need to request additional memory space for related storage when the program is running. (Here's the second way for dynamic allocation we don't discuss for the moment)

We have had this experience before, we define a segment in the program, and then when the program loads into memory, the operating system automatically assigns us a memory segment for storage. On the other hand, we're talking about this, if our program includes not only code, but also data, and even the stack space we learned later. If we save this data to a paragraph, then obviously in the design and use of logic is not clear enough, often we are in the program to define a different field and in memory space to store the code, data and stack separately.


6.1 Using data in a code snippet

Here we first consider such a problem, we need to calculate the following eight numbers and:

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

Here we have to solve this problem need to store the above eight data into a register, and then use a register to save the cumulative result to accumulate. But this is obviously not smart enough, if you can use the loop is the best. So the question is, since we're going to use loops, we need to store these 8 data sequentially in a contiguous area of memory. What if we find a contiguous memory space? We can define a section of these 8 data in the program, and then when the program is compiled and linked into an executable file loaded into memory, the 8 data is stored in contiguous memory space, then we can use the loop to accumulate the sum.

The program code is as follows:


The main explanation of the program, wherein DW for defining font data define Word, here defines 8 font data altogether. Altogether 16 bytes. BX is used to cycle the index, each cycle of memory to move backwards two bytes, ax for the storage of cumulative results, CX for the loop counter, the initial value is 8, each cycle cx=cx-1. The program is very simple, I believe everyone can understand. Specifically, we define a code snippet.   Because the DW is stored at the beginning of the code snippet, the memory offset for the first font data is defined as 0. So its actual memory address is cs:0 Immediately after the 0456h, the two bytes followed are cs:2.

Next we save the program as VPEOT.ASM and compile the link as Vpoet.exe (the code in MOV ax,4c00h forget to add a comma, embarrassed ), and then use the Debug tool to load the program, as follows:



You can see from the CS=14FB that the program is executed from cs:0000, which is 14fb:0000. We use the U command to print the program instructions, and we found that it really started from 14fb:0000. However, the previous part is not the actual instruction of the program, we can boldly guess the previous content to store our defined DW data instructions, now we use the D command to view the memory contents of 14fb:0000.


You can see that the 16 bytes starting from memory 14fb:0000 are the 8 font data defined for us.

So if you want to see the actual assembly instructions you need to start from 14fb:0010:

\

That's right. After the program is loaded, the first 16 bytes of the entire program are the 8 font data we define. The latter only for our assembly instructions.

So if we want to execute our assembly instructions, we just need to set the value of the register IP to 0010. ‘

There is a problem here, if we do not use the Debug tool to change the IP instead of directly loading the program into memory, the program's entry address is not our assembly instructions, how to do?
We can add a label to the program to mark the entry of the program, such as:


Here, we put a label in front of the first assembly instruction start and also add a start after the pseudo-instruction end here the start is marked the entry of the program, indicating that once the program is loaded into memory, CS:IP will point to the first instruction MOV bx,0 We can compile the link again to deliver the EXE and then load it into memory through the Debug tool to confirm this.


Look first we see that the entry of the program has pointed to the first instruction, we do not need to change the IP address as before.


6.2 Using stacks in code snippets

Also look at a problem first, there are 8 of data stored in the memory of a contiguous space.

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

We need to programmatically implement these 8 data in reverse order.


The idea of this problem is that, assuming that the 8 data stored in cs:0~cs:f This memory space, we can use the characteristics of the stack storage space, the 8 font data is pressed into the memory space of the stack, and then popped into this cs:0~cs:f memory space, you can display the reverse storage of data. This idea requires us to define a memory space in the program as a stack space to use, the implementation code is as follows:

Assume Cs:codecode segment          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          mov bx,0          mov cx,8       s0:pop cs:[bx]          add bx,2          loop s0          mov ax,4c00h          int 21hcode endsend start

Here mov sp,30h need to be noted, because we first defined in the beginning of the program 8 glyphs for storing data that need to be stored in reverse order, while allocating 16 font data for stack space use, so a total of 24 glyphs, that is, 48 bytes of storage space. So the address of the SP to the top of the stack is: ss:ip=cs:30h.

Next we compile the link to the program, and debug is loaded into memory as follows:

It can be seen that assembly instructions are indeed starting from cs:0030.

6.3 Put data, code, stacks into different segments

In the previous program we all put the data, code, stack into a stack of space. The flaw is that the logic of the program is not clear enough, and the problem is that if the size of the code data and stack space exceeds the size of one segment, obviously they cannot be placed in a segment. So in the actual program design, we often define the code, the data and the stack separately into the different segment space.

We still use 6.2 of the problem to describe, this time we will code, data, stack space allocated to different segments, the program is as follows:

Assume Cs:code,ds:data,ss:stackdata segment          DW 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987hdata endsstack Segment          DW 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0stack endscode segment    Start:mov ax,stack          mov ss,ax          mov sp , 20h          mov ax,data          mov ds,ax          mov bx,0          mov cx,8        s:push [bx]          add bx,2          loop s          mov bx,0< C13/>mov cx,8       s0:pop [bx]          add bx,2          loop s0          mov ax,4c00h          int 21hcode endsend start

A description of the program:

1. The method of defining multiple segments is the same as the one previously defined, with different segments having different segment names.

2. A reference to the segment name. Now that you have defined different segments, how do you access them through the segment name? The paragraph name actually represents the segment address of the paragraph. For example, assume that the data segment of the 0ABCH address is data:6, to the memory of this address into BX:
MOV Ax,data

MOV Ds,ax

MOV bx,ds:[6]

3. Code snippet, data segment, stack segment is arranged by us. We define three segment names Data,code,stack is not a storage means that data must be stored, and code must store it. We can use data for storing code, and code for storing data is possible. In addition assume cs:code,ss,stack,ds:data is not so associated after CS points to the code snippet, or the SS points to the stack segment. And this is just the compiler let us associate the code segment with CS, that is, the segment address of the code segment is stored in CS. How the CPU handles the contents of our definition segment, whether as instruction execution or data access, is entirely up to the assembler itself and is determined by the value of the CS:IP,DS,SS:SP.


OK, here's what's going on in this chapter.


Assembly language Learning chapter sixth-programs with multiple segments

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.