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

Experimental task 1-1

Source code:

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

 

 

  Now go to line 17 after execution and line 19 before execution

ds=076c, ss=076d, cs=076e

ds=x-2h,    ss=x-1h,    cs=x

 

Task 1-2

Source code:

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

 

 ds=076c, ss=076d, cs=076e

ds=x-2h,    ss=x-1h,    cs=x

 

Task 1-3

Source code:

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

 

 ds=076c, ss=076e, cs=0770

ds=x-4h,  ss=x-2h,  cs=x

 

Tasks 1-4

Source code:

 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

 

 ds=076e, ss=0770, cs=076c

ds=x+2h,  ss=x+4h, cs=x

Tasks 1-5

 

ceil(N/16) N divided by 16 and rounded up

 

  task1_4.asm is executable

If start is not written, the program will execute from the beginning
When start is written, the program starts to execute from the start: label.

And Task1_ 4. The ASM start label is at the head of the program, so whether there is start after end is from the beginning to the end

 

Task 2

 

  This is the content of the memory segment before executing the program segment

code:

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

A few details: ax=0403, the title requirement is 0304 from low to high, so it should be reversed here; 160 bytes, fill two bytes each time, fill 80 times, cx=80 bx, increase 2 each time

 

 

  This is the content of the target memory unit after the program is executed, as shown in the figure above

 

 

Task 3

Supplementary code:

 1 assume cs:code
 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 ax, data2
18     ; mov ax, data3
19     mov ds,ax
20     mov bx,0
21     mov cx,10h
22     s:
23     mov ax,ds:[bx]
24     add ax,ds:[bx+10h]
25     mov ds:[bx+20h],ax
26     inc bx
27     loop s
28 
29     mov ah, 4ch
30     int 21h
31 code ends
32 end start

 

 

 

  In order to determine the location and size of each data segment, first check it with a tool. Can see

data1 start address 076c:0 length 16 bytes

data2 start address 076d:0, length 16 bytes

data3 real address 076e:0, length 16 bytes

 

  Disassembly screenshot

 

 

  This is the content of the memory unit before adding, and the data in it is hexadecimal

 

  This is the value of the memory cell after the addition

 

Task 4

data1 start address 076c:0 16 bytes

data2 start address 076d:0 16 bytes

stack address 076e:0

Full 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(?)
 9 data2 ends
10 
11 stack segment
12 stack ends
13 
14 code segment
15 start:
16     mov ax, data1
17     ; mov ax, data2
18     ; mov ax, stack
19     mov ds,ax
20     mov bx,0
21     mov sp,9    ;7+2
22     mov cx,8
23     a:
24     push [bx]
25     add bx,2
26     loop a
27 
28     mov ax,data2 
29     mov ds,ax
30     mov bx,0
31     mov cx,8
32     b:
33     pop [bx]
34     add bx,2
35     loop b
36 
37     mov ah, 4ch
38     int 21h
39 code ends
40 end start

Analysis: if you want to store in reverse order, you can easily think of using the stack structure, so you can construct a stack segment and associate it with ss.

Stack space is 0-7, sp points to 7 + 2 = 9.

First put 8 numbers into the stack, then change the address, and then out of the stack

  Disassembly  

 

  After exiting the stack, the contents of the data2 memory segment store the target number in reverse order

 

Task 5

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

The operation results are as follows:

 

As can be seen from the above table, the and operation of line19 changes the fifth digit of the character to 0 and converts the lowercase letter to the corresponding uppercase letter

 

 

  After changing to 5 dup(2), the output turns green. It can be seen that this section of memory data controls the display color

 

Task 6

The source code is as follows:

 

 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 
15     mov bx,0
16     mov cx,4
17     a:
18     mov di,cx
19     mov cx,4
20         b:
21         mov dl,[bx]
22         or dl,00100000b
23         mov [bx],dl
24         inc bx
25         loop b
26 
27     mov bx,0
28     inc ax  ;Go to the next line
29     mov ds,ax
30     mov cx,di
31     loop a
32 
33 
34     mov ah, 4ch
35     int 21h
36 code ends
37 end start

 

Check the memory before the program is executed

 

 

 

Then the program executes before returning

 

Quick execution to position 21

 

 

 

Then look at the memory

 

 

Analysis: there are four sentences in total. The first four letters of each sentence should be converted, so it is a 4 * 4 cycle

We use cx to record the loop, but there are two loops, so we need to save the outer loop count with di in the middle

Large to small, or 00100000b

Go to the next line. Because a line is 16 bytes, ax+1 is directly. Note that ax cannot be overwritten in the middle. So I replaced the middle register with dx.

 

 

Task 7

code:

 1 assume cs:code, ds:data, es:table
 2 
 3 data segment
 4     db '1975', '1976', '1977', '1978', '1979'
 5     ;20byte  4*5
 6 
 7     dw  16, 22, 382, 1356, 2390
 8     ;10byte  2*5
 9 
10     dw  3, 7, 9, 13, 28 
11     ;10byte  2*5
12 
13 data ends
14 
15 table segment
16     db 5 dup( 16 dup(' ') )  ;80byte
17 table ends
18 
19 code segment
20 start:
21     mov ax,data
22     mov ds,ax
23     mov ax,table
24     mov es,ax
25 
26     mov bx,0
27     mov dx,0
28     mov si,20
29     mov di,0
30 
31     mov cx,5
32     
33     s:
34     mov ax,[bx]
35     mov es:[di],ax
36     mov ax,[bx+2]
37     mov es:[di+2],ax
38     ;Year of storage
39 
40     mov ax,[bx+si]
41     mov es:[di+5],ax
42     ;Deposit of income
43 
44     mov dx,[bx+si+10]
45     mov es:[di+10],dx
46     ;Number of employees
47 
48     div byte ptr [bx+si+10]
49     mov es:[di+13],al
50     ;average income
51 
52 
53     add bx,4
54     sub si,2
55     add di,16
56 
57     loop s
58 
59 
60     mov ah, 4ch
61     int 21h
62 code ends
63 end start

 

 

This is the value of each memory unit before the program runs

 

 

  Analysis: there are five lines in total. You can do five cycles and read in one line of data each time.

There is a pit here. At first, I thought the distance between date and income and number of people was fixed, 20 bytes and 30 bytes, and then there was a division by 0 error. That is, the memory I accessed exceeded the original data area.

Then, in the process of debug ging repeatedly, I found that with the progression of the date, the distance between them and the corresponding income and number of people decreased by 2 bytes at a time. So I manually subtract two bytes each time to successfully execute the program

 

Posted on Sat, 06 Nov 2021 05:12:27 -0400 by Servyces