"Assembly Language" - Reading Notes - Experiment 5 Write and debug programs with multiple segments
Topic 1
Compile and link the following program, load and track it with Debug, and then answer the question
assume cs:code, ds:data, ss:stack
data segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h ; 占16字节
data ends
stack segment
dw 1,2,3,4,5,6,7,8 ; 占16字节
stack ends
code segment
start: mov ax, stack ; 获取栈段
mov ss, ax ; 设置栈段
mov sp, 16 ; 设置栈顶 ss:sp
mov ax, data
mov ds, ax ; ds 指向 data 段
push ds:[0]
push ds:[2]
pop ds:[2]
pop ds:[0]
mov ax, 4c00h
int 21h
code ends
end start
- The CPU executes the program. How much data is in the data segment before the program returns?
Answer: The data has not changed. First in, first out, order is maintained. - CPU executes the program, before the program returns, cs= 076E , ss= 076D , ds= 076C .
- After the program is loaded, the segment address of the code segment is X, then the segment address of the data segment is = X-2 , and the segment address of the stack segment is X-1 .
Topic 2
Compile and link the following program, load and track it with Debug, and then answer the question
assume cs:code, ds:data, ss:stack
data segment
dw 0123h,0456h
data ends
stack segment
dw 1,2
stack ends
code segment
start: mov ax, stack ; 获取栈段
mov ss, ax ; 设置栈段
mov sp, 16 ; 设置栈顶 ss:sp
mov ax, data
mov ds, ax ; ds 指向 data 段
push ds:[0]
push ds:[2]
pop ds:[2]
pop ds:[0]
mov ax, 4c00h
int 21h
code ends
end start
- The CPU executes the program. How much data is in the data segment before the program returns?
Answer: The data has not changed. First in, first out, order is maintained. - CPU executes the program, before the program returns, cs= 076E , ss= 076D , ds= 076C .
- After the program is loaded, the segment address of the code segment is X, then the segment address of the data segment is = X-2 , and the segment address of the stack segment is X-1 .
- For the section defined as follows: if the data in the section occupies N bytes, after the program is loaded, the space actually occupied by the section is bytes. I saw someone else's formula on the Internet , but when it is exactly a multiple of 16, the formula does not match the observed effect.
n % 16 ? n + (16 - n % 16) : n
( N/16+1) * 16
name segment
...
name ends
Observe that after the program is loaded , CX=0042 is the same as in Question 1. It can be seen that although only 4 bytes are declared, the system still allocates memory according to 16 bytes.
Can see 数据段
and 栈段
be 16字节
. 代码段
From 076C:0020 .
Look at the results of speculation through observation:
[...Array(9527).keys()].map(n => `${
n} = ${
n % 16 ? n + (16 - n % 16) : n}`)
Topic 3
Compile and link the following program, load and track it with Debug, and then answer the question
- The CPU executes the program. How much data is in the data segment before the program returns?
Answer: The data has not changed. First in, first out, order is maintained. - CPU executes the program, before the program returns, cs= 076C , ss= 0770 , ds= 076F .
- After the program is loaded, the segment address of the code segment is X, then the segment address of the data segment is = X+3 , and the segment address of the stack segment is X+4 .
Topic 4
If the last pseudo-instruction "end start" in questions (1), (2), and (3) is changed to "end" (that is, the entry point of the program is not specified), which program can still be executed correctly? Please explain reason.
Answer: The code in question 3 can be executed normally, because the code segment is right at the beginning of the program. CS:IP points to the first line of instruction.
Topic 5
The program is as follows, write the code in the code segment, add the data a
in the segment and b
the segment in turn, and store the result in c
the segment.
assume cs:code
a segment
db 1, 2, 3, 4, 5, 6, 7, 8 ; 占16字节
a ends
b segment
db 1, 2, 3, 4, 5, 6, 7, 8 ; 占16字节
b ends
d segment
db 0, 0, 0, 0, 0, 0, 0, 0 ; 占16字节
d ends
code segment
start: mov ax, a
mov ds, ax
mov bx, 0 ; i = 0
mov cx, 8 ; len = 8
s: mov al, ds:[bx]
add al, ds:[bx+16] ; 寄存器不够用,用偏移量来定位
mov ds:[bx+16+16], al ; 寄存器不够用,用偏移量来定位
inc bx ; i++
loop s ; i < len 循环
mov ax, 4c00h
int 21h
code ends
end start
c segment
It didn't compile, so I replaced it withd
- After the program is loaded, you can see that CS:IP points to 076F:0
[bx+16+16]
This way of writing will be introduced in detail in Chapter 7.
Topic 6
The program is as follows, write the code in the code section, use the push command to store the first 8 font data in the a section into the b section in reverse order.
assume cs:code
a segment ; CS-3
dw 1,2,3,4,5,6,7,8,9,0ah,0bh,0ch,0dh,0eh,0fh,0ffh ; 占32字节
a ends
b segment ; CS-1
dw 0,0,0,0,0,0,0,0 ; 占16字节
b ends
code segment
start: mov ax, a
mov ds, ax
mov ax, b
mov ss, ax
mov sp, 10h
; 下面实现循环处理
mov bx, 0 ; i = 0
mov cx, 8 ; len = 8
s: push [bx] ; a 中取出第一个,放到 b末尾。压栈是从底往上走的
add bx, 2 ; i = i+2 字类型占两字节
loop s ; i < len 循环
mov ax, 4c00h
int 21h
code ends
end start
Summarize
- The system allocates memory starting from 16 bytes.
- By defining a section, you can declare the content space. one thing.
- Knowing the definition order of the segments and knowing their sizes, it is possible to calculate the position of each segment through the offset.