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

Posted by esukf on Mon, 08 Nov 2021 12:01:19 +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
```
##### task1_1 screenshot before the end of line17 and line19 ```1. stay debug Will execute to line17 End line19 Before, record this time: Register(DS) = 076A， register(SS)= 076B， register(CS) = 076C.
2. Suppose that after the program is loaded, code The segment address of the segment is X，Then, data The segment address of the segment is X-2， stack The segment address is X-1.
```

```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
```
##### task1_2. After debugging to the end of line17 and before line19, observe the screenshot of register DS, CS and SS values ```1. stay debug Will execute to line17 End line19 Before, record this time: Register(DS) = 076A， register(SS)= 076B， register(CS) = 076C.
2. Suppose that after the program is loaded, code The segment address of the segment is X，Then, data The segment address of the segment is X-2， stack The segment address 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
```
##### task1_3 screenshot before the end of line17 and line19 ```1. stay debug Will execute to line17 End line19 Before, record this time: Register(DS) = 076A， register(SS)= 076C， register(CS) = 076E.
2. Suppose that after the program is loaded, code The segment address of the segment is X，Then, data The segment address of the segment is X-4， stack The segment address is X-2.
```

```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
```
##### task1_4 screenshot before the end of line17 and line19 ```1. stay debug Will execute to line17 End line19 Before, record this time: Register(DS) = 076C， register(SS)= 076E， register(CS) = 076A.

2. Suppose that after the program is loaded, code The segment address of the segment is X，Then, data The segment address of the segment is X+2， stack The segment address 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+15) / 16 words (integer division).
```xxx segment
b 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. The ASM and cs registers are 076A. By default, the program entry can be found by starting from scratch.

#### Assembly source code

```assume cs:code

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

mov ax, 0403h
s:  mov [bx], ax
inc bx
inc bx
loop s

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

#### Screenshot of operation results (when a problem is encountered, the modification fails when viewing during debug)

#### 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, 10;

s:  mov al, [bx]
mov [bx + 32], al
inc bx
loop s

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

#### Screenshot after debugging #### Complete assembly source code

```assume cs:code

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

data2 segment
dw 8 dup(?)
data2 ends

stack segment
dw 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
stack ends

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

mov ax, stack
mov ss, ax

mov bx, 0

mov cx, 8
s1: push [bx]
loop s1

mov bx, 16
mov cx, 8
s2: pop [bx]
loop s2

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

#### Test screenshot ```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?

0dfh is binary 1101 1111. An and operation is performed to change the sixth bit of al to 0. Because the difference between upper and lower case ASCII codes is fixed, the upper case conversion can be completed.

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

Controls the generation of different word 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 si, 0

mov cx, 4
s1:
mov dx, cx
mov bx, 0

mov cx, 4
s2:
mov al, [si + bx]
or al, 020h
mov [si + bx], al
inc bx
loop s2
mov cx, dx

mov ax, si
mov si, ax
loop s1

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

#### In debug, the screenshots of loading, disassembly and debugging are required. Before the program exits, use the d command to view the screenshot of the memory space corresponding to the data segment data.

```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                ;aaaa-bbbb-cc-dd-
mov ds, ax                  ;0123456789abcdef
mov ax, table
mov es, ax

mov cx, 5
mov bx, 0
mov si, 0
mov di, 0
s:
mov ax, ds:[bx + 2]
mov es:[di + 2], ax
mov ax, [bx]
mov es:[di], ax

mov ax, [si + 20]
mov word ptr es:[di + 5], 0
mov word ptr es:[di + 7], ax

mov ax, [si + 30]
mov word ptr es:[di + 11], ax

mov ax, es:[di + 7]
div byte ptr es:[di + 11]

mov es:[di + 14], al

loop s

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

#### View screenshot of original data information of 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 