Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define and apply the middle segment of assembly language

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to define and apply the middle part of assembly language". The content is simple and clear. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn the article "how to define and apply the middle part of assembly language".

Segment

Define a segment of memory as a segment, indicate the segment with a segment address, and access the cells within the segment with an offset address.

Category code segment

Define

For 8086 PC, a group of memory units can be defined as a segment when programming.

A set of code of length N (N ≤ 64KB) can be stored in a set of memory units with consecutive addresses and multiples of 16 starting addresses. This memory is used to store the code, thus defining a code segment.

For example

This 10-byte instruction exists from a set of memory units in 123B0H~123B9H, so we can assume that the memory unit of 123B0H~123B9H is used to store code, which is a code segment with an address of 123BH and a length of 10 bytes.

How to make instructions in a code snippet be executed

CPU only recognizes the contents of the memory unit pointed to by CS:IP as instructions.

So point the CS:IP to the first address of the first instruction in the defined code snippet.

CS = 123BHMagneIP = 0000H.

Data segment

Define

We can define a data segment by taking a set of memory units of length N (N ≤ 64K) with contiguous addresses and multiples of 16 starting addresses as special memory space for storing data.

For example, we use 123B0H~123B9H to store data:

Segment address: 123BH

Length: 10 bytes

Taking a section of memory as a data segment is an arrangement when we program. We can use ds to store the address of the data segment during the specific operation, and then use relevant instructions to access the specific units in the data segment as needed.

DS and [address]

We need to read the contents of unit 10000H

When CPU wants to read a memory unit, it must first give the address of the memory unit.

In 8086PC, a memory address consists of a segment address and an offset address.

There is a DS register in 8086CPU, which is usually used to hold the segment address of the data to be accessed.

Read the data from 10000H (1000 0) into al.

Mov bx,1000H

Mov ds,bx

Mov al, [0]

The mov instruction can feed the contents of a memory unit into a register.

The format of the mov instruction:

Mov register name, memory unit address

"[...]" Represents a memory unit, "[…]" The 0 in indicates the offset address of the memory unit.

How to read data from 10000H with mov instruction?

10000H is denoted as 1000RU 0 (segment address: offset address)

Put segment address 1000H into ds

Use mov al, [0] to complete the transfer (the [] in the mov instruction indicates that the operator is a memory unit, the 0 in [] indicates that the offset address of the memory unit is 0, and its segment address is placed in ds by default)

How to send 1000H to ds?

8086CPU does not support the operation of feeding data directly into a segment register. Ds is a segment register. (problems with hardware design)

Mov ds,1000H is illegal.

Data > > general register > > segment register

For example

We define the memory unit of 123B0H~123BAH as a data segment, and we are now going to accumulate the data from the first three units in this segment

The code is as follows:

Stack segment stack

The stack space is, of course, part of the memory space, which is just a piece of memory space that can be accessed in a special way.

Mode of operation

The stack has two basic operations

Stack: put a new element on top of the stack

Out of the stack: take an element from the top of the stack

The elements at the top of the stack are always the last to enter the stack, and when they need to be removed from the stack, they are the first to be removed from the stack.

Operating rules of the stack: LIFO (Last In First Out, LIFO)

The stack and unstack operations of 8086CPU are carried out on a word-by-word basis.

Note: font data is stored in two units, 8 bits higher in the high address unit and 8 bits lower in the low address unit.

8086CPU provides in-stack and out-stack instructions

SS:SP

In 8086CPU, there are two registers:

Segment register SS stores the segment address at the top of the stack

Register SP stores the offset address at the top of the stack

At any time, the SS:SP points to the top element of the stack.

SS and SP only record the address at the top of the stack, and rely on SS and SP to ensure that the top of the stack is found when entering and exiting the stack.

The problem of exceeding the bounds at the top of the stack

Kinds

Use push instructions to enter the stack when the stack is full

Use pop instructions to unstack when the stack is empty

8086CPU does not guarantee that stack operations will not be out of bounds.

8086CPU only knows where the top of the stack is (indicated by SS:SP), not how much stack space the reader arranges.

How 8086CPU works, considering only the current situation:

Where is the top of the current stack?

Which instruction is currently to be executed?

Solution.

The size of the stack should be arranged according to the maximum stack space that may be used to prevent overbounds caused by too much data in the stack.

You should also pay attention to the out-of-stack operation to prevent the out-of-bounds caused by continuing to go out of stack when the stack is empty.

Push, pop instruction

Define

Push and pop instructions can transfer data between registers and memory.

PUSH (Stack)

Push ax: feeds data from register ax into the stack

Execution procedure push ax

(1) SP=SP-2

(2) send the contents of the ax to the memory unit pointed to by the SS:SP, and the SS:SP points to the top of the new stack.

Illustration

When entering the stack, the top of the stack grows from a high address to a low address.

POP (out of stack)

Pop ax: take the data from the top of the stack and send it to ax. The execution process (1) feeds the data at the memory unit pointed to by SS:SP into the ax; (2) SP = SP+2,SS:SP points to the unit below the top of the current stack and takes the unit below the top of the current stack as the new top of the stack.

Illustration

Note:

After going off the stack, SS:SP points to the top element of the new stack 1000EH before the pop operation. 2266H at 1000CH still exists, but it is no longer in the stack.

When push and other stack instructions are executed again, SS:SP moves to 1000CH and writes new data in it, which will be overwritten.

Format

(1)

Push registers: stack data from a register

Pop register: take out the stack and use a register to receive the data from the stack.

For example: push axpop bx

(2)

Push segment register: pop stacks data from a segment register

Segment register: out of the stack, using a segment register to receive the data of the stack.

For example: push dspop es

(3)

Push memory unit: put the words at a memory unit on the stack (stack operations are in words)

Pop memory unit: take out the stack and use a memory word unit to receive data from the stack.

For example: push [0] pop [2]

When the instruction is executed, the CPU needs to know the address of the memory unit, it can give the offset address of the memory unit in the push and pop instructions, and the segment address is obtained from the ds when the instruction is executed.

Be careful

Unlike the mov instruction, the address of the memory unit accessed by the push and pop instructions is not given in the instruction, but is indicated by the SS:SP.

CPU only needs one step to execute mov instruction, that is, transfer, while it takes two steps to execute push and pop instructions.

When performing push: change the SP first, and then transmit it to the SS:SP.

When executing pop: read the data at SS:SP first, and then change the SP.

Push, pop and other stack operation instructions, only modify the SP. In other words, the maximum range of variation at the top of the stack is: 0~FFFFH.

Provide: SS, SP indicate the top of the stack; write the stack instruction of memory after changing SP; change the stack instruction of SP after reading memory.

Stack segment definition

A set of memory units can be defined as a segment as needed.

For example, we use the 16-byte memory space of 10010H~1001FH as a stack and access it as a stack.

We can define a stack by using a set of memory cells of length N (N ≤ 64K) with consecutive addresses and a multiple of 16 starting addresses as a stack.

This space can be a stack segment with an address of 1000H and a size of 16 bytes.

How to make stack operation instructions such as push and pop access our defined stack segment?

Point the SS:SP to the stack segment we defined.

Thinking

We use the 10000H~1FFFFH space as the stack segment, SS=1000H, the stack space size is 64KB, and the address of the word unit at the bottom of the stack is 1000:FFFE.

At any time, SS:SP points to the top of the stack, and when there is only one element in the stack, SS=1000H,SP=FFFEH.

If the stack is empty, it is equivalent to the only element in the stack going out of the stack. After leaving the stack, SP=SP+2.

SP used to be FFFEH, but add 2 to SP=0, so when the stack is empty, SS=1000H,SP=0.

At any time, SS:SP points to the top element of the stack, when the stack is empty, there is no element in the stack, so SS:SP can only point to the unit under the bottom of the stack, the offset address of the unit is the offset address of the word unit at the bottom of the stack + 2, and the address of the word unit at the bottom of the stack is 1000:FFFE, so the stack is empty, SP=0000H.

Visit

For a data segment, put its segment address in DS, and when you use instructions such as mov, add, sub, etc., to access memory units, CPU will access the contents of our defined data segments as data segments.

For a code snippet, put its segment address in CS and the offset address of the first instruction in the segment in IP, so that CPU will execute the instructions in the code snippet we defined

For the stack segment, put its segment address in the SS and the offset of the stack top unit in the SP, so that when CPU needs to perform stack operations, such as executing push, pop instructions, etc., the stack segment we define will be used as stack space.

So, no matter how we arrange it, CPU treats a piece of memory as code because CS:IP points there; CPU uses a piece of memory as a stack because SS:IP points there

For example, we arrange 10000H~1001FH as a code snippet and store the following code in it:

Set CS=1000H,IP=0, and this code will be executed.

As you can see, in this code, we arrange the 10000H~1001FH into stack segments and data segments.

The memory of 10000H~1001FH is not only a code segment, but also a stack segment and a data segment.

A piece of memory can be both a code storage space, a data storage space, a stack space, or nothing.

The key lies in the setting of registers in CPU, that is, the direction of CS, IP, SS, SP and DS.

Segment prefix

In instructions to access memory units, "ds:", "cs:", "ss:" or "es:" that explicitly indicate the segment address of the memory unit.

Application

Scenario 1

Problem: copy the data from the ffff:0~ffff:b segment element in memory to the 0RV 2000RU 20b unit.

Analysis.

(1) the 0Rom 2000Rom 20b unit is equivalent to the 0020Rd 010020RGB unit. They describe the same memory space.

(2) the copy process is implemented in a loop, which is briefly described as follows:

Initialization: Xerox 0

Ring 12 times

Send the data in the ffff:X unit to 0020 X (need to transfer with a register)

X=X+1

(3) in the loop, the offset address X of the source unit ffff:X and the target unit 0020 X is a variable. We use bx to store

(4) We use the term 0020 to describe the offset address of the target unit and the offset address of the source unit from the same value 0.

problem

Because the distance between the source unit ffff:X and the target unit 002064KB X is greater than the 64KB, in different 64KB segments, the ds should be set twice in each cycle.

It is right to do so, but it is not efficient.

Solve

We can use two segment registers to store the segment addresses of the source unit ffff:X and the target unit 0020 ds X, respectively, so that we can omit the program segment that needs to be repeated 12 times in the loop.

In the improved program, es is used to store the segment address of the target space 0020 ffff:0~ffff:b and ds is used to store the segment address of the source space.

In the instruction to access the memory unit "mov es: [bx], al", explicitly give the segment address of the unit with the segment prefix "es:" so that you do not have to set the ds repeatedly in the loop

Scenario 2

Different processing of instructions by Debug and assembler Masm

In Debug

Mov ax, [0]

Indicates that the data at ds:0 is sent into al.

In the assembly source program, the instruction "mov ax, [0]" is treated by the compiler as an instruction "mov ax,0".

Solution: using the segment prefix to explicitly indicate the segment address

The above is all the contents of the article "how to define and apply the Middle Segment of Assembly language". Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report