"Assembly Language" - Reading Notes - Chapter 6 - Programs Containing Multiple Segments
Data, code, and stack should be put into different segments.
The program can apply for memory space from the system when it is loaded and running.
It is safe to apply for memory from the system, and you should not directly write memory, which may destroy important data.
6.1 Using data in code snippets
Consider such a problem, programming calculates the sum of the following 8 data, and the result is stored in the ax register:
0123h, 0456h, 0789h, 0abch, 0defh, 0fedh, 0cbah, 0987h
declare in the code 数据段
, so that the program is loaded into the content, and the data legally obtains the memory space from the system.
Procedure 6.1
Without start to lead to 6.2, the program cannot be executed normally.
Procedure 6.2
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
dw
saiddefine word
. Define font data. A total of 8 are defined in the code, accounting for 16 bytes.add ax, cs:[bx]
: Because the data is placed in the code segment,CS
the segment address can be obtained from.- The data is at the beginning of the code section, so the offset starts at 0.
- Because yes
字类型
, every offset+2
. The offsets of the 8 data correspond to:0、2、4、6、8、A、C、E
- The label start is used to specify the program entry. That is,
CS:IP
the pointed position. Because we put a bunch of data at the beginning of the code segment, they cannot be executed normally.
Executable files are made up of 描述信息
and 程序
, CS:IP
where the pointer is in 描述信息
.
描述信息
It is obtained by compiling and linking the pseudo-instructions in the source program.
6.2 Using the stack in code snippets
assume cs:code
code segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h ; cs:00~0F
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; cs:10~2F
start: mov ax, cs
mov ss, ax
mov sp, 30h ; 栈顶指向CS:30,cs:2E + 2
mov bx, 0
mov cx, 8
s: push cs:[bx]
add bx, 2
loop s ; 以上将代码段 0~15 单元中的 8 个字型数据依次入栈
mov bx, 0
mov cx, 8
s0: pop cs:[bx]
add bx, 2
loop s0 ; 以上依次出栈 8 个字型数据到代码段 0~15 单元中
mov ax, 4c00h
int 21h
code ends
end start
It can be said that dw defines the data of xx words, or it opens up the memory space of xx words. Same effect.
6.3 Put data, code, stack into different segments
- Putting
数据
,代码
,栈
into a segment makes the program appear confusing; - If
数据
, ,栈
and代码
require more than 64KB of space, they cannot be placed in a segment. (The capacity of a segment cannot be greater than 64KB, which is the limitation of the 8086 model we used in our study, not all processors are like this).
Procedure 6.4
6.4 Implement the same function as 6.3, the difference is that the data, stack and code are placed in different segments.
assume cs:code, ds:data, ss:stack
data segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
data ends
stack segment
dw 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
stack ends
code segment
start: mov ax, stack ; 获取栈段
mov ss, ax ; 设置栈段
mov sp, 20h ; 设置栈顶 ss:sp 指向 stack:20h
mov ax, data
mov ds, ax ; ds 指向 data 段
mov bx, 0
mov cx, 8
s: push [bx]
add bx, 2
loop s ; 以上将 data 段中的 0~15 单元中的 8 个字型数据依次入栈
mov bx, 0
mov cx, 8
s0: pop [bx]
add bx, 2
loop s0 ; 以上依次出栈 8 个字型数据到 data 段的 0~15 单元中
mov ax, 4c00h
int 21h
code ends
end start
- The method of defining multiple segments: The method of defining a segment is the same as the method of defining a code segment mentioned above, but for different segments, different segment names are required.
- References to segment addresses:
段名
represented in the program段地址
. For example: send the segment addressmov ax,data
of the segment named .data
ax
代码段
,数据段
,栈段
It is completely our arrangement. What to use is completely determined by our settings forCS:IP
,SS:SP
, and other registers.DS
Experiment 5 Write and debug a program with multiple segments
"Assembly Language" - Reading Notes - Experiment 5 Write and debug programs with multiple segments