8086 Assembly Learning DS registers, SS/SP registers __

Source: Internet
Author: User

Related Blogs: 8086 basic knowledge of assembly, General Register, CS/IP Register and debug use one, DS register

A 8086CPU register is 16-bit, and the data type has the following two kinds:
1Byte = 8bit (byte-data)
1word = 2Byte = 16bit (font data)

A 16-bit register stores a word, while in memory it requires two contiguous bytes to store a word. (High-level address to store high data, low address to store low data). Any two contiguous memory units, n units and n+1 units can be considered as two byte units, or as a low byte unit (n) and a high byte unit (n+1) in a word cell with an address of N. For example, figure:

So how does the CPU access the data in these addresses? We know that the CPU obtains the address of the next instruction through the CS:IP register. To get the data, you need to go through the DS register, the DS segment address register: The segment address where you want to access the data.

-e 2000:0000    ///to 2000:0000 set initial value 
-a MOV bx,2000 mov ds,bx   //Set DS segment address register value for
mov al , [0]  
mov cx,[1]  

The above instruction reads the byte data value of the 2000:0000 address cell into the AL (8-bit) register. Reads the font data from the 2000:0001 address unit to a CX (16-bit) register. [] Represents a memory unit in which 0 represents an offset address, and which segment is offset. 8086CPU automatically reads the segment address of the DS data most memory cells. The final register value is:
bx=2000
ds=2000
ax=0011
cx=3322

The test is as follows:

Similarly, DS and CS and other segment address registers are interlinked, does not support the data directly into the segment address register, through other registers relay, this is a hardware design problem.

Of course, we can save [n] to Al, Ax, or AL, Ax to [n] (all reasonable combinations are possible):

mov [0],al  //al value to ds:[0] in the address of
mov [2],ax  //ax font data, Al deposit Ds:[2] and AH deposit ds:[3]
add ax,[0]  // The result of the addition is saved to the result of the
add [0],ax//Add in Ax  saved to ds:[0] and ds:[1] in the address of the
sub al,[0]  //Subtract save to the Al
Sub [0],ah  / /Subtract the result to the address of ds:[0]
...
Here are a few instructions for
mov ds,1000
add ds,ax 
sub Ds,ax
... Wait, it's all unreasonable.

In memory, the data and instructions are completely indistinguishable, and in the register, data and instructions are different, because different registers to save the various types of data address (CS save instruction Data segment address, DS save operation number of address ...) And the CPU is responsible for reading CS:IP determine the next instruction to store the address, read DS know will be processed data segment address, according to the instruction offset address to find specific data. So, the assembler uses the CPU to read the registers to find the instruction and the data, to carry on a series of operations. Second, SS/SP stack segment registers:

In C, C + +, Java ... In a series of high-level languages, we have used the stack of this data structure, in the compilation of this ancient language, the stack of course also exists. So how to use registers to describe a stack space in the assembly. You need to use the SS and SP registers. For the 8086 assembly, the number of bytes per stack/stack is 2 bytes or 1 words.

Push instruction –>①sp-2–>② store font data in SS:SP address
pop instruction –>① will ss:sp address of the font data out –>②SP + 2
The above step is like C in the stack before we move the top pointer, and then into the stack. Out of the stack of data first, and then the top of the stack move to the bottom of the stack (belong to the "full stack" (move top pressure stack first)).

directives use rules:
Push 16bit registers/push "Ds:[n]": Registers can be (AX\BX\CX\DX\DS\ES\SS\SP ...), 8-bit registers are not available. (SP-2, read the data into stack from the specified register/"ds:[n]" address)
Pop 16bit Register/pop "Ds:[n]": (remove data from stack to register/"ds:[n]" address, sp+2)
(In Debug, "DS:" needs to be omitted, and only one [N],CPU] automatically reads the stack address from the DS. You can read the data into the stack from the register or ds:[] address, but you cannot "push FFFF" to direct the value to the stack.

Note: the bottom of the stack at the high memory address, at any time, Ss:sp point to the top of the stack (then SS:SP two registers are similar to a top-level pointer), so that where the stack is determined by the SS:SP (to ss:sp initial value is the bottom of the stack). But the size of the stack is not strictly bound, when sp-2<0, it will start to reduce from FFFF, but does not guarantee its permissions. may be out of bounds error, may not cross the line error (does not automatically judge stack full/stack empty). The problem with the stack's cross-border problems requires the programmer's own attention (this is freer than C, but less secure).

Eg:
/* Modify ss:sp*/-
R SS-
R SP/* via debug-R
or: Modify ss:sp*/mov via register relay
ax,2000 
mov ss,ax
mov sp,10   //IP registers cannot be modified directly by value, but SP registers can

//is the specified stack bottom address is 2000:10, at this time ss:sp=2000:0010
mov ax,1234
mov bx,5678
push ax     //sp-2=000e,2000:e and 2000:f respectively storage 34 and
push BX     //sp-2=000c,2000 : C and 2000:d respectively store 78 and
pop CX  //out stack cx=5678,sp+2=000e   
pop dx  //out Stack dx=1234,sp+2=0010

We'll analyze the stack process, as shown in the picture:

The stack is its inverse process, first out value to register, and then move the top of the stack pointer (modify the SS:SP register value)

Understand the CS, DS, SS Register we can for the data section (text), Code snippets (codes), stack segment (stack) summed up:

Data Segment : The segment where data is stored (segment address in DS)
Code Snippet : The segment where the code is stored (the segment address is in CS)
Stack segment : A segment (segment address in SS) that holds temporary data and is maintained by the SS:SP, DS, SS, SP registers are mixed:

(for example, Wang Yu Teacher's "assembly language" chapter III detection point 3.2)
The eight words in the 10000H~1001FH are copied in reverse order to the 20000H~2001FH, and how to use the stack.
Law I:
From ds:[] into the stack, ds:[] is the source (10000H~1000FH), the stack is the target (20000H~2000FH)

Set data segment address
mov ax,1000h
mov ds,ax   

//Set stack segment address
mov ax,2000h mov
ss,ax mov
sp,10h  

/ Copying from a data segment to a stack segment: A data push from a specified data segment (10000H~1000FH) to a specified stack segment (20000h~2000fh)
[0]
push [2] push [
4] Push
[6]
push [8]
push [A] push [
C] push [
E]

Law II:
Out stack to ds:[], stack is source (20000H~2000FH), ds:[] is target (10000H~1000FH)

Set data segment address
mov ax,2000h
mov ds,ax

//Set stack segment address
mov ax,1000h mov
ss,ax mov
sp,0h

/ Copy from stack segment to data segment: is to place the specified stack (10000H~1000FH) value in the specified data segment (20000H~2000FH)
pop [E]
pop [C] pop [A] pop [
8]
pop [6]
Pop [4]
pop [2]
pop [0]

Mixed diagram:

Law one test:
First set the initial value to the 10000H~1000FH:

After executing all the stack instructions, you can see that the value of the 10000H~1000FH is copied to the 20000H~2000FH in reverse order (not in bytes).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.