"Assembly Language" - Reading Notes - Experiment 5 Write and debug programs with multiple segments

"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
  1. 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.
  2. CPU executes the program, before the program returns, cs= 076E , ss= 076D , ds= 076C .
  3. 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 .

insert image description here insert image description here

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
  1. 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.
  2. CPU executes the program, before the program returns, cs= 076E , ss= 076D , ds= 076C .
  3. 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 .

insert image description here

  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}`)

insert image description here

Topic 3

Compile and link the following program, load and track it with Debug, and then answer the question
insert image description here

  1. 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.
  2. CPU executes the program, before the program returns, cs= 076C , ss= 0770 , ds= 076F .
  3. 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 ain the segment and bthe segment in turn, and store the result in cthe 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

insert image description here

  1. c segmentIt didn't compile, so I replaced it withd
  2. After the program is loaded, you can see that CS:IP points to 076F:0
  3. [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

insert image description here

Summarize

  1. The system allocates memory starting from 16 bytes.
  2. By defining a section, you can declare the content space. one thing.
  3. Knowing the definition order of the segments and knowing their sizes, it is possible to calculate the position of each segment through the offset.

Guess you like

Origin blog.csdn.net/jx520/article/details/130868946