# Experiment 2 compilation and debugging of assembly source program of multiple logic segments

### empirical conclusion

• task1_1 screenshot before the end of line17 and line19

1. In debug, execute until the end of line17 and before line19. Record this time: register (DS) = 076A, register (SS)= 076B, register (CS) = 076C
2. 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-1h_， The segment address of stack is_ X-2h_. (db byte type)

• task1_2. After debugging to the end of line17 and before line19, observe the screenshot of register DS, CS and SS values

1. In debug, execute until the end of line17 and before line19. Record this time: register (DS) = 076A, register (SS)= 076B, register (CS) = 076C
2. 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-2h_， The segment address of stack is_ X-1h_.

• task1_3. Screenshot of the values of registers DS, CS and SS before the end of debugging to line17 and line19

1. In debug, execute until the end of line17 and before line19. Record this time: register (DS) = 076A, register (SS)= 076C, register (CS) = 076E
2. 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-4h_， The segment address of stack is_ X-2h_.

• task1_4. After debugging to the end of line17 and before line19, observe the screenshot of register DS, CS and SS values

1. In debug, execute until the end of line9 and before line11. Record this time: register (DS) = 076C, register (SS) =076E, register (CS) = 076A
2. 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+2h_， The segment address of stack is_ X+4H_
• Based on the practice and observation of the above four experimental tasks, summarize and answer:
1. For the segment defined below, after the program is loaded, the actual memory space allocated to the segment is an integer multiple of 16 (if the actual size is N and the loading size is m (M=n*16), then: M-16 < = N < = m).
2. If the program Task1_ 1.asm, task1_ 2.asm, task1_ 3.asm, task1_ 4. In ASM, if the pseudo instruction end start is changed to end, which program can still be executed correctly. The reasons are analyzed and explained in combination with the conclusions obtained from practical observation.

Only program 4 can execute correctly. Because start only identifies the address, and end start tells the compiler that start is the entry. If the end start in the last line is changed to end, the compiler will ignore start and write the default entry address into the executable description information. The default entry address is the first address (low address) of the three segment addresses codeseg+dataseg+stackseg. If the code segment (code) is already in the first segment when we encode, the compiler writes the first segment address (cs) into the executable description information without end start. However, if the stack/data segment is the first segment, the address where the executable description information is written is (ss)/(ds). If the data is parsed into instructions, the program will certainly not run normally, so only the code segment in front can run correctly

• Assembly source code
```assume cs:code

code segment
start:
mov ax, 0b800h
mov ds, ax
mov bx, 0f00h
mov cx, 50h

s:	mov ds:[bx], 0403h
loop s

mov ah, 4ch
int 21h
code ends
end start
```
• Screenshot of operation results

• Complete assembly source code
```assume cs:code
data1 segment
db 50, 48, 50, 50, 0, 48, 49, 0, 48, 49 ; ten numbers
data1 ends

data2 segment
db 0, 0, 0, 0, 47, 0, 0, 47, 0, 0       ; ten numbers
data2 ends

data3 segment
db 16 dup(0)
data3 ends

code segment
start:
mov ax, data1
mov ds, ax
mov bx, 0
mov cx, 0ah

s:	mov ax, ds:[bx]
mov ds:[bx+20h], ax
inc bx
loops

mov ah, 4ch
int 21h
code ends
end start
```
• Load, disassemble and debug screenshots in debug
Operation results:

Disassembly:

It is required to give the debug command and screenshot to view the original value of memory space data corresponding to logical segments data1, data2 and data3 before adding data items in turn

And, after adding in turn, view the debug command and screenshot of the original value of memory space data corresponding to logical segments data1, data2 and data3

• Complete assembly source code
```assume cs:code

data1 segment
dw 2, 0, 4, 9, 2, 0, 1, 9
data1 ends

data2 segment
dw 8 dup(?)
data2 ends

code segment
start:
mov ax, data1
mov ds, ax
mov sp, 9
mov bx, 0
mov cx, 8

s1:	push ds:[bx]
loop s1

mov ax, data2
mov ds, ax
mov bx, 0
mov cx, 8

s2:	pop ds:[bx]
loop s2

mov ah, 4ch
int 21h
code ends
end start
```
• Load, disassemble and debug screenshots in debug
It is required to give a screenshot of the memory space corresponding to data segment data2 by using the d command before the program exits.

result:

```assume cs:code, ds:data
data segment
db 'Nuist'
db 2, 3, 4, 5, 6
data ends

code segment
start:
mov ax, data
mov ds, ax

mov ax, 0b800H
mov es, ax

mov cx, 5
mov si, 0
mov di, 0f00h
s:      mov al, [si]
and al, 0dfh
mov es:[di], al
mov al, [5+si]
mov es:[di+1], al
inc si
loop s

mov ah, 4ch
int 21h
code ends
end start
```
• Screenshot of operation results
• Use the debug tool to debug the program, and use the g command to execute the screenshot before the program returns (i.e. after ine25 and before line27)
• What is the function of line19 in the source code?

Convert lowercase letters to uppercase letters

• What is the purpose of the byte data in the data segment line4 in the source code?

Try to modify the value of line4 and find that it stores codes of different colors

```assume cs:code, ds:data

data segment
db 'Pink Floyd      '
db 'JOAN Baez       '
db 'NEIL Young      '
db 'Joan Lennon     '
data ends

code segment
start:
mov ax, data
mov ds, ax

mov bx, 0
mov cx, 4
mov si, 0

s1:	mov di, cx
mov cx, 4
s2: 	mov al, [bx+si]
or al, 20h
mov [bx+si], al
inc si
loop s2

mov si, 0
mov cx, di
loop s1

mov ah, 4ch
int 21h
code ends
end start
```
• Load, disassemble and debug screenshots in debug
Disassembly
• It is required to give a screenshot of the memory space corresponding to the data segment data by using the d command before the program exits

```assume cs:code, ds:data, es:table

data segment
db '1975', '1976', '1977', '1978', '1979'
dw  16, 22, 382, 1356, 2390
dw  3, 7, 9, 13, 28
data ends

table segment
db 5 dup( 16 dup(' ') )  ;
table ends

code segment
start:
mov ax, data
mov ds, ax
mov ax, table
mov es, ax
mov cx, 5
mov bx, 0
mov di, 0

s:     	mov al, ds:[bx]
mov es:[di], al

mov al, ds:[bx + 1]
mov es:[di + 1], al

mov al, ds:[bx + 2]
mov es:[di + 2], al

mov al, ds:[bx + 3]
mov es:[di + 3], al

loop s

mov cx, 5
mov di, 5

s1:    	mov al, ds:[bx]
mov es:[di], al

mov ah, ds:[bx + 1]
mov es:[di + 1], ah
loop s1

mov cx, 5
mov di, 7

s2:    	mov al, 0
mov es:[di], al
mov es:[di+1], al
loop s2

mov cx, 5
mov di, 10

s3:     	mov al, ds:[bx]
mov es:[di], al
mov ah, ds:[bx+1]
mov es:[di+1], ah
loop s3

mov cx, 5
mov di, 13
mov si, 5
s4:    	mov al, es:[si]
mov ah, es:[si+1]
mov dl, es:[si+2]
mov dh, es:[si+3]
div word ptr es:[si+5]
mov es:[di], ax