Assembly language (Fourth Edition) Experiment 5 write and debug program solutions with multiple segments

Posted by gatoruss on Wed, 19 Jan 2022 19:16:33 +0100

(1) compile, connect, load and track the following programs with Debug, and then answer questions

assume cs:code,ds:data,ss:stack
data segment
    dw 0123,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
data ends
stack segment
    dw 0,0,0,0,0,0,0,0
stack ends
code segment
start:
    mov ax,stack
    mov ss,ax
    mov sp,16
    
    mov ax,data
    mov ds,ax
    
    push ds:[0]
    push ds:[2]
    pop ds:[2]
    pop ds:[0]
    
    mov ax,4c00h
    int 21h
code ends
end start

① The CPU executes the program. What is the data in the data section before the program returns?

Answer: let's load the trace with debug to see how the result is

When the program is just loaded into memory, because the instruction (mov ds,data) for setting the data segment address has not been executed, the DS segment address points to the program starting position (including PSP data area) by default. At this time, DS:0~DS:FF are PSP data area (accounting for 256 bytes), and DS:100 starts with the contents of data segment, stack segment and code segment. (as shown above)

After the MOV DS and data instructions are executed, the data segment is manually set to DS:0~D:F as the data segment (the address of DS segment has been changed) (as shown in the figure below). However, before and after setting the logical data segment, the physical data segment has not changed and is always in the 1299:0~1299:f part

According to the experimental results, the data in the data section is

23 01 56 04 89 07 BC 0A EF 0D ED 0F BA 0C 87 09

② The CPU executes the program. Before the program returns, CS = __ SS= ______  ,DS= ______

When the MOV, ax, 4c00h instructions are executed, the contents of each register are as follows

According to the experimental results, CS = 129B, SS = 129A, DS = 1299

③ After the program is loaded, the segment address of the code segment is X, and the segment address of the data segment is __, The segment address of the stack segment is __.

According to ②, DS = CS - 2, SS = CS - 1.

Therefore, the data segment address is X - 2 and the stack segment address is X - 1

(2) Compile and connect the following programs, load and track them with Debug, and then answer the questions

assume cs:code,ds:data,ss:stack
data segment
    dw 0123,0456h
data ends
stack segment
    dw 0,0
stack ends
code segment
start:
    mov ax,stack
    mov ss,ax
    mov sp,16
    
    mov ax,data
    mov ds,ax
    
    push ds:[0]
    push ds:[2]
    pop ds:[2]
    pop ds:[0]
    
    mov ax,4c00h
    int 21h
code ends
end start

① The CPU executes the program. What is the data in the data section before the program returns?

Note: the actual space occupied by the data segment and stack segment after the program is loaded is in the unit of 16 bytes. If it is insufficient, fill it with 0

According to the experimental results, the data in the data section is

23 01 56 04 00 00 00 00 00 00 00 00 00 00 00 00

② The CPU executes the program. Before the program returns, CS = __ SS= ______  ,DS= ______

According to the experimental results, CS = 129B, SS = 129A, DS = 1299

③ After the program is loaded, if the segment address of the code segment is X, the segment address of the data segment is and the segment address of the stack segment is.

Here, it is roughly the same as (1), because the structural order and size of segments are roughly the same

According to ②, DS = CS - 2, SS = CS - 1.

Therefore, the data segment address is X - 2 and the stack segment address is X - 1

④ For segments defined as follows:

name segment

...

name ends

If the data in the segment occupies N bytes, the actual space occupied by the segment after the program is loaded is

If n is less than 16, 16 bytes are actually occupied; If n is greater than 16, the actual occupation (integer of N/16 + 1) * 16 bytes. That is, if the data in the segment accounts for n bytes, the actual space occupied by the segment after the program is loaded is

(3) Compile and connect the following programs, load and track them with Debug, and then answer the questions

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

code segment
start:
    mov ax,stack
    mov ss,ax
    mov sp,16
    
    mov ax,data
    mov ds,ax
    
    push ds:[0]
    push ds:[2]
    pop ds:[2]
    pop ds:[0]
    
    mov ax,4c00h
    int 21h
code ends
data segment
    dw 0123,0456h
data ends
stack segment
    dw 0,0
stack ends
end start

① The CPU executes the program. What is the data in the data section before the program returns?

(3) The difference from (1) (2) is that (3) puts the data segment and stack segment after the code segment

According to the experimental results, the data in the data section is

23 01 56 04 00 00 00 00 00 00 00 00 00 00 00 00

② The CPU executes the program. Before the program returns, CS = __ SS= ______  ,DS= ______

According to the experimental results, CS = 1299, SS = 129D, DS = 129C

③ After the program is loaded, if the segment address of the code segment is X, the segment address of the data segment is and the segment address of the stack segment is.

According to ②, DS = CS + 3, SS = CS =+ 4

Therefore, the segment address of the data segment is X + 3, and the segment address of the stack segment is X + 4

(4) If the last pseudo instruction "end start" in questions (1), (2) and (3) is changed to "end" (that is, the entry of the program is not specified), which program can still be executed correctly? Please explain why.

In programming, we use the pseudo instruction end to describe the end of the program and the entry of the program. After compiling the link, the program entry indicated by "end start" is converted into an entry address. We can find that after start and end start are set in the program, the program execution will start at the start position and end at the end start position. If end start is not set, only end is set. At this time, the program will start executing after the PSP data area by default (even if start is set, the program will not start executing from the start position). If you want to ensure the normal execution of the program, it must be a code segment after the PSP data area. It can be seen from the three programs (1), (2) and (3) that only the code segment of program (3) is in front, so after the last pseudo instruction "end start" is changed to "end", program (3) can still be executed correctly

(5) The procedure is as follows: write the code in the code section, add the data in Section a and section b in turn, and save the results in section c.

assume cs:code
a segment
    db 1,2,3,4,5,6,7,8     ;Store by byte
a ends
b segment
    db 1,2,3,4,5,6,7,8
b ends
c segment
    db 0,0,0,0,0,0,0,0
c segment
code segment
start:
    ___________
code ends
end start

The first thing to note here is that the data of segments a, b and c are stored by bytes, so when using registers to transfer data, you must use 8-bit registers instead of 16 bit registers! Otherwise, it may cause errors (although the result of using 16 bit register is the same as that of using 8-bit register, it must be standardized).

The idea used by the author here is to use DS to store segment a address, and segment b address can be directly represented by DS + 16 (segment a is less than 16 bytes, and 0 accounts for 16 bytes). ES stores segment c address. Each cycle adds one byte of segment a and segment b and stores it in segment c

The code is as follows:

assume cs:code

a segment
  db 1,2,3,4,5,6,7,8
a ends

b segment
  db 1,2,3,4,5,6,7,8
b ends

c segment
  db 0,0,0,0,0,0,0,0
c ends

code segment

start:
      mov ax,a
      mov ds,ax    ; ds deposit a Segment address

      mov ax,c
      mov es,ax    ; es deposit c Segment address
      
      mov bx,0
      mov cx,8     ;  Cycle the word 8 times

s0:
      mov dl,[bx]
      add dl,[bx+16]    // Segment a is less than 16 bytes and takes up 16 bytes after 0 is added
      mov es:[bx],dl    // After a + 16, it is the segment b address
      inc bx
      loop s0
      
      mov ax,4c00h
      int 21h

code ends

end start

The experimental results are as follows:

(6) the procedure is as follows: write the code in the code section, and use the push command to store the first 8 font data in Section a in reverse order in section b.

assume cs:code
a segment
    dw 1,2,3,4,5,6,7,8,9,0ah,0bh,0ch,0dh,0eh,0fh,0ffh    ;Store by word
a ends
b segment
    dw 0,0,0,0,0,0,0,0
b ends
code segment
start:
    __________
code ends
end start

This question is different from the previous one. This question is that the data is stored by word, so 16 bit register is used.

It is required to use the push instruction to store the first 8 font data in segment b. here, segment a should be the data segment and segment b should be the stack segment. Only then can the push instruction be used to press the data of segment a into segment b and cycle for 8 times

The code is as follows:

assume cs:code

a segment

  dw 1,2,3,4,5,6,7,8,9,0ah,0bh,0ch,0dh,0eh,0fh,0ffh
  
a ends

b segment

  dw 0,0,0,0,0,0,0,0
  
b ends

code segment

start:

      mov ax,a
      mov ds,ax    ;DS deposit a Segment address
      
      mov ax,b
      mov ss,ax    ;SS deposit b Segment address (stack segment)
      mov sp,10h

      mov bx,0    
      mov cx,8

s0:
      push [bx]    ; a Segment data push in b paragraph
      inc bx       ; Since words occupy 16 bits, a memory unit occupies 8 bits
      inc bx       ; Therefore, each cycle + 2 To point to the next word
      loop s0
      
      mov ax,4c00h
      int 21h

code ends

end start

The experimental results are as follows:

Topics: Assembly Language