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

• ## Task 1-1

To program task1_1.asm assembles and connects, loads and tracks debugging with debug, and answers questions based on the results.

```assume ds:data, cs:code, ss:stack
data segment
db 16 dup(0)	; 16 byte units are reserved, and the initial values are 0
data ends
stack segment
db 16 dup(0)	;16 byte units are reserved, and the initial values are 0
stack ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 16   	; Set stack top
mov ah, 4ch
int 21h
code ends
end start
```

① 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.

• ## Task 1-2

To program task1_2.asm assembles and connects, loads and tracks debugging with debug, and answers questions based on the results.

```assume ds:data, cs:code, ss:stack
data segment
db 4 dup(0)	; Four byte units are reserved, and the initial value is 0
data ends
stack segment
db 8 dup(0)	; 8 byte units are reserved, and the initial values are 0
stack ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 8    	; Set stack top
mov ah, 4ch
int 21h
code ends
end start
```

① 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.

• ## Task 1-3

To program task1_3.asm assembles and connects, loads and tracks debugging with debug, and answers questions based on the results.

```assume ds:data, cs:code, ss:stack
data segment
db 20 dup(0)	; 20 byte units are reserved, and the initial values are 0
data ends
stack segment
db 20 dup(0)	; 20 byte units are reserved, and the initial values are 0
stack ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 20   	; Set initial stack top
mov ah, 4ch
int 21h
code ends
end start
```

① 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.

• ## Tasks 1-4

To program task1_4.asm assembles and connects, loads and tracks debugging with debug, and answers questions based on the results.

```assume ds:data, cs:code, ss:stack
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
data segment
db 20 dup(0)
data ends
stack segment
db 20 dup(0)
stack ends
end start
```

① In debug, execute until the end of line17 and before line19. 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.

• ## Tasks 1-5

Based on the practice and observation of the above four experimental tasks, it is summarized
① For the segment defined below, after the program is loaded, the actual memory space allocated to the segment is N/16 + 1.

```xxx segment
db N dup(0)
xxx ends
```

② 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

task1_4.asm can still be executed correctly, but the first three cannot. The end instruction not only declares the end of the program, but also indicates the entry address of the program, Task1_ 4. If ASM does not specify the program entry address, it still starts from start (program segment area), and the code header of the first three codes is not a program segment. The data runs as code, and there should be an error if there is no accident

Write an assembly source program to realize 160 consecutive bytes to memory units b800:0f00 ~ b800:0f9f, and fill hexadecimal data 03 and 04 repeatedly in turn.

```assume cs:code

data segment
db 80 dup(03h, 04h)
data ends

code segment

start:	mov ax, 0b800h
mov es,ax

mov ax, data
mov ds, ax

mov si, 0f00h

mov bx,0
mov cx,80
s:      mov ax, [bx]
mov es:[bx+si],ax
loop s

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

It is known that the 8086 assembly source program task3.asm code fragment is as follows.
requirement:
① The programming adds the data of logical segment data1 and logical segment data2 in turn, and the results are saved in logical segment data3.
② Load, disassemble and debug in debug. Before and after the data items are added in turn, view the three logical segments data1 respectively,
Confirm that the memory space corresponding to data2 and data3 is added one by one to ensure that the result exists in the logical segment data3.

```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 bx, 0	;Offset
mov dx, 0	;Summation register
mov cx, 10	;10 Secondary cycle

s:
mov dx, 0

mov ax, data1
mov ds, ax

mov ax, data2
mov ds, ax

mov ax, data3
mov ds, ax
mov [bx], dl

inc bx
loop s

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

Data1, data2 and data3 correspond to 076A, 076B and 076C respectively. It can be seen from the results in the figure that the data of 076A and 076B are added and placed in segment 076C

requirement:
① Complete the program to store the eight word data in logical segment data1 in reverse order in logical segment b.
② After assembly and connection, load the program in debug and run it to line15. Before the program exits, use the d command to view the memory space corresponding to data segment data2.

```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 ax, data2
mov ss, ax
mov ax, 16
mov sp, ax

mov cx, 8
mov bx, 0
s:
push [bx]
loop s

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

Before program execution:

After program execution:

```assume cs:code, ds:data
data segment
db 'Nuist'
; db 2, 3, 4, 5, 6
; db 5 dup(2)
db 5 dup(5)

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		; 11011111 Capitalize
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
```

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.

• Assemble and link the program to get the executable file, run and observe the results.

• Use the debug tool to debug the program and observe the results before the program returns, that is, after line25 and before line27.

• What is the function of line19 in the source code?

Change letters to uppercase and 11011111

• Modify the value of 5 byte units in line4, reassemble, link, run and observe the results.

```db 2, 3, 4, 5, 6
--> Change to:
db 5 dup(2) or db 5 dup(5)
```

After changing to db 5 dup(2), they all become the style of the first character: 00000010

After changing to db 5 dup(5), it becomes the style of the fourth character: 00000101

requirement:
① Complete the program and change the first word of each line in the data section from uppercase to lowercase.
② Load the program in debug, disassemble it, and check the memory space corresponding to the data section with the d command before exiting line13. Confirm that the first word of each = line has changed from uppercase to lowercase.

```assume cs:code, ds:data
data segment
db 'Pink Floyd      ' 	; 16 byte
db 'JOAN Baez       '  ; 16 byte
db 'NEIL Young      ' 	; 16 byte
db 'Joan Lennon     '	; 16 byte
data ends
code segment
start:
mov ax, data
mov ds, ax
mov cx, 4
mov bx, 0
upper_word:
mov dx, cx	; Storing outer loop cx
mov cx, 4
mov si, 0
upper_char:
or byte ptr [bx+si], 00100000b
inc si
loop upper_char
mov cx, dx

loop upper_word

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

Operation results:

```assume cs:codesg

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

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

codesg segment
start:

mov ax, data
mov ds, ax
mov ax, table
mov es, ax

;;;;;;;;;;;;;;;;;;;;Insert year;;;;;;;;;;;;;;;;;;;;
mov bx, 0 ; The first bx year
mov bp, 0 ; table Location in
mov cx, 5
lay_years:
;Internal circulation,Place a year
mov dx, cx	; Number of external cycles stored
mov cx, 4	; The length of the year is 4
mov si, 0	;Year No si character
lay_year:
mov al, ds:[bx+si]
mov es:[bp+si], al
inc si
loop lay_year
mov cx, dx

mov byte ptr es:[bp+4],' '  ;Put a space
add bp, 10h	; One line of 16 bytes
loop lay_years

;;;;;;;;;;;;;;;;;;;;Put in income;;;;;;;;;;;;;;;;;;;;
mov bp, 0
mov cx, 5
lay_incomes:
; font data (Double byte), It has to be moved twice
mov ax,ds:[bx]	; Due to continuous storage in data In, bx The pointer continues to accumulate
mov es:[bp+5],ax
mov ax,ds:[bx+2]
mov es:[bp+7],ax
mov byte ptr es:[bp+9], ' '  ;Put a space
loop lay_incomes

;;;;;;;;;;;;;;;;;;;;Number of employees;;;;;;;;;;;;;;;;;;;;
mov bp, 0
mov cx, 5
lay_populations:
mov ax, ds:[bx]	;Due to continuous storage in data In, bx The pointer continues to accumulate
mov es:[bp+0ah], ax
mov byte ptr es:[bp+0ch], ' '  ;Put a space
loop lay_populations

;;;;;;;;;;;;;;;;;;;;Find per capita income and put it in;;;;;;;;;;;;;;;;;;;;
mov bp,0
mov cx,5
lay_average_incomes:
mov ax, es:[bp+5]
mov dx, es:[bp+7]
div word ptr es:[bp+0ah]
mov es:[bp+0dh], ax
mov byte ptr es:[bp+0fh], ' ' ;Put a space