# Compilation experiment II

Posted by beckstei on Sun, 07 Nov 2021 20:16:37 +0100

empirical conclusion

assume ds:data, cs:code, ss:stack

data segment
db 16 dup(0)
data ends

stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax, data
mov ds, ax

mov ax, stack
mov ss, ax
mov sp, 16

mov ah, 4ch
int 21h
code ends
end start

(2)task1_1 screenshot before the end of line17 and line19

(3) Question answer ① in debug, execute until the end of line17 and before line19, and record this time: register (DS) =_ 076A___， Register (SS) =__ 076B__， Register (CS) =_ 076C___

② Assuming that the segment address of the code segment is X after the program is loaded, the segment address of the data segment is X_ X-2___， The segment address of stack is_ X-1___.

(debug shows that CS=076C in this question)

```assume ds:data, cs:code, ss:stack

data segment
db 4 dup(0)
data ends

stack segment
db 8 dup(0)
stack ends
code segment
start:
mov ax, data
mov ds, ax

mov ax, stack
mov ss, ax
mov sp, 8

mov ah, 4ch
int 21h
code ends
end start```

(2) task1_2 screenshot of observing the values of registers DS, CS and SS before debugging to the end of line17 and line19

(3) Question answer ① in debug, execute until the end of line17 and before line19. Record this time: register (DS) = 076a_, register (SS) = 076b_, register (CS) = 076c_

② Assuming that the segment address of the code segment is X after the program is loaded, the segment address of the data segment is _X-2 and the segment address of the stack is _X-1.

```assume ds:data, cs:code, ss:stack

data segment
db 20 dup(0)
data ends

stack segment
db 20 dup(0)
stack ends
code segment
start:
mov ax, data
mov ds, ax

mov ax, stack
mov ss, ax
mov sp, 20

mov ah, 4ch
int 21h
code ends
end start```

(2) task1_3 screenshot of observing the values of registers DS, CS and SS before debugging to the end of line17 and line19

(3) Question answer ① in debug, execute until the end of line17 and before line19. Record this time: register (DS) = 076a_, register (SS) = 076c_, register (CS) = 076e_

② Assuming that the segment address of the code segment is X after the program is loaded, the segment address of the data segment is _X-4 and the segment address of the stack is _X-2.

``` 1 assume ds:data, cs:code, ss:stack
2 code segment
3 start:
4     mov ax, data
5     mov ds, ax
6
7     mov ax, stack
8     mov ss, ax
9     mov sp, 20
10
11     mov ah, 4ch
12     int 21h
13 code ends
14
15 data segment
16     db 20 dup(0)
17 data ends
18
19 stack segment
20     db 20 dup(0)
21 stack ends
22 end start```

(2) task1_4 before the end of line9 and line11 debugging, observe the screenshot of the values of registers DS, CS and SS

(3) Answer to question ① in debug, it will be executed until the end of line9 and before line11. Record this time: register (DS) = 076c_, register (SS) = 076e_, register (CS) = 076a_

② Assuming that the segment address of the code segment is X after the program is loaded, the segment address of the data segment is _X+2 and the segment address of the stack is _X+4.

Based on the practice and observation of the above four experimental tasks, summarize and answer:

① For the segment defined below, after the program is loaded, the actual memory space allocated to the segment is _ N bytes.

xxx segment

db N dup(0)

xxx ends

② If the pseudo instruction end start in the programs task1_1.asm, task1_2.asm, task1_3.asm and task1_4.asm is changed to end, which program can still be executed correctly. Combined with the conclusions obtained from practical observation, analyze and explain the reasons.

A: after practice, only task1_4.asm can run correctly after the pseudo instruction end start is changed to end. Because end can not only inform the compiler of the end of the program, but also inform the compiler of the entry of the program. In the four programs in this question, the entry is at the label start; after removing start, the first three programs cannot find the entry, and only the code segment of the fourth program is at the beginning, which can be used To find the entrance.

(1) Assembly code

``` 1 assume  cs: code
2
3 code segment
4 start:
5     mov ax, 0b800h
6     mov ds, ax
7
8     mov bx, 0f00h
9     mov cx, 50h
10     mov ax, 0403h
11 s :
12     mov [bx], ax
14     loop s
15
16     mov ah, 4ch
17     int 21h
18 code ends
19 end start```

(2) Screenshot of operation results

(file name is 1.ASM)

The result is the same as that of task 3 of Experiment 1

(1) Assembly code

``` 1 assume cs:code ,ds:data1
2 data1 segment
3     db 50, 48, 50, 50, 0, 48, 49, 0, 48, 49 ; ten numbers
4 data1 ends
5
6 data2 segment
7     db 0, 0, 0, 0, 47, 0, 0, 47, 0, 0       ; ten numbers
8 data2 ends
9
10 data3 segment
11     db 16 dup(0)
12 data3 ends
13
14 code segment
15 start:
16 mov ax, data1
17 mov ds, ax
18 mov bx, 0
19 mov cx, 0ah   ;Cycle 10 times
20 s:
21 mov ax, [bx]
23 mov [bx+20h], ax
24 inc bx
25 loop s
26
27 mov ah, 4ch
28 int 21h
29 code ends
30 end start```

(2) Screenshot

It can be seen that although there are only 10 data, the program still allocates 16 byte space for data1 and data2, and the data of data3 is all 0

You can see that data1 and data2 are added to data3

(1) Code

``` 1 assume cs:code
2
3 data1 segment
4     dw 2, 0, 4, 9, 2, 0, 1, 9
5 data1 ends
6
7 data2 segment
8     dw 8 dup(0)
9 data2 ends
10
11 code segment
12 start:
13     mov ax, data1
14     mov ds, ax
15     mov bx, 000eh
16      mov cx, 8h
17      mov si, 2h
18 s:
19     mov ax, [bx]
20     mov  [bx+si], ax
22     sub bx, 2
23     loop s
24
25     mov ah, 4ch
26     int 21h
27 code ends
28 end start```

(2) Screenshot

Initial memory:

Memory space corresponding to data2 stored in reverse order:

You can see that the reverse order of data1 is stored in data2, indicating that the program is successful

Source code:

``` 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
25         loop s
26
27         mov ah, 4ch
28         int 21h
29 code ends
30 end start```

(1) line15-25, the functions of the loop are:

Convert each letter of Nuist to uppercase and assign the corresponding color

(2) Screenshot of operation results:

(3) Use the debug tool to debug the program, and use the g command to execute it once before the program returns (i.e. after ine25 and before line27):

(4) Function of line19 in source code:

Lowercase to uppercase

(5) What is the purpose of the byte data in the data segment line4 in the source code?

Change the value of 5 byte units in line4 to db 5 dup(2) operation result:

Change the value of 5 byte units in line4 to db 5 dup(5) operation result:

Function: set the color of NUIST

(1) Completion code:

``` 1 assume cs:code, ds:data
2
3 data segment
4     db 'Pink Floyd      '
5     db 'JOAN Baez       '
6     db 'NEIL Young      '
7     db 'Joan Lennon     '
8 data ends
9
10 code segment
11 start:
12    mov ax, data
13    mov ds, ax
14    mov bx, 0
15    mov cx, 4           ;Four strings
16    s0:
17     mov dx, cx
18    mov si, 0
19    mov cx, 4           ;Four letters
20    s:
21    mov al, [bx+si]
22    or al, 20h
23    mov [bx+si], al
24     inc si
25    loop s
26
27     mov cx, dx
29     loop s0
30
31    mov ah, 4ch
32    int 21h
33 code ends
34 end start```

(3) Use the d command to view a screenshot of the memory space corresponding to the data segment data:

Run debug until the program exits, and use the d command to view a screenshot of the memory space corresponding to the table segment:

You can see that the first word in each line of the data segment data has been changed from uppercase to lowercase.

(1) Write code:

``` 1 assume cs:code, ds:data, es:table
2
3 data segment
4     db '1975', '1976', '1977', '1978', '1979'
5     dw  16, 22, 382, 1356, 2390
6     dw  3, 7, 9, 13, 28
7 data ends
8
9 table segment
10     db 5 dup( 16 dup(' ') )  ;
11 table ends
12
13 code segment
14 start:
15
16     mov bp, 0                  ;table Starting address of each line
17     mov si, 0                 ;every time+2
18     mov di, 0                  ;every time+4,Year start
19     mov cx, 5
20     mov ax, data
21     mov ds, ax
22     mov ax, table
23      mov es, ax
24
25 s:
26     mov ax, ds:[bp]                    ;particular year
27     mov es:[di], ax
28     mov ax, ds:[bp+2]
29     mov es:[di+2], ax
30     mov ax, [si+014h]                   ;income si+014h
31     mov es:[di+5], ax
32     mov dx,[si+014h+2]
33     mov es:[di+7],dx
34     mov dx , [si+01eh]                   ;employee  si +01eh
35     mov es:[di+0ah], dx
36
37      mov dx, 0                                 ;per capita income
38     div word ptr [si+01eh]            ;dx*16+ax  /si+01eh
39     mov es:[bx+0dh], ax
40
41                                   ;Separate into spaces
42     mov byte ptr es:[bx+4],020h
43     mov byte ptr es:[bx+9],020h
44     mov byte ptr es:[bx+0ch],020h
45     mov byte ptr es:[bx+0fh],020h
46
50    loop s
51
52     mov ah, 4ch
53     int 21h
54 code ends
55 end start```

(2) Debug screenshot: view the screenshot of the original data information of the table segment. Run in debug until the program exits, use the d command to view the screenshot of the memory space corresponding to the table segment, and confirm whether the information is structurally written to the specified memory as required

The es segment address can be seen in debug: 076d

Original data information of table segment:

In debug, before the program exits, use the d command to view the memory space corresponding to the table segment:

empirical conclusion

(1) If the hexadecimal number is preceded by a letter, be sure to add 0; (error prone in writing!);

(2) In experiment task 6, at the beginning, there was no mov dx and cx in s0 (dx was used to temporarily store cx), and mov cx and dx in s (returned the cx of the external circulation) fell into a dead cycle;

(3) When writing, I found that I was not good at using stack segment related operations, which needs to be strengthened.