Compilation Learning--Day 5

Keywords: PHP

Chapter 6 Procedures with Multiple Paragraphs

There are two ways a program can get the space it needs:

1. Assign programs when they are loaded

2. Apply to the system during program execution

 

6.1 Use data in code snippets

**

Mov ax, [0]; in debug, data with ds:0 a word (high and low bits) is passed into the ax 16-bit register

Mov al, [0]; in debug, a byte of data from ds:0 is passed into the al 8-bit register

 

assume cs:code
code segment
    dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
    start:    mov bx,0
            mov ax,0
            
            mov cx,8
    s:        add ax,cs:[bx]
            add bx,2
            loop s
            
            mov ax,4c00h
            int 21h
            
    code ends
    end start

 

"dw"--"define word" defines font data

When we view a program using the u directive

The first bunch of code we didn't know, this is the code snippet in the program, this code snippet is defined by dw here, 8 font data, 16 bytes, so our assembly code should be at cs:10

 

When we execute the program in debug, we need to adjust the IP to 10

 

In the above program, we preface the first assembly instruction with a label start.

This start appears after the end, so we use the end to indicate the program's entry address.You can understand that the start label in front is just a tag. The first time you see an unspecified explanation, you will only know that the start is the entry to the program if you see the end start below.

The program entry address is automatically set when the program is generated

 

6.2 Use stack in code snippets

Complete the following program, using the stack, to store the data defined in the program in reverse order

assume cs:code
code segment
    dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
    dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    
    start:    mov ax,cs
            mov ss,ax
            mov sp,30h

            mov bx,0
            mov cx,8
    s:        push cs:[bx]
            add bx,2
            loop s
            
            mov bx,0
            mov cx,8
    s0:        pop cs:[bx]
            add bx,2
            loop s0
            
            mov ax,4c00h
            int 21h
            
    code ends
    end start

 

Reverse order result

 

In this section, a stack space is defined to store data cs:010h~cs:02fh,16 words, 32 bytes in size

 

Checkpoint 6.1

(1)

mov cs:[bx],ax

 

(2)

Stack in the program, so the segment address is the same as the current address, the first empty cs

20 bytes from cs:10h onwards, which is cs:9h+20h=cs:29h, the stack space is cs:10h~cs:29h

So before stacking, SP = 30h (the first time you stack, the top of the stack is cs:28h)

Third empty pop cs:[bx]

 

6.3 Put data, code, stack in different segments

Experiment 5, debugging programs with multiple segments

(1)

assume cs:code,ds:data,ss:stack
data segment
    dw 0123h,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

 

Assembler has multiple segments, equivalent to separate, that is, addresses start at 0

Because before the debugging code starts, only the CS address is known, data and stack don't know, and the data in the data segment needs to be viewed

1. Execute the code run to mov ds,ax

2. Because the space of the three segments is tightly arranged, first the data segment, then the stack segment, and finally the code segment (the order depends on the order in the code), the stack segment accounts for 16 bytes, the data segment for 16 bytes, and the code segment starts at 076c:0h, so the stack segment starts at 076b:0h and the data segment starts at 076a:0h.

 

1)

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

2)

cs=076ch

ss=076bh

ds=076ah

3)

X-2H

X-1H

 

(2)

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

data segment
    dw 0123h,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

 

1)

 

2)

 

3)

X-2H

X-1H

 

4)

(N / 16 + 1) * 16

N / 16 does not round, and this formula actually states that under 16 bytes it also takes up 16 bytes, or a line

 

(3)

1)

2)

3)

X+0003H

X+0004H

 

(4)

(3) can be correctly executed because (1) (2) in the program, both the stack segment and the data segment are in front of the code segment, the address of the program is not the address of the code segment

Posted by pdub56 on Sat, 22 Jun 2019 12:24:48 -0700