Experiment 3 compilation and debugging of multi segment assembly source program

1. Experimental task 1

Source code file Task1 asm:
 1 assume cs:code, ds:data
 2 data segment
 3         db 'Nuist'
 4         db 2,3,4,5,6
 5 data ends
 6 
 7 code segment
 8 start:
 9         mov ax, data
10         mov ds, ax
11 
12         mov ax, 0b800H
13         mov es, ax
14 
15         mov cx, 5
16         mov si, 0
17         mov di, 0f00h
18 s:      mov al, [si]
19         and al, 0dfh
20         mov es:[di], al
21         mov al, [5+si]
22         mov es:[di+1], al
23         inc si
24         add di, 2
25         loop s
26 
27         mov ah, 4ch
28         int 21h
29 code ends
30 end start
1) Read the source program, theoretically analyze the functions of the source code, especially line15-25. What are the functions realized by the loop, and understand the functions of each instruction line by line.
 
The main function of the source code: convert the first five byte units "Nuist" of the data segment into uppercase, and save the data in the last five byte units of the data segment together to the additional segment for display on the screen.
 15        mov cx, 5    //Set the number of cycles to 5
 16        mov si, 0    //Send 0 to si
 17        mov di, 0f00h    //Set the starting memory unit address
 18s:      mov al, [si]    //Send the memory unit [si] of the first five bytes of the data segment to al
 19        and al, 0dfh    //Convert letters to uppercase
 20        mov es:[di], al    //Save results to additional segments
 21        mov al, [5+si]    //Point the pointer to the last five bytes of memory[5+si]
 22        mov es:[di+1], al    //Save the corresponding data in the last five bytes of the data segment after the capital letter
 23        inc si    //si plus 1
 24        add di, 2    //di plus 2
 25        loop s    //loop
2) Use masm and link for Task1 ASM assembles and links to get the executable file Task1 Exe, run and observe the results.
3) Use the debug tool to debug the program, and observe the results before the program returns, that is, before line27.
4) Modify the value of 5 byte units in line4, reassemble, link, run and observe the results.
The modified source code is as follows:
 1 assume cs:code, ds:data
 2 data segment
 3         db 'Nuist'
 4         db 2,3,4,5,6
 5 data ends
 6 
 7 code segment
 8 start:
 9         mov ax, data
10         mov ds, ax
11 
12         mov ax, 0b800H
13         mov es, ax
14 
15         mov cx, 5
16         mov si, 0
17         mov di, 0f00h
18 s:      mov al, [si]
19         and al, 0dfh
20         mov es:[di], al
21         mov al, [5+si]
22         mov es:[di+1], al
23         inc si
24         add di, 2
25         loop s
26 
27         mov ah, 4ch
28         int 21h
29 code ends
30 end start

Reassemble, link and run again, and the results are as follows:

 

 

5) Based on observation, analyze and guess the numerical function here.

Analysis may be used to change the color of the display.
 

2. Experimental task 2

The byte data defined in the known data segment data is as follows:
data segments
db 23,50,66,71,35
data ends
Write a program to print out these five two digits in the form of decimal integers on the screen.
1) The source code is as follows:
 1 assume cs:code,ds:data
 2 data segment
 3     db 23,50,66,71,35
 4 data ends
 5 code segment
 6 start:
 7     mov ax,data
 8     mov ds,ax
 9     mov di,0
10     mov cx,5
11 
12 s1:    mov ah,0
13     mov al,ds:[di]
14     mov bl,10
15     div bl
16     mov ds:[10+di],al
17     mov ds:[11+di],ah
18 
19     mov ah,2
20     mov dl,ds:[10+di]
21     add dl,30h
22     int 21h
23 
24     mov ah,2
25     mov dl,ds:[11+di]
26     add dl,30h
27     int 21h
28 
29     mov ah,2
30     mov dl," "
31     int 21h
32     
33     inc di
34     loop s1
35 
36     mov ax,4c00h
37     int 21h
38 code ends
39 end start

2) The screenshot of the operation result is as follows:

3. Experimental task 3

The source code is as follows:
 1 assume cs:code, ds:data, ss:stack
 2 data segment
 3   dw 0123h, 0456h, 0789h, 0abch, 0defh, 0fedh, 0cbah, 0987h
 4 data ends
 5 
 6 stack segment
 7   dw 0, 0, 0, 0, 0, 0, 0, 0
 8 stack ends
 9 
10 code segment
11 start:  mov ax,stack
12         mov ss, ax
13         mov sp,16
14         
15         mov ax, data
16         mov ds, ax
17         
18         push ds:[0]
19         push ds:[2]
20         pop ds:[2]
21         pop ds:[0]
22         
23         mov ax,4c00h
24         int 21h
25 
26 code ends
27 end start
Answer the three questions ① ② ③ in the textbook.
① : disassemble to view the screenshot of the data in the data section. Verify whether the observed results are consistent with the theoretical analysis.
1) Before the CPU executes the program and the program returns, the data in the data section is 0123H,0456H,0789H,0ABCH,0DEFH,0FEDH,0CABH,0987H.
2) View after compilation and link:

3) Found after anti remittance compilation:

② : disassembly debugging screenshot, that is, the debugging screenshot from the execution to the return of the program in debug.

It can be seen from the above figure that the CPU executes the program. Before the program returns, cs=076CH, ss=076BH, ds= 076AH.

③ : after the program is loaded, the segment address of the code segment is x, the segment address of the data segment is X-2, and the segment address of the stack segment is X-1.

4. Experimental task 4

The source code is as follows:
 1 assume cs:code, ds:data, ss:stack
 2 data segment
 3   dw 0123h, 0456h
 4 data ends
 5 
 6 stack segment
 7   dw 0, 0
 8 stack ends
 9 
10 code segment
11 start:  mov ax,stack
12         mov ss, ax
13         mov sp,16
14         
15         mov ax, data
16         mov ds, ax
17         
18         push ds:[0]
19         push ds:[2]
20         pop ds:[2]
21         pop ds:[0]
22         
23         mov ax,4c00h
24         int 21h
25 
26 code ends
27 end start
Answer the four questions ① ② ③ ④ in the textbook.
① The CPU executes the program. Before the program returns, the data in the data section is 0123H,0456H
②; Disassembly debugging screenshot, that is, the debugging screenshot from the execution to the return of the program in debug.
The CPU executes the program. Before the program returns, cs=076CH, ss=076BH, ds=076AH.
 
③ After the program is loaded, the segment address of the code segment is x, the segment address of the data segment is X-2, and the segment address of the stack segment is X-1.

④ : for the following definition segments:

name segment
...
name ends

If the data in the segment occupies N bytes, the actual space occupied by the segment is (N/16+1) * 16 after the program is loaded

5. Experimental task 5

The source code is as follows:
 1 assume cs:code, ds:data, ss:stack
 2 
 3 code segment
 4 start:  mov ax,stack
 5         mov ss, ax
 6         mov sp,16
 7         
 8         mov ax, data
 9         mov ds, ax
10         
11         push ds:[0]
12         push ds:[2]
13         pop ds:[2]
14         pop ds:[0]
15         
16         mov ax,4c00h
17         int 21h
18 
19 code ends
20 data segment
21   dw 0123h, 0456h
22 data ends
23 
24 stack segment
25   dw 0,0
26 stack ends
27 end start
Answer the three questions ① ② ③ in the textbook.
① : the CPU executes the program. Before the program returns, the data in the data section is: 0123H,0456H

② Disassembly debugging screenshot, that is, the debugging screenshot from the execution to the return of the program in debug.

 

The CPU executes the program. Before the program returns, cs=076AH, ss=076EH, ds=076DH.

③ After the program is loaded, the segment address of the code segment is X, the segment address of the data segment is X+3, and the segment address of the stack segment is X+4.

6. Experimental task 6

If the "end start" of tasks 3 to 5 is changed to "end", which program can execute normally? Why?

A: only the assembler of task 5 can execute normally. If the entry location is not specified, the program will be executed from the allocated space. Since the beginning of tasks 3 and 4 is a data segment, it will be read in as an instruction; The code segment of task 5 program is defined before the data segment and stack segment. In the process of sequential execution, the code segment will be executed first, so that the program will not be executed normally due to the execution of data segment and stack segment.

7. Experimental task 7

The source code is as follows:
 1 assume cs:code
 2 a segment
 3   db 1,2,3,4,5,6,7,8
 4 a ends
 5 
 6 b segment
 7   db 1,2,3,4,5,6,7,8
 8 b ends
 9 
10 c segment   ; In the integrated software environment, please change the segment name here from c→Change to c1 Or other name
11   db 8 dup(0)
12 c ends
13 
14 code segment
15 start:
16     mov ax,a
17     mov ds,ax
18     mov ax,b
19     mov es,ax
20     mov ax,c
21     mov ss,ax
22     mov bx,0
23     mov cx,8
24 s:    mov ax,[bx]
25     mov ss:[bx],ax
26     mov ax,es:[bx]
27     add ss:[bx],ax
28     inc bx
29     loop s
30     mov ax,4c00h
31     int 21h
32 code ends
33 end start
Attached in the debug environment, before the program returns, view the screenshot of the data of logic segment c, so as to verify that the written program correctly meets the requirements of the topic.

Disassembly:

Before the program returns, check the data of logical segment c:

8. Experimental task 8

The source code is as follows:
 1 assume cs:code
 2 a segment
 3   dw 1,2,3,4,5,6,7,8,9,0ah,0bh,0ch,0dh,0eh,0fh,0ffh
 4 a ends
 5 
 6 b segment
 7   dw 8 dup(0)
 8 b ends
 9 
10 code segment
11 start: 
12     mov ax,a
13     mov ds,ax
14     mov ax,b
15     mov bx,0
16     mov ss,ax
17     mov sp,16
18     mov cx,8
19 s:     push [bx]
20     add bx,2
21     loop s
22     mov ax,4c00h
23     int 21h
24 code ends
25 end start
Attached in the debug environment, before the program returns, view the screenshot of the data of logic segment c, so as to verify that the written program correctly meets the requirements of the topic.

 

 

 

Posted by sprinkles on Fri, 06 May 2022 03:22:16 +0300